diff --git a/makelib/kubebuilder.mk b/makelib/kubebuilder.mk index 119a70f..2dcbc8c 100644 --- a/makelib/kubebuilder.mk +++ b/makelib/kubebuilder.mk @@ -17,6 +17,7 @@ # 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 @@ -25,6 +26,39 @@ TEST_ASSET_ETCD := $(KUBEBUILDER)/etcd TEST_ASSET_KUBECTL := $(KUBEBUILDER)/kubectl export TEST_ASSET_KUBE_APISERVER TEST_ASSET_ETCD TEST_ASSET_KUBECTL +# ==================================================================================== +# Setup environment + +-include golang.mk + +ifeq ($(CRD_DIR),) +$(error please set CRD_DIR prior to including kubebuilder.mk) +endif + +ifeq ($(CONTROLLERGEN_PATHS),) +$(error please set CONTROLLERGEN_PATHS prior to including kubebuilder.mk) +endif + +CONTROLLERGEN := $(TOOLS_HOST_DIR)/controller-gen + +# ==================================================================================== +# Kubebuilder Targets + +# Generate manifests e.g. CRD, RBAC etc. +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) + @$(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 @@ -35,8 +69,9 @@ test.init: $(KUBEBUILDER) define KUBEBULDER_HELPTEXT Kubebuilder Targets: - bin run kubebuilder binary, pass args by setting ARGS="" - codegen run code generation + bin run kubebuilder binary, pass args by setting ARGS="" + generate,codegen Runs go code generation, to execute controller-gen tool + manifests Generates Kubernetes custom resources manifests (e.g. CRDs RBACs, ...) endef export KUBEBULDER_HELPTEXT @@ -46,10 +81,13 @@ kubebuilder.help: help-special: kubebuilder.help +generate codegen: kubebuilder.generate +manifests: kubebuilder.manifests + kubebuilder.bin: $(KUBEBUILDER) @$(KUBEBUILDER)/kubebuilder $(ARGS) -.PHONY: codegen kubebuilder.help kubebuilder.bin +.PHONY: kubebuilder.help kubebuilder.bin kubebuilder.generate kubebuilder.manifests # ==================================================================================== # tools @@ -63,4 +101,16 @@ $(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)