-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
121 lines (99 loc) · 3.26 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
### -----------------------
# --- Variables
### -----------------------
KUBECTL_DEFAULT_VERSION := 1.32
JQ_DEFAULT_VERSION := 1.7.1
KUBECTL_VERSIONS := 1.28 1.29 1.30 1.31 1.32
JQ_VERSIONS := 1.6 1.7.1
# Get CPU count and multiply by 3 for parallel tests
NCPU := $(shell if [ "$$(uname)" = "Darwin" ]; then sysctl -n hw.ncpu; else nproc; fi)
PARALLEL_JOBS := $(shell echo "$$(( $(NCPU) * 3 ))")
### -----------------------
# --- Building & Testing
### -----------------------
.PHONY: all
all:
$(MAKE) build
$(MAKE) test-matrix
.PHONY: build
build: format lint
.PHONY: info
info:
@shellcheck --version
@shellharden --version
.PHONY: format
format:
@shellharden --replace kubectl-envx
@shellharden --replace test/*.bats
.PHONY: lint
lint:
@shellcheck -x kubectl-envx
@shellcheck -x test/*.bats
@shellharden --check kubectl-envx
@shellharden --check test/*.bats
.PHONY: test
test:
@bash test/kind_init.sh
$(MAKE) test-version KUBECTL_VERSION=$(KUBECTL_DEFAULT_VERSION) JQ_VERSION=$(JQ_DEFAULT_VERSION)
# e.g. make test-version KUBECTL_VERSION=1.32 JQ_VERSION=1.7.1
.PHONY: test-version
test-version:
@if [ -z "$(KUBECTL_VERSION)" ]; then echo "KUBECTL_VERSION is required"; exit 1; fi
@if [ -z "$(JQ_VERSION)" ]; then echo "JQ_VERSION is required"; exit 1; fi
@echo "=== Testing with kubectl v$(KUBECTL_VERSION) and jq v$(JQ_VERSION) ==="
@( \
temp_dir=$$(mktemp -d); \
trap 'rm -rf "$$temp_dir"' EXIT; \
ln -s "/opt/jq/bin/jq-$(JQ_VERSION)" "$$temp_dir/jq"; \
ln -s "/opt/kubectl/bin/kubectl-$(KUBECTL_VERSION)" "$$temp_dir/kubectl"; \
PATH="$$temp_dir:$$PATH" \
bash -c 'kubectl version --client \
&& jq --version \
&& bats test -T -j $(PARALLEL_JOBS)' \
)
.PHONY: test-matrix
test-matrix:
@bash test/kind_init.sh
@for k in $(KUBECTL_VERSIONS); do \
for j in $(JQ_VERSIONS); do \
$(MAKE) test-version KUBECTL_VERSION=$$k JQ_VERSION=$$j || exit 1; \
done; \
done
### -----------------------
# --- Kind
### -----------------------
# kind, these steps are meant to run locally on your machine directly!
# https://johnharris.io/2019/04/kubernetes-in-docker-kind-of-a-big-deal/
.PHONY: kind-cluster-clean
kind-cluster-clean:
kind delete cluster --name kubectl-envx
rm -rf .kube/**
# https://hub.docker.com/r/kindest/node/tags
.PHONY: kind-cluster-init
kind-cluster-init:
kind create cluster --name kubectl-envx --config=test/kind.yaml --kubeconfig .kube/config --image "kindest/node:v1.31.4"
$(MAKE) kind-fix-kubeconfig
sleep 1
$(MAKE) kind-cluster-init-script
.PHONY: kind-fix-kubeconfig
kind-fix-kubeconfig:
sed -i.bak -e 's/127.0.0.1/host.docker.internal/' .kube/config
.PHONY: kind-cluster-init-script
kind-cluster-init-script:
docker-compose up --no-start
docker-compose start
docker-compose exec service bash test/kind_init.sh
.PHONY: kind-cluster-reset
kind-cluster-reset:
$(MAKE) kind-cluster-clean
$(MAKE) kind-cluster-init
### -----------------------
# --- Special targets
### -----------------------
# https://unix.stackexchange.com/questions/153763/dont-stop-makeing-if-a-command-fails-but-check-exit-status
# https://www.gnu.org/software/make/manual/html_node/One-Shell.html
# required to ensure make fails if one recipe fails (even on parallel jobs) and on pipefails
.ONESHELL:
# normal POSIX bash shell mode
SHELL = /bin/bash
.SHELLFLAGS = -cEeuo pipefail