From b169d16dddfd9b503ee201aba96dd28a9c0fd4c7 Mon Sep 17 00:00:00 2001 From: soorena776 Date: Fri, 4 Oct 2019 13:36:22 -0700 Subject: [PATCH] move controller-gen tool to golang.mk as generate target depends on it Signed-off-by: soorena776 --- makelib/golang.mk | 21 ++++++++++++++++++--- makelib/kubebuilder.mk | 36 +++++++----------------------------- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/makelib/golang.mk b/makelib/golang.mk index 36fa455..0b5771d 100644 --- a/makelib/golang.mk +++ b/makelib/golang.mk @@ -81,6 +81,8 @@ DEP := $(TOOLS_HOST_DIR)/dep-$(DEP_VERSION) GOJUNIT := $(TOOLS_HOST_DIR)/go-junit-report GOCOVER_COBERTURA := $(TOOLS_HOST_DIR)/gocover-cobertura GOIMPORTS := $(TOOLS_HOST_DIR)/goimports +CONTROLLER_GEN_VERSION ?= v0.2.1 +CONTROLLERGEN := $(TOOLS_HOST_DIR)/controller-gen GO := go GOHOST := GOOS=$(GOHOSTOS) GOARCH=$(GOHOSTARCH) go @@ -282,11 +284,11 @@ go.clean: go.distclean: @rm -rf $(GO_VENDOR_DIR) $(GO_PKG_DIR) -go.generate: $(GOIMPORTS) +go.generate: $(GOIMPORTS) $(CONTROLLERGEN) @$(INFO) go generate $(PLATFORM) - @CGO_ENABLED=0 $(GOHOST) generate $(GO_COMMON_FLAGS) $(GO_PACKAGES) $(GO_INTEGRATION_TEST_PACKAGES) || $(FAIL) - @$(OK) go generate $(PLATFORM) + @CGO_ENABLED=0 CONTROLLERGEN=$(CONTROLLERGEN) $(GOHOST) generate $(GO_COMMON_FLAGS) $(GO_PACKAGES) $(GO_INTEGRATION_TEST_PACKAGES) || $(FAIL) @find $(GO_SUBDIRS) $(GO_INTEGRATION_TESTS_SUBDIRS) -type f -name 'zz_generated*' -exec $(GOIMPORTS) -l -w -local $(GO_PROJECT) {} \; + @$(OK) go generate $(PLATFORM) .PHONY: go.init go.build go.install go.test.unit go.test.integration go.test.codecov go.lint go.vet go.fmt go.generate @@ -387,3 +389,16 @@ $(GOCOVER_COBERTURA): @GO111MODULE=off GOPATH=$(TOOLS_HOST_DIR)/tmp-gocover-cobertura GOBIN=$(TOOLS_HOST_DIR) $(GOHOST) get github.com/t-yuki/gocover-cobertura || rm -fr $(TOOLS_HOST_DIR)/tmp-covcover-cobertura || $(FAIL) @rm -fr $(TOOLS_HOST_DIR)/tmp-gocover-cobertura @$(OK) installing gocover-cobertura + +$(CONTROLLERGEN): + @$(INFO) installing controller-gen @$(CONTROLLER_GEN_VERSION) + @mkdir -p $(TOOLS_HOST_DIR)/tmp-controllergen || $(FAIL) + + @# `go get` only supports versioned go packages when GO111MODULE=on + @# since $(TOOLS_HOST_DIR) is under $(GO_PROJECT), make a temp folder which has a go.mod, + @# so that $(GO_PROJECT)/go.mod doesn't get modified because of running `go get` + @cd $(TOOLS_HOST_DIR)/tmp-controllergen; rm -f go.mod; GO111MODULE=on $(GOHOST) mod init tmp-controllergen + @cd $(TOOLS_HOST_DIR)/tmp-controllergen; GOPATH=$(abspath $(GO_PKG_DIR)) GO111MODULE=on GOBIN=$(TOOLS_HOST_DIR) $(GOHOST) get sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION) || $(FAIL) + @rm -fr $(TOOLS_HOST_DIR)/tmp-controllergen + + @$(OK) installing controller-gen @$(CONTROLLER_GEN_VERSION) diff --git a/makelib/kubebuilder.mk b/makelib/kubebuilder.mk index c23a502..90d2962 100644 --- a/makelib/kubebuilder.mk +++ b/makelib/kubebuilder.mk @@ -17,7 +17,6 @@ # the version of kubebuilder to use KUBEBUILDER_VERSION ?= 1.0.8 -CONTROLLER_GEN_VERSION ?= v0.2.1 KUBEBUILDER := $(TOOLS_HOST_DIR)/kubebuilder-$(KUBEBUILDER_VERSION) # these are use by the kube builder test harness @@ -32,15 +31,13 @@ export TEST_ASSET_KUBE_APISERVER TEST_ASSET_ETCD TEST_ASSET_KUBECTL -include golang.mk ifeq ($(CRD_DIR),) -$(error please set CRD_DIR prior to including kubebuilder.mk) +$(error please set CRD_DIR (directories where CRD manifests will be saved) prior to including kubebuilder.mk) endif -ifeq ($(CONTROLLERGEN_PATHS),) -$(error please set CONTROLLERGEN_PATHS prior to including kubebuilder.mk) +ifeq ($(API_DIR),) +$(error please set API_DIR ()(directories where KUBEBUILDER API types are defined) prior to including kubebuilder.mk) endif -CONTROLLERGEN := $(TOOLS_HOST_DIR)/controller-gen - # ==================================================================================== # Kubebuilder Targets @@ -49,16 +46,9 @@ kubebuilder.manifests: $(CONTROLLERGEN) @$(INFO) Generating CRD manifests @# first delete the CRD_DIR, to remove the CRDs of types that no longer exist @rm -rf $(CRD_DIR) - @$(CONTROLLERGEN) crd:trivialVersions=true paths=$(CONTROLLERGEN_PATHS) output:dir=$(CRD_DIR) + @$(CONTROLLERGEN) crd:trivialVersions=true paths=$(API_DIR) output:dir=$(CRD_DIR) @$(OK) Generating CRD manifests -# Generate controller -kubebuilder.generate: $(GOIMPORTS) $(CONTROLLERGEN) - @$(INFO) go generate $(PLATFORM) - @CGO_ENABLED=0 CONTROLLERGEN=$(CONTROLLERGEN) $(GOHOST) generate $(GO_COMMON_FLAGS) $(GO_PACKAGES) $(GO_INTEGRATION_TEST_PACKAGES) || $(FAIL) - @find $(GO_SUBDIRS) $(GO_INTEGRATION_TESTS_SUBDIRS) -type f -name 'zz_generated*' -exec $(GOIMPORTS) -l -w -local $(GO_PROJECT) {} \; - @$(OK) go generate $(PLATFORM) - # ==================================================================================== # Common Targets @@ -69,8 +59,7 @@ test.init: $(KUBEBUILDER) define KUBEBULDER_HELPTEXT Kubebuilder Targets: - bin run kubebuilder binary, pass args by setting ARGS="" - contgen Runs go code generation, to execute controller-gen tool + bin Run kubebuilder binary, pass args by setting ARGS="" manifests Generates Kubernetes custom resources manifests (e.g. CRDs RBACs, ...) endef @@ -80,6 +69,7 @@ kubebuilder.help: @echo "$$KUBEBULDER_HELPTEXT" help-special: kubebuilder.help +manifests: kubebuilder.manifests contgen: kubebuilder.generate manifests: kubebuilder.manifests @@ -87,7 +77,7 @@ manifests: kubebuilder.manifests kubebuilder.bin: $(KUBEBUILDER) @$(KUBEBUILDER)/kubebuilder $(ARGS) -.PHONY: kubebuilder.help kubebuilder.bin kubebuilder.generate kubebuilder.manifests +.PHONY: kubebuilder.help kubebuilder.bin kubebuilder.manifests # ==================================================================================== # tools @@ -101,16 +91,4 @@ $(KUBEBUILDER): @rm -fr $(TOOLS_HOST_DIR)/tmp @$(OK) installing kubebuilder $(KUBEBUILDER_VERSION) -$(CONTROLLERGEN): - @$(INFO) installing controller-gen @$(CONTROLLER_GEN_VERSION) - @mkdir -p $(TOOLS_HOST_DIR)/tmp-controllergen || $(FAIL) - - @# `go get` only supports versioned go packages when GO111MODULE=on - @# since $(TOOLS_HOST_DIR) is under $(GO_PROJECT), make a temp folder which has a go.mod, - @# so that $(GO_PROJECT)/go.mod doesn't get modified because of running `go get` - @cd $(TOOLS_HOST_DIR)/tmp-controllergen; rm -f go.mod; GO111MODULE=on $(GOHOST) mod init tmp-controllergen - @cd $(TOOLS_HOST_DIR)/tmp-controllergen; GOPATH=$(abspath $(GO_PKG_DIR)) GO111MODULE=on GOBIN=$(TOOLS_HOST_DIR) $(GOHOST) get sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION) || $(FAIL) - @rm -fr $(TOOLS_HOST_DIR)/tmp-controllergen - - @$(OK) installing controller-gen @$(CONTROLLER_GEN_VERSION)