summaryrefslogtreecommitdiff
path: root/hack/local-up-kubeedge.sh
blob: 8ffe0c9f9c8860b1544557f98e1570c561db15b5 (about) (plain)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env bash

# Copyright 2019 The KubeEdge 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.

KUBEEDGE_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/..
ENABLE_DAEMON=${ENABLE_DAEMON:-false}
LOG_DIR=${LOG_DIR:-"/tmp"}
LOG_LEVEL=${LOG_LEVEL:-2}
TIMEOUT=${TIMEOUT:-60}s
PROTOCOL=${PROTOCOL:-"WebSocket"}
CONTAINER_RUNTIME=${CONTAINER_RUNTIME:-"containerd"}
KIND_IMAGE=${1:-"kindest/node:v1.27.0"}
echo -e "The installation of the cni plugin will overwrite the cni config file. Use export CNI_CONF_OVERWRITE=false to disable it."

if [[ "${CLUSTER_NAME}x" == "x" ]]; then
  CLUSTER_NAME="test"
fi

export CLUSTER_CONTEXT="--name ${CLUSTER_NAME}"

function install_cr() {
  if [[ "${CONTAINER_RUNTIME}" = "docker" ]]; then
    install_docker
  elif [[ "${CONTAINER_RUNTIME}" = "cri-o" ]]; then
    install_crio
  elif [[ "${CONTAINER_RUNTIME}" = "isulad" ]]; then
    install_isulad
  fi
}

function check_prerequisites {
  kubeedge::golang::verify_golang_version
  check_kubectl
  check_kind
  if [[ "${CONTAINER_RUNTIME}" = "docker" ]]; then
    # if we will use docker as edgecore container runtime, we need to verify whether docker already installed
    verify_docker_installed
    verify_cridockerd_installed
  elif [[ "${CONTAINER_RUNTIME}" = "cri-o" ]]; then
    # function to verify if cri-o is installed
    verify_crio_installed
  elif [[ "${CONTAINER_RUNTIME}" = "isulad" ]]; then
    # function to verify if isulad is installed
    verify_isulad_installed
  elif [[ "${CONTAINER_RUNTIME}" = "containerd" ]]; then
    # we will use containerd as cri runtime, so need to verify whether containerd already installed
    verify_containerd_installed
  else
    echo "not supported container runtime ${CONTAINER_RUNTIME}"
    exit 1
  fi
}

# spin up cluster with kind command
function kind_up_cluster {
  echo "Running kind: [kind create cluster ${CLUSTER_CONTEXT}]"
  kind create cluster ${CLUSTER_CONTEXT} --image ${KIND_IMAGE}
}

function uninstall_kubeedge {
  # kill the cloudcore
  [[ -n "${CLOUDCORE_PID-}" ]] && sudo kill "${CLOUDCORE_PID}" 2>/dev/null

  # kill the edgecore
  [[ -n "${EDGECORE_PID-}" ]] && sudo kill "${EDGECORE_PID}" 2>/dev/null

  # delete data
  sudo rm -rf /tmp/etc/kubeedge /tmp/var/lib/kubeedge

}

# clean up
function cleanup {
  echo "Cleaning up..."
  uninstall_kubeedge

  echo "Running kind: [kind delete cluster ${CLUSTER_CONTEXT}]"
  kind delete cluster ${CLUSTER_CONTEXT}

}

if [[ "${ENABLE_DAEMON}" = false ]]; then
  trap cleanup EXIT
else
  trap cleanup ERR
  trap cleanup INT
fi

function create_device_crd {
  echo "creating the device crd..."
  kubectl apply -f ${KUBEEDGE_ROOT}/build/crds/devices/devices_v1beta1_device.yaml
  kubectl apply -f ${KUBEEDGE_ROOT}/build/crds/devices/devices_v1beta1_devicemodel.yaml
}

function create_objectsync_crd {
  echo "creating the objectsync crd..."
  kubectl apply -f ${KUBEEDGE_ROOT}/build/crds/reliablesyncs/cluster_objectsync_v1alpha1.yaml
  kubectl apply -f ${KUBEEDGE_ROOT}/build/crds/reliablesyncs/objectsync_v1alpha1.yaml
}

