forked from linkerd/linkerd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
justfile
576 lines (469 loc) · 18.5 KB
/
justfile
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# See https://just.systems/man/en
lint: action-lint action-dev-check md-lint sh-lint rs-fetch rs-clippy rs-check-fmt go-lint
##
## Go
##
export GO111MODULE := "on"
go-fetch:
go mod download
go-fmt *flags:
bin/fmt {{ flags }}
go-lint *flags:
golangci-lint run {{ flags }}
go-test:
LINKERD_TEST_PRETTY_DIFF=1 gotestsum -- -race -v -mod=readonly ./...
##
## Rust
##
# By default we compile in development mode mode because it's faster.
rs-build-type := if env_var_or_default("RELEASE", "") == "" { "debug" } else { "release" }
# Overriddes the default Rust toolchain version.
rs-toolchain := ""
rs-features := 'all'
_cargo := "cargo" + if rs-toolchain != "" { " +" + rs-toolchain } else { "" }
# Fetch Rust dependencies.
rs-fetch:
{{ _cargo }} fetch --locked
# Format Rust code.
rs-fmt:
{{ _cargo }} fmt --all
# Check that the Rust code is formatted correctly.
rs-check-fmt:
{{ _cargo }} fmt --all -- --check
# Lint Rust code.
rs-clippy:
{{ _cargo }} clippy --frozen --workspace --all-targets --no-deps {{ _features }} {{ _fmt }}
alias clippy := rs-clippy
# Audit Rust dependencies.
rs-audit-deps:
{{ _cargo }} deny {{ _features }} check
# Generate Rust documentation.
rs-doc *flags:
{{ _cargo }} doc --frozen \
{{ if rs-build-type == "release" { "--release" } else { "" } }} \
{{ _features }} \
{{ flags }}
rs-test-build:
{{ _cargo-test }} --no-run --frozen \
--workspace --exclude=linkerd-policy-test \
{{ _features }}
# Run Rust unit tests
rs-test *flags:
{{ _cargo-test }} --frozen \
--workspace --exclude=linkerd-policy-test \
{{ if rs-build-type == "release" { "--release" } else { "" } }} \
{{ _features }} \
{{ flags }}
# Check each crate independently to ensure its Cargo.toml is sufficient.
rs-check-dirs:
#!/usr/bin/env bash
set -euo pipefail
while IFS= read -r toml ; do
{{ just_executable() }} \
rs-build-type='{{ rs-build-type }}' \
rs-features='{{ rs-features }}' \
rs-toolchain='{{ rs-toolchain }}' \
_rs-check-dir "${toml%/*}"
{{ just_executable() }} \
rs-build-type='{{ rs-build-type }}' \
rs-features='{{ rs-features }}' \
rs-toolchain='{{ rs-toolchain }}' \
_rs-check-dir "${toml%/*}" --tests
done < <(find . -mindepth 2 -name Cargo.toml | sort -r)
_rs-check-dir dir *flags:
cd {{ dir }} \
&& {{ _cargo }} check --frozen \
{{ if rs-build-type == "release" { "--release" } else { "" } }} \
{{ _features }} \
{{ flags }} \
{{ _fmt }}
# If we're running in github actions and cargo-action-fmt is installed, then add
# a command suffix that formats errors.
_fmt := if env_var_or_default("GITHUB_ACTIONS", "") != "true" { "" } else {
```
if command -v cargo-action-fmt >/dev/null 2>&1; then
echo "--message-format=json | cargo-action-fmt"
fi
```
}
# Configures which features to enable when invoking cargo commands.
_features := if rs-features == "all" {
"--all-features"
} else if rs-features != "" {
"--no-default-features --features=" + rs-features
} else { "" }
# Use cargo-nextest if it is available. It may not be available when running
# outside of the devcontainer.
_cargo-test := _cargo + ```
if command -v cargo-nextest >/dev/null 2>&1 ; then
echo " nextest run"
else
echo " test"
fi
```
##
## Policy integration tests
##
export POLICY_TEST_CONTEXT := env_var_or_default("POLICY_TEST_CONTEXT", "k3d-" + k3d-name)
# Install linkerd in the test cluster and run the policy tests.
policy-test: linkerd-install policy-test-deps-load policy-test-run && policy-test-cleanup linkerd-uninstall
# Run the policy tests without installing linkerd.
policy-test-run *flags:
cd policy-test && {{ _cargo-test }} {{ flags }}
# Build the policy tests without running them.
policy-test-build:
cd policy-test && {{ _cargo-test }} --no-run
# Delete all test namespaces and remove linkerd from the cluster.
policy-test-cleanup:
{{ _kubectl }} delete ns --selector='linkerd-policy-test'
@while [ $({{ _kubectl }} get ns --selector='linkerd-policy-test' -o json |jq '.items | length') != "0" ]; do sleep 1 ; done
policy-test-deps-pull:
docker pull -q docker.io/bitnami/kubectl:latest
docker pull -q docker.io/curlimages/curl:latest
docker pull -q ghcr.io/olix0r/hokay:latest
# Load all images into the test cluster.
policy-test-deps-load: _k3d-init policy-test-deps-pull
for i in {1..3} ; do {{ _k3d-load }} \
bitnami/kubectl:latest \
curlimages/curl:latest \
ghcr.io/olix0r/hokay:latest && exit ; sleep 1 ; done
##
## Test cluster
##
# The name of the k3d cluster to use.
k3d-name := "l5d-test"
# The name of the docker network to use (i.e., for multicluster testing).
k3d-network := "k3d-name"
# The kubernetes version to use for the test cluster. e.g. 'v1.24', 'latest', etc
k3d-k8s := "latest"
k3d-agents := "0"
k3d-servers := "1"
_k3d-flags := "--no-lb --k3s-arg --disable='local-storage,traefik,servicelb,metrics-server@server:*'"
_context := "--context=k3d-" + k3d-name
_kubectl := "kubectl " + _context
_k3d-load := "k3d image import --mode=direct --cluster=" + k3d-name
# Run kubectl with the test cluster context.
k *flags:
{{ _kubectl }} {{ flags }}
# Creates a k3d cluster that can be used for testing.
k3d-create: && _k3d-ready
k3d cluster create {{ k3d-name }} \
--image='+{{ k3d-k8s }}' \
--agents='{{ k3d-agents }}' \
--servers='{{ k3d-servers }}' \
--network='{{ k3d-network }}' \
{{ _k3d-flags }} \
--kubeconfig-update-default \
--kubeconfig-switch-context=false
# Deletes the test cluster.
k3d-delete:
k3d cluster delete {{ k3d-name }}
# Print information the test cluster's detailed status.
k3d-info:
k3d cluster list {{ k3d-name }} -o json | jq .
# Set the default kubectl context to the test cluster.
k3d-use:
k3d kubeconfig merge {{ k3d-name }} \
--kubeconfig-merge-default \
--kubeconfig-switch-context=true \
>/dev/null
# Ensures the test cluster has been initialized.
_k3d-init: && _k3d-ready
#!/usr/bin/env bash
set -euo pipefail
if ! k3d cluster list {{ k3d-name }} >/dev/null 2>/dev/null; then
{{ just_executable() }} \
k3d-k8s='{{ k3d-k8s }}' \
k3d-name='{{ k3d-name }}' \
k3d-network='{{ k3d-network }}' \
_k3d-flags='{{ _k3d-flags }}' \
k3d-create
fi
k3d kubeconfig merge {{ k3d-name }} \
--kubeconfig-merge-default \
--kubeconfig-switch-context=false \
>/dev/null
_k3d-ready: _k3d-api-ready _k3d-dns-ready
# Wait for the cluster's API server to be accessible
_k3d-api-ready:
#!/usr/bin/env bash
set -euo pipefail
for i in {1..6} ; do
if {{ _kubectl }} cluster-info >/dev/null ; then exit 0 ; fi
sleep 10
done
exit 1
# Wait for the cluster's DNS pods to be ready.
_k3d-dns-ready:
while [ $({{ _kubectl }} get po -n kube-system -l k8s-app=kube-dns -o json |jq '.items | length') = "0" ]; do sleep 1 ; done
{{ _kubectl }} wait pod --for=condition=ready \
--namespace=kube-system --selector=k8s-app=kube-dns \
--timeout=1m
##
## Docker images
##
# If DOCKER_REGISTRY is not already set, use a bogus registry with a unique
# name so that it's virtually impossible to accidentally use an incorrect image.
export DOCKER_REGISTRY := env_var_or_default("DOCKER_REGISTRY", "test.l5d.io/" + _test-id )
_test-id := `tr -dc 'a-z0-9' </dev/urandom | fold -w 5 | head -n 1`
# The docker image tag.
linkerd-tag := `bin/root-tag`
docker-arch := ''
_pause-load: _k3d-init
#!/usr/bin/env bash
set -euo pipefail
img="$(yq .gateway.pauseImage multicluster/charts/linkerd-multicluster/values.yaml)"
if [ -z "$(docker image ls -q "$img")" ]; then
docker pull -q "$img"
fi
k3d image import --mode=direct --cluster='{{ k3d-name }}' "$img"
##
## Linkerd CLI
##
# The Linkerd CLI binary.
linkerd-exec := "bin/linkerd"
_linkerd := linkerd-exec + " " + _context
controller-image := DOCKER_REGISTRY + "/controller"
proxy-image := DOCKER_REGISTRY + "/proxy"
policy-controller-image := DOCKER_REGISTRY + "/policy-controller"
# External dependencies
#
# We execute these commands lazily in case `yq` isn't present (so that other
# just recipes can succeed).
_proxy-init-image-cmd := "yq '.proxyInit.image | \"ghcr.io/linkerd/proxy-init:\" + .version' charts/linkerd-control-plane/values.yaml"
_cni-plugin-image-cmd := "yq '.image | \"ghcr.io/linkerd/cni-plugin:\" + .version' charts/linkerd2-cni/values.yaml"
_prometheus-image-cmd := "yq '.prometheus.image | .registry + \"/\" + .name + \":\" + .tag' viz/charts/linkerd-viz/values.yaml"
linkerd *flags:
{{ _linkerd }} {{ flags }}
# Install crds on the test cluster.
linkerd-crds-install: _k3d-init
{{ _linkerd }} install --crds \
| {{ _kubectl }} apply -f -
{{ _kubectl }} wait crd --for condition=established \
--selector='linkerd.io/control-plane-ns' \
--timeout=1m
# Install linkerd on the test cluster using test images.
linkerd-install *args='': linkerd-load linkerd-crds-install && _linkerd-ready
{{ _linkerd }} install \
--set='imagePullPolicy=Never' \
--set='controllerImage={{ controller-image }}' \
--set='linkerdVersion={{ linkerd-tag }}' \
--set='policyController.image.name={{ policy-controller-image }}' \
--set='policyController.image.version={{ linkerd-tag }}' \
--set='policyController.loglevel=info\,linkerd=trace\,kubert=trace' \
--set='proxy.image.name={{ proxy-image }}' \
--set='proxy.image.version={{ linkerd-tag }}' \
--set='proxyInit.image.name=ghcr.io/linkerd/proxy-init' \
{{ args }} \
| {{ _kubectl }} apply -f -
# Wait for all test namespaces to be removed before uninstalling linkerd from the cluster.
linkerd-uninstall:
{{ _linkerd }} uninstall \
| {{ _kubectl }} delete -f -
linkerd-load: _linkerd-images _k3d-init
for i in {1..3} ; do {{ _k3d-load }} \
'{{ controller-image }}:{{ linkerd-tag }}' \
'{{ policy-controller-image }}:{{ linkerd-tag }}' \
'{{ proxy-image }}:{{ linkerd-tag }}' \
$({{ _proxy-init-image-cmd }}) && exit ; sleep 1 ; done
linkerd-load-cni:
docker pull -q $({{ _cni-plugin-image-cmd }})
{{ _k3d-load }} $({{ _cni-plugin-image-cmd }})
linkerd-build: _policy-controller-build
TAG={{ linkerd-tag }} bin/docker-build-controller
TAG={{ linkerd-tag }} bin/docker-build-proxy
_linkerd-images:
#!/usr/bin/env bash
set -xeuo pipefail
docker pull -q $({{ _proxy-init-image-cmd }})
for img in \
'{{ controller-image }}:{{ linkerd-tag }}' \
'{{ policy-controller-image }}:{{ linkerd-tag }}' \
'{{ proxy-image }}:{{ linkerd-tag }}'
do
if [ -z $(docker image ls -q "$img") ]; then
# Build images if any one of the images is missing.
exec {{ just_executable() }} \
controller-image='{{ controller-image }}' \
policy-controller-image='{{ policy-controller-image }}' \
linkerd-tag='{{ linkerd-tag }}' \
linkerd-build
fi
done
# Build the policy controller docker image for testing (on amd64).
_policy-controller-build:
docker buildx build . \
--file='policy-controller/Dockerfile' \
--platform={{ if docker-arch == '' { "amd64" } else { docker-arch} }} \
--build-arg='build_type={{ rs-build-type }}' \
--tag='{{ policy-controller-image }}:{{ linkerd-tag }}' \
--progress=plain \
--load
_linkerd-ready:
if ! {{ _kubectl }} wait pod --for=condition=ready \
--namespace=linkerd --selector='linkerd.io/control-plane-component' \
--timeout=1m ; then \
{{ _kubectl }} describe pods --namespace=linkerd ; \
fi
# Ensure that a linkerd control plane is installed
_linkerd-init: && _linkerd-ready
#!/usr/bin/env bash
set -euo pipefail
if ! {{ _kubectl }} get ns linkerd >/dev/null 2>&1 ; then
{{ just_executable() }} \
k3d-name='{{ k3d-name }}' \
k3d-k8s='{{ k3d-k8s }}' \
k3d-agents='{{ k3d-agents }}' \
k3d-servers='{{ k3d-servers }}' \
k3d-network='{{ k3d-network }}' \
_k3d-flags='{{ _k3d-flags }}' \
linkerd-tag='{{ linkerd-tag }}' \
controller-image='{{ controller-image }}' \
proxy-image='{{ proxy-image }}' \
linkerd-exec='{{ linkerd-exec }}' \
linkerd-install
fi
##
## linkerd viz
##
linkerd-viz *flags: _k3d-init
{{ _linkerd }} viz {{ flags }}
linkerd-viz-install: _linkerd-init linkerd-viz-load && _linkerd-viz-ready
{{ _linkerd }} viz install \
--set='defaultRegistry={{ DOCKER_REGISTRY }}' \
--set='linkerdVersion={{ linkerd-tag }}' \
--set='defaultImagePullPolicy=Never' \
| {{ _kubectl }} apply -f -
# Wait for all test namespaces to be removed before uninstalling linkerd from the cluster.
linkerd-viz-uninstall:
{{ _linkerd }} viz uninstall \
| {{ _kubectl }} delete -f -
_linkerd-viz-images:
#!/usr/bin/env bash
set -euo pipefail
docker pull -q $({{ _prometheus-image-cmd }})
for img in \
'{{ DOCKER_REGISTRY }}/metrics-api:{{ linkerd-tag }}' \
'{{ DOCKER_REGISTRY }}/tap:{{ linkerd-tag }}' \
'{{ DOCKER_REGISTRY }}/web:{{ linkerd-tag }}'
do
if [ -z $(docker image ls -q "$img") ]; then
echo "Missing image: $img" >&2
exec {{ just_executable() }} \
linkerd-tag='{{ linkerd-tag }}' \
linkerd-viz-build
fi
done
linkerd-viz-load: _linkerd-viz-images _k3d-init
for i in {1..3} ; do {{ _k3d-load }} \
{{ DOCKER_REGISTRY }}/metrics-api:{{ linkerd-tag }} \
{{ DOCKER_REGISTRY }}/tap:{{ linkerd-tag }} \
{{ DOCKER_REGISTRY }}/web:{{ linkerd-tag }} \
$({{ _prometheus-image-cmd }}) && exit ; sleep 1 ; done
linkerd-viz-build:
TAG={{ linkerd-tag }} bin/docker-build-metrics-api
TAG={{ linkerd-tag }} bin/docker-build-tap
TAG={{ linkerd-tag }} bin/docker-build-web
_linkerd-viz-ready:
{{ _kubectl }} wait pod --for=condition=ready \
--namespace=linkerd-viz --selector='linkerd.io/extension=viz' \
--timeout=1m
# Ensure that a linkerd control plane is installed
_linkerd-viz-uninit:
#!/usr/bin/env bash
set -euo pipefail
if {{ _kubectl }} get ns linkerd-viz >/dev/null 2>&1 ; then
{{ just_executable() }} \
k3d-name='{{ k3d-name }}' \
linkerd-exec='{{ linkerd-exec }}' \
linkerd-viz-uninstall
fi
# TODO linkerd-jaeger-install
##
## linkerd multicluster
##
_mc-target-k3d-flags := "--k3s-arg --disable='local-storage,metrics-server@server:*' --k3s-arg '--cluster-cidr=10.23.0.0/24@server:*'"
linkerd-mc-install: _linkerd-init
{{ _linkerd }} mc install --set='linkerdVersion={{ linkerd-tag }}' \
| {{ _kubectl }} apply -f -
# Wait for all test namespaces to be removed before uninstalling linkerd-multicluster.
linkerd-mc-uninstall:
{{ _linkerd }} mc uninstall \
| {{ _kubectl }} delete -f -
mc-target-k3d-delete:
#!/usr/bin/env bash
set -euo pipefail
if k3d cluster list '{{ k3d-name }}-target' >/dev/null 2>/dev/null; then
{{ just_executable() }} \
k3d-name='{{ k3d-name }}-target' \
k3d-delete
fi
_mc-load: _k3d-init linkerd-load
_mc-target-load:
@{{ just_executable() }} \
k3d-name='{{ k3d-name }}-target' \
k3d-k8s='{{ k3d-k8s }}' \
k3d-agents='{{ k3d-agents }}' \
k3d-servers='{{ k3d-servers }}' \
k3d-network='{{ k3d-network }}' \
_k3d-flags='{{ _mc-target-k3d-flags }}' \
controller-image='{{ controller-image }}' \
proxy-image='{{ proxy-image }}' \
linkerd-exec='{{ linkerd-exec }}' \
linkerd-tag='{{ linkerd-tag }}' \
_pause-load \
_mc-load
# Run the multicluster tests with cluster setup
mc-test: mc-test-load mc-test-run
mc-test-build:
go build --mod=readonly \
./test/integration/multicluster/...
mc-test-load: _mc-load _mc-target-load mc-flat-network-init
k3d-source-server := "k3d-" + k3d-name + "-server-0"
k3d-target-server := "k3d-" + k3d-name + "-target-server-0"
_mc-route-output-fmt := "-o jsonpath='ip route add {.spec.podCIDR} via {.status.addresses[?(.type==\"InternalIP\")].address}'"
_mc-print-source-route := _kubectl + " " + "get node " + k3d-source-server + " " + _mc-route-output-fmt
_mc-print-target-route := "kubectl --context=k3d-" + k3d-name + "-target "+ "get node " + k3d-target-server + " " + _mc-route-output-fmt
# Allow two k3d server nodes to participate in a flat network
mc-flat-network-init:
@docker exec k3d-{{k3d-name}}-server-0 `{{_mc-print-target-route}}`
@docker exec k3d-{{k3d-name}}-target-server-0 `{{_mc-print-source-route}}`
# Run the multicluster tests without any setup
mc-test-run:
LINKERD_DOCKER_REGISTRY='{{ DOCKER_REGISTRY }}' \
go test -v -test.timeout=15m --failfast --mod=readonly \
./test/integration/multicluster/... \
-integration-tests \
-linkerd='{{ justfile_directory() }}/bin/linkerd' \
-multicluster-source-context='k3d-{{ k3d-name }}' \
-multicluster-target-context='k3d-{{ k3d-name }}-target'
##
## GitHub Actions
##
# Format actionlint output for Github Actions if running in CI.
_actionlint-fmt := if env_var_or_default("GITHUB_ACTIONS", "") != "true" { "" } else {
'{{range $err := .}}::error file={{$err.Filepath}},line={{$err.Line}},col={{$err.Column}}::{{$err.Message}}%0A```%0A{{replace $err.Snippet "\\n" "%0A"}}%0A```\n{{end}}'
}
# Lints all GitHub Actions workflows
action-lint:
actionlint {{ if _actionlint-fmt != '' { "-format '" + _actionlint-fmt + "'" } else { "" } }} .github/workflows/*
# Ensure all devcontainer versions are in sync
action-dev-check:
just-dev check-action-images
##
## Other tools...
##
md-lint:
markdownlint-cli2 '**/*.md' '!**/node_modules' '!target'
sh-lint:
bin/shellcheck-all
##
## Git
##
# Display the git history minus Dependabot updates
history *paths='.':
@git log --oneline --graph --invert-grep --author="dependabot" -- {{ paths }}
# Display the history of Dependabot changes
history-dependabot *paths='.':
@git log --oneline --graph --author="dependabot" -- {{ paths }}
# vim: set ft=make :