11SHELL := bash
22platform := $(shell uname | tr A-Z a-z)
33
4+ # ## Helper functions
5+ # ## https://stackoverflow.com/questions/10858261/how-to-abort-makefile-if-variable-not-set
6+ check_defined = \
7+ $(strip $(foreach 1,$1, \
8+ $(call __check_defined,$1,$(strip $(value 2) ) ) ) )
9+ __check_defined = \
10+ $(if $(value $1) ,, \
11+ $(error Undefined $1$(if $2, ($2) )$(if $(value @) , \
12+ required by target '$@ ') ) )
13+ # ##
14+
415# runs the target list by default
516.DEFAULT_GOAL = list
617
718# Insert a comment starting with '##' after a target, and it will be printed by 'make' and 'make list'
8- list : # # list Makefile targets
19+ list : # # List Makefile targets
920 @grep -E ' ^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST ) | sort | awk ' BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
1021
11- # ## Tools
12- # Allows flexbility to use other build kits, like nerdctl
13- BUILD_KIT ?= /usr/local/bin/docker
22+ # ############
23+ # ## Tools ###
24+ # ############
25+
26+ # Allows flexibility to use other build kits, like nerdctl
27+ BUILD_KIT ?= docker
1428
1529define get_mod_code_generator
1630echo "Only go get & mod k8s.io/code-generator, but do not install it"
1731echo "⚠️ Keep it at the same version as captured in go.mod, otherwise we may end up with version inconsistencies"
1832awk '/k8s.io\/code-generator/ { system("go get -d " $$1 "@" $$2) }' go.mod
1933endef
20- install-tools :
34+ install-tools : # # Install tooling required to configure and build this repo
2135 go mod download
2236 @echo " Install all tools..."
2337 @awk -F ' "' ' /_/ && !/k8s.io\/code-generator/ { system("go install " $$2) }' tools/tools.go
@@ -52,8 +66,21 @@ kubebuilder-assets: $(KUBEBUILDER_ASSETS)
5266$(KUBEBUILDER_ASSETS ) :
5367 setup-envtest --os $(platform ) --arch $(ARCHITECTURE ) --bin-dir $(LOCAL_TESTBIN ) use $(ENVTEST_K8S_VERSION )
5468
55- # ## Targets
56-
69+ # https://github.com/carvel-dev/ytt/releases
70+ YTT_VERSION ?= v0.50.0
71+ YTT = $(LOCAL_BIN ) /ytt-$(YTT_VERSION ) -$(platform ) -$(ARCHITECTURE )
72+ .PHONY : ytt
73+ ytt : | $(YTT )
74+ $(YTT ) : | $(LOCAL_BIN )
75+ @printf " Downloading and installing Carvel YTT\n"
76+ @curl -sSL -o $(YTT ) https://github.com/carvel-dev/ytt/releases/download/$(YTT_VERSION ) /ytt-$(platform ) -$(ARCHITECTURE )
77+ @chmod +x $(YTT )
78+ @ln -s $(LOCAL_BIN ) /ytt-$(YTT_VERSION ) -$(platform ) -$(ARCHITECTURE ) $(LOCAL_BIN ) /ytt
79+ @printf " Carvel YTT $( YTT_VERSION) installed locally\n"
80+
81+ # #############
82+ # ### Tests ###
83+ # #############
5784.PHONY : unit-tests
5885unit-tests ::install-tools # # Run unit tests
5986unit-tests ::$(KUBEBUILDER_ASSETS )
@@ -79,12 +106,18 @@ integration-tests::just-integration-tests
79106just-integration-tests : $(KUBEBUILDER_ASSETS )
80107 ginkgo --randomize-all -r -p $(GINKGO_EXTRA ) controllers/
81108
109+ .PHONY : local-tests
82110local-tests : unit-tests integration-tests # # Run all local tests (unit & integration)
83111
84- system-tests : # # run end-to-end tests against Kubernetes cluster defined in ~/.kube/config. Expects cluster operator and messaging topology operator to be installed in the cluster
112+ .PHONY : system-tests
113+ system-tests : # # Run E2E tests using current context in ~/.kube/config. Expects cluster operator and topology operator to be installed in the cluster
85114 NAMESPACE=" rabbitmq-system" ginkgo --randomize-all -r $(GINKGO_EXTRA ) system_tests/
86115
87- # Build manager binary
116+
117+ # ##################
118+ # ## Build & Run ###
119+ # ##################
120+ .PHONY : manager
88121manager : generate fmt vet vuln
89122 go build -o bin/manager main.go
90123
@@ -95,49 +128,32 @@ manager: generate fmt vet vuln
95128# https://github.com/telepresenceio/telepresence is one way to do this (just run
96129# `telepresence connect` and services like `test-service.test-namespace.svc.cluster.local`
97130# will resolve properly).
131+ .PHONY : run
98132run : generate fmt vet vuln manifests install just-run
99133
134+ .PHONY : just-run
100135just-run : # # Just runs 'go run main.go' without regenerating any manifests or deploying RBACs
101136 KUBE_CONFIG=${HOME} /.kube/config OPERATOR_NAMESPACE=rabbitmq-system ENABLE_WEBHOOKS=false ENABLE_DEBUG_PPROF=true go run ./main.go -metrics-bind-address 127.0.0.1:8080
102137
103- # Install CRDs into a cluster
138+ .PHONY : install
104139install : manifests
105140 kustomize build config/crd | kubectl apply -f -
106141
107- # Uninstall CRDs from a cluster
142+ .PHONY : uninstall
108143uninstall : manifests
109144 kustomize build config/crd | kubectl delete -f -
110145
146+ .PHONY : deploy-manager
111147deploy-manager : cmctl
112148 $(CMCTL ) check api --wait=2m
113149 kustomize build config/default/overlays/cert-manager/ | kubectl apply -f -
114150
115- deploy : manifests deploy-rbac deploy-manager
116-
117- destroy :
118- kustomize build config/rbac | kubectl delete --ignore-not-found=true -f -
119- kustomize build config/default/base | kubectl delete --ignore-not-found=true -f -
120-
121- # Deploy operator with local changes
122- deploy-dev : check-env-docker-credentials cmctl docker-build-dev manifests deploy-rbac docker-registry-secret set-operator-image-repo
123- $(CMCTL ) check api --wait=2m
124- kustomize build config/default/overlays/dev | sed ' s@((operator_docker_image))@"$(DOCKER_REGISTRY_SERVER)/$(OPERATOR_IMAGE):$(GIT_COMMIT)"@' | kubectl apply -f -
125-
126- # Load operator image and deploy operator into current KinD cluster
127- deploy-kind : manifests cmctl deploy-rbac
128- $(BUILD_KIT ) buildx build --build-arg=GIT_COMMIT=$(GIT_COMMIT ) -t $(DOCKER_REGISTRY_SERVER ) /$(OPERATOR_IMAGE ) :$(GIT_COMMIT ) .
129- kind load docker-image $(DOCKER_REGISTRY_SERVER ) /$(OPERATOR_IMAGE ) :$(GIT_COMMIT )
130- $(CMCTL ) check api --wait=2m
131- kustomize build config/default/overlays/kind | sed ' s@((operator_docker_image))@"$(DOCKER_REGISTRY_SERVER)/$(OPERATOR_IMAGE):$(GIT_COMMIT)"@' | kubectl apply -f -
132-
133- deploy-rbac :
134- kustomize build config/rbac | kubectl apply -f -
135-
136151# Generate manifests e.g. CRD, RBAC etc.
137152manifests : install-tools
138153 controller-gen crd rbac:roleName=manager-role webhook paths=" ./..." output:crd:artifacts:config=config/crd/bases
139154
140155# Generate API reference documentation
156+ .PHONY : api-reference
141157api-reference :
142158 crd-ref-docs \
143159 --source-path ./api \
@@ -146,6 +162,14 @@ api-reference:
146162 --output-path ./docs/api/rabbitmq.com.ref.asciidoc \
147163 --max-depth 30
148164
165+ # # used in CI pipeline to create release artifact
166+ .PHONY : generate-manifests
167+ generate-manifests :
168+ mkdir -p releases
169+ kustomize build config/installation/ > releases/messaging-topology-operator.bak
170+ sed ' /CERTIFICATE_NAMESPACE.*CERTIFICATE_NAME/d' releases/messaging-topology-operator.bak > releases/messaging-topology-operator.yaml
171+ kustomize build config/installation/cert-manager/ > releases/messaging-topology-operator-with-certmanager.yaml
172+
149173# Run go fmt against code
150174fmt :
151175 go fmt ./...
@@ -162,61 +186,90 @@ vuln:
162186generate : install-tools api-reference
163187 controller-gen object:headerFile=" hack/NOTICE.go.txt" paths=" ./..."
164188
189+ .PHONY : generate-client-set
165190generate-client-set :
166191 $(get_mod_code_generator )
167192 go mod vendor
168193 ./hack/update-codegen.sh
169194
170- check-env-docker-credentials : check-env-registry-server
171- ifndef DOCKER_REGISTRY_USERNAME
172- $(error DOCKER_REGISTRY_USERNAME is undefined: Username for accessing the docker registry)
173- endif
174- ifndef DOCKER_REGISTRY_PASSWORD
175- $(error DOCKER_REGISTRY_PASSWORD is undefined: Password for accessing the docker registry)
176- endif
177- ifndef DOCKER_REGISTRY_SECRET
178- $(error DOCKER_REGISTRY_SECRET is undefined: Name of Kubernetes secret in which to store the Docker registry username and password)
179- endif
180-
181195GIT_COMMIT =$(shell git rev-parse --short HEAD) -dev
182-
183- docker-build-dev : check-env-docker-repo
196+ OPERATOR_IMAGE ?= rabbitmqoperator/messaging-topology-operator
197+ .PHONY : docker-build-dev
198+ docker-build-dev :
199+ $(call check_defined, DOCKER_REGISTRY_SERVER, URL of docker registry containing the Operator image (e.g. registry.my-company.com) )
184200 $(BUILD_KIT ) buildx build --build-arg=GIT_COMMIT=$(GIT_COMMIT ) -t $(DOCKER_REGISTRY_SERVER ) /$(OPERATOR_IMAGE ) :$(GIT_COMMIT ) .
185201 $(BUILD_KIT ) push $(DOCKER_REGISTRY_SERVER ) /$(OPERATOR_IMAGE ) :$(GIT_COMMIT )
186202
187- docker-registry-secret : check-env-docker-credentials operator-namespace
188- echo " creating registry secret and patching default service account"
189- @kubectl -n $(K8S_OPERATOR_NAMESPACE ) create secret docker-registry $(DOCKER_REGISTRY_SECRET ) --docker-server=' $(DOCKER_REGISTRY_SERVER)' --docker-username=" $$ DOCKER_REGISTRY_USERNAME" --docker-password=" $$ DOCKER_REGISTRY_PASSWORD" || true
203+ # docker-build-local and deploy-local work in local Kubernetes installations where the Kubernetes API
204+ # server runs in the same Docker Context as the build process. This is the case for Rancher Desktop
205+ # and probably for Docker Desktop. These two commands won't have the desired effect if Kubernetes API
206+ # is running remotely e.g. Cloud, or if the build context is not shared with Kubernetes API e.g. containerd
207+ .PHONY : docker-build-local
208+ docker-build-local :
209+ $(BUILD_KIT ) buildx build --build-arg=GIT_COMMIT=$(GIT_COMMIT ) -t localhost/topology-operator:$(GIT_COMMIT ) .
210+
211+ K8S_OPERATOR_NAMESPACE ?= rabbitmq-system
212+ .PHONY : docker-registry-secret
213+ docker-registry-secret :
214+ $(call check_defined, DOCKER_REGISTRY_USERNAME, Username for accessing the docker registry)
215+ $(call check_defined, DOCKER_REGISTRY_PASSWORD. Password for accessing the docker registry)
216+ $(call check_defined, DOCKER_REGISTRY_SECRET, Name of Kubernetes secret in which to store the Docker registry username and password)
217+ $(call check_defined, DOCKER_REGISTRY_SERVER, URL of docker registry containing the Operator image (e.g. registry.my-company.com) )
218+ @echo " Creating registry secret and patching default service account"
219+ @kubectl -n $(K8S_OPERATOR_NAMESPACE ) create secret docker-registry $(DOCKER_REGISTRY_SECRET ) \
220+ --docker-server=' $(DOCKER_REGISTRY_SERVER)' \
221+ --docker-username=" $$ DOCKER_REGISTRY_USERNAME" \
222+ --docker-password=" $$ DOCKER_REGISTRY_PASSWORD" || true
190223 @kubectl -n $(K8S_OPERATOR_NAMESPACE ) patch serviceaccount messaging-topology-operator -p ' {"imagePullSecrets": [{"name": "$(DOCKER_REGISTRY_SECRET)"}]}'
191224
192- check-env-registry-server :
193- ifndef DOCKER_REGISTRY_SERVER
194- $(error DOCKER_REGISTRY_SERVER is undefined: URL of docker registry containing the Operator image (e.g. registry.my-company.com))
195- endif
225+ # ########################
226+ # ## Deploy & Teardown ###
227+ # ########################
228+ .PHONY : deploy
229+ deploy : manifests deploy-rbac deploy-manager # # Deploy latest version of this Operator
196230
197- check-env-docker-repo : check-env-registry-server set-operator-image-repo
231+ .PHONY : destroy
232+ destroy : # # Delete all resources of this Operator
233+ kustomize build config/rbac | kubectl delete --ignore-not-found=true -f -
234+ kustomize build config/default/base | kubectl delete --ignore-not-found=true -f -
198235
199- set-operator-image-repo :
200- OPERATOR_IMAGE? =p-rabbitmq-for-kubernetes/messaging-topology-operator
236+ # Deploy operator with local changes
237+ .PHONY : deploy-dev
238+ deploy-dev : cmctl docker-build-dev manifests deploy-rbac docker-registry-secret # # Build current code as a Docker image, push the image, and deploy to current Kubernetes context
239+ $(call check_defined, DOCKER_REGISTRY_USERNAME, Username for accessing the docker registry)
240+ $(call check_defined, DOCKER_REGISTRY_PASSWORD. Password for accessing the docker registry)
241+ $(call check_defined, DOCKER_REGISTRY_SECRET, Name of Kubernetes secret in which to store the Docker registry username and password)
242+ $(call check_defined, DOCKER_REGISTRY_SERVER, URL of docker registry containing the Operator image (e.g. registry.my-company.com) )
243+ $(CMCTL ) check api --wait=2m
244+ kustomize build config/default/overlays/dev | sed ' s@((operator_docker_image))@"$(DOCKER_REGISTRY_SERVER)/$(OPERATOR_IMAGE):$(GIT_COMMIT)"@' | kubectl apply -f -
201245
202- operator-namespace :
203- ifeq (, $(K8S_OPERATOR_NAMESPACE ) )
204- K8S_OPERATOR_NAMESPACE =rabbitmq-system
205- endif
246+ # Load operator image and deploy operator into current KinD cluster
247+ .PHONY : deploy-kind
248+ deploy-kind : manifests cmctl deploy-rbac
249+ $(BUILD_KIT ) buildx build --build-arg=GIT_COMMIT=$(GIT_COMMIT ) -t $(DOCKER_REGISTRY_SERVER ) /$(OPERATOR_IMAGE ) :$(GIT_COMMIT ) .
250+ kind load docker-image $(DOCKER_REGISTRY_SERVER ) /$(OPERATOR_IMAGE ) :$(GIT_COMMIT )
251+ $(CMCTL ) check api --wait=2m
252+ kustomize build config/default/overlays/kind | sed ' s@((operator_docker_image))@"$(DOCKER_REGISTRY_SERVER)/$(OPERATOR_IMAGE):$(GIT_COMMIT)"@' | kubectl apply -f -
253+
254+ .PHONY : deploy-local
255+ deploy-local : cmctl deploy-rbac $(YTT )
256+ $(CMCTL ) check api --wait=2m
257+ kustomize build config/default/overlays/cert-manager | $(YTT ) -f- -f config/ytt_overlays/change_deployment_image.yml \
258+ --data-value operator_image=" localhost/topology-operator:$( GIT_COMMIT) " \
259+ -f config/ytt_overlays/never_pull.yml | kubectl apply -f-
260+
261+ .PHONY : deploy-rbac
262+ deploy-rbac :
263+ kustomize build config/rbac | kubectl apply -f -
206264
265+ .PHONY : cluster-operator
207266cluster-operator :
208267 @kubectl apply -f https://github.com/rabbitmq/cluster-operator/releases/latest/download/cluster-operator.yml
209268
269+ .PHONY : destroy-cluster-operator
210270destroy-cluster-operator :
211271 @kubectl delete -f https://github.com/rabbitmq/cluster-operator/releases/latest/download/cluster-operator.yml --ignore-not-found
212272
213- # # used in CI pipeline to create release artifact
214- generate-manifests :
215- mkdir -p releases
216- kustomize build config/installation/ > releases/messaging-topology-operator.bak
217- sed ' /CERTIFICATE_NAMESPACE.*CERTIFICATE_NAME/d' releases/messaging-topology-operator.bak > releases/messaging-topology-operator.yaml
218- kustomize build config/installation/cert-manager/ > releases/messaging-topology-operator-with-certmanager.yaml
219-
220273# ###############
221274# Cert Manager #
222275# ###############
@@ -234,8 +287,10 @@ $(CMCTL): | $(LOCAL_BIN) $(LOCAL_TMP)
234287
235288CERT_MANAGER_VERSION ?= v1.15.1
236289CERT_MANAGER_MANIFEST ?= https://github.com/jetstack/cert-manager/releases/download/$(CERT_MANAGER_VERSION ) /cert-manager.yaml
290+ .PHONY : cert-manager
237291cert-manager : # # Deploys Cert Manager from JetStack repo. Use CERT_MANAGER_VERSION to customise version e.g. v1.2.0
238292 kubectl apply -f https://github.com/jetstack/cert-manager/releases/download/$(CERT_MANAGER_VERSION ) /cert-manager.yaml
239293
294+ .PHONY : destroy-cert-manager
240295destroy-cert-manager : # # Deletes Cert Manager deployment created by 'make cert-manager'
241296 kubectl delete -f https://github.com/jetstack/cert-manager/releases/download/$(CERT_MANAGER_VERSION ) /cert-manager.yaml
0 commit comments