1
# Image URL to use all building/pushing image targets
2
IMG ?= controller:latest
3
4
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
5
ifeq (,$(shell go env GOBIN))
6
GOBIN=$(shell go env GOPATH)/bin
7
else
8
GOBIN=$(shell go env GOBIN)
9
endif
10
11
# CONTAINER_TOOL defines the container tool to be used for building images.
12
# Be aware that the target commands are only tested with Docker which is
13
# scaffolded by default. However, you might want to replace it to use other
14
# tools. (i.e. podman)
15
CONTAINER_TOOL ?= docker
16
17
# Setting SHELL to bash allows bash commands to be executed by recipes.
18
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
19
SHELL = /usr/bin/env bash -o pipefail
20
.SHELLFLAGS = -ec
21
22
.PHONY: all
23
all: build
24
25
##@ General
26
27
# The help target prints out all targets with their descriptions organized
28
# beneath their categories. The categories are represented by '##@' and the
29
# target descriptions by '##'. The awk command is responsible for reading the
30
# entire set of makefiles included in this invocation, looking for lines of the
31
# file as xyz: ## something, and then pretty-format the target and help. Then,
32
# if there's a line with ##@ something, that gets pretty-printed as a category.
33
# More info on the usage of ANSI control characters for terminal formatting:
34
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
35
# More info on the awk command:
36
# http://linuxcommand.org/lc3_adv_awk.php
37
38
.PHONY: help
39
help: ## Display this help.
40
@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)
41
42
##@ Development
43
44
.PHONY: manifests
45
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
46
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
47
48
.PHONY: generate
49
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
50
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
51
52
.PHONY: fmt
53
fmt: ## Run go fmt against code.
54
go fmt ./...
55
56
.PHONY: vet
57
vet: ## Run go vet against code.
58
go vet ./...
59
60
.PHONY: test
61
test: manifests generate fmt vet setup-envtest ## Run tests.
62
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out
63
64
# TODO(user): To use a different vendor for e2e tests, modify the setup under 'tests/e2e'.
65
# The default setup assumes Kind is pre-installed and builds/loads the Manager Docker image locally.
66
# CertManager is installed by default; skip with:
67
# - CERT_MANAGER_INSTALL_SKIP=true
68
.PHONY: test-e2e
69
test-e2e: manifests generate fmt vet ## Run the e2e tests. Expected an isolated environment using Kind.
70
@command -v kind >/dev/null 2>&1 || { \
71
echo "Kind is not installed. Please install Kind manually."; \
72
exit 1; \
73
}
74
@kind get clusters | grep -q 'kind' || { \
75
echo "No Kind cluster is running. Please start a Kind cluster before running the e2e tests."; \
76
exit 1; \
77
}
78
go test ./test/e2e/ -v -ginkgo.v
79
80
.PHONY: lint
81
lint: golangci-lint ## Run golangci-lint linter
82
$(GOLANGCI_LINT) run
83
84
.PHONY: lint-fix
85
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
86
$(GOLANGCI_LINT) run --fix
87
88
.PHONY: lint-config
89
lint-config: golangci-lint ## Verify golangci-lint linter configuration
90
$(GOLANGCI_LINT) config verify
91
92
##@ Build
93
94
.PHONY: build
95
build: manifests generate fmt vet ## Build manager binary.
96
shellcheck internal/controller/build.sh
97
go build -o bin/enbi cmd/enbi/main.go
98
99
.PHONY: run
100
run: manifests generate fmt vet ## Run a controller from your host.
101
source .env && go run ./cmd/enbi/main.go -storage-class standard -namespace enbi
102
103
# If you wish to build the manager image targeting other platforms you can use the --platform flag.
104
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
105
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
106
.PHONY: docker-build
107
docker-build: ## Build docker image with the manager.
108
$(CONTAINER_TOOL) build -t ${IMG} .
109
110
.PHONY: docker-push
111
docker-push: ## Push docker image with the manager.
112
$(CONTAINER_TOOL) push ${IMG}
113
114
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
115
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
116
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
117
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
118
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
119
PLATFORMS ?= linux/arm64,linux/amd64
120
.PHONY: docker-buildx
121
docker-buildx: ## Build docker image for the manager for cross-platform support
122
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
123
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
124
- $(CONTAINER_TOOL) buildx create --name enbi-builder
125
$(CONTAINER_TOOL) buildx use enbi-builder
126
- $(CONTAINER_TOOL) buildx build --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
127
- $(CONTAINER_TOOL) buildx rm enbi-builder
128
# rm Dockerfile.cross
129
130
.PHONY: build-installer
131
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
132
mkdir -p dist
133
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
134
$(KUSTOMIZE) build config/default > dist/install.yaml
135
136
##@ Deployment
137
138
ifndef ignore-not-found
139
ignore-not-found = false
140
endif
141
142
.PHONY: install
143
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
144
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
145
146
.PHONY: uninstall
147
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.
148
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
149
150
.PHONY: deploy
151
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
152
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
153
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
154
155
.PHONY: undeploy
156
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.
157
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
158
159
##@ Dependencies
160
161
## Location to install dependencies to
162
LOCALBIN ?= $(shell pwd)/bin
163
$(LOCALBIN):
164
mkdir -p $(LOCALBIN)
165
166
## Tool Binaries
167
KUBECTL ?= kubectl
168
KUSTOMIZE ?= $(LOCALBIN)/kustomize
169
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
170
ENVTEST ?= $(LOCALBIN)/setup-envtest
171
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
172
173
## Tool Versions
174
KUSTOMIZE_VERSION ?= v5.5.0
175
CONTROLLER_TOOLS_VERSION ?= v0.17.2
176
#ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20)
177
ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
178
#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
179
ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}')
180
GOLANGCI_LINT_VERSION ?= v1.63.4
181
182
.PHONY: kustomize
183
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
184
$(KUSTOMIZE): $(LOCALBIN)
185
$(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION))
186
187
.PHONY: controller-gen
188
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
189
$(CONTROLLER_GEN): $(LOCALBIN)
190
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
191
192
.PHONY: setup-envtest
193
setup-envtest: envtest ## Download the binaries required for ENVTEST in the local bin directory.
194
@echo "Setting up envtest binaries for Kubernetes version $(ENVTEST_K8S_VERSION)..."
195
@$(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path || { \
196
echo "Error: Failed to set up envtest binaries for version $(ENVTEST_K8S_VERSION)."; \
197
exit 1; \
198
}
199
200
.PHONY: envtest
201
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
202
$(ENVTEST): $(LOCALBIN)
203
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
204
205
.PHONY: golangci-lint
206
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
207
$(GOLANGCI_LINT): $(LOCALBIN)
208
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
209
210
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
211
# $1 - target path with name of binary
212
# $2 - package url which can be installed
213
# $3 - specific version of package
214
define go-install-tool
215
@[ -f "$(1)-$(3)" ] || { \
216
set -e; \
217
package=$(2)@$(3) ;\
218
echo "Downloading $${package}" ;\
219
rm -f $(1) || true ;\
220
GOBIN=$(LOCALBIN) go install $${package} ;\
221
mv $(1) $(1)-$(3) ;\
222
} ;\
223
ln -sf $(1)-$(3) $(1)
224
endef
225