|
| 1 | +#!/bin/sh |
| 2 | +set -o errexit |
| 3 | +set -x |
| 4 | + |
| 5 | +. ".bingo/variables.env" |
| 6 | + |
| 7 | +# Original source: https://kind.sigs.k8s.io/docs/user/local-registry/ |
| 8 | +KIND_CLUSTER_NAME=${KIND_CLUSTER_NAME:-kind-olmv0} |
| 9 | +KIND=${KIND:-kind} |
| 10 | +DOCKER=${DOCKER:-docker} |
| 11 | +KUBECTL=${KUBECTL:-kubectl} |
| 12 | + |
| 13 | +# 1. Create registry container unless it already exists |
| 14 | +reg_name='kind-registry' |
| 15 | +reg_port='5001' |
| 16 | +if [ "$(${DOCKER} inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)" != 'true' ]; then |
| 17 | + ${DOCKER} run \ |
| 18 | + -d --restart=always -p "127.0.0.1:${reg_port}:5000" --network bridge --name "${reg_name}" \ |
| 19 | + registry:2 |
| 20 | +fi |
| 21 | + |
| 22 | +# 2. Create kind cluster with containerd registry config dir enabled |
| 23 | +# TODO: kind will eventually enable this by default and this patch will |
| 24 | +# be unnecessary. |
| 25 | +# |
| 26 | +# See: |
| 27 | +# https://github.com/kubernetes-sigs/kind/issues/2875 |
| 28 | +# https://github.com/containerd/containerd/blob/main/docs/cri/config.md#registry-configuration |
| 29 | +# See: https://github.com/containerd/containerd/blob/main/docs/hosts.md |
| 30 | +if [ -n "${KIND_CLUSTER_IMAGE}" ]; then |
| 31 | + KIND_IMAGE="--image=${KIND_CLUSTER_IMAGE}" |
| 32 | +fi |
| 33 | +cat <<EOF | ${KIND} create cluster --name="${KIND_CLUSTER_NAME}" "${KIND_IMAGE}" ${KIND_CREATE_OPTS} --config=- |
| 34 | +kind: Cluster |
| 35 | +apiVersion: kind.x-k8s.io/v1alpha4 |
| 36 | +containerdConfigPatches: |
| 37 | +- |- |
| 38 | + [plugins."io.containerd.grpc.v1.cri".registry] |
| 39 | + config_path = "/etc/containerd/certs.d" |
| 40 | +EOF |
| 41 | + |
| 42 | +# 3. Add the registry config to the nodes |
| 43 | +# |
| 44 | +# This is necessary because localhost resolves to loopback addresses that are |
| 45 | +# network-namespace local. |
| 46 | +# In other words: localhost in the container is not localhost on the host. |
| 47 | +# |
| 48 | +# We want a consistent name that works from both ends, so we tell containerd to |
| 49 | +# alias localhost:${reg_port} to the registry container when pulling images |
| 50 | +REGISTRY_DIR="/etc/containerd/certs.d/localhost:${reg_port}" |
| 51 | +for node in $(${KIND} --name="${KIND_CLUSTER_NAME}" get nodes); do |
| 52 | + ${DOCKER} exec "${node}" mkdir -p "${REGISTRY_DIR}" |
| 53 | + cat <<EOF | ${DOCKER} exec -i "${node}" cp /dev/stdin "${REGISTRY_DIR}/hosts.toml" |
| 54 | +[host."http://${reg_name}:5000"] |
| 55 | +EOF |
| 56 | +done |
| 57 | + |
| 58 | +# 4. Connect the registry to the cluster network |
| 59 | +# This allows kind to bootstrap the network but ensures they're on the same network |
| 60 | +if [ "$(docker inspect -f='{{json .NetworkSettings.Networks.kind}}' "${reg_name}")" = 'null' ]; then |
| 61 | + docker network connect "kind" "${reg_name}" |
| 62 | +fi |
| 63 | + |
| 64 | +# 5. Document the local registry |
| 65 | +# https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry |
| 66 | +${KIND} export kubeconfig --name="${KIND_CLUSTER_NAME}" |
| 67 | +cat <<EOF | ${KUBECTL} apply -f - |
| 68 | +apiVersion: v1 |
| 69 | +kind: ConfigMap |
| 70 | +metadata: |
| 71 | + name: local-registry-hosting |
| 72 | + namespace: kube-public |
| 73 | +data: |
| 74 | + localRegistryHosting.v1: | |
| 75 | + host: "localhost:${reg_port}" |
| 76 | + help: "https://kind.sigs.k8s.io/docs/user/local-registry/" |
| 77 | +EOF |
0 commit comments