function create_rule_crd {
  echo "creating the rule crd..."
  kubectl apply -f ${KUBEEDGE_ROOT}/build/crds/router/router_v1_rule.yaml
  kubectl apply -f ${KUBEEDGE_ROOT}/build/crds/router/router_v1_ruleEndpoint.yaml
}

function create_operation_crd {
  echo "creating the operation crd..."
  kubectl apply -f ${KUBEEDGE_ROOT}/build/crds/operations/operations_v1alpha1_nodeupgradejob.yaml
  kubectl apply -f ${KUBEEDGE_ROOT}/build/crds/operations/operations_v1alpha1_imageprepulljob.yaml
}

function create_serviceaccountaccess_crd {
  echo "creating the saaccess crd..."
  kubectl apply -f ${KUBEEDGE_ROOT}/build/crds/policy/policy_v1alpha1_serviceaccountaccess.yaml
}

function build_cloudcore {
  echo "building the cloudcore..."
  make -C "${KUBEEDGE_ROOT}" WHAT="cloudcore"
}

function build_edgecore {
  echo "building the edgecore..."
  make -C "${KUBEEDGE_ROOT}" WHAT="edgecore"
}

function start_cloudcore {
  CLOUD_CONFIGFILE=${KUBEEDGE_ROOT}/_output/local/bin/cloudcore.yaml
  CLOUD_BIN=${KUBEEDGE_ROOT}/_output/local/bin/cloudcore
  ${CLOUD_BIN} --defaultconfig >${CLOUD_CONFIGFILE}
  sed -i '/cloudStream:/{n;s/false/true/;}' ${CLOUD_CONFIGFILE}
  if [[ "${PROTOCOL}" = "QUIC" ]]; then
    sed -i '/quic:/{n;N;s/false/true/;}' ${CLOUD_CONFIGFILE}
  fi

  # enable dynamic controller
  sed -i '/dynamicController:/{n;s/false/true/;}' ${CLOUD_CONFIGFILE}

  sed -i -e "s|kubeConfig: .*|kubeConfig: ${KUBECONFIG}|g" \
    -e "s|/var/lib/kubeedge/|/tmp&|g" \
    -e "s|tlsCAFile: .*|tlsCAFile: /etc/kubeedge/ca/cloudhub/rootCA.crt|g" \
    -e "s|tlsCAKeyFile: .*|tlsCAKeyFile: /etc/kubeedge/ca/cloudhub/rootCA.key|g" \
    -e "s|tlsCertFile: .*|tlsCertFile: /etc/kubeedge/certs/cloudhub/server.crt|g" \
    -e "s|tlsPrivateKeyFile: .*|tlsPrivateKeyFile: /etc/kubeedge/certs/cloudhub/server.key|g" \
    -e "s|/etc/|/tmp/etc/|g" \
    -e '/router:/{n;N;N;N;N;s/false/true/}' ${CLOUD_CONFIGFILE}
  CLOUDCORE_LOG=${LOG_DIR}/cloudcore.log
  echo "start cloudcore..."
  nohup sudo ${CLOUD_BIN} --config=${CLOUD_CONFIGFILE} --v=${LOG_LEVEL} >"${CLOUDCORE_LOG}" 2>&1 &
  CLOUDCORE_PID=$!

  # ensure tokensecret is generated
  while true; do
    sleep 3
    kubectl get secret -nkubeedge | grep -q tokensecret && break
  done
}

