32
32
33
33
KIND_CLUSTER_NAME=${CAPI_KIND_CLUSTER_NAME:- " capi-test" }
34
34
35
+
36
+ # 1. If kind cluster already exists exit.
35
37
if [[ " $( kind get clusters) " =~ .* " ${KIND_CLUSTER_NAME} " .* ]]; then
36
38
echo " kind cluster already exists, moving on"
37
39
exit 0
38
40
fi
39
41
40
- # create registry container unless it already exists
42
+ # 2. Create registry container unless it already exists
41
43
reg_name=' kind-registry'
42
44
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
45
46
docker run \
46
47
-d --restart=always -p " 127.0.0.1:${reg_port} :5000" --name " ${reg_name} " \
47
48
registry:2
48
49
fi
49
50
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
51
58
cat << EOF | kind create cluster --name="$KIND_CLUSTER_NAME " --config=-
52
59
kind: Cluster
53
60
apiVersion: kind.x-k8s.io/v1alpha4
@@ -60,15 +67,33 @@ nodes:
60
67
containerPath: /var/run/docker.sock
61
68
containerdConfigPatches:
62
69
- |-
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"]
65
87
EOF
88
+ done
66
89
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
70
95
71
- # Document the local registry
96
+ # 6. Document the local registry
72
97
# https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry
73
98
cat << EOF | kubectl apply -f -
74
99
apiVersion: v1
81
106
host: "localhost:${reg_port} "
82
107
help: "https://kind.sigs.k8s.io/docs/user/local-registry/"
83
108
EOF
84
-
0 commit comments