This repository has been archived by the owner on Dec 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.kindUtils
89 lines (81 loc) · 3.11 KB
/
.kindUtils
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# shellcheck shell=bash
kindGenExternalKubeconfig() {
# Generate a kubeconfig that uses the docker bridge network IP address of the cluster
# This is required for using the subctl cmd (for submariner)
local master_ip
mkdir -p ${TMP_DIR}/kubeconfigs/external/
EXTERNAL_KUBECONFIG=${TMP_DIR}/kubeconfigs/external/${cluster}.kubeconfig
cp ${TMP_DIR}/kubeconfigs/${cluster}.kubeconfig ${EXTERNAL_KUBECONFIG}
master_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "${cluster}-control-plane" | head -n 1)
${YQ_BIN} -i ".clusters[0].cluster.server = \"https://${master_ip}:6443\"" "${EXTERNAL_KUBECONFIG}"
${YQ_BIN} -i "(.. | select(. == \"kind-${cluster}\")) = \"${cluster}\"" "${EXTERNAL_KUBECONFIG}"
chmod a+r "${EXTERNAL_KUBECONFIG}"
}
kindCreateCluster() {
local cluster=$1;
local port80=$2;
local port443=$3;
local idx=$4
# Each cluster should have a different service & pod network.
# This allows a flat network to be established if submariner is used
local pod_cidr="10.24${idx}.0.0/16"
local service_cidr="100.9${idx}.0.0/16"
local dns_domain="${cluster}.local"
export KIND_EXPERIMENTAL_DOCKER_NETWORK=$KIND_CLUSTER_DOCKER_NETWORK
cat <<EOF | ${KIND_BIN} create cluster --name ${cluster} --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
podSubnet: ${pod_cidr}
serviceSubnet: ${service_cidr}
nodes:
- role: control-plane
image: kindest/node:v1.26.0
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
- |
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
metadata:
name: config
networking:
podSubnet: ${pod_cidr}
serviceSubnet: ${service_cidr}
dnsDomain: ${dns_domain}
extraPortMappings:
- containerPort: 80
hostPort: ${port80}
protocol: TCP
- containerPort: 443
hostPort: ${port443}
protocol: TCP
EOF
mkdir -p ${TMP_DIR}/kubeconfigs
${KIND_BIN} export kubeconfig -n ${cluster} --kubeconfig ${TMP_DIR}/kubeconfig --internal
${KIND_BIN} get kubeconfig --name ${cluster} > ${TMP_DIR}/kubeconfigs/${cluster}.kubeconfig
${KIND_BIN} export kubeconfig --name ${cluster} --kubeconfig ${TMP_DIR}/kubeconfigs/internal/${cluster}.kubeconfig --internal
kindGenExternalKubeconfig
}
setupClusters() {
local controlPlaneName=$1
local dataPlaneName=$2
local port80=$3
local port443=$4
local dataPlaneClusterCount=$5
# Create network for the clusters
docker network create -d bridge --subnet 172.31.0.0/16 ${KIND_CLUSTER_DOCKER_NETWORK} --gateway 172.31.0.1 \
-o "com.docker.network.bridge.enable_ip_masquerade"="true" \
-o "com.docker.network.driver.mtu"="1500"
# Create Kind control plane cluster
kindCreateCluster ${controlPlaneName} ${port80} ${port443}
# Add workload clusters if dataPlaneClusterCount is > 0
if [[ -n "${dataPlaneClusterCount}" ]]; then
for ((i = 1; i <= ${dataPlaneClusterCount}; i++)); do
kindCreateCluster ${dataPlaneName}-${i} $((${port80} + ${i})) $((${port443} + ${i})) $((${i} + 1))
done
fi
}