github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/test/integration/setup-kind-cluster.sh (about) 1 #!/usr/bin/env bash 2 # Copyright 2022 The Kubernetes Authors. 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 # Set up the KIND cluster. 17 18 set -o errexit 19 set -o nounset 20 set -o pipefail 21 22 SCRIPT_ROOT="$(cd "$(dirname "$0")" && pwd)" 23 source "${SCRIPT_ROOT}"/lib.sh 24 25 function usage() { 26 >&2 cat <<EOF 27 Set up the KIND cluster. Ultimately, this results in the running of 2 docker containers, called kind-prow-integration-control-plane and kind-prow-integration-registry. 28 29 Usage: $0 [options] 30 31 Examples: 32 # Setup the KIND cluster with default settings (this is what you want when you 33 # are running this script manually for debugging). 34 $0 35 36 # Use a different node port (32222) for the KIND cluster than the default 37 # 30303 one. This port is used by fakepubsub. Note that the port must be in 38 # the range 30000-32767. 39 $0 -fakepubsub-node-port=32222 40 41 Options: 42 -fakepubsub-node-port='': 43 Make the fakepubsub service use the provided node port (default 30303). 44 45 -help: 46 Display this help message. 47 EOF 48 } 49 50 function main() { 51 local fakepubsub_node_port 52 fakepubsub_node_port=30303 53 # If we abort the setup script with Ctrl+C, delete the cluster because the 54 # setup process was interrupted. 55 # shellcheck disable=SC2064 56 trap "${SCRIPT_ROOT}/teardown.sh -kind-cluster" SIGINT SIGTERM 57 58 if [[ -z "${HOME:-}" ]]; then 59 HOME="$(cd ~ && pwd -P)" 60 export HOME 61 fi 62 63 for arg in "$@"; do 64 case "${arg}" in 65 -fakepubsub-node-port=*) 66 fakepubsub_node_port="${arg#-fakepubsub-node-port=}" 67 ;; 68 -help) 69 usage 70 return 71 ;; 72 --*) 73 echo >&2 "cannot use flags with two leading dashes ('--...'), use single dashes instead ('-...')" 74 return 1 75 ;; 76 esac 77 done 78 79 # The KIND cluster is configured to use a special local docker registry; this 80 # registry must exist before we bring up the cluster. See https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry for more information. 81 setup_local_docker_registry 82 83 # Required for some tests (e.g., horologium_test.go) that use a dummy image. 84 # 85 # TODO(listx): Move this code to horologium_test.go, as it is orthogonal to 86 # KIND cluster setup. 87 populate_registry gcr.io/k8s-prow/alpine:latest alpine:latest 88 89 if cluster_running; then 90 log "Using existing KIND cluster" 91 else 92 "${SCRIPT_ROOT}/teardown.sh" -kind-cluster 93 create_cluster "${fakepubsub_node_port:-30303}" 94 fi 95 setup_cluster 96 97 # Use nginx as a reverse proxy and load balancer for the cluster. We don't 98 # wait for it here in this script because it can take a while to finish. We do 99 # wait for it in the setup-prow-components.sh script. 100 log "Installing nginx ingress controller on KIND cluster" 101 do_kubectl apply -f "${SCRIPT_ROOT}/config/nginx.yaml" 102 } 103 104 function cluster_running() { 105 local running 106 running="$(docker inspect -f '{{.State.Running}}' "${_KIND_CLUSTER_NAME}-control-plane" 2>/dev/null || true)" 107 [[ "${running}" == "true" ]] 108 } 109 110 # Create a cluster with the local registry enabled in containerd, 111 # as well as configure node-labels and extraPortMappings for ingress. 112 # See: https://kind.sigs.k8s.io/docs/user/ingress/#create-cluster. 113 function create_cluster() { 114 log "Creating KIND cluster" 115 116 local fakepubsub_node_port 117 fakepubsub_node_port="${1:-30303}" 118 119 cat <<EOF | kind create cluster --name "${_KIND_CLUSTER_NAME}" --config=- 120 kind: Cluster 121 apiVersion: kind.x-k8s.io/v1alpha4 122 containerdConfigPatches: 123 - |- 124 [plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${LOCAL_DOCKER_REGISTRY_PORT}"] 125 endpoint = ["http://${LOCAL_DOCKER_REGISTRY_NAME}:5000"] 126 nodes: 127 - role: control-plane 128 kubeadmConfigPatches: 129 - | 130 kind: InitConfiguration 131 nodeRegistration: 132 kubeletExtraArgs: 133 node-labels: "ingress-ready=true" 134 extraPortMappings: 135 - containerPort: 80 136 hostPort: 80 137 protocol: TCP 138 - containerPort: 443 139 hostPort: 443 140 protocol: TCP 141 - containerPort: 32000 142 hostPort: 32000 143 protocol: TCP 144 - containerPort: ${fakepubsub_node_port} 145 hostPort: ${fakepubsub_node_port} 146 protocol: TCP 147 EOF 148 149 } 150 151 # Connect the registry to the cluster network. 152 function setup_cluster() { 153 log "Setting up local registry for cluster" 154 # Ignore the error, as the network may already be connected. 155 docker network connect "kind" "${LOCAL_DOCKER_REGISTRY_NAME}" 2>/dev/null || true 156 157 cat <<EOF | do_kubectl apply -f - 158 apiVersion: v1 159 kind: ConfigMap 160 metadata: 161 name: local-registry-hosting 162 namespace: kube-public 163 data: 164 localRegistryHosting.v1: | 165 host: "localhost:${LOCAL_DOCKER_REGISTRY_PORT}" 166 help: "https://kind.sigs.k8s.io/docs/user/local-registry/" 167 EOF 168 169 } 170 171 function setup_local_docker_registry() { 172 # Create registry container unless it already exists. 173 running="$(docker inspect -f '{{.State.Running}}' "${LOCAL_DOCKER_REGISTRY_NAME}" 2>/dev/null || true)" 174 if [[ "${running}" == 'true' ]]; then 175 log "Local registry localhost:${LOCAL_DOCKER_REGISTRY_PORT} already exists" 176 else 177 log "Creating docker container for hosting local registry localhost:${LOCAL_DOCKER_REGISTRY_PORT}" 178 "${SCRIPT_ROOT}/teardown.sh" -local-registry 179 docker run \ 180 -d --restart=always -p "127.0.0.1:${LOCAL_DOCKER_REGISTRY_PORT}:5000" --name "${LOCAL_DOCKER_REGISTRY_NAME}" \ 181 registry:2 182 fi 183 } 184 185 function populate_registry() { 186 local src 187 local dest 188 189 src="${1:-}" 190 dest="${2:-}" 191 dest="localhost:${LOCAL_DOCKER_REGISTRY_PORT}/${dest}" 192 log "Push ${src} to registry as ${2}" 193 docker pull "${src}" 194 docker tag "${src}" "${dest}" 195 docker push "${dest}" 196 } 197 198 main "$@"