function start_edgecore {
  EDGE_CONFIGFILE=${KUBEEDGE_ROOT}/_output/local/bin/edgecore.yaml
  EDGE_BIN=${KUBEEDGE_ROOT}/_output/local/bin/edgecore
  ${EDGE_BIN} --defaultconfig >${EDGE_CONFIGFILE}

  sed -i '/edgeStream:/{n;s/false/true/;}' ${EDGE_CONFIGFILE}
  sed -i '/metaServer:/{n;s/false/true/;}' ${EDGE_CONFIGFILE}

  if [[ "${PROTOCOL}" = "QUIC" ]]; then
    sed -i '/quic:/{n;s/false/true/;}' ${EDGE_CONFIGFILE}
    sed -i '/websocket:/{n;s/true/false/;}' ${EDGE_CONFIGFILE}
  fi

  # if we will use docker as edgecore container runtime
  # we need to change edgecore container runtime from default containerd to docker
  if [[ "${CONTAINER_RUNTIME}" = "docker" ]]; then
    sed -i 's|imageServiceEndpoint: .*|imageServiceEndpoint: unix:///var/run/cri-dockerd.sock|' ${EDGE_CONFIGFILE}
    sed -i 's|containerRuntimeEndpoint: .*|containerRuntimeEndpoint: unix:///var/run/cri-dockerd.sock|' ${EDGE_CONFIGFILE}
  fi

  if [[ "${CONTAINER_RUNTIME}" = "cri-o" ]]; then
    sed -i 's|imageServiceEndpoint: .*|imageServiceEndpoint: unix:///var/run/crio/crio.sock|' ${EDGE_CONFIGFILE}
    sed -i 's|containerRuntimeEndpoint: .*|containerRuntimeEndpoint: unix:///var/run/crio/crio.sock|' ${EDGE_CONFIGFILE}
    sed -i 's|cgroupDriver: .*|cgroupDriver: systemd|' ${EDGE_CONFIGFILE}
  fi

  if [[ "${CONTAINER_RUNTIME}" = "isulad" ]]; then
    sed -i 's|imageServiceEndpoint: .*|imageServiceEndpoint: unix:///var/run/isulad.sock|' ${EDGE_CONFIGFILE}
    sed -i 's|containerRuntimeEndpoint: .*|containerRuntimeEndpoint: unix:///var/run/isulad.sock|' ${EDGE_CONFIGFILE}
  fi

  token=$(kubectl get secret -nkubeedge tokensecret -o=jsonpath='{.data.tokendata}' | base64 -d)

  sed -i -e "s|token: .*|token: ${token}|g" \
    -e "s|hostnameOverride: .*|hostnameOverride: edge-node|g" \
    -e "s|/etc/|/tmp/etc/|g" \
    -e "s|/var/lib/kubeedge/|/tmp&|g" \
    -e "s|mqttMode: .*|mqttMode: 0|g" \
    -e '/serviceBus:/{n;s/false/true/;}' ${EDGE_CONFIGFILE}

  sed -i -e "s|/tmp/etc/resolv|/etc/resolv|g" ${EDGE_CONFIGFILE}
  EDGECORE_LOG=${LOG_DIR}/edgecore.log

  echo "start edgecore..."
  export CHECK_EDGECORE_ENVIRONMENT="false"
  nohup sudo -E ${EDGE_BIN} --config=${EDGE_CONFIGFILE} --v=${LOG_LEVEL} >"${EDGECORE_LOG}" 2>&1 &
  EDGECORE_PID=$!
}

function check_control_plane_ready {
  echo "wait the control-plane ready..."
  kubectl wait --for=condition=Ready node/${CLUSTER_NAME}-control-plane --timeout=${TIMEOUT}
}

# Check if all processes are still running. Prints a warning once each time
# a process dies unexpectedly.
function healthcheck {
  if [[ -n "${CLOUDCORE_PID-}" ]] && ! sudo kill -0 "${CLOUDCORE_PID}" 2>/dev/null; then
    echo "CloudCore terminated unexpectedly, see ${CLOUDCORE_LOG}"
    CLOUDCORE_PID=
  fi
  if [[ -n "${EDGECORE_PID-}" ]] && ! sudo kill -0 "${EDGECORE_PID}" 2>/dev/null; then
    echo "EdgeCore terminated unexpectedly, see ${EDGECORE_LOG}"
    EDGECORE_PID=
  fi
}

