diff --git a/.gitignore b/.gitignore index 41907f331d7..cb379611ef3 100644 --- a/.gitignore +++ b/.gitignore @@ -13,10 +13,12 @@ docs/book/book/ # skip bin bin/* -testbin/* -testdata/project-v3-addon/testbin/* -testdata/project-v3-multigroup/testbin/* -testdata/project-v3/testbin/* +testdata/project-v3-addon/bin/* +testdata/project-v3-multigroup/bin/* +testdata/project-v3/bin/* +testdata/project-v2-addon/bin/* +testdata/project-v2-multigroup/bin/* +testdata/project-v2/bin/* docs/book/src/multiversion-tutorial/testdata/project/bin/* docs/book/src/cronjob-tutorial/testdata/project/bin/* diff --git a/Makefile b/Makefile index 9269277c5fc..2548a998524 100644 --- a/Makefile +++ b/Makefile @@ -42,11 +42,14 @@ help: ## Display this help .PHONY: build build: ## Build the project locally go build -o bin/kubebuilder ./cmd + go build -o bin/tooling ./tooling/main.go + .PHONY: install install: ## Build and install the binary with the current source code. Use it to test your changes locally. make build cp ./bin/kubebuilder $(shell go env GOPATH)/bin/kubebuilder + cp ./bin/tooling $(shell go env GOPATH)/bin/tooling ##@ Development @@ -59,7 +62,7 @@ generate: ## Update/generate all mock data. You should run this commands to upda go mod tidy .PHONY: generate-testdata -generate-testdata: ## Update/generate the testdata in $GOPATH/src/sigs.k8s.io/kubebuilder +generate-testdata: ## Update/generate the testdata in $GOPATH/src/sigs.k8s.io/kubebuilder GO111MODULE=on ./generate_testdata.sh .PHONY: lint diff --git a/docs/book/src/quick-start.md b/docs/book/src/quick-start.md index ffd207d94f4..139a31758a3 100644 --- a/docs/book/src/quick-start.md +++ b/docs/book/src/quick-start.md @@ -27,7 +27,7 @@ arch=$(go env GOARCH) curl -L https://go.kubebuilder.io/dl/2.3.1/${os}/${arch} | tar -xz -C /tmp/ ``` -If you are using a Kubebuilder plugin version less than version `v3+`, you must configure the Kubernetes binaries required for the [envtest][envtest], run: +If you are using a Kubebuilder CLI version =< `v2.3.1`, you must configure the Kubernetes binaries required for the [envtest][envtest], run: ```bash # move to a long-term location and put it on your path diff --git a/go.sum b/go.sum index 50ca1db89b0..50d10e3202e 100644 --- a/go.sum +++ b/go.sum @@ -88,6 +88,7 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= @@ -120,6 +121,7 @@ go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= diff --git a/pkg/plugin/v2/scaffolds/internal/templates/controller/controller_suitetest.go b/pkg/plugin/v2/scaffolds/internal/templates/controller/controller_suitetest.go index 80913f122c2..fbd867e3388 100644 --- a/pkg/plugin/v2/scaffolds/internal/templates/controller/controller_suitetest.go +++ b/pkg/plugin/v2/scaffolds/internal/templates/controller/controller_suitetest.go @@ -34,8 +34,8 @@ type SuiteTest struct { file.BoilerplateMixin file.ResourceMixin - // CRDDirectoryRelativePath define the Path for the CRD when it is multigroup - CRDDirectoryRelativePath string + // RelativePath define the Path for the root when it is multigroup + RelativePath string } // SetTemplateDefaults implements file.Template @@ -56,9 +56,9 @@ func (f *SuiteTest) SetTemplateDefaults() error { // If is multigroup the path needs to be ../../ since it has // the group dir. - f.CRDDirectoryRelativePath = `".."` - if f.MultiGroup { - f.CRDDirectoryRelativePath = `"..", ".."` + f.RelativePath = `".."` + if f.MultiGroup && f.Resource.Group != "" { + f.RelativePath = `"..", ".."` } return nil @@ -148,9 +148,12 @@ var _ = BeforeSuite(func(done Done) { By("bootstrapping test environment") testEnv = &envtest.Environment{ - CRDDirectoryPaths: []string{filepath.Join({{ .CRDDirectoryRelativePath }}, "config", "crd", "bases")}, + CRDDirectoryPaths: []string{filepath.Join({{ .RelativePath }}, "config", "crd", "bases")}, } + By("configuring env test binaries") + configEnvTestBinaries() + var err error cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) @@ -170,4 +173,31 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) }) + +// configEnvTestBinaries will check if the required binaries are in the bin directory +// of the project and configure to use them. +func configEnvTestBinaries() { + binPath := filepath.Join({{ .RelativePath }}, "bin") + if hasEnvTestBinaries(binPath) { + os.Setenv("KUBEBUILDER_ASSETS", binPath) + } +} + +// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found +// in the path informed +func hasEnvTestBinaries(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { + return false + } + return true +} ` diff --git a/pkg/plugin/v2/scaffolds/internal/templates/makefile.go b/pkg/plugin/v2/scaffolds/internal/templates/makefile.go index a680b7cd385..ce9c0cbe402 100644 --- a/pkg/plugin/v2/scaffolds/internal/templates/makefile.go +++ b/pkg/plugin/v2/scaffolds/internal/templates/makefile.go @@ -70,8 +70,24 @@ endif all: manager # Run tests -test: generate fmt vet manifests - go test ./... -coverprofile cover.out +test: generate fmt vet manifests tooling + $(TOOLING); go test ./... -coverprofile cover.out + +# get the kubebuilder tooling +tooling: +ifeq (, $(shell which tooling)) + @{ \ + set -e ;\ + TOOLING_TMP_DIR=$$(mktemp -d) ;\ + cd $$TOOLING_TMP_DIR ;\ + go mod init tmp ;\ + go get sigs.k8s.io/kubebuilder/tooling ;\ + rm -rf $$TOOLING_TMP_DIR ;\ + } +TOOLING=$(GOBIN)/tooling +else +TOOLING=$(shell which tooling) +endif # Build manager binary manager: generate fmt vet diff --git a/pkg/plugin/v3/scaffolds/init.go b/pkg/plugin/v3/scaffolds/init.go index 642e820431b..d182a21a8cf 100644 --- a/pkg/plugin/v3/scaffolds/init.go +++ b/pkg/plugin/v3/scaffolds/init.go @@ -42,9 +42,6 @@ const ( ControllerToolsVersion = "v0.3.0" // KustomizeVersion is the kubernetes-sigs/kustomize version to be used in the project KustomizeVersion = "v3.5.4" - // ControllerRuntimeEnvTestVersion version to be used to download the envtest setup script - // todo: update the tag release when the next version of the project be released with this script - ControllerRuntimeEnvTestVersion = "master" imageName = "controller:latest" ) @@ -115,7 +112,6 @@ func (s *initScaffolder) scaffold() error { BoilerplatePath: s.boilerplatePath, ControllerToolsVersion: ControllerToolsVersion, KustomizeVersion: KustomizeVersion, - ControllerRuntimeEnvTestVersion: ControllerRuntimeEnvTestVersion, }, &templates.Dockerfile{}, &templates.DockerignoreFile{}, diff --git a/pkg/plugin/v3/scaffolds/internal/templates/config/controller/controller_suitetest.go b/pkg/plugin/v3/scaffolds/internal/templates/config/controller/controller_suitetest.go index de0eead25df..bc64e8df2fe 100644 --- a/pkg/plugin/v3/scaffolds/internal/templates/config/controller/controller_suitetest.go +++ b/pkg/plugin/v3/scaffolds/internal/templates/config/controller/controller_suitetest.go @@ -34,8 +34,8 @@ type SuiteTest struct { file.BoilerplateMixin file.ResourceMixin - // CRDDirectoryRelativePath define the Path for the CRD when it is multigroup - CRDDirectoryRelativePath string + // RelativePath define the Path for the root when it is multigroup + RelativePath string } // SetTemplateDefaults implements file.Template @@ -56,9 +56,9 @@ func (f *SuiteTest) SetTemplateDefaults() error { // If is multigroup the path needs to be ../../ since it has // the group dir. - f.CRDDirectoryRelativePath = `".."` + f.RelativePath = `".."` if f.MultiGroup && f.Resource.Group != "" { - f.CRDDirectoryRelativePath = `"..", ".."` + f.RelativePath = `"..", ".."` } return nil @@ -148,9 +148,12 @@ var _ = BeforeSuite(func(done Done) { By("bootstrapping test environment") testEnv = &envtest.Environment{ - CRDDirectoryPaths: []string{filepath.Join({{ .CRDDirectoryRelativePath }}, "config", "crd", "bases")}, + CRDDirectoryPaths: []string{filepath.Join({{ .RelativePath }}, "config", "crd", "bases")}, } + By("configuring env test binaries") + configEnvTestBinaries() + var err error cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) @@ -170,4 +173,31 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) }) + +// configEnvTestBinaries will check if the required binaries are in the bin directory +// of the project and configure to use them. +func configEnvTestBinaries() { + binPath := filepath.Join({{ .RelativePath }}, "bin") + if hasEnvTestBinaries(binPath) { + os.Setenv("KUBEBUILDER_ASSETS", binPath) + } +} + +// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found +// in the path informed +func hasEnvTestBinaries(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { + return false + } + return true +} ` diff --git a/pkg/plugin/v3/scaffolds/internal/templates/gitignore.go b/pkg/plugin/v3/scaffolds/internal/templates/gitignore.go index d78ae478ee0..3910f8d3195 100644 --- a/pkg/plugin/v3/scaffolds/internal/templates/gitignore.go +++ b/pkg/plugin/v3/scaffolds/internal/templates/gitignore.go @@ -46,7 +46,6 @@ const gitignoreTemplate = ` *.so *.dylib bin -testbin/* # Test binary, build with ` + "`go test -c`" + ` *.test diff --git a/pkg/plugin/v3/scaffolds/internal/templates/makefile.go b/pkg/plugin/v3/scaffolds/internal/templates/makefile.go index 34a657aa99a..737c345b69a 100644 --- a/pkg/plugin/v3/scaffolds/internal/templates/makefile.go +++ b/pkg/plugin/v3/scaffolds/internal/templates/makefile.go @@ -34,8 +34,6 @@ type Makefile struct { ControllerToolsVersion string // Kustomize version to use in the project KustomizeVersion string - // ControllerRuntimeEnvTestVersion version to be used to download the envtest setup script - ControllerRuntimeEnvTestVersion string } // SetTemplateDefaults implements input.Template @@ -72,11 +70,24 @@ endif all: manager # Run tests -ENVTEST_ASSETS_DIR=$(shell pwd)/testbin -test: generate fmt vet manifests - mkdir -p ${ENVTEST_ASSETS_DIR} - test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/{{ .ControllerRuntimeEnvTestVersion }}/hack/setup-envtest.sh - source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out +test: generate fmt vet manifests tooling + $(TOOLING); go test ./... -coverprofile cover.out + +# get the kubebuilder tooling +tooling: +ifeq (, $(shell which tooling)) + @{ \ + set -e ;\ + TOOLING_TMP_DIR=$$(mktemp -d) ;\ + cd $$TOOLING_TMP_DIR ;\ + go mod init tmp ;\ + go get sigs.k8s.io/kubebuilder/tooling ;\ + rm -rf $$TOOLING_TMP_DIR ;\ + } +TOOLING=$(GOBIN)/tooling +else +TOOLING=$(shell which tooling) +endif # Build manager binary manager: generate fmt vet diff --git a/testdata/project-v2-addon/Makefile b/testdata/project-v2-addon/Makefile index f6362e191e3..2f1bbbe8a07 100644 --- a/testdata/project-v2-addon/Makefile +++ b/testdata/project-v2-addon/Makefile @@ -14,8 +14,24 @@ endif all: manager # Run tests -test: generate fmt vet manifests - go test ./... -coverprofile cover.out +test: generate fmt vet manifests tooling + $(TOOLING); go test ./... -coverprofile cover.out + +# get the kubebuilder tooling +tooling: +ifeq (, $(shell which tooling)) + @{ \ + set -e ;\ + TOOLING_TMP_DIR=$$(mktemp -d) ;\ + cd $$TOOLING_TMP_DIR ;\ + go mod init tmp ;\ + go get sigs.k8s.io/kubebuilder/tooling ;\ + rm -rf $$TOOLING_TMP_DIR ;\ + } +TOOLING=$(GOBIN)/tooling +else +TOOLING=$(shell which tooling) +endif # Build manager binary manager: generate fmt vet diff --git a/testdata/project-v2-addon/controllers/suite_test.go b/testdata/project-v2-addon/controllers/suite_test.go index 7a1e21d2a31..c4ee5403b91 100644 --- a/testdata/project-v2-addon/controllers/suite_test.go +++ b/testdata/project-v2-addon/controllers/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -57,6 +58,9 @@ var _ = BeforeSuite(func(done Done) { CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, } + By("configuring env test binaries") + configEnvTestBinaries() + var err error cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) @@ -85,3 +89,30 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) }) + +// configEnvTestBinaries will check if the required binaries are in the bin directory +// of the project and configure to use them. +func configEnvTestBinaries() { + binPath := filepath.Join("..", "bin") + if hasEnvTestBinaries(binPath) { + os.Setenv("KUBEBUILDER_ASSETS", binPath) + } +} + +// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found +// in the path informed +func hasEnvTestBinaries(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { + return false + } + return true +} diff --git a/testdata/project-v2-multigroup/Makefile b/testdata/project-v2-multigroup/Makefile index f6362e191e3..2f1bbbe8a07 100644 --- a/testdata/project-v2-multigroup/Makefile +++ b/testdata/project-v2-multigroup/Makefile @@ -14,8 +14,24 @@ endif all: manager # Run tests -test: generate fmt vet manifests - go test ./... -coverprofile cover.out +test: generate fmt vet manifests tooling + $(TOOLING); go test ./... -coverprofile cover.out + +# get the kubebuilder tooling +tooling: +ifeq (, $(shell which tooling)) + @{ \ + set -e ;\ + TOOLING_TMP_DIR=$$(mktemp -d) ;\ + cd $$TOOLING_TMP_DIR ;\ + go mod init tmp ;\ + go get sigs.k8s.io/kubebuilder/tooling ;\ + rm -rf $$TOOLING_TMP_DIR ;\ + } +TOOLING=$(GOBIN)/tooling +else +TOOLING=$(shell which tooling) +endif # Build manager binary manager: generate fmt vet diff --git a/testdata/project-v2-multigroup/controllers/crew/suite_test.go b/testdata/project-v2-multigroup/controllers/crew/suite_test.go index a60ba01768e..65a6b7d6af0 100644 --- a/testdata/project-v2-multigroup/controllers/crew/suite_test.go +++ b/testdata/project-v2-multigroup/controllers/crew/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -57,6 +58,9 @@ var _ = BeforeSuite(func(done Done) { CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, } + By("configuring env test binaries") + configEnvTestBinaries() + var err error cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) @@ -79,3 +83,30 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) }) + +// configEnvTestBinaries will check if the required binaries are in the bin directory +// of the project and configure to use them. +func configEnvTestBinaries() { + binPath := filepath.Join("..", "..", "bin") + if hasEnvTestBinaries(binPath) { + os.Setenv("KUBEBUILDER_ASSETS", binPath) + } +} + +// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found +// in the path informed +func hasEnvTestBinaries(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { + return false + } + return true +} diff --git a/testdata/project-v2-multigroup/controllers/foo.policy/suite_test.go b/testdata/project-v2-multigroup/controllers/foo.policy/suite_test.go index aae53ef9e43..4f6c44b01ce 100644 --- a/testdata/project-v2-multigroup/controllers/foo.policy/suite_test.go +++ b/testdata/project-v2-multigroup/controllers/foo.policy/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -57,6 +58,9 @@ var _ = BeforeSuite(func(done Done) { CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, } + By("configuring env test binaries") + configEnvTestBinaries() + var err error cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) @@ -79,3 +83,30 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) }) + +// configEnvTestBinaries will check if the required binaries are in the bin directory +// of the project and configure to use them. +func configEnvTestBinaries() { + binPath := filepath.Join("..", "..", "bin") + if hasEnvTestBinaries(binPath) { + os.Setenv("KUBEBUILDER_ASSETS", binPath) + } +} + +// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found +// in the path informed +func hasEnvTestBinaries(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { + return false + } + return true +} diff --git a/testdata/project-v2-multigroup/controllers/sea-creatures/suite_test.go b/testdata/project-v2-multigroup/controllers/sea-creatures/suite_test.go index 333399337c4..78cc8c56e0b 100644 --- a/testdata/project-v2-multigroup/controllers/sea-creatures/suite_test.go +++ b/testdata/project-v2-multigroup/controllers/sea-creatures/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -58,6 +59,9 @@ var _ = BeforeSuite(func(done Done) { CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, } + By("configuring env test binaries") + configEnvTestBinaries() + var err error cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) @@ -83,3 +87,30 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) }) + +// configEnvTestBinaries will check if the required binaries are in the bin directory +// of the project and configure to use them. +func configEnvTestBinaries() { + binPath := filepath.Join("..", "..", "bin") + if hasEnvTestBinaries(binPath) { + os.Setenv("KUBEBUILDER_ASSETS", binPath) + } +} + +// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found +// in the path informed +func hasEnvTestBinaries(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { + return false + } + return true +} diff --git a/testdata/project-v2-multigroup/controllers/ship/suite_test.go b/testdata/project-v2-multigroup/controllers/ship/suite_test.go index bff73c3d2c0..1c8344af4ca 100644 --- a/testdata/project-v2-multigroup/controllers/ship/suite_test.go +++ b/testdata/project-v2-multigroup/controllers/ship/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -59,6 +60,9 @@ var _ = BeforeSuite(func(done Done) { CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, } + By("configuring env test binaries") + configEnvTestBinaries() + var err error cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) @@ -87,3 +91,30 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) }) + +// configEnvTestBinaries will check if the required binaries are in the bin directory +// of the project and configure to use them. +func configEnvTestBinaries() { + binPath := filepath.Join("..", "..", "bin") + if hasEnvTestBinaries(binPath) { + os.Setenv("KUBEBUILDER_ASSETS", binPath) + } +} + +// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found +// in the path informed +func hasEnvTestBinaries(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { + return false + } + return true +} diff --git a/testdata/project-v2/Makefile b/testdata/project-v2/Makefile index f6362e191e3..2f1bbbe8a07 100644 --- a/testdata/project-v2/Makefile +++ b/testdata/project-v2/Makefile @@ -14,8 +14,24 @@ endif all: manager # Run tests -test: generate fmt vet manifests - go test ./... -coverprofile cover.out +test: generate fmt vet manifests tooling + $(TOOLING); go test ./... -coverprofile cover.out + +# get the kubebuilder tooling +tooling: +ifeq (, $(shell which tooling)) + @{ \ + set -e ;\ + TOOLING_TMP_DIR=$$(mktemp -d) ;\ + cd $$TOOLING_TMP_DIR ;\ + go mod init tmp ;\ + go get sigs.k8s.io/kubebuilder/tooling ;\ + rm -rf $$TOOLING_TMP_DIR ;\ + } +TOOLING=$(GOBIN)/tooling +else +TOOLING=$(shell which tooling) +endif # Build manager binary manager: generate fmt vet diff --git a/testdata/project-v2/controllers/suite_test.go b/testdata/project-v2/controllers/suite_test.go index 3567d134501..6ab833e3bdc 100644 --- a/testdata/project-v2/controllers/suite_test.go +++ b/testdata/project-v2/controllers/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -57,6 +58,9 @@ var _ = BeforeSuite(func(done Done) { CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, } + By("configuring env test binaries") + configEnvTestBinaries() + var err error cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) @@ -85,3 +89,30 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) }) + +// configEnvTestBinaries will check if the required binaries are in the bin directory +// of the project and configure to use them. +func configEnvTestBinaries() { + binPath := filepath.Join("..", "bin") + if hasEnvTestBinaries(binPath) { + os.Setenv("KUBEBUILDER_ASSETS", binPath) + } +} + +// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found +// in the path informed +func hasEnvTestBinaries(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { + return false + } + return true +} diff --git a/testdata/project-v3-addon/Makefile b/testdata/project-v3-addon/Makefile index 52f5e92d39f..66d02fdc552 100644 --- a/testdata/project-v3-addon/Makefile +++ b/testdata/project-v3-addon/Makefile @@ -14,11 +14,24 @@ endif all: manager # Run tests -ENVTEST_ASSETS_DIR=$(shell pwd)/testbin -test: generate fmt vet manifests - mkdir -p ${ENVTEST_ASSETS_DIR} - test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh - source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out +test: generate fmt vet manifests tooling + $(TOOLING); go test ./... -coverprofile cover.out + +# get the kubebuilder tooling +tooling: +ifeq (, $(shell which tooling)) + @{ \ + set -e ;\ + TOOLING_TMP_DIR=$$(mktemp -d) ;\ + cd $$TOOLING_TMP_DIR ;\ + go mod init tmp ;\ + go get sigs.k8s.io/kubebuilder/tooling ;\ + rm -rf $$TOOLING_TMP_DIR ;\ + } +TOOLING=$(GOBIN)/tooling +else +TOOLING=$(shell which tooling) +endif # Build manager binary manager: generate fmt vet diff --git a/testdata/project-v3-addon/controllers/suite_test.go b/testdata/project-v3-addon/controllers/suite_test.go index 3c600ebd2d6..a3973cad4cf 100644 --- a/testdata/project-v3-addon/controllers/suite_test.go +++ b/testdata/project-v3-addon/controllers/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -57,6 +58,9 @@ var _ = BeforeSuite(func(done Done) { CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, } + By("configuring env test binaries") + configEnvTestBinaries() + var err error cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) @@ -85,3 +89,30 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) }) + +// configEnvTestBinaries will check if the required binaries are in the bin directory +// of the project and configure to use them. +func configEnvTestBinaries() { + binPath := filepath.Join("..", "bin") + if hasEnvTestBinaries(binPath) { + os.Setenv("KUBEBUILDER_ASSETS", binPath) + } +} + +// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found +// in the path informed +func hasEnvTestBinaries(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { + return false + } + return true +} diff --git a/testdata/project-v3-multigroup/Makefile b/testdata/project-v3-multigroup/Makefile index 52f5e92d39f..66d02fdc552 100644 --- a/testdata/project-v3-multigroup/Makefile +++ b/testdata/project-v3-multigroup/Makefile @@ -14,11 +14,24 @@ endif all: manager # Run tests -ENVTEST_ASSETS_DIR=$(shell pwd)/testbin -test: generate fmt vet manifests - mkdir -p ${ENVTEST_ASSETS_DIR} - test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh - source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out +test: generate fmt vet manifests tooling + $(TOOLING); go test ./... -coverprofile cover.out + +# get the kubebuilder tooling +tooling: +ifeq (, $(shell which tooling)) + @{ \ + set -e ;\ + TOOLING_TMP_DIR=$$(mktemp -d) ;\ + cd $$TOOLING_TMP_DIR ;\ + go mod init tmp ;\ + go get sigs.k8s.io/kubebuilder/tooling ;\ + rm -rf $$TOOLING_TMP_DIR ;\ + } +TOOLING=$(GOBIN)/tooling +else +TOOLING=$(shell which tooling) +endif # Build manager binary manager: generate fmt vet diff --git a/testdata/project-v3-multigroup/controllers/crew/suite_test.go b/testdata/project-v3-multigroup/controllers/crew/suite_test.go index 873c85527a5..394c2e3e26f 100644 --- a/testdata/project-v3-multigroup/controllers/crew/suite_test.go +++ b/testdata/project-v3-multigroup/controllers/crew/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -57,6 +58,9 @@ var _ = BeforeSuite(func(done Done) { CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, } + By("configuring env test binaries") + configEnvTestBinaries() + var err error cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) @@ -79,3 +83,30 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) }) + +// configEnvTestBinaries will check if the required binaries are in the bin directory +// of the project and configure to use them. +func configEnvTestBinaries() { + binPath := filepath.Join("..", "..", "bin") + if hasEnvTestBinaries(binPath) { + os.Setenv("KUBEBUILDER_ASSETS", binPath) + } +} + +// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found +// in the path informed +func hasEnvTestBinaries(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { + return false + } + return true +} diff --git a/testdata/project-v3-multigroup/controllers/foo.policy/suite_test.go b/testdata/project-v3-multigroup/controllers/foo.policy/suite_test.go index 0cff9b12f95..0fa741c12fd 100644 --- a/testdata/project-v3-multigroup/controllers/foo.policy/suite_test.go +++ b/testdata/project-v3-multigroup/controllers/foo.policy/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -57,6 +58,9 @@ var _ = BeforeSuite(func(done Done) { CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, } + By("configuring env test binaries") + configEnvTestBinaries() + var err error cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) @@ -79,3 +83,30 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) }) + +// configEnvTestBinaries will check if the required binaries are in the bin directory +// of the project and configure to use them. +func configEnvTestBinaries() { + binPath := filepath.Join("..", "..", "bin") + if hasEnvTestBinaries(binPath) { + os.Setenv("KUBEBUILDER_ASSETS", binPath) + } +} + +// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found +// in the path informed +func hasEnvTestBinaries(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { + return false + } + return true +} diff --git a/testdata/project-v3-multigroup/controllers/sea-creatures/suite_test.go b/testdata/project-v3-multigroup/controllers/sea-creatures/suite_test.go index 96cbdd6fe37..fc332a60b30 100644 --- a/testdata/project-v3-multigroup/controllers/sea-creatures/suite_test.go +++ b/testdata/project-v3-multigroup/controllers/sea-creatures/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -58,6 +59,9 @@ var _ = BeforeSuite(func(done Done) { CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, } + By("configuring env test binaries") + configEnvTestBinaries() + var err error cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) @@ -83,3 +87,30 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) }) + +// configEnvTestBinaries will check if the required binaries are in the bin directory +// of the project and configure to use them. +func configEnvTestBinaries() { + binPath := filepath.Join("..", "..", "bin") + if hasEnvTestBinaries(binPath) { + os.Setenv("KUBEBUILDER_ASSETS", binPath) + } +} + +// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found +// in the path informed +func hasEnvTestBinaries(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { + return false + } + return true +} diff --git a/testdata/project-v3-multigroup/controllers/ship/suite_test.go b/testdata/project-v3-multigroup/controllers/ship/suite_test.go index 14c42187090..139bd4feb97 100644 --- a/testdata/project-v3-multigroup/controllers/ship/suite_test.go +++ b/testdata/project-v3-multigroup/controllers/ship/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -59,6 +60,9 @@ var _ = BeforeSuite(func(done Done) { CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, } + By("configuring env test binaries") + configEnvTestBinaries() + var err error cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) @@ -87,3 +91,30 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) }) + +// configEnvTestBinaries will check if the required binaries are in the bin directory +// of the project and configure to use them. +func configEnvTestBinaries() { + binPath := filepath.Join("..", "..", "bin") + if hasEnvTestBinaries(binPath) { + os.Setenv("KUBEBUILDER_ASSETS", binPath) + } +} + +// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found +// in the path informed +func hasEnvTestBinaries(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { + return false + } + return true +} diff --git a/testdata/project-v3-multigroup/controllers/suite_test.go b/testdata/project-v3-multigroup/controllers/suite_test.go index 2268aac80a6..149131d2057 100644 --- a/testdata/project-v3-multigroup/controllers/suite_test.go +++ b/testdata/project-v3-multigroup/controllers/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -57,6 +58,9 @@ var _ = BeforeSuite(func(done Done) { CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, } + By("configuring env test binaries") + configEnvTestBinaries() + var err error cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) @@ -79,3 +83,30 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) }) + +// configEnvTestBinaries will check if the required binaries are in the bin directory +// of the project and configure to use them. +func configEnvTestBinaries() { + binPath := filepath.Join("..", "bin") + if hasEnvTestBinaries(binPath) { + os.Setenv("KUBEBUILDER_ASSETS", binPath) + } +} + +// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found +// in the path informed +func hasEnvTestBinaries(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { + return false + } + return true +} diff --git a/testdata/project-v3/Makefile b/testdata/project-v3/Makefile index 52f5e92d39f..66d02fdc552 100644 --- a/testdata/project-v3/Makefile +++ b/testdata/project-v3/Makefile @@ -14,11 +14,24 @@ endif all: manager # Run tests -ENVTEST_ASSETS_DIR=$(shell pwd)/testbin -test: generate fmt vet manifests - mkdir -p ${ENVTEST_ASSETS_DIR} - test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh - source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out +test: generate fmt vet manifests tooling + $(TOOLING); go test ./... -coverprofile cover.out + +# get the kubebuilder tooling +tooling: +ifeq (, $(shell which tooling)) + @{ \ + set -e ;\ + TOOLING_TMP_DIR=$$(mktemp -d) ;\ + cd $$TOOLING_TMP_DIR ;\ + go mod init tmp ;\ + go get sigs.k8s.io/kubebuilder/tooling ;\ + rm -rf $$TOOLING_TMP_DIR ;\ + } +TOOLING=$(GOBIN)/tooling +else +TOOLING=$(shell which tooling) +endif # Build manager binary manager: generate fmt vet diff --git a/testdata/project-v3/controllers/suite_test.go b/testdata/project-v3/controllers/suite_test.go index 0e1020cb400..b47091d7db5 100644 --- a/testdata/project-v3/controllers/suite_test.go +++ b/testdata/project-v3/controllers/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -57,6 +58,9 @@ var _ = BeforeSuite(func(done Done) { CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, } + By("configuring env test binaries") + configEnvTestBinaries() + var err error cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) @@ -85,3 +89,30 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) }) + +// configEnvTestBinaries will check if the required binaries are in the bin directory +// of the project and configure to use them. +func configEnvTestBinaries() { + binPath := filepath.Join("..", "bin") + if hasEnvTestBinaries(binPath) { + os.Setenv("KUBEBUILDER_ASSETS", binPath) + } +} + +// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found +// in the path informed +func hasEnvTestBinaries(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { + return false + } + return true +} diff --git a/tooling/go.mod b/tooling/go.mod new file mode 100644 index 00000000000..647e55eabd1 --- /dev/null +++ b/tooling/go.mod @@ -0,0 +1,8 @@ +module sigs.k8s.io/kubebuilder/tooling + +go 1.13 + +require ( + github.com/sirupsen/logrus v1.7.0 + golang.org/x/sys v0.0.0-20201008064518-c1f3e3309c71 // indirect +) diff --git a/tooling/go.sum b/tooling/go.sum new file mode 100644 index 00000000000..844153dc09c --- /dev/null +++ b/tooling/go.sum @@ -0,0 +1,12 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= +github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201008064518-c1f3e3309c71 h1:ZPX6UakxrJCxWiyGWpXtFY+fp86Esy7xJT/jJCG8bgU= +golang.org/x/sys v0.0.0-20201008064518-c1f3e3309c71/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/tooling/main.go b/tooling/main.go new file mode 100644 index 00000000000..f6b68126703 --- /dev/null +++ b/tooling/main.go @@ -0,0 +1,124 @@ +/* +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. +*/ + +package main + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" + + log "github.com/sirupsen/logrus" +) + +// defaultKubebuilderPath is the fixed value defined in the controller-runtime to looking for the +// binaries. +const defaultKubebuilderPath = "/usr/local/kubebuilder/bin" + +func main() { + log.Infof("Checking EnvTest binaries") + + if hasBinaries(defaultKubebuilderPath) { + log.Infof("EnvTest binaries found in : ", defaultKubebuilderPath) + os.Exit(0) + } + + currentPath, err := os.Getwd() + if err != nil { + log.Error(err) + os.Exit(1) + } + + binPath := filepath.Join(currentPath, "bin") + + if hasBinaries(binPath) { + log.Info("EnvTest binaries found in bin/") + os.Exit(0) + } + + log.Infof("Setting up EnvTest") + log.Infof("Downloading EnvTest tools") + + envtest_tools_archive_name := getEnvToolsArchiveName() + envtest_tools_download_url := "https://storage.googleapis.com/kubebuilder-tools/" + envtest_tools_archive_name + + cmd := exec.Command("curl", "-sL", envtest_tools_download_url, "-o", envtest_tools_archive_name) + if err := cmd.Run(); err != nil { + log.Error(err) + os.Exit(1) + } + + if _, err := os.Stat(binPath); os.IsNotExist(err) { + log.Infof("Creating bin/ directory") + err = os.Mkdir(binPath, 0755) + if err != nil { + log.Errorf("error to create the bin/ directory :", err) + os.Exit(1) + } + } + + cmd = exec.Command("tar", "-C", binPath, "--strip-components=2", "-zvxf", envtest_tools_archive_name) + if err := cmd.Run(); err != nil { + log.Error(err) + os.Exit(1) + } + + cmd = exec.Command("rm", "-rf", filepath.Join(currentPath,envtest_tools_archive_name)) + if err := cmd.Run(); err != nil { + log.Error(err) + os.Exit(1) + } +} + +// getEnvToolsArchiveName will return the name of the env tools archive based or the arch and so +func getEnvToolsArchiveName() string { + cmd := exec.Command("go", "env", "GOARCH") + goarch, err := cmd.CombinedOutput() + if err != nil { + log.Error(err) + os.Exit(1) + } + + cmd = exec.Command("go", "env", "GOOS") + goos, err := cmd.CombinedOutput() + if err != nil { + log.Error(err) + os.Exit(1) + } + + return fmt.Sprintf("kubebuilder-tools-%s-%s-%s.tar.gz", "1.16.4", strings.TrimSpace(string(goos)), strings.TrimSpace(string(goarch))) +} + + +// hasBinaries will return true when the testbin exist and has the binaries +func hasBinaries(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) { + return false + } + if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) { + return false + } + return true +} +