This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
helm.sh
executable file
·2235 lines (1741 loc) · 62.7 KB
/
helm.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
#!/bin/bash
SHELL_DIR=$(dirname $0)
. ${SHELL_DIR}/common.sh
. ${SHELL_DIR}/default.sh
################################################################################
title() {
if [ "${TPUT}" != "" ]; then
tput clear
fi
echo
_echo "${THIS_NAME}" 3
echo
_echo "${CLUSTER_NAME}" 4
}
prepare() {
logo
mkdir -p ~/.ssh
mkdir -p ~/.aws
mkdir -p ${SHELL_DIR}/build/${THIS_NAME}
NEED_TOOL=
command -v jq > /dev/null || export NEED_TOOL=jq
command -v git > /dev/null || export NEED_TOOL=git
command -v aws > /dev/null || export NEED_TOOL=awscli
command -v kubectl > /dev/null || export NEED_TOOL=kubectl
command -v helm > /dev/null || export NEED_TOOL=helm
if [ ! -z ${NEED_TOOL} ]; then
question "Do you want to install the required tools? (awscli,kubectl,helm...) [Y/n] : "
if [ "${ANSWER:-Y}" == "Y" ]; then
${SHELL_DIR}/tools.sh
else
_error "Need install tools."
fi
fi
REGION="$(aws configure get region)"
}
run() {
prepare
get_cluster
config_load
main_menu
}
press_enter() {
_result "$(date)"
_read "Press Enter to continue..." 5
echo
case ${1} in
main)
main_menu
;;
kube-ingress)
charts_menu "kube-ingress"
;;
kube-system)
charts_menu "kube-system"
;;
monitor)
charts_menu "monitor"
;;
devops)
charts_menu "devops"
;;
sample)
charts_menu "sample"
;;
batch)
charts_menu "batch"
;;
istio)
istio_menu
;;
esac
}
main_menu() {
title
echo
_echo "1. helm init"
echo
_echo "2. kube-ingress.."
_echo "3. kube-system.."
_echo "4. monitor.."
_echo "5. devops.."
_echo "6. sample.."
_echo "7. batch.."
echo
_echo "i. istio.."
echo
_echo "d. remove"
echo
_echo "c. check PV"
_echo "v. save variables"
echo
_echo "u. update self"
_echo "t. update tools"
echo
_echo "x. Exit"
question
case ${ANSWER} in
1)
helm_init
press_enter main
;;
2)
charts_menu "kube-ingress"
;;
3)
charts_menu "kube-system"
;;
4)
charts_menu "monitor"
;;
5)
charts_menu "devops"
;;
6)
charts_menu "sample"
;;
7)
charts_menu "batch"
;;
i)
istio_menu
;;
d)
helm_delete
press_enter main
;;
c)
validate_pv
press_enter main
;;
v)
variables_save
press_enter main
;;
u)
update_self
press_enter cluster
;;
t)
update_tools
press_enter cluster
;;
x)
_success "Good bye!"
;;
*)
main_menu
;;
esac
}
listup_ing_rules() {
SERVICE_NAME="nginx-ingress-private-controller"
ELB_DNS=$(kubectl -n kube-ingress get svc ${SERVICE_NAME} -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')
ELB_ID=$(echo ${ELB_DNS} | cut -d'-' -f 1)
SG_ID=$(aws elb describe-load-balancers --load-balancer-name ${ELB_ID} | jq -r '.LoadBalancerDescriptions[] | .SecurityGroups[]')
_command "aws ec2 describe-security-groups --group-id ${SG_ID} | jq -r '.SecurityGroups[] | .IpPermissions'"
export SG_INGRESS_RULE=$(aws ec2 describe-security-groups --group-id ${SG_ID} | jq -r '.SecurityGroups[] | .IpPermissions')
echo $SG_INGRESS_RULE
}
remove_ing_rule() {
listup_ing_rules
ALL_RULE=$(echo $SG_INGRESS_RULE | jq -rc . )
_command "aws ec2 revoke-security-group-ingress --group-id $SG_ID --ip-permissions $ALL_RULE"
aws ec2 revoke-security-group-ingress --group-id $SG_ID --ip-permissions "${ALL_RULE}"
listup_ing_rules
}
istio_menu() {
title
echo
_echo "1. install"
_echo "2. install istio-remote"
echo
_echo "3. injection show"
_echo "4. injection enable"
_echo "5. injection disable"
echo
_echo "6. import config to secret"
_echo "7. export config"
_echo "8. show pod IPs"
echo
_echo "9. remove"
echo
_echo "k. kiali service open"
_echo "j. jaeger service open"
question
case ${ANSWER} in
1)
istio_install
press_enter istio
;;
2)
istio_remote_install
press_enter istio
;;
3)
istio_injection
press_enter istio
;;
4)
istio_injection "enable"
press_enter istio
;;
5)
istio_injection "disable"
press_enter istio
;;
6)
istio_import_config_to_secret
press_enter istio
;;
7)
istio_export_config
press_enter istio
;;
8)
istio_show_pod_ips
press_enter istio
;;
9)
istio_delete
press_enter istio
;;
k)
apply_vs_kiali
press_enter istio
;;
j)
apply_vs_jaeger
press_enter istio
;;
*)
main_menu
;;
esac
}
apply_vs_kiali() {
DEFAULT_ADDR="kiali.${ISTIO_DOMAIN}"
question "Enter host address for Kiali [${DEFAULT_ADDR}] : "
KIALI_DOMAIN=${ANSWER:-${DEFAULT_ADDR}}
CHART=${SHELL_DIR}/build/${CLUSTER_NAME}/istio-${NAME}.yaml
get_template charts/istio/vs-kiali.yaml ${CHART}
_replace "s/REPLACE_ME/${KIALI_DOMAIN}/g" ${CHART}
_command "kubectl apply -f ${CHART}"
kubectl apply -f ${CHART}
echo
_result "Connect - http://${KIALI_DOMAIN}/kiali"
_result "Default ID/PW : admin/admin"
}
apply_vs_jaeger() {
DEFAULT_ADDR="jaeger.${ISTIO_DOMAIN}"
question "Enter host address for Jaeger [${DEFAULT_ADDR}] : "
JAEGER_DOMAIN=${ANSWER:-${DEFAULT_ADDR}}
CHART=${SHELL_DIR}/build/${CLUSTER_NAME}/istio-${NAME}.yaml
get_template charts/istio/vs-jaeger.yaml ${CHART}
_replace "s/REPLACE_ME/${JAEGER_DOMAIN}/g" ${CHART}
_command "kubectl apply -f ${CHART}"
kubectl apply -f ${CHART}
echo
_result "Connect - http://${JAEGER_DOMAIN}"
}
charts_menu() {
title
NAMESPACE=$1
LIST=${SHELL_DIR}/build/${CLUSTER_NAME}/charts-list
# find chart
ls ${SHELL_DIR}/charts/${NAMESPACE} | sort | sed 's/.yaml//' > ${LIST}
# select
select_one
if [ -z ${SELECTED} ]; then
main_menu
return
fi
# create_cluster_role_binding admin ${NAMESPACE}
# helm install
helm_install ${SELECTED} ${NAMESPACE}
press_enter ${NAMESPACE}
}
helm_install() {
helm_check
NAME=${1}
NAMESPACE=${2}
create_namespace ${NAMESPACE}
EXTRA_VALUES=
# helm check FAILED
# COUNT=$(helm ls -a | grep ${NAME} | grep ${NAMESPACE} | grep "FAILED" | wc -l | xargs)
# if [ "x${COUNT}" != "x0" ]; then
# _command "helm delete --purge ${NAME}"
# helm delete --purge ${NAME}
# fi
# helm chart
CHART=${SHELL_DIR}/build/${CLUSTER_NAME}/helm-${NAME}.yaml
get_template charts/${NAMESPACE}/${NAME}.yaml ${CHART}
# chart repository
REPO=$(cat ${CHART} | grep '# chart-repo:' | awk '{print $3}')
if [ "${REPO}" == "" ]; then
REPO="stable/${NAME}"
else
PREFIX="$(echo ${REPO} | cut -d'/' -f1)"
if [ "${PREFIX}" == "custom" ]; then
REPO="${SHELL_DIR}/${REPO}"
elif [ "${PREFIX}" != "stable" ]; then
helm_repo "${PREFIX}"
fi
fi
# chart config
VERSION=$(cat ${CHART} | grep '# chart-version:' | awk '{print $3}')
INGRESS=$(cat ${CHART} | grep '# chart-ingress:' | awk '{print $3}')
_result "${REPO} version: ${VERSION}"
# installed chart version
LATEST=$(helm ls ${NAME} | grep ${NAME} | head -1 | awk '{print $9}')
if [ "${LATEST}" != "" ]; then
_result "installed version: ${LATEST}"
fi
# latest chart version
LATEST=$(helm search ${NAME} | grep ${REPO} | head -1 | awk '{print $2}')
if [ "${LATEST}" != "" ]; then
_result "latest chart version: ${LATEST}"
if [ "${VERSION}" != "" ] && [ "${VERSION}" != "latest" ] && [ "${VERSION}" != "${LATEST}" ]; then
LIST=${SHELL_DIR}/build/${CLUSTER_NAME}/version-list
echo "${VERSION} " > ${LIST}
echo "${LATEST} (latest) " >> ${LIST}
# select
select_one
if [ "${SELECTED}" != "" ]; then
VERSION="$(echo "${SELECTED}" | cut -d' ' -f1)"
fi
_result "${VERSION}"
fi
if [ "${VERSION}" == "" ] || [ "${VERSION}" == "latest" ]; then
_replace "s/chart-version:.*/chart-version: ${LATEST}/g" ${CHART}
fi
fi
# global
_replace "s/AWS_REGION/${REGION}/g" ${CHART}
_replace "s/CLUSTER_NAME/${CLUSTER_NAME}/g" ${CHART}
_replace "s/NAMESPACE/${NAMESPACE}/g" ${CHART}
# for cert-manager
if [ "${NAME}" == "cert-manager" ]; then
# Install the CustomResourceDefinition resources separately
# https://github.com/helm/charts/blob/master/stable/cert-manager/README.md
kubectl apply \
-f https://raw.githubusercontent.com/jetstack/cert-manager/release-0.6/deploy/manifests/00-crds.yaml
# Label the cert-manager namespace to disable resource validation
kubectl label namespace ${NAMESPACE} certmanager.k8s.io/disable-validation=true
fi
# for nginx-ingress
if [[ "${NAME}" == "nginx-ingress"* ]]; then
get_base_domain
get_replicas ${NAMESPACE} ${NAME}-controller
if [ "${REPLICAS}" != "" ]; then
EXTRA_VALUES="${EXTRA_VALUES} --set controller.replicaCount=${REPLICAS}"
fi
get_cluster_ip ${NAMESPACE} ${NAME}-controller
if [ "${CLUSTER_IP}" != "" ]; then
EXTRA_VALUES="${EXTRA_VALUES} --set controller.service.clusterIP=${CLUSTER_IP}"
get_cluster_ip ${NAMESPACE} ${NAME}-controller-metrics
if [ "${CLUSTER_IP}" != "" ]; then
EXTRA_VALUES="${EXTRA_VALUES} --set controller.metrics.service.clusterIP=${CLUSTER_IP}"
fi
get_cluster_ip ${NAMESPACE} ${NAME}-controller-stats
if [ "${CLUSTER_IP}" != "" ]; then
EXTRA_VALUES="${EXTRA_VALUES} --set controller.stats.service.clusterIP=${CLUSTER_IP}"
fi
get_cluster_ip ${NAMESPACE} ${NAME}-default-backend
if [ "${CLUSTER_IP}" != "" ]; then
EXTRA_VALUES="${EXTRA_VALUES} --set defaultBackend.service.clusterIP=${CLUSTER_IP}"
fi
fi
fi
# for external-dns
if [ "${NAME}" == "external-dns" ]; then
replace_chart ${CHART} "AWS_ACCESS_KEY"
if [ "${ANSWER}" != "" ]; then
replace_password ${CHART} "AWS_SECRET_KEY" "****"
fi
fi
# for cluster-autoscaler
if [ "${NAME}" == "cluster-autoscaler" ]; then
get_cluster_ip ${NAMESPACE} ${NAME}
if [ "${CLUSTER_IP}" != "" ]; then
EXTRA_VALUES="${EXTRA_VALUES} --set service.clusterIP=${CLUSTER_IP}"
fi
fi
# for efs-provisioner
if [ "${NAME}" == "efs-provisioner" ]; then
efs_create
fi
# for k8s-spot-termination-handler
if [ "${NAME}" == "k8s-spot-termination-handler" ]; then
replace_chart ${CHART} "SLACK_URL"
fi
# for vault
if [ "${NAME}" == "vault" ]; then
replace_chart ${CHART} "AWS_ACCESS_KEY"
if [ "${ANSWER}" != "" ]; then
_replace "s/#:STORAGE://g" ${CHART}
replace_password ${CHART} "AWS_SECRET_KEY" "****"
replace_chart ${CHART} "AWS_BUCKET" "${CLUSTER_NAME}-vault"
fi
fi
# for argo
if [ "${NAME}" == "argo" ]; then
replace_chart ${CHART} "ARTIFACT_REPOSITORY" "${CLUSTER_NAME}-artifact"
fi
# for argocd
if [ "${NAME}" == "argocd" ]; then
replace_chart ${CHART} "GITHUB_ORG"
if [ "${ANSWER}" != "" ]; then
_replace "s/#:GITHUB://g" ${CHART}
_result "New Application: https://github.com/organizations/${ANSWER}/settings/applications"
_result "Homepage: https://${NAME}-${NAMESPACE}.${BASE_DOMAIN}"
_result "Callback: https://${NAME}-${NAMESPACE}.${BASE_DOMAIN}/api/dex/callback"
replace_password ${CHART} "GITHUB_CLIENT_ID" "****"
replace_password ${CHART} "GITHUB_CLIENT_SECRET" "****"
fi
fi
# for jenkins
if [ "${NAME}" == "jenkins" ]; then
# admin password
replace_password ${CHART}
# jenkins jobs
${SHELL_DIR}/templates/jenkins/jobs.sh ${CHART}
fi
# for sonatype-nexus
if [ "${NAME}" == "sonatype-nexus" ]; then
# admin password
replace_password ${CHART}
fi
# for prometheus
if [ "${NAME}" == "prometheus" ]; then
replace_chart ${CHART} "SLACK_TOKEN"
# kube-state-metrics
COUNT=$(kubectl get pods -n kube-system | grep kube-state-metrics | wc -l | xargs)
if [ "x${COUNT}" == "x0" ]; then
_replace "s/KUBE_STATE_METRICS/true/g" ${CHART}
else
_replace "s/KUBE_STATE_METRICS/false/g" ${CHART}
fi
fi
# for grafana
if [ "${NAME}" == "grafana" ]; then
# admin password
replace_password ${CHART}
# auth.google
replace_chart ${CHART} "G_CLIENT_ID"
if [ "${ANSWER}" != "" ]; then
_replace "s/#:G_AUTH://g" ${CHART}
replace_password ${CHART} "G_CLIENT_SECRET" "****"
replace_chart ${CHART} "G_ALLOWED_DOMAINS"
else
# auth.ldap
replace_chart ${CHART} "GRAFANA_LDAP"
if [ "${ANSWER}" != "" ]; then
_replace "s/#:LDAP://g" ${CHART}
fi
fi
fi
# for datadog
if [ "${NAME}" == "datadog" ]; then
# api key
replace_password ${CHART} "API_KEY" "****"
# app key
replace_password ${CHART} "APP_KEY" "****"
# kube-state-metrics
COUNT=$(kubectl get pods -n kube-system | grep kube-state-metrics | wc -l | xargs)
if [ "x${COUNT}" == "x0" ]; then
_replace "s/KUBE_STATE_METRICS/true/g" ${CHART}
else
_replace "s/KUBE_STATE_METRICS/false/g" ${CHART}
fi
fi
# for newrelic-infrastructure
if [ "${NAME}" == "newrelic-infrastructure" ]; then
# license key
replace_password ${CHART} "LICENSE_KEY" "****"
fi
# for jaeger
if [ "${NAME}" == "jaeger" ]; then
# host
replace_chart ${CHART} "CUSTOM_HOST" "elasticsearch.domain.com"
# port
replace_chart ${CHART} "CUSTOM_PORT" "80"
fi
# for fluentd-elasticsearch
if [ "${NAME}" == "fluentd-elasticsearch" ]; then
# host
replace_chart ${CHART} "CUSTOM_HOST" "elasticsearch.domain.com"
# port
replace_chart ${CHART} "CUSTOM_PORT" "80"
fi
# for elasticsearch-snapshot
if [ "${NAME}" == "elasticsearch-snapshot" ]; then
replace_chart ${CHART} "SCHEDULE" "0 0 * * *"
replace_chart ${CHART} "RESTART" "OnFailure" # "Always", "OnFailure", "Never"
replace_chart ${CHART} "CONFIGMAP_NAME" "${NAME}"
fi
# for efs-pvc-exporter
if [ "${NAME}" == "efs-pvc-exporter" ]; then
replace_chart ${CHART} "SCHEDULE" "* * * * *"
replace_chart ${CHART} "RESTART" "OnFailure" # "Always", "OnFailure", "Never"
fi
# for efs-mount
if [ "${EFS_ID}" != "" ]; then
_replace "s/#:EFS://g" ${CHART}
fi
# for master node
NODE=$(cat ${CHART} | grep '# chart-node:' | awk '{print $3}')
if [ "${NODE}" == "master" ]; then
COUNT=$(kubectl get no | grep Ready | grep master | wc -l | xargs)
if [ "x${COUNT}" != "x0" ]; then
_replace "s/#:MASTER://g" ${CHART}
fi
fi
# for istio
if [ "${ISTIO}" == "true" ]; then
COUNT=$(kubectl get ns ${NAMESPACE} --show-labels | grep 'istio-injection=enabled' | wc -l | xargs)
if [ "x${COUNT}" != "x0" ]; then
ISTIO_ENABLED=true
else
ISTIO_ENABLED=false
fi
else
ISTIO_ENABLED=false
fi
_replace "s/ISTIO_ENABLED/${ISTIO_ENABLED}/g" ${CHART}
# for ingress
if [ "${INGRESS}" == "true" ]; then
if [ -z ${BASE_DOMAIN} ]; then
DOMAIN=
_replace "s/SERVICE_TYPE/LoadBalancer/g" ${CHART}
_replace "s/INGRESS_ENABLED/false/g" ${CHART}
else
DOMAIN="${NAME}-${NAMESPACE}.${BASE_DOMAIN}"
_replace "s/SERVICE_TYPE/ClusterIP/g" ${CHART}
_replace "s/INGRESS_ENABLED/true/g" ${CHART}
_replace "s/INGRESS_DOMAIN/${DOMAIN}/g" ${CHART}
_replace "s/BASE_DOMAIN/${BASE_DOMAIN}/g" ${CHART}
fi
_replace "s/#:ING://g" ${CHART}
fi
# check exist persistent volume
COUNT=$(cat ${CHART} | grep '# chart-pvc:' | wc -l | xargs)
if [ "x${COUNT}" != "x0" ]; then
LIST=${SHELL_DIR}/build/${CLUSTER_NAME}/pvc-${NAME}-yaml
cat ${CHART} | grep '# chart-pvc:' | awk '{print $3,$4,$5}' > ${LIST}
while IFS='' read -r line || [[ -n "$line" ]]; do
ARR=(${line})
check_exist_pv ${NAMESPACE} ${ARR[0]} ${ARR[1]} ${ARR[2]}
RELEASED=$?
if [ "${RELEASED}" -gt "0" ]; then
echo " To use an existing volume, remove the PV's '.claimRef.uid' attribute to make the PV an 'Available' status and try again."
return
fi
done < "${LIST}"
fi
# helm install
if [ "${VERSION}" == "" ] || [ "${VERSION}" == "latest" ]; then
_command "helm upgrade --install ${NAME} ${REPO} --namespace ${NAMESPACE} --values ${CHART}"
helm upgrade --install ${NAME} ${REPO} --namespace ${NAMESPACE} --values ${CHART} ${EXTRA_VALUES}
else
_command "helm upgrade --install ${NAME} ${REPO} --namespace ${NAMESPACE} --values ${CHART} --version ${VERSION}"
helm upgrade --install ${NAME} ${REPO} --namespace ${NAMESPACE} --values ${CHART} --version ${VERSION} ${EXTRA_VALUES}
fi
# config save
config_save
# create pdb
PDB_MIN=$(cat ${CHART} | grep '# chart-pdb:' | awk '{print $3}')
PDB_MAX=$(cat ${CHART} | grep '# chart-pdb:' | awk '{print $4}')
if [ "${PDB_MIN}" != "" ] || [ "${PDB_MAX}" != "" ]; then
create_pdb ${NAMESPACE} ${NAME} ${PDB_MIN:-N} ${PDB_MAX:-N}
fi
_command "helm history ${NAME}"
helm history ${NAME}
# waiting 2
waiting_pod "${NAMESPACE}" "${NAME}"
_command "kubectl get deploy,pod,svc,ing,pvc,pv -n ${NAMESPACE}"
kubectl get deploy,pod,svc,ing,pvc,pv -n ${NAMESPACE}
# for argo
if [ "${NAME}" == "argo" ]; then
create_cluster_role_binding admin default default
fi
# for jenkins
if [ "${NAME}" == "jenkins" ]; then
create_cluster_role_binding admin ${NAMESPACE} default
fi
# for nginx-ingress
if [[ "${NAME}" == "nginx-ingress"* ]]; then
set_base_domain "${NAME}"
fi
# for nginx-ingress-private
if [ "${NAME}" == "nginx-ingress-private" ]; then
question "ingress rule delete all? (YES/[no]) : "
if [ "${ANSWER}" == "YES" ]; then
remove_ing_rule
fi
fi
# for efs-provisioner
if [ "${NAME}" == "efs-provisioner" ]; then
_command "kubectl get sc -n ${NAMESPACE}"
kubectl get sc -n ${NAMESPACE}
fi
# for kubernetes-dashboard
if [ "${NAME}" == "kubernetes-dashboard" ]; then
create_cluster_role_binding view ${NAMESPACE} ${NAME}-view true
fi
# chart ingress = true
if [ "${INGRESS}" == "true" ]; then
if [ -z ${BASE_DOMAIN} ]; then
get_elb_domain ${NAME} ${NAMESPACE}
_result "${NAME}: http://${ELB_DOMAIN}"
else
if [ -z ${ROOT_DOMAIN} ]; then
_result "${NAME}: http://${DOMAIN}"
else
_result "${NAME}: https://${DOMAIN}"
fi
fi
fi
}
helm_delete() {
NAME=
TEMP=${SHELL_DIR}/build/${CLUSTER_NAME}/helm-temp
LIST=${SHELL_DIR}/build/${CLUSTER_NAME}/helm-list
_command "helm ls --all"
# find all
helm ls --all --output json \
| jq -r '"NAMESPACE NAME STATUS REVISION CHART",
(.Releases[] | "\(.Namespace) \(.Name) \(.Status) \(.Revision) \(.Chart)")' \
| column -t > ${TEMP}
printf "\n $(head -1 ${TEMP})"
cat ${TEMP} | grep -v "NAMESPACE" | sort > ${LIST}
# select
select_one
if [ "${SELECTED}" == "" ]; then
return
fi
NAME="$(echo ${SELECTED} | awk '{print $2}')"
if [ "${NAME}" == "" ]; then
return
fi
# for nginx-ingress
if [[ "${NAME}" == "nginx-ingress"* ]]; then
ROOT_DOMAIN=
BASE_DOMAIN=
fi
# for efs-provisioner
if [ "${NAME}" == "efs-provisioner" ]; then
efs_delete
fi
# helm delete
_command "helm delete --purge ${NAME}"
helm delete --purge ${NAME}
# for argo
if [ "${NAME}" == "argo" ]; then
COUNT=$(kubectl get pod -n devops | grep argo | grep Running | wc -l | xargs)
if [ "x${COUNT}" == "x0" ]; then
delete_crds "argoproj.io"
fi
fi
# for cert-manager
if [ "${NAME}" == "cert-manager" ]; then
# delete crds
delete_crds "certmanager.k8s.io"
fi
# config save
config_save
}
helm_check() {
_command "kubectl get pod -n kube-system | grep tiller-deploy"
COUNT=$(kubectl get pod -n kube-system | grep tiller-deploy | wc -l | xargs)
if [ "x${COUNT}" == "x0" ] || [ ! -d ~/.helm ]; then
helm_init
fi
}
helm_init() {
NAMESPACE="kube-system"
ACCOUNT="tiller"
create_cluster_role_binding cluster-admin ${NAMESPACE} ${ACCOUNT}
_command "helm init --upgrade --service-account=${ACCOUNT}"
helm init --upgrade --service-account=${ACCOUNT}
# default pdb
default_pdb "${NAMESPACE}"
# waiting 5
waiting_pod "${NAMESPACE}" "tiller"
_command "kubectl get pod,svc -n ${NAMESPACE}"
kubectl get pod,svc -n ${NAMESPACE}
helm_repo_update
}
helm_repo() {
_NAME=$1
_REPO=$2
if [ "${_REPO}" == "" ]; then
if [ "${_NAME}" == "incubator" ]; then
_REPO="https://storage.googleapis.com/kubernetes-charts-incubator"
elif [ "${_NAME}" == "argo" ]; then
_REPO="https://argoproj.github.io/argo-helm"
elif [ "${_NAME}" == "monocular" ]; then
_REPO="https://helm.github.io/monocular"
fi
fi
if [ "${_REPO}" != "" ]; then
COUNT=$(helm repo list | grep -v NAME | awk '{print $1}' | grep "${_NAME}" | wc -l | xargs)
if [ "x${COUNT}" == "x0" ]; then
_command "helm repo add ${_NAME} ${_REPO}"
helm repo add ${_NAME} ${_REPO}
helm_repo_update
fi
fi
}
helm_repo_update() {
_command "helm repo list"
helm repo list
_command "helm repo update"
helm repo update
}
create_namespace() {
_NAMESPACE=$1
CHECK=
_command "kubectl get ns ${_NAMESPACE}"
kubectl get ns ${_NAMESPACE} > /dev/null 2>&1 || export CHECK=CREATE
if [ "${CHECK}" == "CREATE" ]; then
_result "${_NAMESPACE}"
_command "kubectl create ns ${_NAMESPACE}"
kubectl create ns ${_NAMESPACE}
fi
}
create_service_account() {
_NAMESPACE=$1
_ACCOUNT=$2
create_namespace ${_NAMESPACE}
CHECK=
_command "kubectl get sa ${_ACCOUNT} -n ${_NAMESPACE}"
kubectl get sa ${_ACCOUNT} -n ${_NAMESPACE} > /dev/null 2>&1 || export CHECK=CREATE
if [ "${CHECK}" == "CREATE" ]; then
_result "${_NAMESPACE}:${_ACCOUNT}"
_command "kubectl create sa ${_ACCOUNT} -n ${_NAMESPACE}"
kubectl create sa ${_ACCOUNT} -n ${_NAMESPACE}
fi
}
create_cluster_role_binding() {
_ROLE=$1
_NAMESPACE=$2
_ACCOUNT=${3:-default}
_TOKEN=${4:-false}
create_service_account ${_NAMESPACE} ${_ACCOUNT}
CHECK=
_command "kubectl get clusterrolebinding ${_ROLE}:${_NAMESPACE}:${_ACCOUNT}"
kubectl get clusterrolebinding ${_ROLE}:${_NAMESPACE}:${_ACCOUNT} > /dev/null 2>&1 || export CHECK=CREATE
if [ "${CHECK}" == "CREATE" ]; then
_result "${_ROLE}:${_NAMESPACE}:${_ACCOUNT}"
_command "kubectl create clusterrolebinding ${_ROLE}:${_NAMESPACE}:${_ACCOUNT} --clusterrole=${_ROLE} --serviceaccount=${_NAMESPACE}:${_ACCOUNT}"
kubectl create clusterrolebinding ${_ROLE}:${_NAMESPACE}:${_ACCOUNT} --clusterrole=${_ROLE} --serviceaccount=${_NAMESPACE}:${_ACCOUNT}
fi
if [ "${_TOKEN}" == "true" ]; then
SECRET=$(kubectl get secret -n ${_NAMESPACE} | grep ${_ACCOUNT}-token | awk '{print $1}')
_command "kubectl describe secret ${SECRET} -n ${_NAMESPACE}"
kubectl describe secret ${SECRET} -n ${_NAMESPACE} | grep 'token:'
fi
}
get_replicas() {
REPLICAS=$(kubectl get deployment -n ${1} -o json | jq -r ".items[] | select(.metadata.name == \"${2}\") | .spec.replicas")
}
get_cluster_ip() {
CLUSTER_IP=$(kubectl get svc -n ${1} -o json | jq -r ".items[] | select(.metadata.name == \"${2}\") | .spec.clusterIP")
}
default_pdb() {
_NAMESPACE=${1}
create_pdb ${_NAMESPACE} coredns 1 N k8s-app kube-dns
create_pdb ${_NAMESPACE} kube-dns 1 N k8s-app
create_pdb ${_NAMESPACE} kube-dns-autoscaler N 1 k8s-app
create_pdb ${_NAMESPACE} tiller-deploy N 1 tiller
}
create_pdb() {
_NAMESPACE=${1}
_PDB_NAME=${2}
_PDB_MIN=${3:-N}
_PDB_MAX=${4:-N}
_LABELS=${5:-app}
_APP_NAME=${6:-${_PDB_NAME}}
COUNT=$(kubectl get deploy -n kube-system | grep ${_PDB_NAME} | grep -v NAME | wc -l | xargs)
if [ "x${COUNT}" == "x0" ]; then
return
fi
if [ "${_PDB_NAME}" == "heapster" ]; then
_APP_NAME="heapster-heapster"
fi
YAML=${SHELL_DIR}/build/${CLUSTER_NAME}/pdb-${_PDB_NAME}.yaml
get_template templates/pdb/pdb-${_LABELS}.yaml ${YAML}
_replace "s/PDB_NAME/${_PDB_NAME}/g" ${YAML}
_replace "s/APP_NAME/${_APP_NAME}/g" ${YAML}