|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2024 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 | +# This file contains code derived from Envoy Gateway, |
| 18 | +# https://github.com/envoyproxy/gateway |
| 19 | +# from the source file |
| 20 | +# https://github.com/envoyproxy/gateway/blob/main/tools/hack/create-cluster.sh |
| 21 | +# and is provided here subject to the following: |
| 22 | +# Copyright Envoy Gateway Authors |
| 23 | +# SPDX-License-Identifier: Apache-2.0 |
| 24 | + |
| 25 | +set -euo pipefail |
| 26 | + |
| 27 | +# Setup default values |
| 28 | +CLUSTER_NAME=${CLUSTER_NAME:-"envoy-gateway"} |
| 29 | +METALLB_VERSION=${METALLB_VERSION:-"v0.13.10"} |
| 30 | +KIND_NODE_TAG=${KIND_NODE_TAG:-"v1.28.0"} |
| 31 | +NUM_WORKERS=${NUM_WORKERS:-""} |
| 32 | + |
| 33 | + |
| 34 | +KIND_CFG=$(cat <<-EOM |
| 35 | +kind: Cluster |
| 36 | +apiVersion: kind.x-k8s.io/v1alpha4 |
| 37 | +nodes: |
| 38 | +- role: control-plane |
| 39 | +EOM |
| 40 | +) |
| 41 | + |
| 42 | +# https://kind.sigs.k8s.io/docs/user/quick-start/#multi-node-clusters |
| 43 | +if [[ -n "${NUM_WORKERS}" ]]; then |
| 44 | +for _ in $(seq 1 "${NUM_WORKERS}"); do |
| 45 | + KIND_CFG+=$(printf "\n%s" "- role: worker") |
| 46 | +done |
| 47 | +fi |
| 48 | + |
| 49 | +## Check if kind cluster already exists. |
| 50 | +if kind get clusters | grep -q "${CLUSTER_NAME}"; then |
| 51 | + echo "Cluster ${CLUSTER_NAME} already exists." |
| 52 | +else |
| 53 | +## Create kind cluster. |
| 54 | +if [[ -z "${KIND_NODE_TAG}" ]]; then |
| 55 | + cat << EOF | kind create cluster --name "${CLUSTER_NAME}" --config - |
| 56 | +${KIND_CFG} |
| 57 | +EOF |
| 58 | +else |
| 59 | + cat << EOF | kind create cluster --image "kindest/node:${KIND_NODE_TAG}" --name "${CLUSTER_NAME}" --config - |
| 60 | +${KIND_CFG} |
| 61 | +EOF |
| 62 | +fi |
| 63 | +fi |
| 64 | + |
| 65 | + |
| 66 | +## Install MetalLB. |
| 67 | +kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/"${METALLB_VERSION}"/config/manifests/metallb-native.yaml |
| 68 | +needCreate="$(kubectl get secret -n metallb-system memberlist --no-headers --ignore-not-found -o custom-columns=NAME:.metadata.name)" |
| 69 | +if [ -z "$needCreate" ]; then |
| 70 | + kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)" |
| 71 | +fi |
| 72 | + |
| 73 | +# Wait for MetalLB to become available. |
| 74 | +kubectl rollout status -n metallb-system deployment/controller --timeout 5m |
| 75 | +kubectl rollout status -n metallb-system daemonset/speaker --timeout 5m |
| 76 | + |
| 77 | +# Apply config with addresses based on docker network IPAM. |
| 78 | +subnet=$(docker network inspect kind | jq -r '.[].IPAM.Config[].Subnet | select(contains(":") | not)') |
| 79 | +# Assume default kind network subnet prefix of 16, and choose addresses in that range. |
| 80 | +address_first_octets=$(echo "${subnet}" | awk -F. '{printf "%s.%s",$1,$2}') |
| 81 | +address_range="${address_first_octets}.255.200-${address_first_octets}.255.250" |
| 82 | +kubectl apply -f - <<EOF |
| 83 | +apiVersion: metallb.io/v1beta1 |
| 84 | +kind: IPAddressPool |
| 85 | +metadata: |
| 86 | + namespace: metallb-system |
| 87 | + name: kube-services |
| 88 | +spec: |
| 89 | + addresses: |
| 90 | + - ${address_range} |
| 91 | +--- |
| 92 | +apiVersion: metallb.io/v1beta1 |
| 93 | +kind: L2Advertisement |
| 94 | +metadata: |
| 95 | + name: kube-services |
| 96 | + namespace: metallb-system |
| 97 | +spec: |
| 98 | + ipAddressPools: |
| 99 | + - kube-services |
| 100 | +EOF |
0 commit comments