forked from chaos-mesh/chaos-mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·2701 lines (2617 loc) · 78.4 KB
/
install.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# Copyright 2021 Chaos Mesh 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 is a script to quickly install chaos-mesh.
# This script will check if docker and kubernetes are installed. If local mode is set and kubernetes is not installed,
# it will use kind or minikube to install the kubernetes cluster according to the configuration.
# Finally, when all dependencies are installed, chaos-mesh will be installed.
VERSION=${VERSION:-latest}
usage() {
cat << EOF
This script is used to install chaos-mesh.
Before running this script, please ensure that:
* have installed docker if you run chaos-mesh in local.
* have installed Kubernetes if you run chaos-mesh in normal Kubernetes cluster
USAGE:
install.sh [FLAGS] [OPTIONS]
FLAGS:
-h, --help Prints help information
-d, --dependency-only Install dependencies only, including kind, kubectl, local-kube.
--force Force reinstall all components if they are already installed, include: kind, local-kube, chaos-mesh
--force-chaos-mesh Force reinstall chaos-mesh if it is already installed
--force-local-kube Force reinstall local Kubernetes cluster if it is already installed
--force-kubectl Force reinstall kubectl client if it is already installed
--force-kind Force reinstall Kind if it is already installed
--volume-provisioner Deploy volume provisioner in local Kubernetes cluster
--local-registry Deploy local docker registry in local Kubernetes cluster
--template Locally render templates
--k3s Install chaos-mesh in k3s environment
--microk8s Install chaos-mesh in microk8s environment
--host-network Install chaos-mesh using hostNetwork
OPTIONS:
-v, --version Version of chaos-mesh, default value: ${VERSION}
-l, --local [kind] Choose a way to run a local kubernetes cluster, supported value: kind,
If this value is not set and the Kubernetes is not installed, this script will exit with 1.
-n, --name Name of Kubernetes cluster, default value: kind
-c --crd The path of the crd files. Get the crd file from "https://mirrors.chaos-mesh.org" if the crd path is empty.
-r --runtime Runtime specifies which container runtime to use. Currently we only supports docker and containerd. default value: docker
--kind-version Version of the Kind tool, default value: v0.11.1
--node-num The count of the cluster nodes,default value: 3
--k8s-version Version of the Kubernetes cluster,default value: v1.17.2
--volume-num The volumes number of each kubernetes node,default value: 5
--release-name Release name of chaos-mesh, default value: chaos-mesh
--namespace Namespace of chaos-mesh, default value: chaos-mesh
--timezone Specifies timezone to be used by chaos-dashboard, chaos-daemon and controller.
EOF
}
main() {
local local_kube=""
local cm_version="${VERSION}"
local kind_name="kind"
local kind_version="v0.11.1"
local node_num=3
local k8s_version="v1.17.2"
local volume_num=5
local release_name="chaos-mesh"
local namespace="chaos-mesh"
local timezone="UTC"
local force_chaos_mesh=false
local force_local_kube=false
local force_kubectl=false
local force_kind=false
local volume_provisioner=false
local local_registry=false
local crd=""
local runtime="docker"
local template=false
local install_dependency_only=false
local k3s=false
local microk8s=false
local host_network=false
local docker_registry="ghcr.io"
while [[ $# -gt 0 ]]
do
key="$1"
case "$key" in
-h|--help)
usage
exit 0
;;
-l|--local)
local_kube="$2"
shift
shift
;;
-v|--version)
cm_version="$2"
shift
shift
;;
-n|--name)
kind_name="$2"
shift
shift
;;
-c|--crd)
crd="$2"
shift
shift
;;
-r|--runtime)
runtime="$2"
shift
shift
;;
-d|--dependency-only)
install_dependency_only=true
shift
;;
--force)
force_chaos_mesh=true
force_local_kube=true
force_kubectl=true
force_kind=true
shift
;;
--force-local-kube)
force_local_kube=true
shift
;;
--force-kubectl)
force_kubectl=true
shift
;;
--force-kind)
force_kind=true
shift
;;
--force-chaos-mesh)
force_chaos_mesh=true
shift
;;
--template)
template=true
shift
;;
--volume-provisioner)
volume_provisioner=true
shift
;;
--local-registry)
local_registry=true
shift
;;
--kind-version)
kind_version="$2"
shift
shift
;;
--node-num)
node_num="$2"
shift
shift
;;
--k8s-version)
k8s_version="$2"
shift
shift
;;
--volume-num)
volume_num="$2"
shift
shift
;;
--release-name)
release_name="$2"
shift
shift
;;
--namespace)
namespace="$2"
shift
shift
;;
--k3s)
k3s=true
shift
;;
--microk8s)
microk8s=true
shift
;;
--host-network)
host_network=true
shift
;;
--timezone)
timezone="$2"
shift
shift
;;
--docker-registry)
docker_registry="$2"
shift
shift
;;
*)
echo "unknown flag or option $key"
usage
exit 1
;;
esac
done
if [ "${runtime}" != "docker" ] && [ "${runtime}" != "containerd" ]; then
printf "container runtime %s is not supported\n" "${runtime}"
exit 1
fi
if [ "${local_kube}" != "" ] && [ "${local_kube}" != "kind" ]; then
printf "local Kubernetes by %s is not supported\n" "${local_kube}"
exit 1
fi
if [ "${local_kube}" == "kind" ]; then
runtime="containerd"
fi
if [ "${k3s}" == "true" ]; then
runtime="containerd"
fi
if [ "${microk8s}" == "true" ]; then
runtime="containerd"
fi
if [ "${crd}" == "" ]; then
if kubectl api-versions | grep -q -w apiextensions.k8s.io/v1 ; then
crd="https://mirrors.chaos-mesh.org/${cm_version}/crd.yaml"
else
crd="https://mirrors.chaos-mesh.org/${cm_version}/crd-v1beta1.yaml"
fi
fi
if $template; then
ensure gen_crd_manifests "${crd}"
ensure gen_chaos_mesh_manifests "${runtime}" "${k3s}" "${cm_version}" "${timezone}" "${host_network}" "${docker_registry}" "${microk8s}"
exit 0
fi
need_cmd "sed"
need_cmd "tr"
if [ "${local_kube}" == "kind" ]; then
prepare_env
install_kubectl "${k8s_version}" ${force_kubectl}
check_docker
install_kind "${kind_version}" ${force_kind}
install_kubernetes_by_kind "${kind_name}" "${k8s_version}" "${node_num}" "${volume_num}" ${force_local_kube} ${volume_provisioner} ${local_registry}
fi
if [ "${install_dependency_only}" = true ]; then
exit 0
fi
check_kubernetes
install_chaos_mesh "${release_name}" "${namespace}" "${local_kube}" ${force_chaos_mesh} "${crd}" "${runtime}" "${k3s}" "${cm_version}" "${timezone}" "${docker_registry}" "${microk8s}"
ensure_pods_ready "${namespace}" "app.kubernetes.io/component=controller-manager" 100
ensure_pods_ready "${namespace}" "app.kubernetes.io/component=chaos-daemon" 100
ensure_pods_ready "${namespace}" "app.kubernetes.io/component=chaos-dashboard" 100
printf "Chaos Mesh %s is installed successfully\n" "${release_name}"
}
prepare_env() {
mkdir -p "$HOME/local/bin"
local set_path="export PATH=$HOME/local/bin:\$PATH"
local env_file="$HOME/.bash_profile"
if [[ ! -e "${env_file}" ]]; then
ensure touch "${env_file}"
fi
grep -qF -- "${set_path}" "${env_file}" || echo "${set_path}" >> "${env_file}"
ensure source "${env_file}"
}
check_kubernetes() {
need_cmd "kubectl"
kubectl_err_msg=$(kubectl version --output=yaml 2>&1 1>/dev/null)
if [ "$kubectl_err_msg" != "" ]; then
printf "check Kubernetes failed, error: %s\n" "${kubectl_err_msg}"
exit 1
fi
check_kubernetes_version
}
check_kubernetes_version() {
version_info=$(kubectl version --output=yaml | grep gitVersion | sed 's/.*gitVersion: v\([0-9.]*\).*/\1/g')
for v in $version_info
do
if version_lt "$v" "1.12.0"; then
printf "Chaos Mesh requires Kubernetes cluster running 1.12 or later\n"
exit 1
fi
done
}
install_kubectl() {
local kubectl_version=$1
local force_install=$2
printf "Install kubectl client\n"
err_msg=$(kubectl version --client=true --output=yaml 2>&1 1>/dev/null)
if [ "$err_msg" == "" ]; then
v=$(kubectl version --client=true --output=yaml | grep gitVersion | sed 's/.*gitVersion: v\([0-9.]*\).*/\1/g')
target_version=$(echo "${kubectl_version}" | sed s/v//g)
if version_lt "$v" "${target_version}"; then
printf "Chaos Mesg requires kubectl version %s or later\n" "${target_version}"
else
printf "kubectl Version %s has been installed\n" "$v"
if [ "$force_install" != "true" ]; then
return
fi
fi
fi
need_cmd "curl"
local KUBECTL_BIN="${HOME}/local/bin/kubectl"
local target_os=$(lowercase $(uname))
ensure curl -Lo /tmp/kubectl https://storage.googleapis.com/kubernetes-release/release/${kubectl_version}/bin/${target_os}/amd64/kubectl
ensure chmod +x /tmp/kubectl
ensure mv /tmp/kubectl "${KUBECTL_BIN}"
}
install_kubernetes_by_kind() {
local cluster_name=$1
local cluster_version=$2
local node_num=$3
local volume_num=$4
local force_install=$5
local volume_provisioner=$6
local local_registry=$7
printf "Install local Kubernetes %s\n" "${cluster_name}"
need_cmd "kind"
work_dir=${HOME}/kind/${cluster_name}
kubeconfig_path=${work_dir}/config
data_dir=${work_dir}/data
clusters=$(kind get clusters)
cluster_exist=false
for c in $clusters
do
if [ "$c" == "$cluster_name" ]; then
printf "Kind cluster %s has been installed\n" "${cluster_name}"
cluster_exist=true
break
fi
done
if [ "$cluster_exist" == "true" ]; then
if [ "$force_install" == "true" ]; then
printf "Delete Kind Kubernetes cluster %s\n" "${cluster_name}"
kind delete cluster --name="${cluster_name}"
status=$?
if [ $status -ne 0 ]; then
printf "Delete Kind Kubernetes cluster %s failed\n" "${cluster_name}"
exit 1
fi
else
ensure kind get kubeconfig --name="${cluster_name}" > "${kubeconfig_path}"
return
fi
fi
ensure mkdir -p "${work_dir}"
printf "Clean data dir: %s\n" "${data_dir}"
if [ -d "${data_dir}" ]; then
ensure rm -rf "${data_dir}"
fi
config_file=${work_dir}/kind-config.yaml
cat <<EOF > "${config_file}"
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
kubeadmConfigPatches:
- |
kind: ClusterConfiguration
metadata:
name: config
apiServerExtraArgs:
enable-admission-plugins: NodeRestriction,MutatingAdmissionWebhook,ValidatingAdmissionWebhook
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 5000
hostPort: 5000
listenAddress: 127.0.0.1
protocol: TCP
EOF
for ((i=0;i<"${node_num}";i++))
do
ensure mkdir -p "${data_dir}/worker${i}"
cat <<EOF >> "${config_file}"
- role: worker
extraMounts:
EOF
for ((k=1;k<="${volume_num}";k++))
do
ensure mkdir -p "${data_dir}/worker${i}/vol${k}"
cat <<EOF >> "${config_file}"
- containerPath: /mnt/disks/vol${k}
hostPath: ${data_dir}/worker${i}/vol${k}
EOF
done
done
local kind_image="kindest/node:${cluster_version}"
printf "start to create kubernetes cluster %s" "${cluster_name}"
ensure kind create cluster --config "${config_file}" --image="${kind_image}" --name="${cluster_name}" --retain -v 1
ensure kind get kubeconfig --name="${cluster_name}" > "${kubeconfig_path}"
ensure export KUBECONFIG="${kubeconfig_path}"
if [ "$volume_provisioner" == "true" ]; then
deploy_volume_provisioner "${work_dir}"
fi
}
deploy_volume_provisioner() {
local data_dir=$1
local config_file=${data_dir}/local-volume-provisionser.yaml
volume_provisioner_image="quay.io/external_storage/local-volume-provisioner:v2.3.2"
cat <<EOF >"${config_file}"
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: "local-storage"
provisioner: "kubernetes.io/no-provisioner"
volumeBindingMode: "WaitForFirstConsumer"
---
apiVersion: v1
kind: ConfigMap
metadata:
name: local-provisioner-config
namespace: kube-system
data:
nodeLabelsForPV: |
- kubernetes.io/hostname
storageClassMap: |
local-storage:
hostDir: /mnt/disks
mountDir: /mnt/disks
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: local-volume-provisioner
namespace: kube-system
labels:
app: local-volume-provisioner
spec:
selector:
matchLabels:
app: local-volume-provisioner
template:
metadata:
labels:
app: local-volume-provisioner
spec:
serviceAccountName: local-storage-admin
containers:
- image: ${volume_provisioner_image}
name: provisioner
securityContext:
privileged: true
env:
- name: MY_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: MY_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: JOB_CONTAINER_IMAGE
value: "quay.io/external_storage/local-volume-provisioner:v2.3.2"
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 100m
memory: 100Mi
volumeMounts:
- mountPath: /etc/provisioner/config
name: provisioner-config
readOnly: true
# mounting /dev in DinD environment would fail
# - mountPath: /dev
# name: provisioner-dev
- mountPath: /mnt/disks
name: local-disks
mountPropagation: "HostToContainer"
volumes:
- name: provisioner-config
configMap:
name: local-provisioner-config
# - name: provisioner-dev
# hostPath:
# path: /dev
- name: local-disks
hostPath:
path: /mnt/disks
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: local-storage-admin
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: local-storage-provisioner-pv-binding
namespace: kube-system
subjects:
- kind: ServiceAccount
name: local-storage-admin
namespace: kube-system
roleRef:
kind: ClusterRole
name: system:persistent-volume-provisioner
apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: local-storage-provisioner-node-clusterrole
namespace: kube-system
rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: local-storage-provisioner-node-binding
namespace: kube-system
subjects:
- kind: ServiceAccount
name: local-storage-admin
namespace: kube-system
roleRef:
kind: ClusterRole
name: local-storage-provisioner-node-clusterrole
apiGroup: rbac.authorization.k8s.io
EOF
ensure kubectl apply -f "${config_file}"
}
install_kind() {
local kind_version=$1
local force_install=$2
printf "Install Kind tool\n"
err_msg=$(kind version 2>&1 1>/dev/null)
if [ "$err_msg" == "" ]; then
v=$(kind version | awk '{print $2}' | sed s/v//g)
target_version=$(echo "${kind_version}" | sed s/v//g)
if version_lt "$v" "${target_version}"; then
printf "Chaos Mesh requires Kind version %s or later\n" "${target_version}"
else
printf "Kind Version %s has been installed\n" "$v"
if [ "$force_install" != "true" ]; then
return
fi
fi
fi
local KIND_BIN="${HOME}/local/bin/kind"
local target_os=$(lowercase $(uname))
ensure curl -Lo /tmp/kind https://github.com/kubernetes-sigs/kind/releases/download/"$1"/kind-"${target_os}"-amd64
ensure chmod +x /tmp/kind
ensure mv /tmp/kind "$KIND_BIN"
}
install_chaos_mesh() {
local release_name=$1
local namespace=$2
local local_kube=$3
local force_install=$4
local crd=$5
local runtime=$6
local k3s=$7
local version=$8
local timezone=$9
local docker_registry=${10}
local microk8s=${11}
printf "Install Chaos Mesh %s\n" "${release_name}"
gen_crd_manifests "${crd}" | kubectl create --validate=false -f - || exit 1
gen_chaos_mesh_manifests "${runtime}" "${k3s}" "${version}" "${timezone}" "${host_network}" "${docker_registry}" "${microk8s}" | kubectl apply -f - || exit 1
}
version_lt() {
vercomp $1 $2
if [ $? == 2 ]; then
return 0
fi
return 1
}
vercomp () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=("$1") ver2=("$2")
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
check_docker() {
need_cmd "docker"
docker_err_msg=$(docker version 2>&1 1>/dev/null)
if [ "$docker_err_msg" != "" ]; then
printf "check docker failed:\n"
echo "$docker_err_msg"
exit 1
fi
}
say() {
printf 'install chaos-mesh: %s\n' "$1"
}
err() {
say "$1" >&2
exit 1
}
need_cmd() {
if ! check_cmd "$1"; then
err "need '$1' (command not found)"
fi
}
check_cmd() {
command -v "$1" > /dev/null 2>&1
}
lowercase() {
echo "$@" | tr "[A-Z]" "[a-z]"
}
# Run a command that should never fail. If the command fails execution
# will immediately terminate with an error showing the failing
# command.
ensure() {
if ! "$@"; then err "command failed: $*"; fi
}
ensure_pods_ready() {
local namespace=$1
local labels=""
local limit=$3
if [ "$2" != "" ]; then
labels="-l $2"
fi
count=0
while [ -n "$(kubectl get pods -n "${namespace}" ${labels} --no-headers | grep -v Running)" ];
do
echo "Waiting for pod running" && sleep 10;
kubectl get pods -n "${namespace}" ${labels} --no-headers | >&2 grep -v Running || true
((count=count+1))
if [ $count -gt $limit ]; then
printf "Waiting for pod status running timeout\n"
exit 1
fi
done
}
gen_crd_manifests() {
local crd=$1
if check_url "$crd"; then
need_cmd curl
ensure curl -sSL "$crd"
return
fi
ensure cat "$crd"
}
check_url() {
local url=$1
local regex='^(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]\.[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]$'
if [[ $url =~ $regex ]];then
return 0
else
return 1
fi
}
gen_chaos_mesh_manifests() {
local runtime=$1
local k3s=$2
local version=$3
local timezone=$4
local host_network=$5
local docker_registry=$6
local microk8s=$7
local socketDir="/var/run"
local socketName="docker.sock"
if [ "${runtime}" == "containerd" ]; then
socketDir="/run/containerd"
socketName="containerd.sock"
fi
if [ "${k3s}" == "true" ]; then
socketDir="/run/k3s/containerd"
socketName="containerd.sock"
fi
if [ "${microk8s}" == "true" ]; then
socketDir="/var/snap/microk8s/common/run"
socketName="containerd.sock"
fi
need_cmd mktemp
need_cmd openssl
need_cmd curl
K8S_SERVICE="chaos-mesh-controller-manager"
K8S_NAMESPACE="chaos-mesh"
VERSION_TAG="${version}"
IMAGE_REGISTRY_PREFIX="${docker_registry}"
tmpdir=$(mktemp -d)
ensure openssl genrsa -out ${tmpdir}/ca.key 2048 > /dev/null 2>&1
ensure openssl req -x509 -new -nodes -key ${tmpdir}/ca.key -subj "/CN=${K8S_SERVICE}.${K8S_NAMESPACE}.svc" -days 1875 -out ${tmpdir}/ca.crt > /dev/null 2>&1
ensure openssl genrsa -out ${tmpdir}/server.key 2048 > /dev/null 2>&1
cat <<EOF > ${tmpdir}/csr.conf
[req]
prompt = no
req_extensions = v3_req
distinguished_name = dn
[dn]
CN = ${K8S_SERVICE}.${K8S_NAMESPACE}.svc
[v3_req]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = ${K8S_SERVICE}
DNS.2 = ${K8S_SERVICE}.${K8S_NAMESPACE}
DNS.3 = ${K8S_SERVICE}.${K8S_NAMESPACE}.svc
EOF
ensure openssl req -new -key ${tmpdir}/server.key -out ${tmpdir}/server.csr -config ${tmpdir}/csr.conf > /dev/null 2>&1
ensure openssl x509 -req -in ${tmpdir}/server.csr -CA ${tmpdir}/ca.crt -CAkey ${tmpdir}/ca.key -CAcreateserial -out ${tmpdir}/server.crt -days 1875 -extensions v3_req -extfile ${tmpdir}/csr.conf > /dev/null 2>&1
TLS_KEY=$(openssl base64 -A -in ${tmpdir}/server.key)
TLS_CRT=$(openssl base64 -A -in ${tmpdir}/server.crt)
CA_BUNDLE=$(openssl base64 -A -in ${tmpdir}/ca.crt)
# chaos-mesh.yaml start
cat <<EOF
---
apiVersion: v1
kind: Namespace
metadata:
name: chaos-mesh
---
# Source: chaos-mesh/templates/chaos-daemon-rbac.yaml
kind: ServiceAccount
apiVersion: v1
metadata:
namespace: "chaos-mesh"
name: chaos-daemon
labels:
app.kubernetes.io/name: chaos-mesh
app.kubernetes.io/instance: chaos-mesh
app.kubernetes.io/part-of: chaos-mesh
app.kubernetes.io/version: ${VERSION_TAG##v}
app.kubernetes.io/component: chaos-daemon
---
# Source: chaos-mesh/templates/chaos-dashboard-rbac.yaml
# Copyright 2022 Chaos Mesh 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.
#
# ServiceAccount for component chaos-dashboard
kind: ServiceAccount
apiVersion: v1
metadata:
namespace: "chaos-mesh"
name: chaos-dashboard
labels:
app.kubernetes.io/name: chaos-mesh
app.kubernetes.io/instance: chaos-mesh
app.kubernetes.io/part-of: chaos-mesh
app.kubernetes.io/version: ${VERSION_TAG##v}
app.kubernetes.io/component: chaos-dashboard
---
# Source: chaos-mesh/templates/controller-manager-rbac.yaml
# Copyright 2021 Chaos Mesh 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.
#
kind: ServiceAccount
apiVersion: v1
metadata:
namespace: "chaos-mesh"
name: chaos-controller-manager
labels:
app.kubernetes.io/name: chaos-mesh
app.kubernetes.io/instance: chaos-mesh
app.kubernetes.io/part-of: chaos-mesh
app.kubernetes.io/version: ${VERSION_TAG##v}
app.kubernetes.io/component: controller-manager
---
# Source: chaos-mesh/templates/secrets-configuration.yaml
# Copyright 2021 Chaos Mesh 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.
#
kind: Secret
apiVersion: v1
metadata:
name: chaos-mesh-webhook-certs
namespace: "chaos-mesh"
labels:
app.kubernetes.io/name: chaos-mesh
app.kubernetes.io/instance: chaos-mesh
app.kubernetes.io/part-of: chaos-mesh
app.kubernetes.io/version: ${VERSION_TAG##v}
app.kubernetes.io/component: webhook-secret
type: Opaque
data:
ca.crt: "${CA_BUNDLE}"
tls.crt: "${TLS_CRT}"
tls.key: "${TLS_KEY}"
---
# Source: chaos-mesh/templates/chaos-dashboard-rbac.yaml
# ClusterRole for chaos-dashboard at cluster scope
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: chaos-mesh-chaos-dashboard-cluster-level
labels:
app.kubernetes.io/name: chaos-mesh
app.kubernetes.io/instance: chaos-mesh
app.kubernetes.io/part-of: chaos-mesh
app.kubernetes.io/version: ${VERSION_TAG##v}
app.kubernetes.io/component: chaos-dashboard
rules:
# chaos-dashboard could list namespace for selector hints
- apiGroups: [ "" ]
resources:
- namespaces
verbs:
- get
- list
- watch
# chaos-dashboard use subjectaccessreviews to authorize the requests
- apiGroups: [ "authorization.k8s.io" ]
resources:
- subjectaccessreviews
verbs:
- create
---
# Source: chaos-mesh/templates/chaos-dashboard-rbac.yaml
# ClusterRole for chaos-dashboard in target namespace
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: chaos-mesh-chaos-dashboard-target-namespace
labels:
app.kubernetes.io/name: chaos-mesh
app.kubernetes.io/instance: chaos-mesh
app.kubernetes.io/part-of: chaos-mesh
app.kubernetes.io/version: ${VERSION_TAG##v}
app.kubernetes.io/component: chaos-dashboard
rules:
# chaos dashboard could list pods for selector hints
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- list
- watch
# chaos dashboard could record evnets from chaos experiments
- apiGroups:
- ""
resources:
- events
verbs:
- get
- list
- watch
# chaos dashboard could record and manipulate all the Chaos Mesh resources in target namespace
- apiGroups: [ "chaos-mesh.org" ]
resources:
- "*"
verbs: [ "*" ]
---
# Source: chaos-mesh/templates/controller-manager-rbac.yaml
# roles
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: chaos-mesh-chaos-controller-manager-target-namespace
labels:
app.kubernetes.io/name: chaos-mesh
app.kubernetes.io/instance: chaos-mesh
app.kubernetes.io/part-of: chaos-mesh
app.kubernetes.io/version: ${VERSION_TAG##v}
app.kubernetes.io/component: controller-manager
rules:
- apiGroups: [ "" ]