Skip to content

Commit c740a6d

Browse files
chore: restructure Makefile
1 parent 476b26d commit c740a6d

File tree

1 file changed

+129
-162
lines changed

1 file changed

+129
-162
lines changed

Makefile

Lines changed: 129 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ SHELL = /usr/bin/env bash -o pipefail
3030
all: build
3131

3232
##@ General
33-
3433
# The help target prints out all targets with their descriptions organized
3534
# beneath their categories. The categories are represented by '##@' and the
3635
# target descriptions by '##'. The awk commands is responsible for reading the
@@ -44,10 +43,72 @@ all: build
4443

4544
.PHONY: help
4645
help: ## Display this help.
47-
@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)
46+
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-25s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
47+
48+
#----------------------------------------------------------------------------------------------
49+
##@ Build Dependencies
50+
51+
## Location to install dependencies to
52+
LOCALBIN ?= $(shell pwd)/bin
53+
$(LOCALBIN):
54+
mkdir -p $(LOCALBIN)
55+
56+
## Tool Binaries
57+
KUBECTL ?= kubectl
58+
KIND ?= kind # fix this to use tools
59+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
60+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
61+
ENVTEST ?= $(LOCALBIN)/setup-envtest
62+
GOTESTSUM ?= $(LOCALBIN)/gotestsum
63+
GOLANGCILINT ?= $(LOCALBIN)/golangci-lint
64+
65+
66+
67+
## Tool Versions
68+
KUSTOMIZE_VERSION ?= v5.4.1
69+
CONTROLLER_TOOLS_VERSION ?= v0.17.2
70+
GOLANGCILINT_VERSION ?= v2.0.2
71+
72+
.PHONY: kustomize
73+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
74+
$(KUSTOMIZE): $(LOCALBIN)
75+
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
76+
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
77+
rm -rf $(LOCALBIN)/kustomize; \
78+
fi
79+
test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
4880

49-
##@ Development
81+
.PHONY: controller-gen
82+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
83+
$(CONTROLLER_GEN): $(LOCALBIN)
84+
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
85+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
5086

87+
.PHONY: envtest
88+
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
89+
$(ENVTEST): $(LOCALBIN)
90+
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
91+
92+
.PHONY: envtest-bins
93+
envtest-bins: envtest ## Download envtest binaries
94+
$(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN)
95+
96+
.PHONY: gotestsum
97+
gotestsum: $(GOTESTSUM) ## Download gotestsum locally if necessary.
98+
$(GOTESTSUM): $(LOCALBIN)
99+
test -s $(LOCALBIN)/gotestsum || GOBIN=$(LOCALBIN) go install gotest.tools/gotestsum@latest
100+
101+
.PHONY: lint
102+
lint: $(GOLANGCILINT) ## Run golangci-lint against code.
103+
$(GOLANGCILINT) config verify
104+
$(GOLANGCILINT) run ./...
105+
106+
.PHONY: lint-fix
107+
lint-fix: ## Run golangci-lint with --fix option to automatically fix issues.
108+
$(GOLANGCILINT) run --fix
109+
110+
#----------------------------------------------------------------------------------------------
111+
##@ Code Generation & Formatting
51112
.PHONY: manifests
52113
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
53114
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
@@ -65,28 +126,16 @@ fmt: ## Run go fmt against code.
65126
vet: ## Run go vet against code.
66127
go vet ./...
67128

68-
.PHONY: test
69-
test: manifests generate fmt vet envtest ## Run tests.
70-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
71-
129+
#----------------------------------------------------------------------------------------------
72130
##@ Build
73-
74131
.PHONY: build
75132
build: manifests generate fmt vet ## Build manager binary.
76133
go build -o bin/manager cmd/main.go
77134

78-
.PHONY: run
79-
run: manifests generate fmt vet ## Run a controller from your host.
80-
OPERATOR_CONFIG_NAMESPACE=metrics-operator-system go run ./cmd/main.go start
81-
82135
.PHONY: build-docker-binary
83-
build-docker-binary: manifests generate fmt vet ## Build manager binary.
136+
build-docker-binary: manifests generate fmt vet ## Build manager binary for Docker image.
84137
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o bin/manager-linux.amd64 cmd/main.go
85138

