forked from kanisterio/kanister
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_kubernetes.sh
executable file
·150 lines (138 loc) · 3.89 KB
/
local_kubernetes.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
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
#!/usr/bin/env bash
#
# Copyright 2019 The Kanister Authors.
# The wrapper for https://kubernetes-v1-4.github.io/docs/getting-started-guides/locally/#requirements
# also script checks if shippable cache is used for any thing
set -o errexit
set -o nounset
set -o xtrace
set -o pipefail
readonly BASE_DIR=$(dirname ${0})
export MINIKUBE_WANTUPDATENOTIFICATION=false
export MINIKUBE_WANTREPORTERRORPROMPT=false
export MINIKUBE_HOME=$HOME
export CHANGE_MINIKUBE_NONE_USER=true
export KUBECONFIG=$HOME/.kube/config
export KUBE_VERSION=${KUBE_VERSION:-"v1.21.1"}
export KIND_VERSION=${KIND_VERSION:-"v0.11.1"}
export LOCAL_CLUSTER_NAME=${LOCAL_CLUSTER_NAME:-"kanister"}
export LOCAL_PATH_PROV_VERSION="v0.0.11"
declare -a REQUIRED_BINS=( docker jq go )
if command -v apt-get
then
lin_repo_pre_cmd="apt-get install -y "
elif command -v apk
then
lin_repo_pre_cmd="apk add --update "
else
echo "apk or apt-get is supported at this moment"
exit 1
fi
check_or_get_dependencies() {
for dep in ${REQUIRED_BINS[@]}
do
if ! command -v ${dep}
then
echo "Missing ${dep}. Trying to install"
if ! err=$(${lin_repo_pre_cmd} ${dep} 2>&1)
then
echo "Insatlletion failed with $err"
exit 1
fi
fi
done
}
start_localkube() {
if ! command -v kind
then
get_localkube
fi
kind create cluster --name ${LOCAL_CLUSTER_NAME} --image=kindest/node:${KUBE_VERSION} -v 1
if [ -e ${KUBECONFIG} ]; then
cp -fr ${KUBECONFIG} ${HOME}/.kube/config_bk
fi
kind get kubeconfig --name="kanister" > "${HOME}/.kube/config"
wait_for_nodes
wait_for_pods
}
stop_localkube() {
if ! command -v kind
then
get_localkube
fi
kind delete cluster --name ${LOCAL_CLUSTER_NAME}
}
get_localkube() {
mkdir $HOME/.kube || true
touch $HOME/.kube/config
GO111MODULE="on" go get sigs.k8s.io/kind@${KIND_VERSION}
}
wait_for_nodes() {
local nodes_ready=$(kubectl get nodes 2>/dev/null | grep -c Ready)
local retries=20
while [[ ${nodes_ready} -eq 0 ]]
do
if [[ ${retries} -le 0 ]]
then
echo "Minikube nodes are not ready"
kubectl get nodes
minikube status
return 1
fi
sleep 5
if ! nodes_ready=$(kubectl get nodes 2>/dev/null | grep -c Ready)
then
nodes_ready=0
fi
retries=$((retries-1))
done
kubectl get nodes
}
wait_for_pods() {
local namespace=${1:-"kube-system"}
local pod_status=$(kubectl get pod --namespace=${namespace} -o json | jq -j '.items | .[] | .status | .containerStatuses | .[]? | .state.running != null or .state.terminated.reason == "Completed"')
local retries=20
while [[ ${pod_status} == *false* ]] || [[ ${pod_status} == '' ]]
do
if [[ ${retries} -le 0 ]]
then
echo "Error some objects are not ready"
kubectl get pod --namespace=${namespace}
return 1
fi
sleep 5
if ! pod_status=$(kubectl get pod --namespace=${namespace} -o json | jq -j '.items | .[] | .status | .containerStatuses | .[]? | .state.running != null or .state.terminated.reason == "Completed"')
then
pod_status=''
fi
retries=$((retries-1))
done
kubectl cluster-info
}
usage() {
cat <<EOM
Usage: ${0} <operation>
Where operation is one of the following:
get_localkube: installs kind
start_localkube : localkube start
stop_localkube : localkube stop
EOM
exit 1
}
[ ${#@} -gt 0 ] || usage
check_or_get_dependencies
case "${1}" in
# Alphabetically sorted
get_localkube)
time -p get_localkube
;;
start_localkube)
time -p start_localkube
;;
stop_localkube)
time -p stop_localkube
;;
*)
usage
exit 1
esac