|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Copyright 2023 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +set -o errexit |
| 18 | +set -o nounset |
| 19 | +set -o pipefail |
| 20 | + |
| 21 | +KIND_CLUSTER_NAME=${CAPI_KIND_CLUSTER_NAME:-"capi-test"} |
| 22 | + |
| 23 | + |
| 24 | +# 1. If kind cluster already exists exit. |
| 25 | +if [[ "$(kind get clusters)" =~ .*"${KIND_CLUSTER_NAME}".* ]]; then |
| 26 | + echo "kind cluster already exists, moving on" |
| 27 | + exit 0 |
| 28 | +fi |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +# 2. Create registry container unless it already exists |
| 33 | +reg_name='kind-registry' |
| 34 | +reg_port='5001' |
| 35 | +if [ "$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)" != 'true' ]; then |
| 36 | + docker run \ |
| 37 | + -d --restart=always -p "127.0.0.1:${reg_port}:5000" --name "${reg_name}" \ |
| 38 | + registry:2 |
| 39 | +fi |
| 40 | + |
| 41 | +# 3. Create kind cluster with containerd registry config dir enabled |
| 42 | +# TODO: kind will eventually enable this by default and this patch will |
| 43 | +# be unnecessary. |
| 44 | +# |
| 45 | +# See: |
| 46 | +# https://github.com/kubernetes-sigs/kind/issues/2875 |
| 47 | +# https://github.com/containerd/containerd/blob/main/docs/cri/config.md#registry-configuration |
| 48 | +# See: https://github.com/containerd/containerd/blob/main/docs/hosts.md |
| 49 | +cat <<EOF | kind create cluster --name="$KIND_CLUSTER_NAME" --config=- |
| 50 | +kind: Cluster |
| 51 | +apiVersion: kind.x-k8s.io/v1alpha4 |
| 52 | +nodes: |
| 53 | +- role: control-plane |
| 54 | +- role: worker |
| 55 | +containerdConfigPatches: |
| 56 | +- |- |
| 57 | + [plugins."io.containerd.grpc.v1.cri".registry] |
| 58 | + config_path = "/etc/containerd/certs.d" |
| 59 | +EOF |
| 60 | + |
| 61 | +# 4. Add the registry config to the nodes |
| 62 | +# |
| 63 | +# This is necessary because localhost resolves to loopback addresses that are |
| 64 | +# network-namespace local. |
| 65 | +# In other words: localhost in the container is not localhost on the host. |
| 66 | +# |
| 67 | +# We want a consistent name that works from both ends, so we tell containerd to |
| 68 | +# alias localhost:${reg_port} to the registry container when pulling images |
| 69 | +REGISTRY_DIR="/etc/containerd/certs.d/localhost:${reg_port}" |
| 70 | +for node in $(kind get nodes --name "$KIND_CLUSTER_NAME"); do |
| 71 | + docker exec "${node}" mkdir -p "${REGISTRY_DIR}" |
| 72 | + cat <<EOF | docker exec -i "${node}" cp /dev/stdin "${REGISTRY_DIR}/hosts.toml" |
| 73 | +[host."http://${reg_name}:5000"] |
| 74 | +EOF |
| 75 | +done |
| 76 | + |
| 77 | +# 5. Connect the registry to the cluster network if not already connected |
| 78 | +# This allows kind to bootstrap the network but ensures they're on the same network |
| 79 | +if [ "$(docker inspect -f='{{json .NetworkSettings.Networks.kind}}' "${reg_name}")" = 'null' ]; then |
| 80 | + docker network connect "kind" "${reg_name}" |
| 81 | +fi |
| 82 | + |
| 83 | +# 5. Document the local registry |
| 84 | +# https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry |
| 85 | +cat <<EOF | kubectl apply -f - |
| 86 | +apiVersion: v1 |
| 87 | +kind: ConfigMap |
| 88 | +metadata: |
| 89 | + name: local-registry-hosting |
| 90 | + namespace: kube-public |
| 91 | +data: |
| 92 | + localRegistryHosting.v1: | |
| 93 | + host: "localhost:${reg_port}" |
| 94 | + help: "https://kind.sigs.k8s.io/docs/user/local-registry/" |
| 95 | +EOF |
0 commit comments