sigs.k8s.io/cluster-api@v1.7.1/hack/kind-install-for-capd.sh (about) 1 #!/usr/bin/env bash 2 3 # Copyright 2021 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 18 # This script installs a local kind cluster with a local container registry and the correct files mounted for using CAPD 19 # to test Cluster API. 20 # This script is a customized version of the kind_with_local_registry script supplied by the kind maintainers at 21 # https://kind.sigs.k8s.io/docs/user/local-registry/ 22 # The modifications mount the docker socket inside the kind cluster so that CAPD can be used to 23 # created docker containers. 24 25 set -o errexit 26 set -o nounset 27 set -o pipefail 28 29 if [[ "${TRACE-0}" == "1" ]]; then 30 set -o xtrace 31 fi 32 33 KIND_CLUSTER_NAME=${CAPI_KIND_CLUSTER_NAME:-"capi-test"} 34 # See: https://kind.sigs.k8s.io/docs/user/configuration/#ip-family 35 KIND_NETWORK_IPFAMILY=${KIND_NETWORK_IPFAMILY:-"dual"} 36 37 # 1. If kind cluster already exists exit. 38 if [[ "$(kind get clusters)" =~ .*"${KIND_CLUSTER_NAME}".* ]]; then 39 echo "kind cluster already exists, moving on" 40 exit 0 41 fi 42 43 # 2. Create registry container unless it already exists 44 reg_name='kind-registry' 45 reg_port='5000' 46 if [ "$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)" != 'true' ]; then 47 docker run \ 48 -d --restart=always -p "127.0.0.1:${reg_port}:5000" --name "${reg_name}" \ 49 registry:2 50 fi 51 52 # 3. Create kind cluster with containerd registry config dir enabled. 53 # TODO(killianmuldoon): kind will eventually enable this by default and this patch will be unnecessary. 54 # 55 # See: 56 # https://github.com/kubernetes-sigs/kind/issues/2875 57 # https://github.com/containerd/containerd/blob/main/docs/cri/config.md#registry-configuration 58 # See: https://github.com/containerd/containerd/blob/main/docs/hosts.md 59 cat <<EOF | kind create cluster --name="$KIND_CLUSTER_NAME" --config=- 60 kind: Cluster 61 apiVersion: kind.x-k8s.io/v1alpha4 62 networking: 63 ipFamily: ${KIND_NETWORK_IPFAMILY} 64 nodes: 65 - role: control-plane 66 extraMounts: 67 - hostPath: /var/run/docker.sock 68 containerPath: /var/run/docker.sock 69 containerdConfigPatches: 70 - |- 71 [plugins."io.containerd.grpc.v1.cri".registry] 72 config_path = "/etc/containerd/certs.d" 73 EOF 74 75 # 4. Add the registry config to the nodes 76 # 77 # This is necessary because localhost resolves to loopback addresses that are 78 # network-namespace local. 79 # In other words: localhost in the container is not localhost on the host. 80 # 81 # We want a consistent name that works from both ends, so we tell containerd to 82 # alias localhost:${reg_port} to the registry container when pulling images 83 REGISTRY_DIR="/etc/containerd/certs.d/localhost:${reg_port}" 84 for node in $(kind get nodes --name "$KIND_CLUSTER_NAME"); do 85 docker exec "${node}" mkdir -p "${REGISTRY_DIR}" 86 cat <<EOF | docker exec -i "${node}" cp /dev/stdin "${REGISTRY_DIR}/hosts.toml" 87 [host."http://${reg_name}:5000"] 88 EOF 89 done 90 91 # 5. Connect the registry to the cluster network if not already connected 92 # This allows kind to bootstrap the network but ensures they're on the same network 93 if [ "$(docker inspect -f='{{json .NetworkSettings.Networks.kind}}' "${reg_name}")" = 'null' ]; then 94 docker network connect "kind" "${reg_name}" 95 fi 96 97 # 6. Document the local registry 98 # https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry 99 cat <<EOF | kubectl apply -f - 100 apiVersion: v1 101 kind: ConfigMap 102 metadata: 103 name: local-registry-hosting 104 namespace: kube-public 105 data: 106 localRegistryHosting.v1: | 107 host: "localhost:${reg_port}" 108 help: "https://kind.sigs.k8s.io/docs/user/local-registry/" 109 EOF