-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkind-dev.sh
More file actions
executable file
·211 lines (175 loc) · 5.38 KB
/
Copy pathkind-dev.sh
File metadata and controls
executable file
·211 lines (175 loc) · 5.38 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
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "${ROOT_DIR}"
DEFAULT_CLUSTER_NAME="coder-k8s-dev"
if [[ -n "${MUX_WORKSPACE_NAME:-}" ]]; then
DEFAULT_CLUSTER_NAME="coder-k8s-${MUX_WORKSPACE_NAME}"
fi
CLUSTER_NAME="${CLUSTER_NAME:-${DEFAULT_CLUSTER_NAME}}"
KUBE_CONTEXT="kind-${CLUSTER_NAME}"
NAMESPACE="${NAMESPACE:-coder-system}"
DEPLOYMENT="${DEPLOYMENT:-coder-k8s}"
KIND_NODE_IMAGE="${KIND_NODE_IMAGE:-kindest/node:v1.34.0}"
IMAGE="${IMAGE:-ghcr.io/coder/coder-k8s:e2e}"
GOARCH="${GOARCH:-}"
NODE_READY_TIMEOUT="${NODE_READY_TIMEOUT:-300s}"
require_cmd() {
local cmd="${1}"
if ! command -v "${cmd}" >/dev/null 2>&1; then
echo "assertion failed: missing required command: ${cmd}" >&2
exit 1
fi
}
kubectl_ctx() {
require_cmd kubectl
kubectl --context "${KUBE_CONTEXT}" "$@"
}
ensure_cluster() {
require_cmd kind
if ! kind get clusters | grep -qx "${CLUSTER_NAME}"; then
echo "assertion failed: kind cluster ${CLUSTER_NAME} does not exist (run: ./hack/kind-dev.sh up)" >&2
exit 1
fi
}
ensure_cluster_node_image_matches() {
local control_plane_container expected_image actual_image expected_image_id actual_image_id
require_cmd docker
expected_image="${KIND_NODE_IMAGE}"
if [[ -z "${expected_image}" ]]; then
echo "assertion failed: KIND_NODE_IMAGE must not be empty" >&2
exit 1
fi
control_plane_container="${CLUSTER_NAME}-control-plane"
if ! docker inspect "${control_plane_container}" >/dev/null 2>&1; then
echo "assertion failed: expected kind control-plane container ${control_plane_container} for existing cluster ${CLUSTER_NAME}" >&2
exit 1
fi
actual_image="$(docker inspect --format '{{.Config.Image}}' "${control_plane_container}")"
if [[ -z "${actual_image}" ]]; then
echo "assertion failed: kind control-plane container ${control_plane_container} has an empty image value" >&2
exit 1
fi
if [[ "${actual_image}" == "${expected_image}" ]]; then
return
fi
expected_image_id="$(docker image inspect --format '{{.ID}}' "${expected_image}" 2>/dev/null || true)"
actual_image_id="$(docker image inspect --format '{{.ID}}' "${actual_image}" 2>/dev/null || true)"
if [[ -n "${expected_image_id}" && -n "${actual_image_id}" && "${expected_image_id}" == "${actual_image_id}" ]]; then
return
fi
echo "assertion failed: existing cluster ${CLUSTER_NAME} uses node image ${actual_image}, but KIND_NODE_IMAGE=${expected_image}. Recreate the cluster to apply a different node image (run: ./hack/kind-dev.sh down && KIND_NODE_IMAGE=${expected_image} ./hack/kind-dev.sh up)" >&2
exit 1
}
build_binary() {
local resolved_goarch="${GOARCH}"
if [[ -z "${resolved_goarch}" ]]; then
resolved_goarch="$(go env GOARCH)"
if [[ -z "${resolved_goarch}" ]]; then
echo "assertion failed: go env GOARCH returned an empty value" >&2
exit 1
fi
fi
GOFLAGS=-mod=vendor CGO_ENABLED=0 GOOS=linux GOARCH="${resolved_goarch}" go build -o coder-k8s ./
}
build_and_load_image() {
docker build -f Dockerfile.goreleaser -t "${IMAGE}" .
kind load docker-image "${IMAGE}" --name "${CLUSTER_NAME}"
}
cmd_up() {
require_cmd kind
require_cmd kubectl
if [[ -z "${KIND_NODE_IMAGE}" ]]; then
echo "assertion failed: KIND_NODE_IMAGE must not be empty" >&2
exit 1
fi
if kind get clusters | grep -qx "${CLUSTER_NAME}"; then
ensure_cluster_node_image_matches
echo "Using existing KIND cluster ${CLUSTER_NAME} with node image: ${KIND_NODE_IMAGE}"
else
kind create cluster --name "${CLUSTER_NAME}" --image "${KIND_NODE_IMAGE}"
echo "Created KIND cluster ${CLUSTER_NAME} with node image: ${KIND_NODE_IMAGE}"
fi
kind export kubeconfig --name "${CLUSTER_NAME}" >/dev/null
kubectl config use-context "${KUBE_CONTEXT}" >/dev/null
kubectl_ctx wait --for=condition=Ready node --all --timeout="${NODE_READY_TIMEOUT}"
kubectl_ctx apply -f config/e2e/namespace.yaml
kubectl_ctx apply -f config/crd/bases/
kubectl_ctx apply -f config/rbac/
cmd_ctx
echo
echo "KIND cluster bootstrapped for coder-k8s."
echo
echo "Run controller locally (out-of-cluster):"
echo " GOFLAGS=-mod=vendor go run . --app=controller"
echo
echo "Or deploy controller in-cluster:"
echo " ./hack/kind-dev.sh load-image"
echo " kubectl apply -f config/e2e/deployment.yaml"
echo " kubectl wait --for=condition=Available deploy/${DEPLOYMENT} -n ${NAMESPACE} --timeout=120s"
echo
echo "Demo with k9s:"
echo " ./hack/kind-dev.sh k9s"
}
cmd_ctx() {
require_cmd kind
require_cmd kubectl
ensure_cluster
kind export kubeconfig --name "${CLUSTER_NAME}" >/dev/null
kubectl config use-context "${KUBE_CONTEXT}" >/dev/null
echo "Using kubectl context: ${KUBE_CONTEXT}"
}
cmd_load_image() {
require_cmd kind
require_cmd docker
require_cmd go
ensure_cluster
build_binary
build_and_load_image
echo "Loaded ${IMAGE} into cluster ${CLUSTER_NAME}."
}
cmd_k9s() {
require_cmd k9s
ensure_cluster
exec k9s --context "${KUBE_CONTEXT}"
}
cmd_status() {
require_cmd kubectl
ensure_cluster
kubectl_ctx get nodes -o wide
kubectl_ctx get codercontrolplanes -A || true
kubectl_ctx -n "${NAMESPACE}" get deploy,pods -o wide || true
}
cmd_down() {
require_cmd kind
kind delete cluster --name "${CLUSTER_NAME}"
}
usage() {
cat <<'EOF' >&2
usage: ./hack/kind-dev.sh {up|ctx|load-image|k9s|status|down}
EOF
exit 2
}
case "${1:-}" in
up)
cmd_up
;;
ctx | context | use-context)
cmd_ctx
;;
load-image)
cmd_load_image
;;
k9s)
cmd_k9s
;;
status)
cmd_status
;;
down)
cmd_down
;;
*)
usage
;;
esac