-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
404 lines (352 loc) · 15.7 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
SHELL := /bin/bash
# Image URL to use all building/pushing image targets
IMG ?= controller:latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd"
CONTROLLER_NAMESPACE ?= lagoon-builddeploy
## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)
OVERRIDE_BUILD_DEPLOY_DIND_IMAGE ?= uselagoon/build-deploy-image:main
INGRESS_VERSION=4.9.1
HARBOR_VERSION=1.14.3
KIND_CLUSTER ?= remote-controller
KIND_NETWORK ?= remote-controller
TIMEOUT = 30m
KIND_VERSION = v0.25.0
KUBECTL_VERSION := v1.31.0
HELM_VERSION := v3.16.1
GOJQ_VERSION = v0.12.16
KUSTOMIZE_VERSION := v5.4.3
HELM = $(realpath ./local-dev/helm)
KUBECTL = $(realpath ./local-dev/kubectl)
JQ = $(realpath ./local-dev/jq)
KIND = $(realpath ./local-dev/kind)
KUSTOMIZE = $(realpath ./local-dev/kustomize)
ARCH := $(shell uname | tr '[:upper:]' '[:lower:]')
.PHONY: local-dev/kind
local-dev/kind:
ifeq ($(KIND_VERSION), $(shell kind version 2>/dev/null | sed -nE 's/kind (v[0-9.]+).*/\1/p'))
$(info linking local kind version $(KIND_VERSION))
ln -sf $(shell command -v kind) ./local-dev/kind
else
ifneq ($(KIND_VERSION), $(shell ./local-dev/kind version 2>/dev/null | sed -nE 's/kind (v[0-9.]+).*/\1/p'))
$(info downloading kind version $(KIND_VERSION) for $(ARCH))
mkdir -p local-dev
rm local-dev/kind || true
curl -sSLo local-dev/kind https://kind.sigs.k8s.io/dl/$(KIND_VERSION)/kind-$(ARCH)-amd64
chmod a+x local-dev/kind
endif
endif
.PHONY: local-dev/kustomize
local-dev/kustomize:
ifeq ($(KUSTOMIZE_VERSION), $(shell kustomize version 2>/dev/null | sed -nE 's/(v[0-9.]+).*/\1/p'))
$(info linking local kustomize version $(KUSTOMIZE_VERSION))
ln -sf $(shell command -v kind) ./local-dev/kind
else
ifneq ($(KUSTOMIZE_VERSION), $(shell ./local-dev/kustomize version 2>/dev/null | sed -nE 's/(v[0-9.]+).*/\1/p'))
$(info downloading kustomize version $(KUSTOMIZE_VERSION) for $(ARCH))
rm local-dev/kustomize || true
curl -sSL https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2F$(KUSTOMIZE_VERSION)/kustomize_$(KUSTOMIZE_VERSION)_$(ARCH)_amd64.tar.gz | tar -xzC local-dev
chmod a+x local-dev/kustomize
endif
endif
.PHONY: local-dev/helm
local-dev/helm:
ifeq ($(HELM_VERSION), $(shell helm version --short --client 2>/dev/null | sed -nE 's/(v[0-9.]+).*/\1/p'))
$(info linking local helm version $(HELM_VERSION))
ln -sf $(shell command -v helm) ./local-dev/helm
else
ifneq ($(HELM_VERSION), $(shell ./local-dev/helm version --short --client 2>/dev/null | sed -nE 's/(v[0-9.]+).*/\1/p'))
$(info downloading helm version $(HELM_VERSION) for $(ARCH))
rm local-dev/helm || true
curl -sSL https://get.helm.sh/helm-$(HELM_VERSION)-$(ARCH)-amd64.tar.gz | tar -xzC local-dev --strip-components=1 $(ARCH)-amd64/helm
chmod a+x local-dev/helm
endif
endif
.PHONY: local-dev/jq
local-dev/jq:
ifeq ($(GOJQ_VERSION), $(shell gojq -v 2>/dev/null | sed -nE 's/gojq ([0-9.]+).*/v\1/p'))
$(info linking local gojq version $(GOJQ_VERSION))
ln -sf $(shell command -v gojq) ./local-dev/jq
else
ifneq ($(GOJQ_VERSION), $(shell ./local-dev/jq -v 2>/dev/null | sed -nE 's/gojq ([0-9.]+).*/v\1/p'))
$(info downloading gojq version $(GOJQ_VERSION) for $(ARCH))
rm local-dev/jq || true
ifeq ($(ARCH), darwin)
TMPDIR=$$(mktemp -d) \
&& curl -sSL https://github.com/itchyny/gojq/releases/download/$(GOJQ_VERSION)/gojq_$(GOJQ_VERSION)_$(ARCH)_arm64.zip -o $$TMPDIR/gojq.zip \
&& (cd $$TMPDIR && unzip gojq.zip) && cp $$TMPDIR/gojq_$(GOJQ_VERSION)_$(ARCH)_arm64/gojq ./local-dev/jq && rm -rf $$TMPDIR
else
curl -sSL https://github.com/itchyny/gojq/releases/download/$(GOJQ_VERSION)/gojq_$(GOJQ_VERSION)_$(ARCH)_amd64.tar.gz | tar -xzC local-dev --strip-components=1 gojq_$(GOJQ_VERSION)_$(ARCH)_amd64/gojq
mv ./local-dev/{go,}jq
endif
chmod a+x local-dev/jq
endif
endif
.PHONY: local-dev/kubectl
local-dev/kubectl:
ifeq ($(KUBECTL_VERSION), $(shell kubectl version --client 2>/dev/null | grep Client | sed -E 's/Client Version: (v[0-9.]+).*/\1/'))
$(info linking local kubectl version $(KUBECTL_VERSION))
ln -sf $(shell command -v kubectl) ./local-dev/kubectl
else
ifneq ($(KUBECTL_VERSION), $(shell ./local-dev/kubectl version --client 2>/dev/null | grep Client | sed -E 's/Client Version: (v[0-9.]+).*/\1/'))
$(info downloading kubectl version $(KUBECTL_VERSION) for $(ARCH))
rm local-dev/kubectl || true
curl -sSLo local-dev/kubectl https://storage.googleapis.com/kubernetes-release/release/$(KUBECTL_VERSION)/bin/$(ARCH)/amd64/kubectl
chmod a+x local-dev/kubectl
endif
endif
.PHONY: local-dev/tools
local-dev/tools: local-dev/kind local-dev/kustomize local-dev/kubectl local-dev/jq local-dev/helm
.PHONY: helm/repos
helm/repos: local-dev/helm
# install repo dependencies required by the charts
$(HELM) repo add lagoon https://uselagoon.github.io/lagoon-charts/
$(HELM) repo add harbor https://helm.goharbor.io
$(HELM) repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
$(HELM) repo add jetstack https://charts.jetstack.io
$(HELM) repo add metallb https://metallb.github.io/metallb
$(HELM) repo update
# 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
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.29.0
ENVTEST ?= $(LOCALBIN)/setup-envtest-$(ENVTEST_VERSION)
ENVTEST_VERSION ?= latest
all: manager
.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
.PHONY: fmt
fmt: ## Run go fmt against code.
go fmt ./...
.PHONY: vet
vet: ## Run go vet against code.
go vet ./...
.PHONY: envtest
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
$(ENVTEST): $(LOCALBIN)
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out
# Build manager binary
.PHONY: manager
manager: generate fmt vet
go build -o bin/manager cmd/main.go
# Run against the configured Kubernetes cluster in ~/.kube/config
.PHONY: run
run: generate fmt vet manifests
go run ./cmd/main.go --controller-namespace=${CONTROLLER_NAMESPACE}
# Install CRDs into a cluster
.PHONY: install
install: manifests
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
.PHONY: outputcrds
outputcrds: manifests
$(KUSTOMIZE) build config/crd
.PHONY: uninstall
# Uninstall CRDs from a cluster
uninstall: manifests
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete -f -
# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
.PHONY: preview
preview: manifests
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
@export HARBOR_URL="https://registry.$$($(KUBECTL) -n ingress-nginx get services ingress-nginx-controller -o jsonpath='{.status.loadBalancer.ingress[0].ip}' || echo 127.0.0.1).nip.io" && \
echo "OVERRIDE_BUILD_DEPLOY_DIND_IMAGE=${OVERRIDE_BUILD_DEPLOY_DIND_IMAGE}" > config/default/config.properties && \
echo "HARBOR_URL=$${HARBOR_URL}" >> config/default/config.properties && \
echo "HARBOR_API=$${HARBOR_URL}/api" >> config/default/config.properties
OVERRIDE_BUILD_DEPLOY_DIND_IMAGE=${OVERRIDE_BUILD_DEPLOY_DIND_IMAGE} $(KUSTOMIZE) build config/default
cp config/default/config.properties.default config/default/config.properties
# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
# this is only used locally for development or in the test suite
.PHONY: deploy
deploy: manifests
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
@if $(KIND) get clusters | grep -q $(KIND_CLUSTER); then \
$(KIND) export kubeconfig --name=$(KIND_CLUSTER) \
&& export HARBOR_URL="https://registry.$$($(KUBECTL) -n ingress-nginx get services ingress-nginx-controller -o jsonpath='{.status.loadBalancer.ingress[0].ip}').nip.io"; \
fi && \
echo "OVERRIDE_BUILD_DEPLOY_DIND_IMAGE=${OVERRIDE_BUILD_DEPLOY_DIND_IMAGE}" > config/default/config.properties && \
echo "HARBOR_URL=$${HARBOR_URL}" >> config/default/config.properties && \
echo "HARBOR_API=$${HARBOR_URL}/api" >> config/default/config.properties
OVERRIDE_BUILD_DEPLOY_DIND_IMAGE=${OVERRIDE_BUILD_DEPLOY_DIND_IMAGE} $(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
cp config/default/config.properties.default config/default/config.properties
.PHONY: manifests
manifests: controller-gen local-dev/tools ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
# Build the docker image
docker-build: test
docker build . -t ${IMG}
# Push the docker image
docker-push:
docker push ${IMG}
# find or download controller-gen
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
go mod init tmp ;\
go install sigs.k8s.io/controller-tools/cmd/[email protected] ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
.PHONY: install-metallb
install-metallb:
LAGOON_KIND_CIDR_BLOCK=$$(docker network inspect $(KIND_NETWORK) | $(JQ) '. [0].IPAM.Config[0].Subnet' | tr -d '"') && \
export LAGOON_KIND_NETWORK_RANGE=$$(echo $${LAGOON_KIND_CIDR_BLOCK%???} | awk -F'.' '{print $$1,$$2,$$3,240}' OFS='.')/29 && \
$(HELM) upgrade \
--install \
--create-namespace \
--namespace metallb-system \
--wait \
--timeout $(TIMEOUT) \
--version=v0.13.12 \
metallb \
metallb/metallb && \
$$(envsubst < test-resources/test-suite.metallb-pool.yaml.tpl > test-resources/test-suite.metallb-pool.yaml) && \
$(KUBECTL) apply -f test-resources/test-suite.metallb-pool.yaml
# cert-manager is used to allow self-signed certificates to be generated automatically by ingress in the same way lets-encrypt would
.PHONY: install-certmanager
install-certmanager: install-metallb
$(HELM) upgrade \
--install \
--create-namespace \
--namespace cert-manager \
--wait \
--timeout $(TIMEOUT) \
--set installCRDs=true \
--set ingressShim.defaultIssuerName=lagoon-testing-issuer \
--set ingressShim.defaultIssuerKind=ClusterIssuer \
--set ingressShim.defaultIssuerGroup=cert-manager.io \
--version=v1.11.0 \
cert-manager \
jetstack/cert-manager
$(KUBECTL) apply -f test-resources/test-suite.certmanager-issuer-ss.yaml
# installs ingress-nginx
.PHONY: install-ingress
install-ingress: install-certmanager
$(HELM) upgrade \
--install \
--create-namespace \
--namespace ingress-nginx \
--wait \
--timeout $(TIMEOUT) \
--set controller.allowSnippetAnnotations=true \
--set controller.service.type=LoadBalancer \
--set controller.service.nodePorts.http=32080 \
--set controller.service.nodePorts.https=32443 \
--set controller.config.proxy-body-size=0 \
--set controller.config.hsts="false" \
--set controller.watchIngressWithoutClass=true \
--set controller.ingressClassResource.default=true \
--version=$(INGRESS_VERSION) \
ingress-nginx \
ingress-nginx/ingress-nginx
# installs harbor
.PHONY: install-registry
install-registry: install-ingress
$(HELM) upgrade \
--install \
--create-namespace \
--namespace registry \
--wait \
--timeout $(TIMEOUT) \
--set expose.tls.enabled=true \
--set expose.tls.certSource=secret \
--set expose.tls.secret.secretName=harbor-ingress \
--set expose.ingress.className=nginx \
--set-string expose.ingress.annotations.kubernetes\\.io/tls-acme=true \
--set "expose.ingress.hosts.core=registry.$$($(KUBECTL) -n ingress-nginx get services ingress-nginx-controller -o jsonpath='{.status.loadBalancer.ingress[0].ip}').nip.io" \
--set "externalURL=https://registry.$$($(KUBECTL) -n ingress-nginx get services ingress-nginx-controller -o jsonpath='{.status.loadBalancer.ingress[0].ip}').nip.io" \
--set chartmuseum.enabled=false \
--set clair.enabled=false \
--set notary.enabled=false \
--set trivy.enabled=false \
--version=$(HARBOR_VERSION) \
registry \
harbor/harbor
# installs lagoon-remote mainly for the docker-host
.PHONY: install-lagoon-remote
install-lagoon-remote: install-registry
$(HELM) upgrade \
--install \
--create-namespace \
--namespace lagoon \
--wait \
--timeout $(TIMEOUT) \
--set "lagoon-build-deploy.enabled=false" \
--set "dockerHost.registry=registry.$$($(KUBECTL) -n ingress-nginx get services ingress-nginx-controller -o jsonpath='{.status.loadBalancer.ingress[0].ip}').nip.io" \
--set "dockerHost.storage.size=10Gi" \
--set "dbaas-operator.enabled=false" \
lagoon \
lagoon/lagoon-remote
.PHONY: create-kind-cluster
create-kind-cluster: local-dev/tools helm/repos
docker network inspect $(KIND_NETWORK) >/dev/null || docker network create $(KIND_NETWORK) \
&& LAGOON_KIND_CIDR_BLOCK=$$(docker network inspect $(KIND_NETWORK) | $(JQ) '. [0].IPAM.Config[0].Subnet' | tr -d '"') \
&& export KIND_NODE_IP=$$(echo $${LAGOON_KIND_CIDR_BLOCK%???} | awk -F'.' '{print $$1,$$2,$$3,240}' OFS='.') \
&& envsubst < test-resources/test-suite.kind-config.yaml.tpl > test-resources/test-suite.kind-config.yaml \
&& export KIND_EXPERIMENTAL_DOCKER_NETWORK=$(KIND_NETWORK) \
&& $(KIND) create cluster --wait=60s --name=$(KIND_CLUSTER) --config=test-resources/test-suite.kind-config.yaml
# Create a kind cluster locally and run the test e2e test suite against it
.PHONY: kind/test-e2e # Run the e2e tests against a Kind k8s instance that is spun up locally
kind/test-e2e: create-kind-cluster install-lagoon-remote kind/re-test-e2e
.PHONY: local-kind/test-e2e # Run the e2e tests against a Kind k8s instance that is spun up locally
kind/re-test-e2e:
export KIND_PATH=$(KIND) && \
export KUBECTL_PATH=$(KUBECTL) && \
export KIND_CLUSTER=$(KIND_CLUSTER) && \
$(KIND) export kubeconfig --name=$(KIND_CLUSTER) && \
export HARBOR_VERSION=$(HARBOR_VERSION) && \
export OVERRIDE_BUILD_DEPLOY_DIND_IMAGE=$(OVERRIDE_BUILD_DEPLOY_DIND_IMAGE) && \
$(MAKE) test-e2e
.PHONY: clean
kind/clean:
docker compose down && \
$(KIND) delete cluster --name=$(KIND_CLUSTER) && docker network rm $(KIND_NETWORK)
# Utilize Kind or modify the e2e tests to load the image locally, enabling compatibility with other vendors.
.PHONY: test-e2e # Run the e2e tests against a Kind k8s instance that is spun up inside github action.
test-e2e:
export HARBOR_VERSION=$(HARBOR_VERSION) && \
export OVERRIDE_BUILD_DEPLOY_DIND_IMAGE=$(OVERRIDE_BUILD_DEPLOY_DIND_IMAGE) && \
go test ./test/e2e/ -v -ginkgo.v
.PHONY: github/test-e2e
github/test-e2e: local-dev/tools install-lagoon-remote test-e2e
.PHONY: kind/set-kubeconfig
kind/set-kubeconfig:
export KIND_CLUSTER=$(KIND_CLUSTER) && \
$(KIND) export kubeconfig --name=$(KIND_CLUSTER)
.PHONY: kind/logs-remote-controller
kind/logs-remote-controller:
export KIND_CLUSTER=$(KIND_CLUSTER) && \
$(KIND) export kubeconfig --name=$(KIND_CLUSTER) && \
$(KUBECTL) -n remote-controller-system logs -f \
$$($(KUBECTL) -n remote-controller-system get pod -l control-plane=controller-manager -o jsonpath="{.items[0].metadata.name}") \
-c manager
# 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