From 71a77cdcea29890aedfebca31159062341d12112 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Tue, 10 Nov 2020 22:30:47 -0800 Subject: [PATCH 1/2] (go/v3-alpha) Makefile: install kustomize and controller-gen to '/bin', and always use these instead of global binaries --- .../scaffolds/internal/templates/makefile.go | 48 ++++++++----------- testdata/project-v3-addon/Makefile | 48 ++++++++----------- testdata/project-v3-multigroup/Makefile | 48 ++++++++----------- testdata/project-v3/Makefile | 48 ++++++++----------- 4 files changed, 80 insertions(+), 112 deletions(-) diff --git a/pkg/plugin/v3/scaffolds/internal/templates/makefile.go b/pkg/plugin/v3/scaffolds/internal/templates/makefile.go index d69b27741cb..c7cdee637dd 100644 --- a/pkg/plugin/v3/scaffolds/internal/templates/makefile.go +++ b/pkg/plugin/v3/scaffolds/internal/templates/makefile.go @@ -127,35 +127,27 @@ docker-build: test docker-push: docker push ${IMG} -# find or download controller-gen -# download controller-gen if necessary +# Download controller-gen locally if necessary +CONTROLLER_GEN = $(shell pwd)/bin/controller-gen controller-gen: -ifeq (, $(shell which controller-gen)) - @{ \ - set -e ;\ - CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\ - cd $$CONTROLLER_GEN_TMP_DIR ;\ - go mod init tmp ;\ - go get sigs.k8s.io/controller-tools/cmd/controller-gen@{{ .ControllerToolsVersion }} ;\ - rm -rf $$CONTROLLER_GEN_TMP_DIR ;\ - } -CONTROLLER_GEN=$(GOBIN)/controller-gen -else -CONTROLLER_GEN=$(shell which controller-gen) -endif + $(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@{{ .ControllerToolsVersion }}) +# Download kustomize locally if necessary +KUSTOMIZE = $(shell pwd)/bin/kustomize kustomize: -ifeq (, $(shell which kustomize)) - @{ \ - set -e ;\ - KUSTOMIZE_GEN_TMP_DIR=$$(mktemp -d) ;\ - cd $$KUSTOMIZE_GEN_TMP_DIR ;\ - go mod init tmp ;\ - go get sigs.k8s.io/kustomize/kustomize/v3@{{ .KustomizeVersion }} ;\ - rm -rf $$KUSTOMIZE_GEN_TMP_DIR ;\ - } -KUSTOMIZE=$(GOBIN)/kustomize -else -KUSTOMIZE=$(shell which kustomize) -endif + $(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v3@{{ .KustomizeVersion }}) + +# go-get-tool will 'go get' any package $2 and install it to $1. +PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) +define go-get-tool +@[ -f $(1) ] || { \ +set -e ;\ +TMP_DIR=$$(mktemp -d) ;\ +cd $$TMP_DIR ;\ +go mod init tmp ;\ +echo "Downloading $(2)" ;\ +GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\ +rm -rf $$TMP_DIR ;\ +} +endef ` diff --git a/testdata/project-v3-addon/Makefile b/testdata/project-v3-addon/Makefile index 981f020fecc..b7fe96c750a 100644 --- a/testdata/project-v3-addon/Makefile +++ b/testdata/project-v3-addon/Makefile @@ -69,34 +69,26 @@ docker-build: test docker-push: docker push ${IMG} -# find or download controller-gen -# download controller-gen if necessary +# Download controller-gen locally if necessary +CONTROLLER_GEN = $(shell pwd)/bin/controller-gen controller-gen: -ifeq (, $(shell which controller-gen)) - @{ \ - set -e ;\ - CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\ - cd $$CONTROLLER_GEN_TMP_DIR ;\ - go mod init tmp ;\ - go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.3.0 ;\ - rm -rf $$CONTROLLER_GEN_TMP_DIR ;\ - } -CONTROLLER_GEN=$(GOBIN)/controller-gen -else -CONTROLLER_GEN=$(shell which controller-gen) -endif + $(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.3.0) +# Download kustomize locally if necessary +KUSTOMIZE = $(shell pwd)/bin/kustomize kustomize: -ifeq (, $(shell which kustomize)) - @{ \ - set -e ;\ - KUSTOMIZE_GEN_TMP_DIR=$$(mktemp -d) ;\ - cd $$KUSTOMIZE_GEN_TMP_DIR ;\ - go mod init tmp ;\ - go get sigs.k8s.io/kustomize/kustomize/v3@v3.5.4 ;\ - rm -rf $$KUSTOMIZE_GEN_TMP_DIR ;\ - } -KUSTOMIZE=$(GOBIN)/kustomize -else -KUSTOMIZE=$(shell which kustomize) -endif + $(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v3@v3.5.4) + +# go-get-tool will 'go get' any package $2 and install it to $1. +PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) +define go-get-tool +@[ -f $(1) ] || { \ +set -e ;\ +TMP_DIR=$$(mktemp -d) ;\ +cd $$TMP_DIR ;\ +go mod init tmp ;\ +echo "Downloading $(2)" ;\ +GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\ +rm -rf $$TMP_DIR ;\ +} +endef diff --git a/testdata/project-v3-multigroup/Makefile b/testdata/project-v3-multigroup/Makefile index 981f020fecc..b7fe96c750a 100644 --- a/testdata/project-v3-multigroup/Makefile +++ b/testdata/project-v3-multigroup/Makefile @@ -69,34 +69,26 @@ docker-build: test docker-push: docker push ${IMG} -# find or download controller-gen -# download controller-gen if necessary +# Download controller-gen locally if necessary +CONTROLLER_GEN = $(shell pwd)/bin/controller-gen controller-gen: -ifeq (, $(shell which controller-gen)) - @{ \ - set -e ;\ - CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\ - cd $$CONTROLLER_GEN_TMP_DIR ;\ - go mod init tmp ;\ - go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.3.0 ;\ - rm -rf $$CONTROLLER_GEN_TMP_DIR ;\ - } -CONTROLLER_GEN=$(GOBIN)/controller-gen -else -CONTROLLER_GEN=$(shell which controller-gen) -endif + $(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.3.0) +# Download kustomize locally if necessary +KUSTOMIZE = $(shell pwd)/bin/kustomize kustomize: -ifeq (, $(shell which kustomize)) - @{ \ - set -e ;\ - KUSTOMIZE_GEN_TMP_DIR=$$(mktemp -d) ;\ - cd $$KUSTOMIZE_GEN_TMP_DIR ;\ - go mod init tmp ;\ - go get sigs.k8s.io/kustomize/kustomize/v3@v3.5.4 ;\ - rm -rf $$KUSTOMIZE_GEN_TMP_DIR ;\ - } -KUSTOMIZE=$(GOBIN)/kustomize -else -KUSTOMIZE=$(shell which kustomize) -endif + $(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v3@v3.5.4) + +# go-get-tool will 'go get' any package $2 and install it to $1. +PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) +define go-get-tool +@[ -f $(1) ] || { \ +set -e ;\ +TMP_DIR=$$(mktemp -d) ;\ +cd $$TMP_DIR ;\ +go mod init tmp ;\ +echo "Downloading $(2)" ;\ +GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\ +rm -rf $$TMP_DIR ;\ +} +endef diff --git a/testdata/project-v3/Makefile b/testdata/project-v3/Makefile index 981f020fecc..b7fe96c750a 100644 --- a/testdata/project-v3/Makefile +++ b/testdata/project-v3/Makefile @@ -69,34 +69,26 @@ docker-build: test docker-push: docker push ${IMG} -# find or download controller-gen -# download controller-gen if necessary +# Download controller-gen locally if necessary +CONTROLLER_GEN = $(shell pwd)/bin/controller-gen controller-gen: -ifeq (, $(shell which controller-gen)) - @{ \ - set -e ;\ - CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\ - cd $$CONTROLLER_GEN_TMP_DIR ;\ - go mod init tmp ;\ - go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.3.0 ;\ - rm -rf $$CONTROLLER_GEN_TMP_DIR ;\ - } -CONTROLLER_GEN=$(GOBIN)/controller-gen -else -CONTROLLER_GEN=$(shell which controller-gen) -endif + $(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.3.0) +# Download kustomize locally if necessary +KUSTOMIZE = $(shell pwd)/bin/kustomize kustomize: -ifeq (, $(shell which kustomize)) - @{ \ - set -e ;\ - KUSTOMIZE_GEN_TMP_DIR=$$(mktemp -d) ;\ - cd $$KUSTOMIZE_GEN_TMP_DIR ;\ - go mod init tmp ;\ - go get sigs.k8s.io/kustomize/kustomize/v3@v3.5.4 ;\ - rm -rf $$KUSTOMIZE_GEN_TMP_DIR ;\ - } -KUSTOMIZE=$(GOBIN)/kustomize -else -KUSTOMIZE=$(shell which kustomize) -endif + $(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v3@v3.5.4) + +# go-get-tool will 'go get' any package $2 and install it to $1. +PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) +define go-get-tool +@[ -f $(1) ] || { \ +set -e ;\ +TMP_DIR=$$(mktemp -d) ;\ +cd $$TMP_DIR ;\ +go mod init tmp ;\ +echo "Downloading $(2)" ;\ +GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\ +rm -rf $$TMP_DIR ;\ +} +endef From f4d30ae236c494f738ebc9798484dcb9d8bd6644 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Tue, 10 Nov 2020 23:35:06 -0800 Subject: [PATCH 2/2] Makefile: improve golangci-lint installer, clean up generate_testdata.sh,test.sh: use 'command -v', which won't print if a binary isn't found, over 'which' to avoid unnecessary error text --- Makefile | 22 +++++++++------------- generate_testdata.sh | 4 ++-- test.sh | 4 ++-- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index f07ee7a40bc..30f07e844d0 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,6 @@ # Makefile with some common workflow for dev, build and test # export GOPROXY?=https://proxy.golang.org/ -CONTROLLER_GEN_BIN_PATH := $(shell which controller-gen) # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) ifeq (,$(shell go env GOBIN)) @@ -46,7 +45,7 @@ build: ## Build the project locally .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/kubebuilder $(GOBIN)/kubebuilder ##@ Development @@ -60,22 +59,19 @@ generate-testdata: ## Update/generate the testdata in $GOPATH/src/sigs.k8s.io/ku ./generate_testdata.sh .PHONY: lint -lint: golangci-lint ## Run golangci lint checks - @$(GOLANGCI_LINT) run +lint: golangci-lint ## Run golangci-lint linter + $(GOLANGCI_LINT) run -lint-fix: golangci-lint ## Run golangci lint to automatically perform fixes - @$(GOLANGCI_LINT) run --fix +.PHONY: lint-fix +lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes + $(GOLANGCI_LINT) run --fix +GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint golangci-lint: -ifeq (, $(shell which golangci-lint)) - @{ \ + @[ -f $(GOLANGCI_LINT) ] || { \ set -e ;\ - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.29.0 ;\ + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) v1.29.0 ;\ } -GOLANGCI_LINT=$(shell go env GOPATH)/bin/golangci-lint -else -GOLANGCI_LINT=$(shell which golangci-lint) -endif ##@ Tests diff --git a/generate_testdata.sh b/generate_testdata.sh index b837a1cdbc5..53bf8bd68ee 100755 --- a/generate_testdata.sh +++ b/generate_testdata.sh @@ -39,8 +39,8 @@ scaffold_test_project() { local plugin_flag="${plugin:+--plugins $plugin}" # Remove tool binaries so the correct versions are used for each plugin version. - rm -f "$(which controller-gen)" - rm -f "$(which kustomize)" + rm -f "$(command -v controller-gen)" + rm -f "$(command -v kustomize)" if [ $version == "2" ] || [ $version == "3-alpha" ]; then if [ $version == "2" ] && [ -n "$plugin_flag" ]; then diff --git a/test.sh b/test.sh index bf0a1fbc12a..75303a31a83 100755 --- a/test.sh +++ b/test.sh @@ -92,8 +92,8 @@ EOF function test_project { local project_dir=$1 local version=$2 - rm -f "$(which controller-gen)" - rm -f "$(which kustomize)" + rm -f "$(command -v controller-gen)" + rm -f "$(command -v kustomize)" header_text "performing tests in dir $project_dir for project version v$version" cd testdata/$project_dir make all test