86-
87-
# If you wish built the manager image targeting other platforms you can use the --platform flag.
88-
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
89-
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
90139
.PHONY: docker-build
91140
docker-build: build-docker-binary test ## Build docker image with the manager.
92141
$(CONTAINER_TOOL) build -t ${IMG} .
@@ -103,7 +152,7 @@ docker-push: ## Push docker image with the manager.
103152
# To properly provided solutions that supports more than one platform you should use this option.
104153
PLATFORMS ?= linux/arm64 linux/amd64
105154
.PHONY: docker-buildx
106-
docker-buildx: #test ## Build and tag docker image for each platform locally using --load
155+
docker-buildx: ## Build and tag docker image for each platform locally using --load
107156
sed '1 s/^FROM/FROM --platform=$${BUILDPLATFORM}/' Dockerfile > Dockerfile.cross
108157
$(CONTAINER_TOOL) buildx create --name project-v3-builder || true
109158
$(CONTAINER_TOOL) buildx use project-v3-builder
@@ -115,12 +164,8 @@ docker-buildx: #test ## Build and tag docker image for each platform locally usi
115164
$(CONTAINER_TOOL) buildx rm project-v3-builder
116165
rm Dockerfile.cross
117166

167+
#----------------------------------------------------------------------------------------------
118168
##@ Deployment
119-
120-
ifndef ignore-not-found
121-
ignore-not-found = false
122-
endif
123-
124169
.PHONY: install
125170
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
126171
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
@@ -138,98 +183,46 @@ deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in
138183
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
139184
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
140185

141-
##@ Build Dependencies
142-
143-
## Location to install dependencies to
144-
LOCALBIN ?= $(shell pwd)/bin
145-
$(LOCALBIN):
146-
mkdir -p $(LOCALBIN)
147-
148-
## Tool Binaries
149-
KUBECTL ?= kubectl
150-
KIND ?= kind # fix this to use tools
151-
KUSTOMIZE ?= $(LOCALBIN)/kustomize
152-
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
153-
ENVTEST ?= $(LOCALBIN)/setup-envtest
154-
GOTESTSUM ?= $(LOCALBIN)/gotestsum
155-
GOLANGCILINT ?= $(LOCALBIN)/golangci-lint
156-
157-
158-
159-
## Tool Versions
160-
KUSTOMIZE_VERSION ?= v5.4.1
161-
CONTROLLER_TOOLS_VERSION ?= v0.17.2
162-
GOLANGCILINT_VERSION ?= v2.0.2
163-
164-
.PHONY: kustomize
165-
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
166-
$(KUSTOMIZE): $(LOCALBIN)
167-
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
168-
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
169-
rm -rf $(LOCALBIN)/kustomize; \
170-
fi
171-
test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
172-
173-
.PHONY: controller-gen
174-
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
175-
$(CONTROLLER_GEN): $(LOCALBIN)
176-
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
177-
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
178-
179-
.PHONY: envtest
180-
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
181-
$(ENVTEST): $(LOCALBIN)
182-
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
183-
184-
.PHONY: envtest-bins
185-
envtest-bins: envtest ## Download envtest binaries
186-
$(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN)
187-
188-
.PHONY: gotestsum
189-
gotestsum: $(GOTESTSUM) ## Download gotestsum locally if necessary.
190-
$(GOTESTSUM): $(LOCALBIN)
191-
test -s $(LOCALBIN)/gotestsum || GOBIN=$(LOCALBIN) go install gotest.tools/gotestsum@latest
192-
193-
194-
### ------------------------------------ DEVELOPMENT - LOCAL ------------------------------------ ###
195-
##@ Local Development
196-
.PHONY: dev-all
197-
dev-all-deploy: # Build the Docker image, create a local kind cluster, deploy the Operator, and install Crossplane and Helm provider.
198-
$(MAKE) dev-deploy
199-
$(MAKE) crossplane-install
200-
$(MAKE) crossplane-provider-install
201-
$(MAKE) helm-provider-sample
202-
203-
204-
.PHONY: dev-deploy
205-
dev-deploy: manifests kustomize dev-clean ## Deploy the Operator to a local kind cluster via Kustomize.
206-
$(KIND) create cluster --name=$(PROJECT_NAME)-dev
207-
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
208-
$(KUSTOMIZE) build config/default | kubectl apply -f -
209-
$(KIND) load docker-image ${IMG} --name=$(PROJECT_NAME)-dev
210-
211-
186+
#----------------------------------------------------------------------------------------------
187+
##@ Local Development Utilities
212188
.PHONY: dev-build
213189
dev-build: docker-build ## Build the Docker image for local development.
214190
@echo "Finished building docker image" ${IMG}
215191

216-
.PHONY: dev-base
217-
dev-base: manifests kustomize dev-build dev-clean dev-cluster helm-install-local # Build the Docker image, create a local kind cluster, and deploys the Operator.
192+
.PHONY: kind-load-image
193+
kind-load-image: ## Load the Docker image into the local kind cluster for development.
194+
$(KIND) load docker-image ${IMG} --name=$(PROJECT_FULL_NAME)-dev
218195

