-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMakefile
More file actions
362 lines (327 loc) Β· 15.1 KB
/
Copy pathMakefile
File metadata and controls
362 lines (327 loc) Β· 15.1 KB
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
SHELL = bash
.DEFAULT_GOAL = build
$(shell mkdir -p bin)
export OS := $(shell if [ "$(shell uname)" = "Darwin" ]; then echo "darwin"; else echo "linux"; fi)
export ARCH := $(shell if [ "$(shell uname -m)" = "x86_64" ]; then echo "amd64"; else echo "arm64"; fi)
export PATH := $(PATH):$(PWD)/bin
# Extracts the version number for a given dependency found in go.mod.
# Makes the test setup be in sync with what the operator itself uses.
extract-version = $(shell cat go.mod | grep $(1) | awk '{$$1=$$1};1' | cut -d' ' -f2 | sed 's/^v//')
#### TOOLS ####
TOOLS_DIR := $(PWD)/.tools
KUBECTL := $(PWD)/bin/kubectl
KIND := $(TOOLS_DIR)/kind
CERT_MANAGER_VERSION := $(call extract-version,github.com/cert-manager/cert-manager)
ISTIO_VERSION := $(call extract-version,istio.io/client-go)
PROMETHEUS_VERSION := $(call extract-version,github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring)
#### VARS ####
SKIPERATOR_CONTEXT ?= kind-$(KIND_CLUSTER_NAME)
KUBERNETES_VERSION = 1.34.3
KIND_IMAGE ?= kindest/node:v$(KUBERNETES_VERSION)
KIND_CLUSTER_NAME ?= skiperator
LOCAL_WEBHOOK_CERTS_DIR := $(shell mktemp -d -t skiperator-webhook-certs.XXXXXXX)
WEBHOOK_HOST = 0.0.0.0
WEBHOOK_ARGS = --webhook-cert-dir=$(LOCAL_WEBHOOK_CERTS_DIR) --webhook-host=$(WEBHOOK_HOST)
SKIPJOB_CRD_FILE ?= config/crd/skiperator.kartverket.no_skipjobs.yaml
.PHONY: ensure-kubectl
ensure-kubectl:
@if command -v kubectl >/dev/null 2>&1; then \
:; \
else \
echo "kubectl not found on PATH, downloading v$(KUBERNETES_VERSION) to $(KUBECTL)"; \
mkdir -p $(dir $(KUBECTL)); \
tmp_file=$$(mktemp -t kubectl.XXXXXXX); \
curl -fL -o "$$tmp_file" https://dl.k8s.io/release/v$(KUBERNETES_VERSION)/bin/$(OS)/$(ARCH)/kubectl; \
chmod +x "$$tmp_file"; \
mv "$$tmp_file" $(KUBECTL); \
fi
.PHONY: generate
generate:
go generate ./...
@$(MAKE) patch-skipjob-crd SKIPJOB_CRD_FILE=$(SKIPJOB_CRD_FILE)
.PHONY: patch-skipjob-crd
patch-skipjob-crd: ensure-kubectl
@tmp_file=$$(mktemp -t skipjob-crd.XXXXXXX); \
normalized_file=$$(mktemp -t skipjob-crd-normalized.XXXXXXX); \
kubectl kustomize --load-restrictor LoadRestrictionsNone config/crd/skipjob > "$$tmp_file"; \
if [ ! -s "$$tmp_file" ]; then \
echo "ERROR: generated SKIPJob CRD is empty"; \
rm -f "$$tmp_file" "$$normalized_file"; \
exit 1; \
fi; \
awk 'NR==1 && $$0!="---"{print "---"} {print}' "$$tmp_file" > "$$normalized_file"; \
rm -f "$$tmp_file"; \
mv "$$normalized_file" "$(SKIPJOB_CRD_FILE)"
.PHONY: build
build: generate
go build \
-tags osusergo,netgo \
-trimpath \
-ldflags="-s -w" \
-o ./bin/skiperator \
./cmd/skiperator
.PHONY: run-local
run-local: build install-skiperator local-webhook
@echo ""
@echo "Starting skiperator with webhook on 0.0.0.0:9443 (accessible from kind cluster)..."
./bin/skiperator $(WEBHOOK_ARGS)
.PHONY: setup-local
setup-local: kind-cluster install-istio install-cert-manager install-prometheus-crds install-digdirator-crds install-skiperator install-webhook
@echo "Cluster $(SKIPERATOR_CONTEXT) is setup"
.PHONY: local-webhook
local-webhook: ensure-kubectl
@echo "Extracting webhook certificates for local development..."
./hack/extract-webhook-certs.sh $(LOCAL_WEBHOOK_CERTS_DIR) $(SKIPERATOR_CONTEXT)
@echo "Setting up webhook service to route to host..."
@./hack/setup-local-webhook-endpoint.sh $(SKIPERATOR_CONTEXT)
#### KIND ####
.PHONY: kind-cluster check-kind
check-kind:
@which kind >/dev/null || (echo "kind not installed, please install it to proceed"; exit 1)
.PHONY: kind-cluster
kind-cluster: check-kind
@echo Create kind cluster... >&2
@kind create cluster --image $(KIND_IMAGE) --name ${KIND_CLUSTER_NAME}
#### SKIPERATOR DEPENDENCIES ####
# Image tags for istio
ISTIO_IMAGES = docker.io/istio/proxyv2:$(ISTIO_VERSION) docker.io/istio/pilot:$(ISTIO_VERSION)
.PHONY: install-istio
install-istio: ensure-kubectl
@echo "Creating istio-gateways namespace..."
@kubectl create namespace istio-gateways --context $(SKIPERATOR_CONTEXT) || true
# Manually pull and load images into the cluster for local testing
@for image in $(ISTIO_IMAGES); do \
echo "Removing cached $$image if exists"; \
docker rmi -f "$$image" 2>/dev/null || true; \
echo "Pulling $$image for platform linux/$(ARCH)"; \
docker pull --platform linux/$(ARCH) "$$image"; \
echo "Loading $$image into kind cluster '$(KIND_CLUSTER_NAME)'"; \
docker save "$$image" | docker exec -i $(KIND_CLUSTER_NAME)-control-plane ctr --namespace=k8s.io images import -; \
done
@echo "Downloading Istio..."
@curl -L https://istio.io/downloadIstio | ISTIO_VERSION=$(ISTIO_VERSION) TARGET_ARCH=$(ARCH) sh -
@echo "Installing Istio on Kubernetes cluster..."
@./istio-$(ISTIO_VERSION)/bin/istioctl install -y --context $(SKIPERATOR_CONTEXT)
@echo "Istio installation complete."
.PHONY: install-cert-manager
install-cert-manager: ensure-kubectl
# Manually pull and load images into the cluster for local testing
@echo "Pulling and loading cert-manager images"
@curl -L -s https://github.com/cert-manager/cert-manager/releases/download/v$(CERT_MANAGER_VERSION)/cert-manager.yaml \
| grep 'image:' \
| sed -E 's/.*image:[[:space:]]*"?([^"]*)"?/\1/' \
| while read -r image; do \
echo "Removing cached $$image if exists"; \
docker rmi -f "$$image" 2>/dev/null || true; \
echo "Pulling $$image for platform linux/$(ARCH)"; \
docker pull --platform linux/$(ARCH) "$$image"; \
echo "Loading $$image into kind cluster '$(KIND_CLUSTER_NAME)'"; \
docker save "$$image" | docker exec -i $(KIND_CLUSTER_NAME)-control-plane ctr --namespace=k8s.io images import -; \
done
@echo "Installing cert-manager"
@kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v$(CERT_MANAGER_VERSION)/cert-manager.yaml --context $(SKIPERATOR_CONTEXT)
@echo "Waiting for cert-manager to be ready..."
@kubectl wait deployment --for=condition=Available --all --timeout=120s --namespace cert-manager --context $(SKIPERATOR_CONTEXT)
@echo "Waiting for cert-manager-webhook EndpointSlice to be ready..."
@until kubectl get endpointslice \
-n cert-manager \
-l kubernetes.io/service-name=cert-manager-webhook \
--context $(SKIPERATOR_CONTEXT) \
-o jsonpath='{.items[*].endpoints[?(@.conditions.ready==true)].addresses[*]}' \
| grep -q .; do \
echo "Waiting for cert-manager-webhook EndpointSlice..."; \
sleep 2; \
done
.PHONY: install-prometheus-crds
install-prometheus-crds: ensure-kubectl
@echo "Installing prometheus crds"
@kubectl apply -f https://github.com/prometheus-operator/prometheus-operator/releases/download/v$(PROMETHEUS_VERSION)/stripped-down-crds.yaml --context $(SKIPERATOR_CONTEXT)
.PHONY: install-digdirator-crds
install-digdirator-crds: ensure-kubectl
@echo "Installing digdirator crds"
@kubectl apply -f https://raw.githubusercontent.com/nais/liberator/main/config/crd/bases/nais.io_idportenclients.yaml --context $(SKIPERATOR_CONTEXT)
@kubectl apply -f https://raw.githubusercontent.com/nais/liberator/main/config/crd/bases/nais.io_maskinportenclients.yaml --context $(SKIPERATOR_CONTEXT)
.PHONY: install-skiperator
install-skiperator: generate ensure-kubectl
@kubectl create namespace skiperator-system --context $(SKIPERATOR_CONTEXT) || true
@kubectl apply -f config/cert-manager --context $(SKIPERATOR_CONTEXT) || true
@kubectl kustomize config/crd | kubectl apply -f - --context $(SKIPERATOR_CONTEXT) || true
@kubectl apply -f config/rbac --context $(SKIPERATOR_CONTEXT) || true
@kubectl apply -f config/static --context $(SKIPERATOR_CONTEXT) || true
@kubectl apply -f config/skiperator-config.yaml --context $(SKIPERATOR_CONTEXT) || true
@kubectl apply -f config/docker-config.yaml --context $(SKIPERATOR_CONTEXT) || true
@kubectl apply -f config/github-config.yaml --context $(SKIPERATOR_CONTEXT) || true
@kubectl apply -f tests/cluster-config/ --recursive --context $(SKIPERATOR_CONTEXT) || true
# Wait for PKI infrastructure to be ready (webhook)
@kubectl wait certificate skiperator-webhook-ca --for=condition=Ready --timeout=60s --namespace skiperator-system --context $(SKIPERATOR_CONTEXT)
@kubectl wait certificate skiperator-webhook-serving-cert --for=condition=Ready --timeout=60s --namespace skiperator-system --context $(SKIPERATOR_CONTEXT)
# CRD's must be present and accepted by the cluster
@kubectl wait --for=condition=Established --timeout=60s \
crd/applications.skiperator.kartverket.no \
crd/routings.skiperator.kartverket.no \
crd/skipjobs.skiperator.kartverket.no \
--context $(SKIPERATOR_CONTEXT)
.PHONY: install-webhook
install-webhook: install-skiperator
@kubectl apply -f config/webhook --context $(SKIPERATOR_CONTEXT) || true
#### TESTS ####
.PHONY: test-single
test-single: install-skiperator
@go tool chainsaw test --kube-context $(SKIPERATOR_CONTEXT) --config tests/config.yaml --test-dir $(dir) && \
echo "Test succeeded" || (echo "Test failed" && exit 1)
.PHONY: test
test: install-skiperator
@go tool chainsaw test --kube-context $(SKIPERATOR_CONTEXT) --config tests/config.yaml --test-dir tests/ && \
echo "Test succeeded" || (echo "Test failed" && exit 1)
.PHONY: run-unit-tests
run-unit-tests:
@failed_tests=$$(go test ./... 2>&1 | grep "^FAIL" | awk '{print $$2}'); \
if [ -n "$$failed_tests" ]; then \
echo -e "\033[31mFailed Unit Tests: [$$failed_tests]\033[0m" && exit 1; \
else \
echo -e "\033[32mAll unit tests passed\033[0m"; \
fi
.PHONY: run-test
run-test: build install-skiperator local-webhook
@echo "Starting skiperator in background..."
@LOG_FILE=$$(mktemp -t skiperator-test.XXXXXXX); \
./bin/skiperator $(WEBHOOK_ARGS) > "$$LOG_FILE" 2>&1 & \
PID=$$!; \
echo "skiperator PID: $$PID"; \
echo "Log redirected to file: $$LOG_FILE"; \
( \
if [ -z "$(TEST_DIR)" ]; then \
$(MAKE) test; \
else \
$(MAKE) test-single dir=$(TEST_DIR); \
fi; \
) && \
(echo "Stopping skiperator (PID $$PID)..." && kill $$PID && echo "running unit tests..." && $(MAKE) run-unit-tests) || (echo "Test or skiperator failed. Stopping skiperator (PID $$PID)" && kill $$PID && exit 1)
# Checks the delta of requests made to the kube api from the controller.
.PHONY: benchmark-chainsaw-tests
benchmark-chainsaw-tests: build install-skiperator local-webhook
@echo "Starting skiperator in background..."
@LOG_FILE=$$(mktemp -t skiperator-test.XXXXXXX); \
METRICS_BEFORE=$$(mktemp -t metrics-before.XXXXXXX); \
METRICS_AFTER=$$(mktemp -t metrics-after.XXXXXXX); \
./bin/skiperator $(WEBHOOK_ARGS) > "$$LOG_FILE" 2>&1 & \
PID=$$!; \
echo "Waiting for skiperator to start and sync..."; \
sleep 10s; \
echo "Fetching metrics before..."; \
curl -s http://127.0.0.1:8181/metrics | grep rest_client_requests_total{ > "$$METRICS_BEFORE"; \
echo "Run application tests"; \
make test; \
echo "Fetching metrics after..."; \
curl -s http://127.0.0.1:8181/metrics | grep rest_client_requests_total{ > "$$METRICS_AFTER"; \
kill $$PID; \
cat $$METRICS_BEFORE; \
echo "---"; \
cat $$METRICS_AFTER; \
echo "Kubernetes API usage (delta):"; \
gawk ' \
FNR==NR && $$0 ~ /rest_client_requests_total/ { \
split($$0, a, " "); \
split(a[1], b, "method=\""); \
split(b[2], c, "\""); \
method = c[1]; \
val = a[2]; \
before[method] += val; \
next; \
} \
$$0 ~ /rest_client_requests_total/ { \
split($$0, a, " "); \
split(a[1], b, "method=\""); \
split(b[2], c, "\""); \
method = c[1]; \
val = a[2]; \
delta = val - before[method]; \
if (delta > 0) { \
method_delta[method] += delta; \
total += delta; \
} \
before[method] = val; \
} \
END { \
n = asorti(method_delta, sorted); \
for (i=1; i<=n; i++) { \
m = sorted[i]; \
printf(" %s: %d\n", m, method_delta[m]); \
} \
printf(" Total: %d\n", total); \
} \
' "$$METRICS_BEFORE" "$$METRICS_AFTER"; \
echo "Done. Logs saved to $$LOG_FILE"
.PHONY: benchmark-long-run
benchmark-long-run: build install-skiperator local-webhook
@echo "Applying anonymous metrics RBAC..."; \
kubectl apply -f tests/cluster-config/allow-anonymous-metrics.yaml; \
echo "Starting port-forward to API server..."; \
APISERVER_POD=$$(kubectl -n kube-system get pods -l component=kube-apiserver -o jsonpath='{.items[0].metadata.name}'); \
kubectl -n kube-system port-forward pod/$$APISERVER_POD 8443:6443 >/dev/null 2>&1 & \
PID=$$!; \
sleep 3; \
echo "Starting skiperator in background..."; \
@LOG_FILE=$$(mktemp -t skiperator-test.XXXXXXX); \
./bin/skiperator $(WEBHOOK_ARGS) > "$$LOG_FILE" 2>&1 & \
SPID=$$!; \
echo "Waiting for skiperator to start and sync..."; \
sleep 20; \
echo "Summing apiserver_request_total metrics by verb (before)..."; \
METRICS_BEFORE=$$(curl -sk https://localhost:8443/metrics | grep '^apiserver_request_total{' | \
awk ' \
{ \
if (match($$0, /verb="[^"]+"/)) { \
verb=substr($$0, RSTART+6, RLENGTH-7); \
val=$$NF; \
sum[verb]+=val; \
} \
} \
END { \
for (v in sum) printf "%s %d\n", v, sum[v]; \
}'); \
echo "Applying resources..."; \
kubectl apply -f tests/application/access-policy/external-ip-policy.yaml; \
kubectl apply -f tests/application/ingress/application.yaml; \
kubectl apply -f tests/application/minimal/application.yaml; \
kubectl apply -f tests/application/custom-certificate/application.yaml; \
kubectl apply -f tests/application/copy/application.yaml; \
kubectl apply -f tests/application/gcp/application.yaml; \
kubectl apply -f tests/application/replicas/application.yaml; \
kubectl apply -f tests/application/service/application.yaml; \
kubectl apply -f tests/application/telemetry/application.yaml; \
sleep 600s; \
echo "Summing apiserver_request_total metrics by verb (after)..."; \
METRICS_AFTER=$$(curl -sk https://localhost:8443/metrics | grep '^apiserver_request_total{' | \
awk ' \
{ \
if (match($$0, /verb="[^"]+"/)) { \
verb=substr($$0, RSTART+6, RLENGTH-7); \
val=$$NF; \
sum[verb]+=val; \
} \
} \
END { \
for (v in sum) printf "%s %d\n", v, sum[v]; \
}'); \
echo "Delta by verb:"; \
echo "$$METRICS_AFTER" | while read va aval; do \
bval=$$(echo "$$METRICS_BEFORE" | awk '$$1=="'$$va'" {print $$2}'); \
[ -z "$$bval" ] && bval=0; \
delta=$$((aval - bval)); \
if [ "$$delta" != "0" ]; then \
printf "%s: %d\n" "$$va" "$$delta"; \
fi; \
done; \
echo "Cleaning up port-forward, skiperator, resources..."; \
kubectl delete -f tests/application/access-policy/external-ip-policy.yaml; \
kubectl delete -f tests/application/ingress/application.yaml; \
kubectl delete -f tests/application/minimal/application.yaml; \
kubectl delete -f tests/application/custom-certificate/application.yaml; \
kubectl delete -f tests/application/copy/application.yaml; \
kubectl delete -f tests/application/gcp/application.yaml; \
kubectl delete -f tests/application/replicas/application.yaml; \
kubectl delete -f tests/application/service/application.yaml; \
kubectl delete -f tests/application/telemetry/application.yaml; \
kill $$PID; \
kill $$SPID