22# Image URL to use all building/pushing image targets
33IMG ?= controller:latest
44# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
5- ENVTEST_K8S_VERSION = 1.22
5+ ENVTEST_K8S_VERSION = 1.28.0
66
77# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
88ifeq (,$(shell go env GOBIN) )
1111GOBIN =$(shell go env GOBIN)
1212endif
1313
14+ # CONTAINER_TOOL defines the container tool to be used for building images.
15+ # Be aware that the target commands are only tested with Docker which is
16+ # scaffolded by default. However, you might want to replace it to use other
17+ # tools. (i.e. podman)
18+ CONTAINER_TOOL ?= docker
19+
1420# Setting SHELL to bash allows bash commands to be executed by recipes.
15- # This is a requirement for 'setup-envtest.sh' in the test target.
1621# Options are set to exit when a recipe line exits non-zero or a piped command fails.
1722SHELL = /usr/bin/env bash -o pipefail
1823.SHELLFLAGS = -ec
@@ -24,7 +29,7 @@ all: build
2429
2530# The help target prints out all targets with their descriptions organized
2631# beneath their categories. The categories are represented by '##@' and the
27- # target descriptions by '##'. The awk commands is responsible for reading the
32+ # target descriptions by '##'. The awk command is responsible for reading the
2833# entire set of makefiles included in this invocation, looking for lines of the
2934# file as xyz: ## something, and then pretty-format the target and help. Then,
3035# if there's a line with ##@ something, that gets pretty-printed as a category.
@@ -57,29 +62,45 @@ vet: ## Run go vet against code.
5762
5863.PHONY : test
5964test : manifests generate fmt vet envtest # # Run tests.
60- KUBEBUILDER_ASSETS=" $( shell $( ENVTEST) use $( ENVTEST_K8S_VERSION) -p path) " go test ./... -coverprofile cover.out
61-
62- .PHONY : test-debug
63- test-debug : manifests generate fmt vet envtest # # Run tests.
64- KUBEBUILDER_ASSETS=" $( shell $( ENVTEST) use $( ENVTEST_K8S_VERSION) -p path) " go test ./... -coverprofile cover.out -ginkgo.v -ginkgo.progress -test.v
65+ KUBEBUILDER_ASSETS=" $( shell $( ENVTEST) use $( ENVTEST_K8S_VERSION) --bin-dir $( LOCALBIN) -p path) " go test ./... -coverprofile cover.out
6566
6667# #@ Build
6768
6869.PHONY : build
69- build : generate fmt vet # # Build manager binary.
70+ build : manifests generate fmt vet # # Build manager binary.
7071 go build -o bin/manager main.go
7172
7273.PHONY : run
7374run : manifests generate fmt vet # # Run a controller from your host.
74- go run ./main.go
75+ ENABLE_WEBHOOKS= " false " go run ./main.go
7576
77+ # If you wish to build the manager image targeting other platforms you can use the --platform flag.
78+ # (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
79+ # More info: https://docs.docker.com/develop/develop-images/build_enhancements/
7680.PHONY : docker-build
77- docker-build : test # # Build docker image with the manager.
78- docker build -t ${IMG} .
81+ docker-build : # # Build docker image with the manager.
82+ $( CONTAINER_TOOL ) build -t ${IMG} .
7983
8084.PHONY : docker-push
8185docker-push : # # Push docker image with the manager.
82- docker push ${IMG}
86+ $(CONTAINER_TOOL ) push ${IMG}
87+
88+ # PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
89+ # architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
90+ # - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
91+ # - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
92+ # - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
93+ # To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
94+ PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
95+ .PHONY : docker-buildx
96+ docker-buildx : # # Build and push docker image for the manager for cross-platform support
97+ # copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
98+ sed -e ' 1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
99+ - $(CONTAINER_TOOL ) buildx create --name project-v3-builder
100+ $(CONTAINER_TOOL ) buildx use project-v3-builder
101+ - $(CONTAINER_TOOL ) buildx build --push --platform=$(PLATFORMS ) --tag ${IMG} -f Dockerfile.cross .
102+ - $(CONTAINER_TOOL ) buildx rm project-v3-builder
103+ rm Dockerfile.cross
83104
84105# #@ Deployment
85106
@@ -89,46 +110,54 @@ endif
89110
90111.PHONY : install
91112install : manifests kustomize # # Install CRDs into the K8s cluster specified in ~/.kube/config.
92- $(KUSTOMIZE ) build config/crd | kubectl apply -f -
113+ $(KUSTOMIZE ) build config/crd | $( KUBECTL ) apply -f -
93114
94115.PHONY : uninstall
95116uninstall : manifests kustomize # # Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
96- $(KUSTOMIZE ) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found ) -f -
117+ $(KUSTOMIZE ) build config/crd | $( KUBECTL ) delete --ignore-not-found=$(ignore-not-found ) -f -
97118
98119.PHONY : deploy
99120deploy : manifests kustomize # # Deploy controller to the K8s cluster specified in ~/.kube/config.
100121 cd config/manager && $(KUSTOMIZE ) edit set image controller=${IMG}
101- $(KUSTOMIZE ) build config/default | kubectl apply -f -
122+ $(KUSTOMIZE ) build config/default | $( KUBECTL ) apply -f -
102123
103124.PHONY : undeploy
104125undeploy : # # Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
105- $(KUSTOMIZE ) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found ) -f -
126+ $(KUSTOMIZE ) build config/default | $( KUBECTL ) delete --ignore-not-found=$(ignore-not-found ) -f -
106127
107- CONTROLLER_GEN = $(shell pwd) /bin/controller-gen
108- .PHONY : controller-gen
109- controller-gen : # # Download controller-gen locally if necessary.
110- $(call go-get-tool,$(CONTROLLER_GEN ) ,sigs.k8s.io/controller-tools/cmd/controller-gen@v0.7.0)
128+ # #@ Build Dependencies
129+
130+ # # Location to install dependencies to
131+ LOCALBIN ?= $(shell pwd) /bin
132+ $(LOCALBIN ) :
133+ mkdir -p $(LOCALBIN )
134+
135+ # # Tool Binaries
136+ KUBECTL ?= kubectl
137+ KUSTOMIZE ?= $(LOCALBIN ) /kustomize
138+ CONTROLLER_GEN ?= $(LOCALBIN ) /controller-gen
139+ ENVTEST ?= $(LOCALBIN ) /setup-envtest
140+
141+ # # Tool Versions
142+ KUSTOMIZE_VERSION ?= v5.1.1
143+ CONTROLLER_TOOLS_VERSION ?= v0.14.0
111144
112- KUSTOMIZE = $(shell pwd) /bin/kustomize
113145.PHONY : kustomize
114- kustomize : # # Download kustomize locally if necessary.
115- $(call go-get-tool,$(KUSTOMIZE ) ,sigs.k8s.io/kustomize/kustomize/v3@v3.8.7)
146+ kustomize : $(KUSTOMIZE ) # # Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
147+ $(KUSTOMIZE ) : $(LOCALBIN )
148+ @if test -x $(LOCALBIN ) /kustomize && ! $(LOCALBIN ) /kustomize version | grep -q $(KUSTOMIZE_VERSION ) ; then \
149+ echo " $( LOCALBIN) /kustomize version is not expected $( KUSTOMIZE_VERSION) . Removing it before installing." ; \
150+ rm -rf $(LOCALBIN ) /kustomize; \
151+ fi
152+ test -s $(LOCALBIN ) /kustomize || GOBIN=$(LOCALBIN ) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION )
153+
154+ .PHONY : controller-gen
155+ controller-gen : $(CONTROLLER_GEN ) # # Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
156+ $(CONTROLLER_GEN ) : $(LOCALBIN )
157+ test -s $(LOCALBIN ) /controller-gen && $(LOCALBIN ) /controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION ) || \
158+ GOBIN=$(LOCALBIN ) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION )
116159
117- ENVTEST = $(shell pwd) /bin/setup-envtest
118160.PHONY : envtest
119- envtest : # # Download envtest-setup locally if necessary.
120- $(call go-get-tool,$(ENVTEST ) ,sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
121-
122- # go-get-tool will 'go get' any package $2 and install it to $1.
123- PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST ) ) ) )
124- define go-get-tool
125- @[ -f $(1 ) ] || { \
126- set -e ;\
127- TMP_DIR=$$(mktemp -d ) ;\
128- cd $$TMP_DIR ;\
129- go mod init tmp ;\
130- echo "Downloading $(2 ) " ;\
131- GOBIN=$(PROJECT_DIR ) /bin go get $(2 ) ;\
132- rm -rf $$TMP_DIR ;\
133- }
134- endef
161+ envtest : $(ENVTEST ) # # Download envtest-setup locally if necessary.
162+ $(ENVTEST ) : $(LOCALBIN )
163+ test -s $(LOCALBIN ) /setup-envtest || GOBIN=$(LOCALBIN ) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
0 commit comments