-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkind-setup.sh
executable file
·85 lines (76 loc) · 2.29 KB
/
kind-setup.sh
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
#!/bin/bash
version=${KIND_VERSION:-v1.19.7}
clusters=$(kind get clusters)
reg_name='registry.local'
reg_port='5000'
# desired cluster name; default is "kind"
KIND_CLUSTER_NAME="${KIND_CLUSTER_NAME:-kind}"
function start_registry() {
# create registry container unless it already exists
running=$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)
if [ "${running}" != 'true' ]; then
docker run \
-d --restart=always -p "${reg_port}:5000" --name "${reg_name}" \
registry:2
fi
}
function create_cluster() {
version=$1
reg_ip=$(docker inspect -f '{{.NetworkSettings.IPAddress}}' "${reg_name}")
# create a cluster with the local registry enabled in containerd
cat <<EOF | kind create cluster --name "${KIND_CLUSTER_NAME}" --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."${reg_name}:${reg_port}"]
endpoint = ["http://${reg_name}:${reg_port}"]
nodes:
- role: control-plane
image: kindest/node:${version}
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
extraPortMappings:
- containerPort: 80
hostPort: 80
protocol: TCP
- containerPort: 443
hostPort: 443
protocol: TCP
EOF
docker network connect "kind" "${reg_name}"
}
start_registry
if [[ "${clusters}" == *"${KIND_CLUSTER_NAME}"* ]]; then
if [ "$1" != "--force" ]; then
echo "Cluster already active: ${clusters}"
else
kind delete cluster
create_cluster ${version}
fi
else
create_cluster ${version}
fi
echo Setting up kubeconfig
mkdir -p ~/.kube
kind get kubeconfig --internal > ~/.kube/kind-config-internal
kind get kubeconfig > ~/.kube/kind
KUBECONFIG=~/.kube/kind:~/.kube/config kubectl config view --merge --flatten > .config.yaml
mv .config.yaml ~/.kube/config
# Document the local registry
# https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
name: local-registry-hosting
namespace: kube-public
data:
localRegistryHosting.v1: |
host: "localhost:${reg_port}"
help: "https://kind.sigs.k8s.io/docs/user/local-registry/"
EOF