Skip to content

Commit 2c07a29

Browse files
Align kind registry script with upstream
Signed-off-by: killianmuldoon <[email protected]>
1 parent 74e4592 commit 2c07a29

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

hack/kind-install-for-capd.sh

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,29 @@ fi
3232

3333
KIND_CLUSTER_NAME=${CAPI_KIND_CLUSTER_NAME:-"capi-test"}
3434

35+
36+
# 1. If kind cluster already exists exit.
3537
if [[ "$(kind get clusters)" =~ .*"${KIND_CLUSTER_NAME}".* ]]; then
3638
echo "kind cluster already exists, moving on"
3739
exit 0
3840
fi
3941

40-
# create registry container unless it already exists
42+
# 2. Create registry container unless it already exists
4143
reg_name='kind-registry'
4244
reg_port='5000'
43-
running="$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)"
44-
if [ "${running}" != 'true' ]; then
45+
if [ "$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)" != 'true' ]; then
4546
docker run \
4647
-d --restart=always -p "127.0.0.1:${reg_port}:5000" --name "${reg_name}" \
4748
registry:2
4849
fi
4950

50-
# create a cluster with the local registry enabled in containerd
51+
# 3. Create kind cluster with containerd registry config dir enabled.
52+
# TODO(killianmuldoon): kind will eventually enable this by default and this patch will be unnecessary.
53+
#
54+
# See:
55+
# https://github.com/kubernetes-sigs/kind/issues/2875
56+
# https://github.com/containerd/containerd/blob/main/docs/cri/config.md#registry-configuration
57+
# See: https://github.com/containerd/containerd/blob/main/docs/hosts.md
5158
cat <<EOF | kind create cluster --name="$KIND_CLUSTER_NAME" --config=-
5259
kind: Cluster
5360
apiVersion: kind.x-k8s.io/v1alpha4
@@ -60,15 +67,33 @@ nodes:
6067
containerPath: /var/run/docker.sock
6168
containerdConfigPatches:
6269
- |-
63-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${reg_port}"]
64-
endpoint = ["http://${reg_name}:${reg_port}"]
70+
[plugins."io.containerd.grpc.v1.cri".registry]
71+
config_path = "/etc/containerd/certs.d"
72+
EOF
73+
74+
# 4. Add the registry config to the nodes
75+
#
76+
# This is necessary because localhost resolves to loopback addresses that are
77+
# network-namespace local.
78+
# In other words: localhost in the container is not localhost on the host.
79+
#
80+
# We want a consistent name that works from both ends, so we tell containerd to
81+
# alias localhost:${reg_port} to the registry container when pulling images
82+
REGISTRY_DIR="/etc/containerd/certs.d/localhost:${reg_port}"
83+
for node in $(kind get nodes --name "$KIND_CLUSTER_NAME"); do
84+
docker exec "${node}" mkdir -p "${REGISTRY_DIR}"
85+
cat <<EOF | docker exec -i "${node}" cp /dev/stdin "${REGISTRY_DIR}/hosts.toml"
86+
[host."http://${reg_name}:5000"]
6587
EOF
88+
done
6689

67-
# connect the registry to the cluster network
68-
# (the network may already be connected)
69-
docker network connect "kind" "${reg_name}" || true
90+
# 5. Connect the registry to the cluster network if not already connected
91+
# This allows kind to bootstrap the network but ensures they're on the same network
92+
if [ "$(docker inspect -f='{{json .NetworkSettings.Networks.kind}}' "${reg_name}")" = 'null' ]; then
93+
docker network connect "kind" "${reg_name}"
94+
fi
7095

71-
# Document the local registry
96+
# 6. Document the local registry
7297
# https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry
7398
cat <<EOF | kubectl apply -f -
7499
apiVersion: v1
@@ -81,4 +106,3 @@ data:
81106
host: "localhost:${reg_port}"
82107
help: "https://kind.sigs.k8s.io/docs/user/local-registry/"
83108
EOF
84-

0 commit comments

Comments
 (0)