@@ -35,10 +35,24 @@ IMAGE_TAG_BASE ?= adyanth/cloudflare-operator
3535# You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=<some-registry>/<project-name-bundle>:<tag>)
3636BUNDLE_IMG ?= $(IMAGE_TAG_BASE ) -bundle:v$(VERSION )
3737
38+ # BUNDLE_GEN_FLAGS are the flags passed to the operator-sdk generate bundle command
39+ BUNDLE_GEN_FLAGS ?= -q --overwrite --version $(VERSION ) $(BUNDLE_METADATA_OPTS )
40+
41+ # USE_IMAGE_DIGESTS defines if images are resolved via tags or digests
42+ # You can enable this value if you would like to use SHA Based Digests
43+ # To enable set flag to true
44+ USE_IMAGE_DIGESTS ?= false
45+ ifeq ($(USE_IMAGE_DIGESTS ) , true)
46+ BUNDLE_GEN_FLAGS += --use-image-digests
47+ endif
48+
49+ # Set the Operator SDK version to use. By default, what is installed on the system is used.
50+ # This is useful for CI or a project to utilize a specific version of the operator-sdk toolkit.
51+ OPERATOR_SDK_VERSION ?= v1.39.2
3852# Image URL to use all building/pushing image targets
3953IMG ?= $(IMAGE_TAG_BASE ) :$(VERSION )
4054# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
41- ENVTEST_K8S_VERSION = 1.22
55+ ENVTEST_K8S_VERSION = 1.31.0
4256
4357# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
4458ifeq (,$(shell go env GOBIN) )
4761GOBIN =$(shell go env GOBIN)
4862endif
4963
64+ # CONTAINER_TOOL defines the container tool to be used for building images.
65+ # Be aware that the target commands are only tested with Docker which is
66+ # scaffolded by default. However, you might want to replace it to use other
67+ # tools. (i.e. podman)
68+ CONTAINER_TOOL ?= docker
69+
5070# Setting SHELL to bash allows bash commands to be executed by recipes.
51- # This is a requirement for 'setup-envtest.sh' in the test target.
5271# Options are set to exit when a recipe line exits non-zero or a piped command fails.
5372SHELL = /usr/bin/env bash -o pipefail
5473.SHELLFLAGS = -ec
5574
75+ .PHONY : all
5676all : build
5777
5878# #@ General
5979
6080# The help target prints out all targets with their descriptions organized
6181# beneath their categories. The categories are represented by '##@' and the
62- # target descriptions by '##'. The awk commands is responsible for reading the
82+ # target descriptions by '##'. The awk command is responsible for reading the
6383# entire set of makefiles included in this invocation, looking for lines of the
6484# file as xyz: ## something, and then pretty-format the target and help. Then,
6585# if there's a line with ##@ something, that gets pretty-printed as a category.
@@ -68,91 +88,191 @@ all: build
6888# More info on the awk command:
6989# http://linuxcommand.org/lc3_adv_awk.php
7090
91+ .PHONY : help
7192help : # # Display this help.
7293 @awk ' BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST )
7394
7495# #@ Development
7596
97+ .PHONY : manifests
7698manifests : controller-gen # # Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
7799 $(CONTROLLER_GEN ) rbac:roleName=manager-role crd webhook paths=" ./..." output:crd:artifacts:config=config/crd/bases
78100
101+ .PHONY : generate
79102generate : controller-gen # # Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
80103 $(CONTROLLER_GEN ) object:headerFile=" hack/boilerplate.go.txt" paths=" ./..."
81104
105+ .PHONY : fmt
82106fmt : # # Run go fmt against code.
83107 go fmt ./...
84108
109+ .PHONY : vet
85110vet : # # Run go vet against code.
86111 go vet ./...
87112
113+ .PHONY : test
88114test : manifests generate fmt vet envtest # # Run tests.
89- KUBEBUILDER_ASSETS=" $( shell $( ENVTEST) use $( ENVTEST_K8S_VERSION) -p path) " go test ./... -coverprofile cover.out
115+ KUBEBUILDER_ASSETS=" $( shell $( ENVTEST) use $( ENVTEST_K8S_VERSION) --bin-dir $( LOCALBIN) -p path) " go test $$(go list ./... | grep -v /e2e ) -coverprofile cover.out
116+
117+ # Utilize Kind or modify the e2e tests to load the image locally, enabling compatibility with other vendors.
118+ .PHONY : test-e2e # Run the e2e tests against a Kind k8s instance that is spun up.
119+ test-e2e :
120+ go test ./test/e2e/ -v -ginkgo.v
121+
122+ .PHONY : lint
123+ lint : golangci-lint # # Run golangci-lint linter
124+ $(GOLANGCI_LINT ) run
125+
126+ .PHONY : lint-fix
127+ lint-fix : golangci-lint # # Run golangci-lint linter and perform fixes
128+ $(GOLANGCI_LINT ) run --fix
90129
91130# #@ Build
92131
93- build : generate fmt vet # # Build manager binary.
94- go build -o bin/manager main.go
132+ .PHONY : build
133+ build : manifests generate fmt vet # # Build manager binary.
134+ go build -o bin/manager cmd/main.go
95135
136+ .PHONY : run
96137run : manifests generate fmt vet # # Run a controller from your host.
97- go run ./main.go --cluster-resource-namespace=default
138+ go run ./cmd/ main.go
98139
99- docker-build : test # # Build docker image with the manager.
100- docker build -t ${IMG} .
140+ # If you wish to build the manager image targeting other platforms you can use the --platform flag.
141+ # (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
142+ # More info: https://docs.docker.com/develop/develop-images/build_enhancements/
143+ .PHONY : docker-build
144+ docker-build : # # Build docker image with the manager.
145+ $(CONTAINER_TOOL ) build -t ${IMG} .
101146
147+ .PHONY : docker-push
102148docker-push : # # Push docker image with the manager.
103- docker push ${IMG}
104-
105- docker-multiarch-push : test # # Build and push for multiple architectures (amd64, arm and arm64)
106- docker buildx build --push --platform linux/amd64,linux/arm,linux/arm64 --tag ${IMG} .
149+ $(CONTAINER_TOOL ) push ${IMG}
150+
151+ # PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
152+ # architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
153+ # - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
154+ # - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
155+ # - 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)
156+ # To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
157+ PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
158+ .PHONY : docker-buildx
159+ docker-buildx : # # Build and push docker image for the manager for cross-platform support
160+ # copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
161+ sed -e ' 1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
162+ - $(CONTAINER_TOOL ) buildx create --name cloudflare-operator-builder
163+ $(CONTAINER_TOOL ) buildx use cloudflare-operator-builder
164+ - $(CONTAINER_TOOL ) buildx build --push --platform=$(PLATFORMS ) --tag ${IMG} -f Dockerfile.cross .
165+ - $(CONTAINER_TOOL ) buildx rm cloudflare-operator-builder
166+ rm Dockerfile.cross
167+
168+ .PHONY : build-installer
169+ build-installer : manifests generate kustomize # # Generate a consolidated YAML with CRDs and deployment.
170+ mkdir -p dist
171+ cd config/manager && $(KUSTOMIZE ) edit set image controller=${IMG}
172+ $(KUSTOMIZE ) build config/default > dist/install.yaml
107173
108174# #@ Deployment
109175
176+ ifndef ignore-not-found
177+ ignore-not-found = false
178+ endif
179+
180+ .PHONY : install
110181install : manifests kustomize # # Install CRDs into the K8s cluster specified in ~/.kube/config.
111- $(KUSTOMIZE ) build config/crd | kubectl apply -f -
182+ $(KUSTOMIZE ) build config/crd | $( KUBECTL ) apply -f -
112183
113- uninstall : manifests kustomize # # Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
114- $(KUSTOMIZE ) build config/crd | kubectl delete -f -
184+ .PHONY : uninstall
185+ uninstall : 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.
186+ $(KUSTOMIZE ) build config/crd | $(KUBECTL ) delete --ignore-not-found=$(ignore-not-found ) -f -
115187
188+ .PHONY : deploy
116189deploy : manifests kustomize # # Deploy controller to the K8s cluster specified in ~/.kube/config.
117190 cd config/manager && $(KUSTOMIZE ) edit set image controller=${IMG}
118- $(KUSTOMIZE ) build config/default | kubectl apply -f -
119-
120- undeploy : # # Undeploy controller from the K8s cluster specified in ~/.kube/config.
121- $(KUSTOMIZE ) build config/default | kubectl delete -f -
122-
123-
124- CONTROLLER_GEN = $(shell pwd) /bin/controller-gen
125- controller-gen : # # Download controller-gen locally if necessary.
126- $(call go-get-tool,$(CONTROLLER_GEN ) ,sigs.k8s.io/controller-tools/cmd/[email protected] ) 127-
128- KUSTOMIZE = $(shell pwd) /bin/kustomize
129- kustomize : # # Download kustomize locally if necessary.
130- $(call go-get-tool,$(KUSTOMIZE ) ,sigs.k8s.io/kustomize/kustomize/[email protected] ) 131-
132- ENVTEST = $(shell pwd) /bin/setup-envtest
133- envtest : # # Download envtest-setup locally if necessary.
134- $(call go-get-tool,$(ENVTEST ) ,sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
135-
136- # go-get-tool will 'go get' any package $2 and install it to $1.
137- PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST ) ) ) )
138- define go-get-tool
139- @[ -f $(1 ) ] || { \
140- set -e ;\
141- TMP_DIR=$$(mktemp -d ) ;\
142- cd $$TMP_DIR ;\
143- go mod init tmp ;\
144- echo "Downloading $(2 ) " ;\
145- GOBIN=$(PROJECT_DIR ) /bin go get $(2 ) ;\
146- rm -rf $$TMP_DIR ;\
147- }
191+ $(KUSTOMIZE ) build config/default | $(KUBECTL ) apply -f -
192+
193+ .PHONY : undeploy
194+ undeploy : kustomize # # Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
195+ $(KUSTOMIZE ) build config/default | $(KUBECTL ) delete --ignore-not-found=$(ignore-not-found ) -f -
196+
197+ # #@ Dependencies
198+
199+ # # Location to install dependencies to
200+ LOCALBIN ?= $(shell pwd) /bin
201+ $(LOCALBIN ) :
202+ mkdir -p $(LOCALBIN )
203+
204+ # # Tool Binaries
205+ KUBECTL ?= kubectl
206+ KUSTOMIZE ?= $(LOCALBIN ) /kustomize
207+ CONTROLLER_GEN ?= $(LOCALBIN ) /controller-gen
208+ ENVTEST ?= $(LOCALBIN ) /setup-envtest
209+ GOLANGCI_LINT = $(LOCALBIN ) /golangci-lint
210+
211+ # # Tool Versions
212+ KUSTOMIZE_VERSION ?= v5.4.3
213+ CONTROLLER_TOOLS_VERSION ?= v0.16.1
214+ ENVTEST_VERSION ?= release-0.19
215+ GOLANGCI_LINT_VERSION ?= v1.59.1
216+
217+ .PHONY : kustomize
218+ kustomize : $(KUSTOMIZE ) # # Download kustomize locally if necessary.
219+ $(KUSTOMIZE ) : $(LOCALBIN )
220+ $(call go-install-tool,$(KUSTOMIZE ) ,sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION ) )
221+
222+ .PHONY : controller-gen
223+ controller-gen : $(CONTROLLER_GEN ) # # Download controller-gen locally if necessary.
224+ $(CONTROLLER_GEN ) : $(LOCALBIN )
225+ $(call go-install-tool,$(CONTROLLER_GEN ) ,sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION ) )
226+
227+ .PHONY : envtest
228+ envtest : $(ENVTEST ) # # Download setup-envtest locally if necessary.
229+ $(ENVTEST ) : $(LOCALBIN )
230+ $(call go-install-tool,$(ENVTEST ) ,sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION ) )
231+
232+ .PHONY : golangci-lint
233+ golangci-lint : $(GOLANGCI_LINT ) # # Download golangci-lint locally if necessary.
234+ $(GOLANGCI_LINT ) : $(LOCALBIN )
235+ $(call go-install-tool,$(GOLANGCI_LINT ) ,github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION ) )
236+
237+ # go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
238+ # $1 - target path with name of binary
239+ # $2 - package url which can be installed
240+ # $3 - specific version of package
241+ define go-install-tool
242+ @[ -f "$(1 ) -$(3 ) " ] || { \
243+ set -e; \
244+ package=$(2 ) @$(3 ) ;\
245+ echo "Downloading $${package}" ;\
246+ rm -f $(1 ) || true ;\
247+ GOBIN=$(LOCALBIN ) go install $${package} ;\
248+ mv $(1 ) $(1 ) -$(3 ) ;\
249+ } ;\
250+ ln -sf $(1 ) -$(3 ) $(1 )
148251endef
149252
253+ .PHONY : operator-sdk
254+ OPERATOR_SDK ?= $(LOCALBIN ) /operator-sdk
255+ operator-sdk : # # Download operator-sdk locally if necessary.
256+ ifeq (,$(wildcard $(OPERATOR_SDK ) ) )
257+ ifeq (, $(shell which operator-sdk 2>/dev/null) )
258+ @{ \
259+ set -e ;\
260+ mkdir -p $(dir $(OPERATOR_SDK)) ;\
261+ OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \
262+ curl -sSLo $(OPERATOR_SDK) https://github.com/operator-framework/operator-sdk/releases/download/$(OPERATOR_SDK_VERSION)/operator-sdk_$${OS}_$${ARCH} ;\
263+ chmod +x $(OPERATOR_SDK) ;\
264+ }
265+ else
266+ OPERATOR_SDK = $(shell which operator-sdk)
267+ endif
268+ endif
269+
150270.PHONY : bundle
151- bundle : manifests kustomize # # Generate bundle manifests and metadata, then validate generated files.
152- operator-sdk generate kustomize manifests -q
271+ bundle : manifests kustomize operator-sdk # # Generate bundle manifests and metadata, then validate generated files.
272+ $( OPERATOR_SDK ) generate kustomize manifests -q
153273 cd config/manager && $(KUSTOMIZE ) edit set image controller=$(IMG )
154- $(KUSTOMIZE ) build config/manifests | operator-sdk generate bundle -q --overwrite --version $( VERSION ) $( BUNDLE_METADATA_OPTS )
155- operator-sdk bundle validate ./bundle
274+ $(KUSTOMIZE ) build config/manifests | $( OPERATOR_SDK ) generate bundle $( BUNDLE_GEN_FLAGS )
275+ $( OPERATOR_SDK ) bundle validate ./bundle
156276
157277.PHONY : bundle-build
158278bundle-build : # # Build the bundle image.
@@ -163,15 +283,15 @@ bundle-push: ## Push the bundle image.
163283 $(MAKE ) docker-push IMG=$(BUNDLE_IMG )
164284
165285.PHONY : opm
166- OPM = ./bin /opm
286+ OPM = $( LOCALBIN ) /opm
167287opm : # # Download opm locally if necessary.
168288ifeq (,$(wildcard $(OPM ) ) )
169289ifeq (,$(shell which opm 2>/dev/null) )
170290 @{ \
171291 set -e ;\
172292 mkdir -p $(dir $(OPM)) ;\
173293 OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \
174- curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.15.1 /$${OS}-$${ARCH}-opm ;\
294+ curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.23.0 /$${OS}-$${ARCH}-opm ;\
175295 chmod +x $(OPM) ;\
176296 }
177297else
0 commit comments