219-
.PHONY: dev-cluster
220-
dev-cluster: ## Create a local kind cluster and load the Docker image.
196+
.PHONY: kind-cluster
197+
kind-cluster: ## Create a kind cluster for development (no CRDs or resources applied).
221198
$(KIND) create cluster --name=$(PROJECT_FULL_NAME)-dev
222-
$(KIND) load docker-image ${IMG} --name=$(PROJECT_FULL_NAME)-dev
223199

200+
.PHONY: dev-clean
201+
dev-clean: ## Delete the local kind cluster used for development.
202+
$(KIND) delete cluster --name=$(PROJECT_FULL_NAME)-dev
203+
204+
.PHONY: dev-run
205+
dev-run: ## Run the operator locally (for debugging/development).
206+
## todo: add flag --debug
207+
go run ./cmd/main.go start
208+
209+
#----------------------------------------------------------------------------------------------
210+
##@ Testing
211+
.PHONY: test
212+
test: manifests generate fmt vet envtest ## Run tests.
213+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
214+
215+
#----------------------------------------------------------------------------------------------
216+
##@ Testing: Run Operator outside of the cluster
224217
.PHONY: dev-local
225218
dev-local: ## Create a local kind cluster and install CRDs for development.
226219
$(KIND) create cluster --name=$(PROJECT_FULL_NAME)-dev
227220
$(MAKE) install
228221

229222
.PHONY: dev-local-all
230-
dev-local-all: ## Full local dev setup: clean, create cluster, install CRDs, Crossplane, namespaces, secrets, and sample metrics.
223+
dev-local-all: ## Full local dev setup: clean-up, create cluster, install CRDs, Crossplane, namespaces, secrets, and sample metrics.
231224
$(MAKE) dev-clean
232-
$(KIND) create cluster --name=$(PROJECT_FULL_NAME)-dev
225+
$(MAKE) kind-cluster
233226
$(MAKE) install
234227
$(MAKE) crossplane-install
235228
$(MAKE) crossplane-provider-install
@@ -240,7 +233,14 @@ dev-local-all: ## Full local dev setup: clean, create cluster, install CRDs, Cro
240233
$(MAKE) dev-basic-metric
241234
$(MAKE) dev-managed-metric
242235

243-
##@ Examples for local development
236+
#----------------------------------------------------------------------------------------------
237+
##@ Testing: Run Operator inside the cluster (production scenario)
238+
239+
.PHONY: dev-deploy
240+
dev-deploy: manifests kustomize dev-build dev-clean kind-cluster kind-load-image helm-install-local ## Build the Docker image, create a local kind cluster, and deploy the Operator.
241+
242+
#----------------------------------------------------------------------------------------------
243+
##@ Example Resources
244244
.PHONY: dev-secret
245245
dev-secret: ## Apply the example secret to the cluster.
246246
kubectl apply -f examples/secret.yaml
@@ -273,84 +273,51 @@ dev-apply-metric-dynatrace-prod: ## Apply metric using Dynatrace production exam
273273
dev-v1beta1-compmetric: ## Apply v1beta1 compmetric example to the cluster.
274274
kubectl apply -f examples/v1beta1/compmetric.yaml
275275

276-
.PHONY: dev-kind
277-
dev-kind: ## Create a kind cluster for development (no CRDs or resources applied).
278-
$(KIND) create cluster --name=$(PROJECT_FULL_NAME)-dev
279-
280-
.PHONY: dev-clean
281-
dev-clean: ## Delete the local kind cluster used for development.
282-
$(KIND) delete cluster --name=$(PROJECT_FULL_NAME)-dev
283-
284-
.PHONY: dev-run
285-
dev-run: ## Run the operator locally (for debugging/development).
286-
## todo: add flag --debug
287-
go run ./cmd/main.go
288-
289-
$(GOLANGCILINT): $(LOCALBIN)
290-
@if test -x $(LOCALBIN)/golangci-lint && ! $(LOCALBIN)/golangci-lint version | grep -q $(GOLANGCILINT_VERSION); then \
291-
echo "$(LOCALBIN)/golangci-lint version is not expected $(GOLANGCILINT_VERSION). Removing it before installing."; \
292-
rm -rf $(LOCALBIN)/golangci-lint; \
293-
fi
294-
test -s $(LOCALBIN)/golangci-lint || GOBIN=$(LOCALBIN) GO111MODULE=on go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCILINT_VERSION)
295-
296-
297-
.PHONY: lint
298-
lint: ## Run golangci-lint against code.
299-
$(GOLANGCILINT) config verify
300-
$(GOLANGCILINT) run ./...
301-
302-
.PHONY: lint-fix
303-
lint-fix: ## Run golangci-lint with --fix option to automatically fix issues.
304-
$(GOLANGCILINT) run --fix
305-
306-
### ------------------------------------ HELM ------------------------------------ ###
307-
##@ Local Testing
308-
276+
#----------------------------------------------------------------------------------------------
277+
##@ Helm
309278
.PHONY: helm-chart
310279
helm-chart: ## Generate Helm chart files from templates.
311280
OPERATOR_VERSION=$(shell cat VERSION) envsubst < charts/$(PROJECT_FULL_NAME)/Chart.yaml.tpl > charts/$(PROJECT_FULL_NAME)/Chart.yaml
312281
OPERATOR_VERSION=$(shell cat VERSION) envsubst < charts/$(PROJECT_FULL_NAME)/values.yaml.tpl > charts/$(PROJECT_FULL_NAME)/values.yaml
313282

