diff --git a/.gitignore b/.gitignore index 8c3a9d0f52a..ad8ee336193 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,16 @@ testbin/* /testdata/project-v3/go.sum /testdata/project-v3-multigroup/go.sum /testdata/project-v3-addon/go.sum + +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib +bin +testbin + +# skip shell script to setup envtest from testdata and local +testdata/*/fetch_ext_bins.sh +fetch_ext_bins.sh diff --git a/docs/book/src/quick-start.md b/docs/book/src/quick-start.md index b17d107706f..3780a3fb73e 100644 --- a/docs/book/src/quick-start.md +++ b/docs/book/src/quick-start.md @@ -35,7 +35,13 @@ export PATH=$PATH:/usr/local/kubebuilder/bin @@ -229,4 +235,5 @@ Now, follow up the [CronJob tutorial][cronjob-tutorial] to better understand how [cronjob-tutorial]: https://book.kubebuilder.io/cronjob-tutorial/cronjob-tutorial.html [GOPATH-golang-docs]: https://golang.org/doc/code.html#GOPATH [how-to-write-go-code-golang-docs]: https://golang.org/doc/code.html - +[cronjob-tutorial]: https://book.kubebuilder.io/cronjob-tutorial/cronjob-tutorial.html +[config-test]: https://book.kubebuilder.io/reference/testing/envtest.html?highlight=#configuring-your-test-control-plane \ No newline at end of file diff --git a/docs/book/src/reference/envtest.md b/docs/book/src/reference/envtest.md index dc855ae9809..8ad92903610 100644 --- a/docs/book/src/reference/envtest.md +++ b/docs/book/src/reference/envtest.md @@ -25,8 +25,19 @@ err = testEnv.Stop() Logs from the test runs are prefixed with `test-env`. ### Configuring your test control plane -You can use environment variables and/or flags to specify the `api-server` and `etcd` setup within your integration tests. +You can use environment variables and/or flags to specify the `kubectl`, `api-server` and `etcd` setup within your +integration tests. The location of the binaries which will be used by the [EnvtTest][envtest] is done due the following environment +variables. See: + +```shell +$ export TEST_ASSET_KUBECTL= +$ export TEST_ASSET_KUBE_APISERVER= +$ export TEST_ASSET_ETCD= +``` + +Note that `kubebuilder` scaffold in your Makefile the `setupenvtest` target which download the binaries and setup it. + #### Environment Variables | Variable name | Type | When to use | @@ -71,3 +82,4 @@ expectedOwnerReference := v1.OwnerReference{ } Expect(deployment.ObjectMeta.OwnerReferences).To(ContainElement(expectedOwnerReference)) ``` +[envtest]: https://godoc.org/sigs.k8s.io/controller-runtime/pkg/envtest \ No newline at end of file diff --git a/pkg/plugin/v3/scaffolds/init.go b/pkg/plugin/v3/scaffolds/init.go index 7ea614e98c8..166d45cbbe4 100644 --- a/pkg/plugin/v3/scaffolds/init.go +++ b/pkg/plugin/v3/scaffolds/init.go @@ -40,7 +40,8 @@ const ( ControllerToolsVersion = "v0.3.0" // KustomizeVersion is the kubernetes-sigs/kustomize version to be used in the project KustomizeVersion = "v3.5.4" - + // ClusterApiTag tag to use to get the script to setup the envtest + ClusterApiTag = "release-0.2" imageName = "controller:latest" ) @@ -110,6 +111,7 @@ func (s *initScaffolder) scaffold() error { BoilerplatePath: s.boilerplatePath, ControllerToolsVersion: ControllerToolsVersion, KustomizeVersion: KustomizeVersion, + ClusterApiTag: ClusterApiTag, }, &templates.Dockerfile{}, &templates.DockerignoreFile{}, diff --git a/pkg/plugin/v3/scaffolds/internal/templates/gitignore.go b/pkg/plugin/v3/scaffolds/internal/templates/gitignore.go index 3910f8d3195..ebadcd2955e 100644 --- a/pkg/plugin/v3/scaffolds/internal/templates/gitignore.go +++ b/pkg/plugin/v3/scaffolds/internal/templates/gitignore.go @@ -39,6 +39,9 @@ func (f *GitIgnore) SetTemplateDefaults() error { } const gitignoreTemplate = ` +# ignore shell script to setup env test +fetch_ext_bins.sh + # Binaries for programs and plugins *.exe *.exe~ diff --git a/pkg/plugin/v3/scaffolds/internal/templates/makefile.go b/pkg/plugin/v3/scaffolds/internal/templates/makefile.go index ae4e140ed00..7ccaa9f5bd7 100644 --- a/pkg/plugin/v3/scaffolds/internal/templates/makefile.go +++ b/pkg/plugin/v3/scaffolds/internal/templates/makefile.go @@ -34,6 +34,8 @@ type Makefile struct { ControllerToolsVersion string //Kustomize version to use in the project KustomizeVersion string + //ClusterApiTag tag to use to get the script to setup the envtest + ClusterApiTag string } // SetTemplateDefaults implements input.Template @@ -70,7 +72,7 @@ endif all: manager # Run tests -test: generate fmt vet manifests +test: setupenvtest generate fmt vet manifests go test -race -coverprofile cover.out ./... # Build manager binary @@ -153,4 +155,10 @@ KUSTOMIZE=$(GOBIN)/kustomize else KUSTOMIZE=$(shell which kustomize) endif + +# Setup binaries required to run the tests +setupenvtest: + curl -sSLo fetch_ext_bins.sh https://raw.githubusercontent.com/kubernetes-sigs/cluster-api/{{ .ClusterApiTag }}/scripts/fetch_ext_bins.sh + chmod +x fetch_ext_bins.sh + . ./fetch_ext_bins.sh && fetch_tools && setup_envs ` diff --git a/scripts/setup_envtest_bins.sh b/scripts/setup_envtest_bins.sh deleted file mode 100755 index be49b0de9c2..00000000000 --- a/scripts/setup_envtest_bins.sh +++ /dev/null @@ -1,86 +0,0 @@ -#!/bin/sh - -# Copyright 2020 The Kubernetes Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# This file will be fetched as: curl -L https://git.io/getLatestKubebuilder | sh - -# so it should be pure bourne shell, not bash (and not reference other scripts) - -set -eu - -# To use envtest is required to have etcd, kube-apiserver and kubetcl binaries installed locally. -# This script will create the directory testbin and perform this setup for linux or mac os x envs in - -# Kubernetes version e.g v1.18.2 -K8S_VER=$1 -# ETCD version e.g v3.4.3 -ETCD_VER=$2 -OS=$(uname -s | tr '[:upper:]' '[:lower:]') -ARCH=$(uname -m | sed 's/x86_64/amd64/') -ETCD_EXT="tar.gz" -TESTBIN_DIR=testbin - -setup_testenv_bin() { - # Do nothing if the $TESTBIN_DIR directory exist already. - if [ ! -d $TESTBIN_DIR ]; then - mkdir -p $TESTBIN_DIR - - # install etcd binary - # the extension for linux env is not equals for mac os x - if [ $OS == "darwin" ]; then - ETCD_EXT="zip" - fi - [[ -x ${TESTBIN_DIR}/etcd ]] || curl -L https://storage.googleapis.com/etcd/${ETCD_VER}/etcd-${ETCD_VER}-${OS}-${ARCH}.${ETCD_EXT} | tar zx -C ${TESTBIN_DIR} --strip-components=1 etcd-${ETCD_VER}-${OS}-${ARCH}/etcd - - # install kube-apiserver and kubetcl binaries - if [ $OS == "darwin" ] - then - # kubernetes do not provide the kubernetes-server for darwin, - # In this way, to have the kube-apiserver is required to build it locally - # if the project is cloned locally already do nothing - if [ ! -d $GOPATH/src/k8s.io/kubernetes ]; then - git clone https://github.com/kubernetes/kubernetes $GOPATH/src/k8s.io/kubernetes --depth=1 -b ${K8S_VER} - fi - - # if the kube-apiserve is built already then, just copy it - if [ ! -f $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver ]; then - DIR=$(pwd) - cd $GOPATH/src/k8s.io/kubernetes - # Build for linux first otherwise it won't work for darwin - :( - export KUBE_BUILD_PLATFORMS="linux/amd64" - make WHAT=cmd/kube-apiserver - export KUBE_BUILD_PLATFORMS="darwin/amd64" - make WHAT=cmd/kube-apiserver - cd ${DIR} - fi - cp $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver $TESTBIN_DIR/ - - # setup kubectl binary - curl -LO https://storage.googleapis.com/kubernetes-release/release/${K8S_VER}/bin/darwin/amd64/kubectl - chmod +x kubectl - mv kubectl $TESTBIN_DIR/ - - # allow run the tests without the Mac OS Firewall popup shows for each execution - codesign --deep --force --verbose --sign - ./${TESTBIN_DIR}/kube-apiserver - else - [[ -x $TESTBIN_DIR/kube-apiserver && -x ${TESTBIN_DIR}/kubectl ]] || curl -L https://dl.k8s.io/${K8S_VER}/kubernetes-server-${OS}-${ARCH}.tar.gz | tar zx -C ${TESTBIN_DIR} --strip-components=3 kubernetes/server/bin/kube-apiserver kubernetes/server/bin/kubectl - fi - fi - export PATH=/$TESTBIN_DIR:$PATH - export TEST_ASSET_KUBECTL=/$TESTBIN_DIR/kubectl - export TEST_ASSET_KUBE_APISERVER=/$TESTBIN_DIR/kube-apiserver - export TEST_ASSET_ETCD=/$TESTBIN_DIR/etcd -} - -setup_testenv_bin diff --git a/test.sh b/test.sh index af5f0c580c4..9f769bd8168 100755 --- a/test.sh +++ b/test.sh @@ -152,7 +152,4 @@ GO111MODULE=on test_project project-v3 3-alpha GO111MODULE=on test_project project-v3-multigroup 3-alpha GO111MODULE=on test_project project-v3-addon 3-alpha -# test script that setup envtest -./scripts/setup_envtest_bins.sh v1.18.2 v3.4.3 - exit $rc diff --git a/testdata/project-v3-addon/Makefile b/testdata/project-v3-addon/Makefile index 514e029eedd..c49a6e91ff3 100644 --- a/testdata/project-v3-addon/Makefile +++ b/testdata/project-v3-addon/Makefile @@ -14,7 +14,7 @@ endif all: manager # Run tests -test: generate fmt vet manifests +test: setupenvtest generate fmt vet manifests go test -race -coverprofile cover.out ./... # Build manager binary @@ -97,3 +97,9 @@ KUSTOMIZE=$(GOBIN)/kustomize else KUSTOMIZE=$(shell which kustomize) endif + +# Setup binaries required to run the tests +setupenvtest: + curl -sSLo fetch_ext_bins.sh https://raw.githubusercontent.com/kubernetes-sigs/cluster-api/release-0.2/scripts/fetch_ext_bins.sh + chmod +x fetch_ext_bins.sh + . ./fetch_ext_bins.sh && fetch_tools && setup_envs diff --git a/testdata/project-v3-multigroup/Makefile b/testdata/project-v3-multigroup/Makefile index 514e029eedd..c49a6e91ff3 100644 --- a/testdata/project-v3-multigroup/Makefile +++ b/testdata/project-v3-multigroup/Makefile @@ -14,7 +14,7 @@ endif all: manager # Run tests -test: generate fmt vet manifests +test: setupenvtest generate fmt vet manifests go test -race -coverprofile cover.out ./... # Build manager binary @@ -97,3 +97,9 @@ KUSTOMIZE=$(GOBIN)/kustomize else KUSTOMIZE=$(shell which kustomize) endif + +# Setup binaries required to run the tests +setupenvtest: + curl -sSLo fetch_ext_bins.sh https://raw.githubusercontent.com/kubernetes-sigs/cluster-api/release-0.2/scripts/fetch_ext_bins.sh + chmod +x fetch_ext_bins.sh + . ./fetch_ext_bins.sh && fetch_tools && setup_envs diff --git a/testdata/project-v3/Makefile b/testdata/project-v3/Makefile index 514e029eedd..c49a6e91ff3 100644 --- a/testdata/project-v3/Makefile +++ b/testdata/project-v3/Makefile @@ -14,7 +14,7 @@ endif all: manager # Run tests -test: generate fmt vet manifests +test: setupenvtest generate fmt vet manifests go test -race -coverprofile cover.out ./... # Build manager binary @@ -97,3 +97,9 @@ KUSTOMIZE=$(GOBIN)/kustomize else KUSTOMIZE=$(shell which kustomize) endif + +# Setup binaries required to run the tests +setupenvtest: + curl -sSLo fetch_ext_bins.sh https://raw.githubusercontent.com/kubernetes-sigs/cluster-api/release-0.2/scripts/fetch_ext_bins.sh + chmod +x fetch_ext_bins.sh + . ./fetch_ext_bins.sh && fetch_tools && setup_envs