forked from aws/aws-node-termination-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovision-cluster
executable file
·150 lines (131 loc) · 5.43 KB
/
provision-cluster
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
set -euo pipefail
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
PLATFORM=$(uname | tr '[:upper:]' '[:lower:]')
TEST_ID=$(uuidgen | cut -d'-' -f1 | tr '[:upper:]' '[:lower:]')
CLUSTER_NAME_BASE=$(uuidgen | cut -d'-' -f1 | tr '[:upper:]' '[:lower:]')
OVERRIDE_PATH=0
KIND_CONFIG_FILE=$SCRIPTPATH/kind-three-node-cluster.yaml
use_psp=false
# shellcheck disable=SC2034
K8_1_29="kindest/node:v1.29.2@sha256:51a1434a5397193442f0be2a297b488b6c919ce8a3931be0ce822606ea5ca245"
# shellcheck disable=SC2034
K8_1_28="kindest/node:v1.28.7@sha256:9bc6c451a289cf96ad0bbaf33d416901de6fd632415b076ab05f5fa7e4f65c58"
# shellcheck disable=SC2034
K8_1_27="kindest/node:v1.27.11@sha256:681253009e68069b8e01aad36a1e0fa8cf18bb0ab3e5c4069b2e65cafdd70843"
# shellcheck disable=SC2034
K8_1_26="kindest/node:v1.26.14@sha256:5d548739ddef37b9318c70cb977f57bf3e5015e4552be4e27e57280a8cbb8e4f"
# shellcheck disable=SC2034
K8_1_25="kindest/node:v1.25.16@sha256:e8b50f8e06b44bb65a93678a65a26248fae585b3d3c2a669e5ca6c90c69dc519"
# shellcheck disable=SC2034
K8_1_24="kindest/node:v1.24.17@sha256:bad10f9b98d54586cba05a7eaa1b61c6b90bfc4ee174fdc43a7b75ca75c95e51"
# shellcheck disable=SC2034
K8_1_23="kindest/node:v1.23.17@sha256:14d0a9a892b943866d7e6be119a06871291c517d279aedb816a4b4bc0ec0a5b3"
K8_VERSION="$K8_1_29"
KUBECTL_VERSION=$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)
KIND_VERSION="0.22.0"
HELM_VERSION="3.14.3"
echoerr() { echo "$@" 1>&2; }
USAGE=$(cat << 'EOM'
Usage: provision-cluster [-b <BASE_CLUSTER_NAME>] [-i <TEST_IDENTIFIER>] [-v K8s_VERSION] [-o]
Executes the spot termination integration test for the Node Termination Handler.
Outputs the cluster context directory to stdout on successful completion
Example: provision-cluster -b my-test -i 123 -v 1.29
Optional:
-b Base Name of cluster
-i Test Identifier to suffix Cluster Name and tmp dir
-v K8s version to use in this test
-k Kind cluster config file
-o Override path w/ your own kubectl and kind binaries
EOM
)
# Process our input arguments
while getopts "b:i:v:k:o" opt; do
case ${opt} in
b ) # BASE CLUSTER NAME
CLUSTER_NAME_BASE=$OPTARG
;;
i ) # Test ID
TEST_ID=$OPTARG
echoerr "👉 Test Run: $TEST_ID 👈"
;;
v ) # K8s version to provision
maj_ver=`echo $OPTARG | cut -d '.' -f 1`
min_ver=`echo $OPTARG | cut -d '.' -f 2`
if [[ $maj_ver -eq 1 && $min_ver -lt 25 ]]; then
use_psp=true
fi
OPTARG="K8_${maj_ver}_${min_ver}"
if [ ! -z ${OPTARG+x} ]; then
K8_VERSION=${!OPTARG}
else
echoerr "K8s version not supported"
exit 2
fi
;;
k ) # Kind cluster config file
KIND_CONFIG_FILE=$OPTARG
;;
o ) # Override path with your own kubectl and kind binaries
OVERRIDE_PATH=1
;;
\? )
echoerr "$USAGE" 1>&2
exit
;;
esac
done
CLUSTER_NAME="$CLUSTER_NAME_BASE"-"$TEST_ID"
TMP_DIR=$SCRIPTPATH/../../build/tmp-$CLUSTER_NAME
echoerr "🐳 Using Kubernetes $K8_VERSION"
mkdir -p $TMP_DIR
deps=("docker")
for dep in "${deps[@]}"; do
path_to_executable=$(which $dep)
if [ ! -x "$path_to_executable" ]; then
echoerr "You are required to have $dep installed on your system..."
echoerr "Please install $dep and try again. "
exit 3
fi
done
## Append to the end of PATH so that the user can override the executables if they want
if [[ OVERRIDE_PATH -eq 1 ]]; then
export PATH=$PATH:$TMP_DIR
else
if [ ! -x "$TMP_DIR/kubectl" ]; then
echoerr "🥑 Downloading the \"kubectl\" binary"
retry 5 curl -Lo $TMP_DIR/kubectl "https://storage.googleapis.com/kubernetes-release/release/$KUBECTL_VERSION/bin/$PLATFORM/amd64/kubectl"
chmod +x $TMP_DIR/kubectl
echoerr "👍 Downloaded the \"kubectl\" binary"
fi
if [ ! -x "$TMP_DIR/kind" ]; then
echoerr "🥑 Downloading the \"kind\" binary"
retry 5 curl -Lo $TMP_DIR/kind https://github.com/kubernetes-sigs/kind/releases/download/v$KIND_VERSION/kind-$PLATFORM-amd64
chmod +x $TMP_DIR/kind
echoerr "👍 Downloaded the \"kind\" binary"
fi
if [ ! -x "$TMP_DIR/helm" ]; then
echoerr "🥑 Downloading the \"helm\" binary"
retry 5 curl -L https://get.helm.sh/helm-v$HELM_VERSION-$PLATFORM-amd64.tar.gz | tar zxf - -C $TMP_DIR
mv $TMP_DIR/$PLATFORM-amd64/helm $TMP_DIR/.
chmod +x $TMP_DIR/helm
echoerr "👍 Downloaded the \"helm\" binary"
fi
export PATH=$TMP_DIR:$PATH
fi
# Disable spinners and color in kind output
export TERM=dumb
echoerr "🥑 Creating k8s cluster using \"kind\""
if [[ "$use_psp" = false ]]; then
no_psp_kind_config_file="${TMP_DIR}/`basename $KIND_CONFIG_FILE`"
cat $KIND_CONFIG_FILE | sed 's/,PodSecurityPolicy//' > "${no_psp_kind_config_file}"
KIND_CONFIG_FILE="${no_psp_kind_config_file}"
fi
retry 3 kind create cluster --name "$CLUSTER_NAME" --image $K8_VERSION --config "$KIND_CONFIG_FILE" --kubeconfig $TMP_DIR/kubeconfig 1>&2
echo "$CLUSTER_NAME" > "$TMP_DIR/clustername"
echoerr "👍 Created k8s cluster using \"kind\""
if [[ "$use_psp" = true ]]; then
kubectl apply -f "$SCRIPTPATH/psp-default.yaml" --context "kind-$CLUSTER_NAME" --kubeconfig "$TMP_DIR/kubeconfig" 1>&2
kubectl apply -f "$SCRIPTPATH/psp-privileged.yaml" --context "kind-$CLUSTER_NAME" --kubeconfig "$TMP_DIR/kubeconfig" 1>&2
fi
echo "$TMP_DIR"