@@ -6,7 +6,7 @@ SHELL = /usr/bin/env bash -o pipefail
66# Current Operator version
77VERSION ?= 0.0.21
88# Image to use for building Go
9- GO_BUILDER_IMG ?= "golang:1.22 "
9+ GO_BUILDER_IMG ?= "golang:1.23 "
1010# Image URL to use all building/pushing image targets
1111IMG_NAME ?= ghcr.io/grafana/k6-operator
1212IMG_TAG ?= latest
@@ -24,53 +24,24 @@ BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
2424endif
2525BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS ) $(BUNDLE_DEFAULT_CHANNEL )
2626
27- # # Location to install dependencies to
28- LOCALBIN ?= $(shell pwd) /bin
29- $(LOCALBIN ) :
30- mkdir -p $(LOCALBIN )
31-
32- # # Tool Binaries
33- KUSTOMIZE ?= $(LOCALBIN ) /kustomize
34- CONTROLLER_GEN ?= $(LOCALBIN ) /controller-gen
35- ENVTEST ?= $(LOCALBIN ) /setup-envtest
36-
37- # # Tool Versions
38- KUSTOMIZE_VERSION ?= v4.5.5 # v3.8.7
39- CONTROLLER_TOOLS_VERSION ?= v0.16.1
40-
4127CRD_OPTIONS ?= "crd:maxDescLen=0"
4228
43- all : manager
29+ .PHONY : all
30+ all : build
4431
45- KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
46- .PHONY : kustomize
47- kustomize : $(KUSTOMIZE ) # # Download kustomize locally if necessary.
48- $(KUSTOMIZE ) : $(LOCALBIN )
49- test -s $(LOCALBIN ) /kustomize || { curl -Ss $( KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $( subst v,,$( KUSTOMIZE_VERSION) ) $( LOCALBIN) ; }
50-
51- .PHONY : controller-gen
52- controller-gen : $(CONTROLLER_GEN ) # # Download controller-gen locally if necessary.
53- $(CONTROLLER_GEN ) : $(LOCALBIN )
54- test -s $(LOCALBIN ) /controller-gen || GOBIN=$(LOCALBIN ) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION )
55-
56- .PHONY : envtest
57- envtest : $(ENVTEST ) # # Download envtest-setup locally if necessary.
58- $(ENVTEST ) : $(LOCALBIN )
59- test -s $(LOCALBIN ) /setup-envtest || GOBIN=$(LOCALBIN ) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
32+ .PHONY : help
33+ help : # # Display this help.
34+ @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 )
6035
6136# Run tests
62- ENVTEST_VERSION ?= latest # ref. https://github.com/kubernetes-sigs/controller-runtime/tree/main/tools/setup-envtest
63- ENVTEST_ASSETS_DIR = $(shell pwd) /testbin
64- ENVTEST_K8S_VERSION ?= 1.30.0
6537GOOS =$(shell go env GOOS)
6638GOARCH =$(shell go env GOARCH)
6739KUBEBUILDER_ASSETS_ROOT =/tmp
6840KUBEBUILDER_ASSETS =$(KUBEBUILDER_ASSETS_ROOT ) /kubebuilder/bin
6941
70- test : manifests generate fmt vet # # Run tests.
71- mkdir -p ${ENVTEST_ASSETS_DIR}
72- test -f ${ENVTEST_ASSETS_DIR} /setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR} /setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh
73- source ${ENVTEST_ASSETS_DIR} /setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR ) ; setup_envtest_env $(ENVTEST_ASSETS_DIR ) ; go test ./... -coverprofile cover.out
42+ .PHONY : test
43+ test : manifests generate fmt vet envtest # # Run tests.
44+ KUBEBUILDER_ASSETS=" $( shell $( ENVTEST) use $( ENVTEST_K8S_VERSION) --bin-dir $( LOCALBIN) -p path) " go test $$(go list ./... | grep -v /e2e ) -coverprofile cover.out
7445
7546e2e : deploy
7647 kubectl create configmap crocodile-stress-test --from-file e2e/test.js
@@ -80,30 +51,13 @@ e2e-cleanup:
8051 kubectl delete configmap crocodile-stress-test
8152 kubectl delete -f e2e/test.yaml
8253
83- # Build manager binary
84- manager : generate fmt vet
85- go build -o bin/manager main.go
54+ .PHONY : build
55+ build : manifests generate fmt vet # # Build manager binary.
56+ go build -o bin/manager cmd/ main.go
8657
87- # Run against the configured Kubernetes cluster in ~/.kube/config
88- run : generate fmt vet manifests
89- go run ./main.go
90-
91- # Install CRDs into a cluster
92- install : manifests kustomize
93- $(KUSTOMIZE ) build config/crd | kubectl apply -f -
94-
95- # Uninstall CRDs from a cluster
96- uninstall : manifests kustomize
97- $(KUSTOMIZE ) build config/crd | kubectl delete -f -
98-
99- # Deploy controller in the configured Kubernetes cluster in ~/.kube/config
100- deploy : manifests kustomize
101- cd config/manager && $(KUSTOMIZE ) edit set image controller=${IMG_NAME} :${IMG_TAG}
102- $(KUSTOMIZE ) build config/default | kubectl apply -f -
103-
104- # Delete operator from a cluster
105- delete : manifests kustomize
106- $(KUSTOMIZE ) build config/default | kubectl delete -f -
58+ .PHONY : run
59+ run : manifests generate fmt vet # # Run a controller from your host.
60+ go run ./cmd/main.go
10761
10862# Generate manifests e.g. CRD, RBAC etc.
10963manifests : controller-gen
@@ -117,10 +71,13 @@ fmt:
11771vet :
11872 go vet ./...
11973
120- # Run golangci-lint
121- lint :
122- go install github.com/golangci/golangci-lint/cmd/
[email protected] 123- golangci-lint --timeout 5m run ./...
74+ .PHONY : lint
75+ lint : golangci-lint # # Run golangci-lint linter
76+ $(GOLANGCI_LINT ) --timeout 5m run ./...
77+
78+ .PHONY : lint-fix
79+ lint-fix : golangci-lint # # Run golangci-lint linter and perform fixes
80+ $(GOLANGCI_LINT ) run --fix
12481
12582# Generate code
12683generate : controller-gen
@@ -134,6 +91,34 @@ docker-build: test
13491docker-push :
13592 docker push ${IMG_NAME} :${IMG_TAG}
13693
94+ # TODO: check and re-use in bundle.yaml creation
95+ # .PHONY: build-installer
96+ # build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
97+ # mkdir -p dist
98+ # cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
99+ # $(KUSTOMIZE) build config/default > dist/install.yaml
100+
101+ ifndef ignore-not-found
102+ ignore-not-found = false
103+ endif
104+
105+ .PHONY : install
106+ install : manifests kustomize # # Install CRDs into the K8s cluster specified in ~/.kube/config.
107+ $(KUSTOMIZE ) build config/crd | $(KUBECTL ) apply -f -
108+
109+ .PHONY : uninstall
110+ 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.
111+ $(KUSTOMIZE ) build config/crd | $(KUBECTL ) delete --ignore-not-found=$(ignore-not-found ) -f -
112+
113+ # Deploy controller in the configured Kubernetes cluster in ~/.kube/config
114+ deploy : manifests kustomize
115+ cd config/manager && $(KUSTOMIZE ) edit set image controller=${IMG_NAME} :${IMG_TAG}
116+ $(KUSTOMIZE ) build config/default | $(KUBECTL ) apply -f -
117+
118+ # Delete operator from a cluster
119+ undeploy : manifests kustomize
120+ $(KUSTOMIZE ) build config/default | $(KUBECTL ) delete --ignore-not-found=$(ignore-not-found ) -f -
121+
137122# Generate bundle manifests and metadata, then validate generated files.
138123.PHONY : bundle
139124bundle : manifests
@@ -189,3 +174,64 @@ HELM=$(shell which helm)
189174else
190175HELM =$(shell which helm)
191176endif
177+
178+ # ===============================================================
179+ # Dependencies
180+ # ===============================================================
181+
182+ # # Location to install dependencies to
183+ LOCALBIN ?= $(shell pwd) /bin
184+ $(LOCALBIN ) :
185+ mkdir -p $(LOCALBIN )
186+
187+ # # Tool Binaries
188+ KUBECTL ?= kubectl
189+ KUSTOMIZE ?= $(LOCALBIN ) /kustomize
190+ CONTROLLER_GEN ?= $(LOCALBIN ) /controller-gen
191+ ENVTEST ?= $(LOCALBIN ) /setup-envtest
192+ GOLANGCI_LINT = $(LOCALBIN ) /golangci-lint
193+
194+ # # Tool Versions
195+ KUSTOMIZE_VERSION ?= v5.5.0
196+ CONTROLLER_TOOLS_VERSION ?= v0.16.1
197+ # ENVTEST_VERSION ?= latest # ref. https://github.com/kubernetes-sigs/controller-runtime/tree/main/tools/setup-envtest
198+ ENVTEST_VERSION ?= release-0.19
199+ GOLANGCI_LINT_VERSION ?= v1.61.0
200+ # ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
201+ ENVTEST_K8S_VERSION = 1.31.0
202+
203+ .PHONY : kustomize
204+ kustomize : $(KUSTOMIZE ) # # Download kustomize locally if necessary.
205+ $(KUSTOMIZE ) : $(LOCALBIN )
206+ $(call go-install-tool,$(KUSTOMIZE ) ,sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION ) )
207+
208+ .PHONY : controller-gen
209+ controller-gen : $(CONTROLLER_GEN ) # # Download controller-gen locally if necessary.
210+ $(CONTROLLER_GEN ) : $(LOCALBIN )
211+ $(call go-install-tool,$(CONTROLLER_GEN ) ,sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION ) )
212+
213+ .PHONY : envtest
214+ envtest : $(ENVTEST ) # # Download setup-envtest locally if necessary.
215+ $(ENVTEST ) : $(LOCALBIN )
216+ $(call go-install-tool,$(ENVTEST ) ,sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION ) )
217+
218+ .PHONY : golangci-lint
219+ golangci-lint : $(GOLANGCI_LINT ) # # Download golangci-lint locally if necessary.
220+ $(GOLANGCI_LINT ) : $(LOCALBIN )
221+ $(call go-install-tool,$(GOLANGCI_LINT ) ,github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION ) )
222+
223+ # go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
224+ # $1 - target path with name of binary
225+ # $2 - package url which can be installed
226+ # $3 - specific version of package
227+ define go-install-tool
228+ @[ -f "$(1 ) -$(3 ) " ] || { \
229+ set -e; \
230+ package=$(2 ) @$(3 ) ;\
231+ echo "Downloading $${package}" ;\
232+ rm -f $(1 ) || true ;\
233+ GOBIN=$(LOCALBIN ) go install $${package} ;\
234+ mv $(1 ) $(1 ) -$(3 ) ;\
235+ } ;\
236+ ln -sf $(1 ) -$(3 ) $(1 )
237+ endef
0 commit comments