You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

install.sh 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #!/bin/bash
  2. # Copyright 2021 The KubeEdge Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. set -o errexit
  16. set -o nounset
  17. set -o pipefail
  18. TMP_DIR=$(mktemp -d --suffix=.sedna)
  19. SEDNA_ROOT=${SEDNA_ROOT:-$TMP_DIR}
  20. trap "rm -rf '$TMP_DIR'" EXIT
  21. _download_yamls() {
  22. yaml_dir=$1
  23. mkdir -p ${SEDNA_ROOT}/$yaml_dir
  24. cd ${SEDNA_ROOT}/$yaml_dir
  25. for yaml in ${yaml_files[@]}; do
  26. # the yaml file already exists, no need to download
  27. [ -e "$yaml" ] && continue
  28. echo downloading $yaml into ${SEDNA_ROOT}/$yaml_dir
  29. local try_times=30 i=1 timeout=2
  30. while ! timeout ${timeout}s curl -sSO https://raw.githubusercontent.com/kubeedge/sedna/main/$yaml_dir/$yaml; do
  31. ((++i>try_times)) && {
  32. echo timeout to download $yaml
  33. exit 2
  34. }
  35. echo -en "retrying to download $yaml after $[i*timeout] seconds...\r"
  36. done
  37. done
  38. }
  39. download_yamls() {
  40. yaml_files=(
  41. sedna.io_datasets.yaml
  42. sedna.io_federatedlearningjobs.yaml
  43. sedna.io_incrementallearningjobs.yaml
  44. sedna.io_jointinferenceservices.yaml
  45. sedna.io_models.yaml
  46. )
  47. _download_yamls build/crds
  48. yaml_files=(
  49. gm.yaml
  50. )
  51. _download_yamls build/gm/rbac
  52. }
  53. prepare() {
  54. mkdir -p ${SEDNA_ROOT}
  55. # we only need build directory
  56. # here don't use git clone because of large vendor directory
  57. download_yamls
  58. }
  59. create_crds() {
  60. cd ${SEDNA_ROOT}
  61. kubectl create -f build/crds
  62. }
  63. delete_crds() {
  64. cd ${SEDNA_ROOT}
  65. kubectl delete -f build/crds --timeout=90s
  66. }
  67. prepare_gm_config_map() {
  68. cm_name=${1:-gm-config}
  69. config_file=${TMP_DIR}/${2:-gm.yaml}
  70. if [ -n "${SEDNA_GM_CONFIG:-}" ] && [ -f "${SEDNA_GM_CONFIG}" ] ; then
  71. cp "$SEDNA_GM_CONFIG" $config_file
  72. else
  73. cat > $config_file << EOF
  74. kubeConfig: ""
  75. master: ""
  76. namespace: ""
  77. websocket:
  78. address: 0.0.0.0
  79. port: 9000
  80. localController:
  81. server: http://localhost:${SEDNA_LC_BIND_PORT:-9100}
  82. EOF
  83. fi
  84. kubectl $action -n sedna configmap $cm_name --from-file=$config_file
  85. }
  86. create_gm() {
  87. cd ${SEDNA_ROOT}
  88. kubectl apply -f build/gm/rbac/
  89. kubectl label node/$GM_NODE_NAME sedna=gm --overwrite
  90. cm_name=gm-config
  91. config_file_name=gm.yaml
  92. prepare_gm_config_map $cm_name $config_file_name
  93. kubectl $action -f - <<EOF
  94. apiVersion: v1
  95. kind: Service
  96. metadata:
  97. name: gm
  98. namespace: sedna
  99. spec:
  100. selector:
  101. sedna: gm
  102. type: NodePort
  103. ports:
  104. - protocol: TCP
  105. port: 9000
  106. targetPort: 9000
  107. ---
  108. apiVersion: apps/v1
  109. kind: Deployment
  110. metadata:
  111. name: gm
  112. labels:
  113. sedna: gm
  114. namespace: sedna
  115. spec:
  116. replicas: 1
  117. selector:
  118. matchLabels:
  119. sedna: gm
  120. template:
  121. metadata:
  122. labels:
  123. sedna: gm
  124. spec:
  125. nodeSelector:
  126. sedna: gm
  127. serviceAccountName: sedna
  128. containers:
  129. - name: gm
  130. image: kubeedge/sedna-gm:v0.1.0
  131. command: ["sedna-gm", "--config", "/config/$config_file_name", "-v2"]
  132. volumeMounts:
  133. - name: gm-config
  134. mountPath: /config
  135. resources:
  136. requests:
  137. memory: 32Mi
  138. cpu: 100m
  139. limits:
  140. memory: 128Mi
  141. volumes:
  142. - name: gm-config
  143. configMap:
  144. name: $cm_name
  145. EOF
  146. }
  147. delete_gm() {
  148. cd ${SEDNA_ROOT}
  149. # sedna namespace would be deleted in here
  150. kubectl delete -f build/gm/rbac/
  151. kubectl label node/$GM_NODE_NAME sedna- | sed 's/labeled$/un&/' || true
  152. # no need to clean gm deployment alone
  153. }
  154. create_lc() {
  155. gm_node_port=$(kubectl -n sedna get svc gm -ojsonpath='{.spec.ports[0].nodePort}')
  156. # here try to get node ip by kubectl
  157. gm_node_ip=$(kubectl get node $GM_NODE_NAME -o jsonpath='{ .status.addresses[?(@.type=="ExternalIP")].address }')
  158. gm_node_internal_ip=$(kubectl get node $GM_NODE_NAME -o jsonpath='{ .status.addresses[?(@.type=="InternalIP")].address }')
  159. GM_ADDRESS=${gm_node_ip:-$gm_node_internal_ip}:$gm_node_port
  160. kubectl $action -f- <<EOF
  161. apiVersion: apps/v1
  162. kind: DaemonSet
  163. metadata:
  164. labels:
  165. sedna: lc
  166. name: lc
  167. namespace: sedna
  168. spec:
  169. selector:
  170. matchLabels:
  171. sedna: lc
  172. template:
  173. metadata:
  174. labels:
  175. sedna: lc
  176. spec:
  177. containers:
  178. - name: lc
  179. image: kubeedge/sedna-lc:v0.1.0
  180. env:
  181. - name: GM_ADDRESS
  182. value: $GM_ADDRESS
  183. - name: BIND_PORT
  184. value: "${LC_BIND_PORT:-9100}"
  185. - name: NODENAME
  186. valueFrom:
  187. fieldRef:
  188. fieldPath: spec.nodeName
  189. - name: ROOTFS_MOUNT_DIR
  190. # the value of ROOTFS_MOUNT_DIR is same with the mount path of volume
  191. value: /rootfs
  192. resources:
  193. requests:
  194. memory: 32Mi
  195. cpu: 100m
  196. limits:
  197. memory: 128Mi
  198. volumeMounts:
  199. - name: localcontroller
  200. mountPath: /rootfs
  201. volumes:
  202. - name: localcontroller
  203. hostPath:
  204. path: /
  205. restartPolicy: Always
  206. hostNetwork: true
  207. EOF
  208. }
  209. delete_lc() {
  210. # ns would be deleted in delete_gm
  211. # so no need to clean lc alone
  212. return
  213. }
  214. wait_ok() {
  215. kubectl -n sedna wait --for=condition=available --timeout=600s deployment/gm
  216. kubectl -n sedna wait pod --for=condition=Ready --selector=sedna
  217. kubectl -n sedna get pod
  218. }
  219. delete_pods() {
  220. # in case some nodes are not ready, here delete with a 60s timeout, otherwise force delete these
  221. kubectl -n sedna delete pod --all --timeout=60s || kubectl -n sedna delete pod --all --force --grace-period=0
  222. }
  223. check_kubectl () {
  224. kubectl get pod >/dev/null
  225. }
  226. check_action() {
  227. action=${SEDNA_ACTION:-create}
  228. support_action_list="create delete"
  229. if ! echo "$support_action_list" | grep -w -q "$action"; then
  230. echo "\`$action\` not in support action list: create/delete!" >&2
  231. echo "You need to specify it by setting $(red_text SEDNA_ACTION) environment variable when running this script!" >&2
  232. exit 2
  233. fi
  234. }
  235. check_gm_node() {
  236. GM_NODE_NAME=${SEDNA_GM_NODE:-}
  237. if [ -z "$GM_NODE_NAME" ] || ! kubectl get node $GM_NODE_NAME; then
  238. echo "ERROR: $(red_text GM node name \`$GM_NODE_NAME\` does not exist in k8s cluster)!" >&2
  239. echo "You need to specify it by setting $(red_text SEDNA_GM_NODE) environment variable when running this script!" >&2
  240. exit 1
  241. fi
  242. }
  243. do_check() {
  244. check_kubectl
  245. check_action
  246. check_gm_node
  247. }
  248. show_debug_infos() {
  249. cat - <<EOF
  250. Sedna is $(green_text running):
  251. See GM status: kubectl -n sedna get deploy
  252. See LC status: kubectl -n sedna get ds lc
  253. See Pod status: kubectl -n sedna get pod
  254. EOF
  255. }
  256. NO_COLOR='\033[0m'
  257. RED='\033[0;31m'
  258. GREEN='\033[0;32m'
  259. green_text() {
  260. echo -ne "$GREEN$@$NO_COLOR"
  261. }
  262. red_text() {
  263. echo -ne "$RED$@$NO_COLOR"
  264. }
  265. do_check
  266. prepare
  267. case "$action" in
  268. create)
  269. create_crds
  270. create_gm
  271. create_lc
  272. wait_ok
  273. show_debug_infos
  274. ;;
  275. delete)
  276. delete_pods
  277. delete_gm
  278. delete_lc
  279. delete_crds
  280. echo "$(green_text Sedna is uninstalled successfully)"
  281. ;;
  282. esac