function generate_streamserver_cert {
  CA_PATH=${CA_PATH:-/tmp/etc/kubeedge/ca}
  CERT_PATH=${CERT_PATH:-/tmp/etc/kubeedge/certs}
  STREAM_KEY_FILE=${CERT_PATH}/stream.key
  STREAM_CSR_FILE=${CERT_PATH}/stream.csr
  STREAM_CRT_FILE=${CERT_PATH}/stream.crt
  K8SCA_FILE=/tmp/etc/kubernetes/pki/ca.crt
  K8SCA_KEY_FILE=/tmp/etc/kubernetes/pki/ca.key
  streamsubject=${SUBJECT:-/C=CN/ST=Zhejiang/L=Hangzhou/O=KubeEdge}

  if [[ ! -d /tmp/etc/kubernetes/pki ]]; then
    mkdir -p /tmp/etc/kubernetes/pki
  fi
  if [[ ! -d $CA_PATH ]]; then
    mkdir -p $CA_PATH
  fi
  if [[ ! -d $CERT_PATH ]]; then
    mkdir -p $CERT_PATH
  fi

  docker cp ${CLUSTER_NAME}-control-plane:/etc/kubernetes/pki/ca.crt $K8SCA_FILE
  docker cp ${CLUSTER_NAME}-control-plane:/etc/kubernetes/pki/ca.key $K8SCA_KEY_FILE
  cp /tmp/etc/kubernetes/pki/ca.crt /tmp/etc/kubeedge/ca/streamCA.crt

  SUBJECTALTNAME="subjectAltName = IP.1:127.0.0.1"
  echo $SUBJECTALTNAME >/tmp/server-extfile.cnf

  touch ~/.rnd

  openssl genrsa -out ${STREAM_KEY_FILE} 2048
  openssl req -new -key ${STREAM_KEY_FILE} -subj ${streamsubject} -out ${STREAM_CSR_FILE}
  openssl x509 -req -in ${STREAM_CSR_FILE} -CA ${K8SCA_FILE} -CAkey ${K8SCA_KEY_FILE} -CAcreateserial -out ${STREAM_CRT_FILE} -days 5000 -sha256 -extfile /tmp/server-extfile.cnf
}

cleanup

source "${KUBEEDGE_ROOT}/hack/lib/golang.sh"
source "${KUBEEDGE_ROOT}/hack/lib/install.sh"

install_cr

check_prerequisites

# Stop right away if there's an error
set -eE

build_cloudcore
build_edgecore

kind_up_cluster

export KUBECONFIG=$HOME/.kube/config

check_control_plane_ready

# edge side don't support kind cni now, delete kind cni plugin for workaround
kubectl delete daemonset kindnet -nkube-system
kubectl create ns kubeedge

create_device_crd
create_objectsync_crd
create_rule_crd
create_operation_crd
create_serviceaccountaccess_crd

generate_streamserver_cert

start_cloudcore

# install CNI plugins
if [[ "${CONTAINER_RUNTIME}" = "containerd" || "${CONTAINER_RUNTIME}" = "docker" || "${CONTAINER_RUNTIME}" = "isulad" ]]; then
  # we need to install CNI plugins only when we use remote(containerd) as edgecore container runtime
  install_cni_plugins
  if [[ "${CONTAINER_RUNTIME}" = "docker" ]]; then
    sudo systemctl restart docker
    sudo systemctl restart cri-docker
    sleep 2
  elif [[ "${CONTAINER_RUNTIME}" = "containerd" ]]; then
    sudo systemctl restart containerd
    sleep 2
  elif [[ "${CONTAINER_RUNTIME}" = "isulad" ]]; then
    sudo systemctl restart isulad
    sleep 2
  fi
fi

sleep 2

start_edgecore

if [[ "${ENABLE_DAEMON}" = false ]]; then
  echo "Local KubeEdge cluster is running. Press Ctrl-C to shut it down."
else
  echo "Local KubeEdge cluster is running. Use \"kill $BASHPID\" to shut it down."
fi

echo "Logs:
  /tmp/cloudcore.log
  /tmp/edgecore.log

To start using your kubeedge, you can run:

  export PATH=$PATH:$GOPATH/bin
  export KUBECONFIG=$HOME/.kube/config
  kubectl get nodes
"

if [[ "${ENABLE_DAEMON}" = false ]]; then
  while true; do
    sleep 1
    healthcheck
  done
else
  while true; do
    sleep 3
    kubectl get nodes | grep edge-node | grep -q -w Ready && break
  done
  kubectl label node edge-node disktype=test
fi