This repository has been archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
k3d-with-registry.sh
executable file
·81 lines (70 loc) · 2.57 KB
/
k3d-with-registry.sh
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
#!/usr/bin/env bash
#
# Starts a k3s cluster (via k3d) with local image registry enabled,
# and with nodes annotated such that Tilt (https://tilt.dev/) can
# auto-detect the registry.
set -o errexit
# 🚨 only compatible with k3d v1.x (at least for now) 🚨
if ! k3d -version | grep 'v1' > /dev/null 2>&1; then
echo "This script only works with k3d v1.x"
exit 1
fi
# desired cluster name (default is "k3s-default")
CLUSTER_NAME="${CLUSTER_NAME:-k3s-default}"
# Check if cluster already exists.
# AFAICT there's no good way to get the registry name/port from a running
# cluster, so if it already exists, just bail.
for cluster in $(k3d ls 2>/dev/null | tail -n +4 | head -n -1 | awk '{print $2}'); do
if [ "$cluster" == "$CLUSTER_NAME" ]; then
# TODO(maia): check if the cluster already has the appropriate annotations--then we're okay
# TODO(maia): if cluster exists, has registry, doesn't have annotations, apply them.
# (Unfortunately there's no easy way to check what registristry (if any) the cluster
# is running, see https://github.com/rancher/k3d/issues/193)
echo "Cluster '$cluster' already exists, aborting script."
echo "\t(You can delete the cluster with 'k3d delete --name=$CLUSTER_NAME' and rerun this script.)"
exit 1
fi
done
k3d create --enable-registry --name=${CLUSTER_NAME} "$@"
echo
echo "Waiting for Kubeconfig to be ready..."
timeout=$(($(date +%s) + 30))
until [[ $(date +%s) -gt $timeout ]]; do
if k3d get-kubeconfig --name=${CLUSTER_NAME} > /dev/null 2>&1; then
export KUBECONFIG="$(k3d get-kubeconfig --name=${CLUSTER_NAME})"
DONE=true
break
fi
sleep 0.2
done
if [ -z "$DONE" ]; then
echo "Timed out trying to get Kubeconfig"
exit 1
fi
# default name/port
# TODO(maia): support other names/ports
reg_name='registry.local'
reg_port='5000'
# Annotate nodes with registry info for Tilt to auto-detect
echo "Waiting for node(s) + annotating with registry info..."
DONE=""
timeout=$(($(date +%s) + 30))
until [[ $(date +%s) -gt $timeout ]]; do
nodes=$(kubectl get nodes -o go-template --template='{{range .items}}{{printf "%s\n" .metadata.name}}{{end}}')
if [ ! -z $nodes ]; then
for node in $nodes; do
kubectl annotate node "${node}" \
tilt.dev/registry=localhost:${reg_port} \
tilt.dev/registry-from-cluster=${reg_name}:${reg_port}
done
DONE=true
break
fi
sleep 0.2
done
if [ -z "$DONE" ]; then
echo "Timed out waiting for node(s) to be up"
exit 1
fi
echo "Set kubecontext with:"
echo "\texport KUBECONFIG=\"\$(k3d get-kubeconfig --name=${CLUSTER_NAME})\""