forked from openyurtio/openyurt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
local_up_openyurt.sh
executable file
·303 lines (253 loc) · 10.1 KB
/
local_up_openyurt.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env bash
# Copyright 2020 The OpenYurt Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This shell will create a openyurt cluster locally with kind. The yurt-tunnel will be
# automatically deployed, and the autonomous mode will be active.
#
# It uses the following env variables:
# REGION
# REGION affects the GOPROXY to use. You can set it to "cn" to use GOPROXY="https://goproxy.cn".
# Default value is "us", which means using GOPROXY="https://goproxy.io".
#
# KIND_KUBECONFIG
# KIND_KUBECONFIG represents the path to store the kubeconfig file of the cluster
# which is created by this shell. The default value is "$HOME/.kube/config".
#
# NODES_NUM
# NODES_NUM represents the number of nodes to set up in the new-created cluster.
# There is one control-plane node and NODES_NUM-1 worker nodes. Thus, NODES_NUM must
# not be less than 2. The default value is 2.
#
# KUBERNETESVERSION
# KUBERNETESVERSION declares the kubernetes version the cluster will use. The format is "1.XX".
# Now only 1.18, 1.19 and 1.20 are supported. The default value is 1.20.
#
# TIMEOUT
# TIMEOUT represents the time to wait for the kind control-plane, yurt-tunnel-server and
# yurt-tunnel-agent to be ready. If they are not ready after the duration, the shell will exit.
# The default value is 120s.
#
# YURTTUNNEL
# If set YURTTUNNEL=disable, the yurt-tunnel-agent and yurt-tunnel-server will not be
# deployed in the openyurt cluster. The default value is "enable".
set -x
set -e
set -u
YURT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
KIND_KUBECONFIG=${KIND_KUBECONFIG:-${HOME}/.kube/config}
readonly KIND_NODE_IMAGES=(
kindest/node:v1.18.19@sha256:7af1492e19b3192a79f606e43c35fb741e520d195f96399284515f077b3b622c
kindest/node:v1.19.11@sha256:07db187ae84b4b7de440a73886f008cf903fcf5764ba8106a9fd5243d6f32729
kindest/node:v1.20.7@sha256:cbeaf907fc78ac97ce7b625e4bf0de16e3ea725daf6b04f930bd14c67c671ff9
)
readonly REQUIRED_CMD=(
go
docker
kubectl
kind
)
readonly BUILD_TARGETS=(
yurthub
yurt-controller-manager
yurtctl
yurt-tunnel-server
yurt-tunnel-agent
yurt-node-servant
)
readonly LOCAL_ARCH=$(go env GOHOSTARCH)
readonly LOCAL_OS=$(go env GOHOSTOS)
readonly IMAGES_DIR=${YURT_ROOT}/_output/images
readonly CLUSTER_NAME="openyurt-e2e-test"
readonly TIMEOUT=${TIMEOUT:-"120s"}
readonly KUBERNETESVERSION=${KUBERNETESVERSION:-"1.20"}
readonly KIND_CONFIG=${YURT_ROOT}/_output/kind-config-v${KUBERNETESVERSION}.yaml
readonly NODES_NUM=${NODES_NUM:-2}
readonly KIND_CONTEXT="kind-${CLUSTER_NAME}"
master=
edgenodes=
# $1 string to escape
function escape_slash {
echo $1 | sed "s/\//\\\\\//g"
}
function gen_kind_config {
# check if number of nodes is valid
if [[ ${NODES_NUM} -lt 2 ]]; then
echo "NODES_NUM should be greater than 2"
exit -1
fi
# check if kubernetes version is valid
local k8s_version=($(echo ${KUBERNETESVERSION} | sed "s/\./ /g"))
local major=${k8s_version[0]}
local minor=${k8s_version[1]}
if [[ ${major} -ne 1 ]] || [[ ${minor} -gt 20 ]] || [[ ${minor} -lt 18 ]]; then
echo "Invalid KUBERNETESVERSION, it should be between 1.18 and 1.20."
exit -1
fi
# create and init kind config file
local gen_config_path=${KIND_CONFIG}
cat ${YURT_ROOT}/hack/kind-config-template.yaml > ${gen_config_path}
# add additional node spec into kind config
for ((count=2; count<${NODES_NUM}; count++)); do
echo -e "\n - role: worker\n image: |fill image here|" >> ${gen_config_path}
done
# fill name:tag of images and fill bin dir
local bindir=$(escape_slash ${YURT_LOCAL_BIN_DIR}/${LOCAL_OS}/${LOCAL_ARCH})
local node_image=$(escape_slash $(echo ${KIND_NODE_IMAGES[${minor}-18]}))
# there are some differences between UNIX and Linux when executing the SED command.
# just add a "" blank character after the - i instruction
if [ $LOCAL_OS == "darwin" ]; then
bindir=$(escape_slash ${YURT_LOCAL_BIN_DIR}/"linux"/${LOCAL_ARCH})
sed -i "" "s/image: |fill image here|$/image: ${node_image}/g
s/- hostPath: |fill local bin dir|/- hostPath: ${bindir}/g" \
${gen_config_path}
else
sed -i "s/image: |fill image here|$/image: ${node_image}/g
s/- hostPath: |fill local bin dir|/- hostPath: ${bindir}/g" \
${gen_config_path}
fi
}
function install_kind {
echo "Begin to install kind"
GO111MODULE="on" go get sigs.k8s.io/[email protected]
}
function install_docker {
echo "docker should be installed first"
return -1
}
function install_kubectl {
echo "kubectl should be installed first"
return -1
}
function install_go {
echo "go should be installed first"
return -1
}
function preflight {
echo "Preflight Check..."
for bin in "${REQUIRED_CMD[@]}"; do
command -v ${bin} > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "Cannot find command ${bin}."
install_${bin}
if [[ $? -ne 0 ]]; then
echo "Error occurred, exit"
exit -1
fi
fi
done
}
function build_target_binaries_and_images {
echo "Begin to build binaries and images"
export WHAT=${BUILD_TARGETS[@]}
export ARCH=${LOCAL_ARCH}
source ${YURT_ROOT}/hack/make-rules/release-images.sh
}
function kind_load_images {
local postfix="${LOCAL_OS}-${LOCAL_ARCH}.tar"
if [[ ${LOCAL_OS} == "darwin" ]]; then
postfix="linux-${LOCAL_ARCH}.tar"
fi
for bin in ${BUILD_TARGETS[@]}; do
local imagename="${bin}-${postfix}"
if [[ "${bin}" = "yurtctl" ]]; then
imagename="yurtctl-servant-${postfix}"
fi
if [[ "${bin}" = "yurt-node-servant" ]]; then
imagename="node-servant-${postfix}"
fi
echo "loading image ${imagename} to nodes"
local nodesarg=$(echo ${master} ${edgenodes[@]} | sed "s/ /,/g")
kind load image-archive ${IMAGES_DIR}/${imagename} \
--name ${CLUSTER_NAME} --nodes ${nodesarg}
done
}
function local_up_cluster {
echo "Creating kubernetes cluster with ${NODES_NUM} nodes"
gen_kind_config
# output kind config before kind cmd for the convenience of debugging
echo $(cat ${KIND_CONFIG})
kind create cluster --config ${KIND_CONFIG}
echo "Waiting for the control-plane ready..."
kubectl wait --for=condition=Ready node/${CLUSTER_NAME}-control-plane --context ${KIND_CONTEXT} --timeout=${TIMEOUT}
master=$(kubectl get node -A -o custom-columns=NAME:.metadata.name --context ${KIND_CONTEXT} | grep control-plane)
edgenodes=$(kubectl get node -A -o custom-columns=NAME:.metadata.name --context ${KIND_CONTEXT} | grep work)
}
# $1 yurt-tunnel-agent image with the format of "name:tag"
function deploy_yurt_tunnel_agent {
local yurt_tunnel_agent_yaml=${YURT_OUTPUT_DIR}/yurt-tunnel-agent.yaml
# revise the yurt-tunnel-agent.yaml
cat ${YURT_ROOT}/config/setup/yurt-tunnel-agent.yaml |
sed "s/image: openyurt\/yurt-tunnel-agent:latest/image: $(escape_slash ${1})/" |
tee ${yurt_tunnel_agent_yaml}
kubectl apply -f ${yurt_tunnel_agent_yaml} --context ${KIND_CONTEXT}
}
# $1 yurt-tunnel-server image with the format of "name:tag"
function deploy_yurt_tunnel_server {
local yurt_tunnel_server_yaml=${YURT_OUTPUT_DIR}/yurt-tunnel-server.yaml
# revise the yurt-tunnel-server.yaml
cat ${YURT_ROOT}/config/setup/yurt-tunnel-server.yaml |
sed "s/image: openyurt\/yurt-tunnel-server:latest/image: $(escape_slash ${1})/;
s/imagePullPolicy: Always/imagePullPolicy: IfNotPresent/" |
tee ${yurt_tunnel_server_yaml}
kubectl apply -f ${yurt_tunnel_server_yaml} --context ${KIND_CONTEXT}
}
function deploy_yurt_tunnel {
deploy_yurt_tunnel_agent $(get_image_name "yurt-tunnel-agent" ${LOCAL_ARCH})
deploy_yurt_tunnel_server $(get_image_name "yurt-tunnel-server" ${LOCAL_ARCH})
}
function convert_to_openyurt {
echo "converting to openyurt cluster "
local yurtctl_dir="/root/openyurt"
# local yurtctl_dir=${YURT_LOCAL_BIN_DIR}/${LOCAL_OS}/${LOCAL_ARCH}
docker exec ${master} \
${yurtctl_dir}/yurtctl convert --provider kubeadm --cloud-nodes ${master} \
--yurthub-image=$(get_image_name "yurthub" ${LOCAL_ARCH}) \
--yurt-controller-manager-image=$(get_image_name "yurt-controller-manager" ${LOCAL_ARCH}) \
--node-servant-image=$(get_image_name "node-servant" ${LOCAL_ARCH})
# --yurt-tunnel-server-image=$(get_image_name "yurt-tunnel-server" ${LOCAL_ARCH}) \
# --yurt-tunnel-agent-image=$(get_image_name "yurt-tunnel-agent" ${LOCAL_ARCH})
local nodearg=$(echo ${edgenodes[@]} | sed "s/ /,/g")
docker exec ${master} \
${yurtctl_dir}/yurtctl markautonomous -a "${nodearg}"
if [ ! "${YURTTUNNEL:-"enable"}" = "disable" ]; then
deploy_yurt_tunnel
kubectl rollout status deploy yurt-tunnel-server -n kube-system \
--timeout=${TIMEOUT} --context ${KIND_CONTEXT}
kubectl rollout status daemonset yurt-tunnel-agent -n kube-system \
--timeout=${TIMEOUT} --context ${KIND_CONTEXT}
fi
}
function get_kubeconfig {
mkdir -p ${HOME}/.kube
kind get kubeconfig --name ${CLUSTER_NAME} > ${KIND_KUBECONFIG}
}
function cleanup {
rm -rf ${YURT_ROOT}/_output
rm -rf ${YURT_ROOT}/dockerbuild
rm -f ${KIND_CONFIG}
kind delete clusters ${CLUSTER_NAME}
}
function cleanup_on_err {
if [[ $? -ne 0 ]]; then
cleanup
fi
}
trap cleanup_on_err EXIT
cleanup
preflight
build_target_binaries_and_images
local_up_cluster
kind_load_images
convert_to_openyurt
get_kubeconfig