314283
.PHONY: helm-install-local
315-
helm-install-local: docker-build # Build the Docker image and install the Helm chart locally
284+
helm-install-local: ## Install the Helm chart locally using the Docker image
316285
helm upgrade --install $(PROJECT_FULL_NAME) charts/$(PROJECT_FULL_NAME)/ --set image.repository=$(IMG_BASE) --set image.tag=$(IMG_VERSION) --set image.pullPolicy=Never
317286
$(KIND) load docker-image ${IMG} --name=$(PROJECT_FULL_NAME)-dev
318287

319-
.PHONY: helm-work
320-
helm-work: dev-kind crossplane-install helm-install-local # Install Crossplane and Helm provider, then deploy the Helm chart
321-
echo "Helm work done"
288+
#----------------------------------------------------------------------------------------------
289+
##@ Crossplane
322290

323-
.PHONY: lefthook
324-
lefthook: # Initializes pre-commit hooks using lefthook https://github.com/evilmartians/lefthook
325-
lefthook install
326-
327-
.PHONY: check-diff
328-
check-diff: generate manifests # Ensures that go generate doesn't create a diff
329-
@echo checking clean branch
330-
@if git status --porcelain | grep . ; then echo Uncommitted changes found after running make generate manifests. Please ensure you commit all generated files in this branch after running make generate. && false; else echo branch is clean; fi
331-
332-
.PHONY: reviewable
333-
reviewable: # Ensures that the code is reviewable by running generate, lint, and test
334-
@$(MAKE) generate
335-
@$(MAKE) lint
336-
@$(MAKE) test
337-
338-
### ------------------------------------ CROSSPLANE ------------------------------------ ###
339-
340-
# Namespace where Crossplane is installed
291+
# CROSSPLANE_NAMESPACE defines the namespace where Crossplane will be installed.
341292
CROSSPLANE_NAMESPACE ?= crossplane-system
342293

343294
.PHONY: crossplane-install
344-
crossplane-install: # Install Crossplane into the cluster.
345-
helm install crossplane crossplane-stable/crossplane --namespace crossplane-system --create-namespace --wait
295+
crossplane-install: ## Install Crossplane into the cluster.
296+
helm install crossplane crossplane-stable/crossplane --namespace $(CROSSPLANE_NAMESPACE) --create-namespace --wait
346297

347298
.PHONY: crossplane-provider-install
348-
crossplane-provider-install: # Install the Helm provider using kubectl
299+
crossplane-provider-install: ## Install the Helm provider using kubectl
349300
kubectl apply -f examples/crossplane/provider.yaml -n $(CROSSPLANE_NAMESPACE)
350301
kubectl wait --for=condition=Healthy provider/provider-helm --timeout=1m
351302
kubectl apply -f examples/crossplane/provider-config.yaml -n $(CROSSPLANE_NAMESPACE)
352303

353-
354304
.PHONY: helm-provider-sample
355-
helm-provider-sample: # Apply the Helm provider sample to the cluster.
305+
helm-provider-sample: ## Apply the Helm provider sample to the cluster.
356306
kubectl apply -f examples/crossplane/release.yaml -n $(CROSSPLANE_NAMESPACE)
307+
308+
#----------------------------------------------------------------------------------------------
309+
##@ Utility
310+
.PHONY: lefthook
311+
lefthook: ## Initializes pre-commit hooks using lefthook https://github.com/evilmartians/lefthook
312+
lefthook install
313+
314+
.PHONY: check-diff
315+
check-diff: generate manifests ## Ensures that go generate doesn't create a diff
316+
@echo checking clean branch
317+
@if git status --porcelain | grep . ; then echo Uncommitted changes found after running make generate manifests. Please ensure you commit all generated files in this branch after running make generate. && false; else echo branch is clean; fi
318+
319+
.PHONY: reviewable
320+
reviewable: ## Ensures that the code is reviewable by running generate, lint, and test
321+
@$(MAKE) generate
322+
@$(MAKE) lint
323+
@$(MAKE) test

0 commit comments

Comments
 (0)