-
-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathMakefile
268 lines (219 loc) · 7.95 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# ===========================
# Variables
# ===========================
# Current Operator version
VERSION ?= 0.19.1
# Default bundle image tag
BUNDLE_IMG ?= controller-bundle:$(VERSION)
# Kubernetes version to use for envtest
ENVTEST_K8S_VERSION = 1.31.0
# Image URL to use for all building/pushing image targets
IMG ?= quay.io/opstree/redis-operator:v$(VERSION)
AGENT_IMG ?= quay.io/opstree/redis-agent:v$(VERSION)
# Container engine to use (docker or podman)
CONTAINER_ENGINE ?= docker
# Platforms for multi-arch builds
PLATFORMS = "linux/arm64,linux/amd64"
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN = $(shell go env GOPATH)/bin
else
GOBIN = $(shell go env GOBIN)
endif
# Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
# Tool Binaries
KUBECTL ?= kubectl
KUSTOMIZE ?= $(LOCALBIN)/kustomize-$(KUSTOMIZE_VERSION)
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen-$(CONTROLLER_TOOLS_VERSION)
ENVTEST ?= $(LOCALBIN)/setup-envtest-$(ENVTEST_VERSION)
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
KUTTL = $(LOCALBIN)/kuttl-$(KUTTL_VERSION)
KIND = $(LOCALBIN)/kind-$(KIND_VERSION)
# Tool Versions
KUSTOMIZE_VERSION ?= v5.3.0
CONTROLLER_TOOLS_VERSION ?= v0.14.0
ENVTEST_VERSION ?= release-0.17
GOLANGCI_LINT_VERSION ?= v1.57.2
KUTTL_VERSION ?= 0.15.0
KIND_VERSION ?= v0.24.0
# Options for 'bundle-build'
ifneq ($(origin CHANNELS), undefined)
BUNDLE_CHANNELS := --channels=$(CHANNELS)
endif
ifneq ($(origin DEFAULT_CHANNEL), undefined)
BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
endif
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
# ===========================
# Targets
# ===========================
# Default target
all: manager
# Run tests
.PHONY: test
test: generate fmt vet manifests
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
# Build manager binary
.PHONY: manager
manager: generate fmt vet
go build -o bin/manager cmd/manager/main.go
# Build agent binary
.PHONY: agent
agent: generate fmt vet
go build -o bin/agent cmd/agent/main.go
# Run against the configured Kubernetes cluster in ~/.kube/config
.PHONY: run
run: generate fmt vet manifests
go run cmd/manager/main.go
# Install CRDs into a cluster
.PHONY: install
install: manifests kustomize
$(KUSTOMIZE) build config/crd | kubectl apply --server-side=true -f -
# Uninstall CRDs from a cluster
.PHONY: uninstall
uninstall: manifests kustomize
$(KUSTOMIZE) build config/crd | kubectl delete -f -
# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
.PHONY: deploy
deploy: manifests kustomize
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default | kubectl apply --server-side=true -f -
# UnDeploy controller from the configured Kubernetes cluster in ~/.kube/config
.PHONY: undeploy
undeploy:
$(KUSTOMIZE) build config/default | kubectl delete -f -
# Generate manifests e.g. CRD, RBAC etc.
.PHONY: manifests
manifests: controller-gen
$(CONTROLLER_GEN) crd rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
# Run go fmt against code
.PHONY: fmt
fmt:
go fmt ./...
# Run go vet against code
.PHONY: vet
vet:
go vet ./...
# Generate code
.PHONY: generate
generate: controller-gen
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
# Create a new builder instance for Docker Buildx with the specified platforms and set it as the current builder
.PHONY: docker-create
docker-create:
${CONTAINER_ENGINE} buildx create --platform $(PLATFORMS) --use
# Build the manager Docker image
.PHONY: docker-build
docker-build:
${CONTAINER_ENGINE} buildx build --platform=$(PLATFORMS) -t ${IMG} -f Dockerfile .
# Push the manager Docker image
.PHONY: docker-push
docker-push:
${CONTAINER_ENGINE} buildx build --push --platform="$(PLATFORMS)" -t ${IMG} -f Dockerfile .
# Load the manager image into docker
.PHONY: docker-load
docker-load:
${CONTAINER_ENGINE} buildx build --load -t ${IMG} -f Dockerfile .
# Generate bundle manifests and metadata, then validate generated files.
.PHONY: bundle
bundle: manifests kustomize
operator-sdk generate kustomize manifests -q
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
operator-sdk bundle validate ./bundle
# Build the bundle image.
.PHONY: bundle-build
bundle-build:
${CONTAINER_ENGINE} buildx build --platform="$(PLATFORMS)" -f bundle.Dockerfile -t $(BUNDLE_IMG) .
# Rebuild all generated code
.PHONY: codegen
codegen: generate manifests
# Verify that codegen is up to date.
.PHONY: verify-codegen
verify-codegen: codegen
@echo Checking codegen is up to date... >&2
@if [ -n "$$(git status --porcelain)" ]; then \
echo "There are uncommitted changes or untracked files after running codegen:" >&2; \
git status --porcelain >&2; \
echo "To correct this, locally run 'make codegen', commit the changes, and re-run tests." >&2; \
exit 1; \
fi
# ===========================
# Testing
# ===========================
.PHONY: tests
tests: integration-test-setup unit-tests
.PHONY: unit-tests
unit-tests:
@echo Running tests... >&2
@go test ./... -race -coverprofile=coverage.out -covermode=atomic
@go tool cover -html=coverage.out
.PHONY: e2e-test
e2e-test: e2e-kind-setup kuttl
$(LOCALBIN)/kuttl test --config tests/_config/kuttl-test.yaml
.PHONY: integration-test-setup
integration-test-setup:
./hack/integrationSetup.sh
.PHONY: e2e-kind-setup
e2e-kind-setup:
${CONTAINER_ENGINE} build -t redis-operator:e2e -f Dockerfile .
$(KIND) create cluster --config tests/_config/kind-config.yaml
$(KIND) load docker-image redis-operator:e2e --name kind
make deploy IMG=redis-operator:e2e
# ===========================
# Dependencies
# ===========================
$(LOCALBIN):
mkdir -p $(LOCALBIN)
# Download kustomize locally if necessary.
.PHONY: kustomize
kustomize: $(KUSTOMIZE)
$(KUSTOMIZE): $(LOCALBIN)
$(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION))
# Download controller-gen locally if necessary.
.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN)
$(CONTROLLER_GEN): $(LOCALBIN)
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
# Download setup-envtest locally if necessary.
.PHONY: envtest
envtest: $(ENVTEST)
$(ENVTEST): $(LOCALBIN)
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
# Download golangci-lint locally if necessary.
.PHONY: golangci-lint
golangci-lint: $(GOLANGCI_LINT)
$(GOLANGCI_LINT): $(LOCALBIN)
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,${GOLANGCI_LINT_VERSION})
# Download kind locally if necessary.
.PHONY: kind
kind: $(KIND)
$(KIND): $(LOCALBIN)
$(call go-install-tool,$(KIND),sigs.k8s.io/kind,${KIND_VERSION})
# Download kuttl locally if necessary.
.PHONY: kuttl
kuttl: $(KUTTL)
$(KUTTL): $(LOCALBIN)
curl -L https://github.com/kudobuilder/kuttl/releases/download/v$(KUTTL_VERSION)/kubectl-kuttl_$(KUTTL_VERSION)_linux_x86_64 -o $(LOCALBIN)/kuttl
chmod +x $(LOCALBIN)/kuttl
.PHONY: generate-metricsdocs
generate-metricsdocs:
mkdir -p $(shell pwd)/docs/content/en/docs/Monitoring
go run -ldflags="${LDFLAGS}" ./pkg/monitoring/metricsdocs > docs/content/en/docs/Monitoring/metrics.md
# ===========================
# Helper Functions
# ===========================
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
# $1 - target path with name of binary (ideally with version)
# $2 - package url which can be installed
# $3 - specific version of package
define go-install-tool
@[ -f $(1) ] || { \
set -e; \
package=$(2)@$(3) ;\
echo "Downloading $${package}" ;\
GOBIN=$(LOCALBIN) go install $${package} ;\
mv "$$(echo "$(1)" | sed "s/-$(3)$$//")" $(1) ;\
}
endef