/
install_asm
executable file
·2777 lines (2417 loc) · 83.3 KB
/
install_asm
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
set -CeE
set -o pipefail
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
cat << EOF >&2
WARNING: bash ${BASH_VERSION} does not support several modern safety features.
This script was written with the latest POSIX standard in mind, and was only
tested with modern shell standards. This script may not perform correctly in
this environment.
EOF
sleep 1
else
set -u
fi
### These are hooks for Cloud Build to be able to use debug/staging images
### when necessary. Don't set these environment variables unless you're testing
### in CI/CD.
_CI_ASM_IMAGE_LOCATION="${_CI_ASM_IMAGE_LOCATION:=}"
_CI_ASM_IMAGE_TAG="${_CI_ASM_IMAGE_TAG:=}"
_CI_ASM_PKG_LOCATION="${_CI_ASM_PKG_LOCATION:=}"
_CI_CLOUDRUN_IMAGE_HUB="${_CI_CLOUDRUN_IMAGE_HUB:=}"
_CI_CLOUDRUN_IMAGE_TAG="${_CI_CLOUDRUN_IMAGE_TAG:=}"
_CI_REVISION_PREFIX="${_CI_REVISION_PREFIX:=}"
_CI_NO_VALIDATE="${_CI_NO_VALIDATE:=0}"
_CI_NO_REVISION="${_CI_NO_REVISION:=0}"
_CI_ISTIOCTL_REL_PATH="${_CI_ISTIOCTL_REL_PATH:=}"
_CI_TRUSTED_GCP_PROJECTS="${_CI_TRUSTED_GCP_PROJECTS:=}"
_CI_ENVIRON_PROJECT_NUMBER="${_CI_ENVIRON_PROJECT_NUMBER:=}"
_CI_CRC_VERSION="${_CI_CRC_VERSION:=0}"
_CI_I_AM_A_TEST_ROBOT="${_CI_I_AM_A_TEST_ROBOT:=0}"
### Internal variables ###
MAJOR="${MAJOR:=1}"; readonly MAJOR;
MINOR="${MINOR:=9}"; readonly MINOR;
POINT="${POINT:=8}"; readonly POINT;
REV="${REV:=6}"; readonly REV;
CONFIG_VER="${CONFIG_VER:="1"}"; readonly CONFIG_VER;
K8S_MINOR=0
### File related constants ###
VALIDATION_FIX_FILE_NAME="istiod-service.yaml"
ASM_VERSION_FILE=".asm_version"
ISTIO_FOLDER_NAME=""
ISTIOCTL_REL_PATH=""
BASE_REL_PATH=""
PACKAGE_DIRECTORY=""
VALIDATION_FIX_SERVICE=""
OPTIONS_DIRECTORY=""
OPERATOR_MANIFEST=""
BETA_CRD_MANIFEST=""
MANAGED_MANIFEST=""
MANAGED_WEBHOOKS=""
EXPOSE_ISTIOD_SERVICE=""
CANONICAL_CONTROLLER_MANIFEST=""
SCRIPT_NAME="${0##*/}"
PROJECT_NUMBER=""
GKE_CLUSTER_URI=""
GCE_NETWORK_NAME=""
GCLOUD_USER_OR_SA=""
KPT_URL=""
KUBECONFIG=""
CONTEXT=""
APATH=""
AKUBECTL=""
AKPT=""
AGCLOUD=""
ABS_OVERLAYS=""
RELEASE=""
REVISION_LABEL=""
RELEASE_LINE=""
PREVIOUS_RELEASE_LINE=""
KPT_BRANCH=""
NAMESPACE_EXISTS=0
KUBECONFIG_SUPPLIED=0
### Option variables ###
PROJECT_ID="${PROJECT_ID:=}"
CLUSTER_NAME="${CLUSTER_NAME:=}"
CLUSTER_LOCATION="${CLUSTER_LOCATION:=}"
MODE="${MODE:=}"
CA="${CA:=}"
KUBECONFIG="${KUBECONFIG_FILE:=}"
CONTEXT="${CONTEXT:=}"
CUSTOM_OVERLAY=""
OPTIONAL_OVERLAY=""
ENABLE_ALL="${ENABLE_ALL:=0}"
ENABLE_CLUSTER_ROLES="${ENABLE_CLUSTER_ROLES:=0}"
ENABLE_CLUSTER_LABELS="${ENABLE_CLUSTER_LABELS:=0}"
ENABLE_GCP_APIS="${ENABLE_GCP_APIS:=0}"
ENABLE_GCP_IAM_ROLES="${ENABLE_GCP_IAM_ROLES:=0}"
ENABLE_GCP_COMPONENTS="${ENABLE_GCP_COMPONENTS:=0}"
ENABLE_REGISTRATION="${ENABLE_REGISTRATION:=0}"
DISABLE_CANONICAL_SERVICE="${DISABLE_CANONICAL_SERVICE:=0}"
PRINT_CONFIG="${PRINT_CONFIG:=0}"
SERVICE_ACCOUNT="${SERVICE_ACCOUNT:=}"
KEY_FILE="${KEY_FILE:=}"
OUTPUT_DIR="${OUTPUT_DIR:=}"
CA_CERT="${CA_CERT:=}"
CA_KEY="${CA_KEY:=}"
CA_ROOT="${CA_ROOT:=}"
CA_CHAIN="${CA_CHAIN:=}"
CA_NAME="${CA_NAME:=}"
DRY_RUN="${DRY_RUN:=0}"
ONLY_VALIDATE="${ONLY_VALIDATE:=0}"
ONLY_ENABLE="${ONLY_ENABLE:=0}"
VERBOSE="${VERBOSE:=0}"
MANAGED="${MANAGED:=0}"
PRINT_HELP=0
PRINT_VERSION=0
CUSTOM_CA=0
USE_HUB_WIP=0
USE_MCP_CNI=0
USE_VM=0
ENVIRON_PROJECT_ID=""
HUB_MEMBERSHIP_ID=""
WI_ENABLED=0
CUSTOM_REVISION=0
NETWORK_POLICY_ENABLED=0
HAS_TS=0
main() {
init
parse_args "${@}"
# make sure to redirect stdout as soon as possible if we're dumping the config
if [[ "${PRINT_CONFIG}" -eq 1 ]]; then
exec 3>&1
exec 1>&2
fi
validate_args
set_up_local_workspace
configure_kubectl
validate_cli_dependencies
if is_sa; then
auth_service_account
fi
if should_validate || can_modify_at_all; then
local_iam_user > /dev/null
validate_gcp_resources
fi
if needs_asm && needs_kpt; then
download_kpt
fi
if needs_asm && ! necessary_files_exist; then
download_asm
fi
if needs_asm; then
organize_kpt_files
fi
if should_validate && ! is_managed; then
validate_dependencies
fi
if can_modify_gcp_apis; then
enable_gcloud_apis
elif should_validate; then
exit_if_apis_not_enabled
fi
if can_register_cluster; then
register_cluster
elif should_validate && [[ "${USE_HUB_WIP}" -eq 1 ]]; then
exit_if_cluster_unregistered
fi
# Managed must be able to set IAM permissions on a generated user, so the flow
# is a bit different
if is_managed; then
if can_modify_gcp_components; then
enable_workload_identity
if ! is_stackdriver_enabled; then
enable_stackdriver_kubernetes
fi
if is_mcp_cni; then
enable_network_policy
fi
else
exit_if_no_workload_identity
exit_if_stackdriver_not_enabled
if is_mcp_cni; then
exit_if_no_network_policy
fi
fi
if can_modify_gcp_iam_roles; then
bind_user_to_iam_policy "roles/meshconfig.admin" "$(local_iam_user)"
fi
if can_modify_at_all; then
if ! init_meshconfig_managed; then
fatal "Couldn't initialize meshconfig, do you have the required permission resourcemanager.projects.setIamPolicy?"
fi
fi
else
if can_modify_gcp_iam_roles; then
bind_user_to_iam_policy "$(required_iam_roles)" "$(local_iam_user)"
elif should_validate; then
exit_if_out_of_iam_policy
fi
fi
get_project_number
if can_modify_cluster_labels; then
add_cluster_labels
elif should_validate; then
exit_if_cluster_unlabeled
fi
if ! is_managed; then
if can_modify_gcp_components; then
enable_workload_identity
init_meshconfig
if [[ "${CA}" = "gcp_cas" ]]; then
# This sets up IAM privileges for the project to be able to access GCP CAS.
# If modify_gcp_component permissions are not granted, it is assumed that the
# user has taken care of this, else Istio setup will fail
init_gcp_cas
fi
enable_stackdriver_kubernetes
if should_enable_service_mesh_feature; then
enable_service_mesh_feature
fi
elif should_validate; then
exit_if_no_workload_identity
exit_if_stackdriver_not_enabled
if should_enable_service_mesh_feature; then
exit_if_service_mesh_feature_not_enabled
fi
fi
fi
if can_modify_cluster_roles; then
bind_user_to_cluster_admin
elif should_validate; then
exit_if_not_cluster_admin
fi
if can_create_namespace; then
create_istio_namespace
elif should_validate; then
warn_if_istio_namespace_not_exists
fi
if [[ "${ONLY_VALIDATE}" -eq 1 ]]; then
info "Successfully validated all requirements to install ASM in this environment."
return 0
fi
if only_enable; then
info "Successfully performed specified --enable actions."
return 0
fi
if [[ "${PRINT_CONFIG}" -eq 1 && "${USE_HUB_WIP}" -eq 1 ]]; then
populate_environ_info
fi
if can_modify_at_all && [[ "${MANAGED}" -eq 1 ]]; then
start_managed_control_plane
configure_package
install_managed_components
outro
return 0
fi
if [[ "${USE_VM}" -eq 1 ]]; then
register_gce_identity_provider
fi
configure_package
handle_multi_yaml_bug
if [[ "${PRINT_CONFIG}" -eq 1 ]]; then
print_config >&3
return 0
fi
if [[ "${CUSTOM_CA}" -eq 1 ]]; then
install_secrets
fi
install_asm
info "Successfully installed ASM."
return 0
}
init() {
# BSD-style readlink apparently doesn't have the same -f toggle on readlink
case "$(uname)" in
Linux ) APATH="readlink";;
Darwin) APATH="stat";;
*);;
esac
if hash ts 2>/dev/null; then
HAS_TS=1
fi
if [[ "${POINT}" == "alpha" ]]; then
RELEASE="${MAJOR}.${MINOR}-alpha.${REV}"
REVISION_LABEL="${_CI_REVISION_PREFIX}asm-${MAJOR}${MINOR}${POINT}"
KPT_BRANCH="${_CI_ASM_KPT_BRANCH:=master}"
else
RELEASE="${MAJOR}.${MINOR}.${POINT}-asm.${REV}"
REVISION_LABEL="${_CI_REVISION_PREFIX}asm-${MAJOR}${MINOR}${POINT}-${REV}"
KPT_BRANCH="${_CI_ASM_KPT_BRANCH:=release-${MAJOR}.${MINOR}-asm}"
fi
RELEASE_LINE="${MAJOR}.${MINOR}."
PREVIOUS_RELEASE_LINE="${MAJOR}.$(( MINOR - 1 ))."
readonly RELEASE; readonly RELEASE_LINE; readonly PREVIOUS_RELEASE_LINE; readonly KPT_BRANCH;
ISTIO_FOLDER_NAME="istio-${RELEASE}"; readonly ISTIO_FOLDER_NAME;
ISTIOCTL_REL_PATH="${ISTIO_FOLDER_NAME}/bin/istioctl"; readonly ISTIOCTL_REL_PATH;
BASE_REL_PATH="${ISTIO_FOLDER_NAME}/manifests/charts/base/files/gen-istio-cluster.yaml"; readonly BASE_REL_PATH;
PACKAGE_DIRECTORY="asm/istio"; readonly PACKAGE_DIRECTORY;
VALIDATION_FIX_SERVICE="${PACKAGE_DIRECTORY}/${VALIDATION_FIX_FILE_NAME}"; readonly VALIDATION_FIX_SERVICE;
OPTIONS_DIRECTORY="${PACKAGE_DIRECTORY}/options"; readonly OPTIONS_DIRECTORY;
OPERATOR_MANIFEST="${PACKAGE_DIRECTORY}/istio-operator.yaml"; readonly OPERATOR_MANIFEST;
BETA_CRD_MANIFEST="${OPTIONS_DIRECTORY}/v1beta1-crds.yaml"; readonly BETA_CRD_MANIFEST;
MANAGED_MANIFEST="${OPTIONS_DIRECTORY}/managed-control-plane.yaml"; readonly MANAGED_MANIFEST;
MANAGED_WEBHOOKS="${OPTIONS_DIRECTORY}/managed-control-plane-webhooks.yaml"; readonly MANAGED_WEBHOOKS;
MANAGED_CNI="${OPTIONS_DIRECTORY}/cni-managed.yaml"; readonly MANAGED_CNI;
EXPOSE_ISTIOD_SERVICE="${PACKAGE_DIRECTORY}/expansion/expose-istiod.yaml"; readonly EXPOSE_ISTIOD_SERVICE;
CANONICAL_CONTROLLER_MANIFEST="asm/canonical-service/controller.yaml"; readonly CANONICAL_CONTROLLER_MANIFEST;
AKUBECTL="$(which kubectl || true)"
AGCLOUD="$(which gcloud || true)"
AKPT="$(which kpt || true)"
}
### Convenience functions ###
#######
# run takes a list of arguments that represents a command
# If DRY_RUN or VERBOSE is enabled, it will print the command, and if DRY_RUN is
# not enabled it runs the command.
#######
run() {
if [[ "${DRY_RUN}" -eq 1 ]]; then
warn "Would have executed: ${*}"
return
elif [[ "${VERBOSE}" -eq 0 ]]; then
"${@}" 2>/dev/null
return "$?"
fi
info "Running: '${*}'"
info "-------------"
local RETVAL
{ "${@}"; RETVAL="$?"; } || true
return $RETVAL
}
apath() {
"${APATH}" "${@}"
}
gcloud() {
run "${AGCLOUD}" "${@}"
}
kubectl() {
run "${AKUBECTL}" --kubeconfig "${KUBECONFIG}" "${@}"
}
kpt() {
run "${AKPT}" "${@}"
}
istioctl() {
run "$(istioctl_path)" "${@}"
}
istioctl_path() {
if [[ -n "${_CI_ISTIOCTL_REL_PATH}" && -f "${_CI_ISTIOCTL_REL_PATH}" ]]; then
echo "${_CI_ISTIOCTL_REL_PATH}"
else
echo "./${ISTIOCTL_REL_PATH}"
fi
}
#######
# retry takes an integer N as the first argument, and a list of arguments
# representing a command afterwards. It will retry the given command up to N
# times before returning 1. If the command is kubectl, it will try to
# re-get credentials in case something caused the k8s IP to change.
#######
retry() {
local MAX_TRIES; MAX_TRIES="${1}";
shift 1
for i in $(seq 0 "${MAX_TRIES}"); do
if [[ "${i}" -eq "${MAX_TRIES}" ]]; then
break
fi
{ "${@}" && return 0; } || true
warn "Failed, retrying...($((i+1)) of ${MAX_TRIES})"
sleep 2
done
local CMD="'$*'"
warn "Command $CMD failed."
false
}
find_missing_strings() {
local NEEDLES; NEEDLES="${1}";
local HAYSTACK; HAYSTACK="${2}";
local NOTFOUND; NOTFOUND="";
while read -r needle; do
EXITCODE=0
grep -q "${needle}" <<EOF || EXITCODE=$?
${HAYSTACK}
EOF
if [[ "${EXITCODE}" -ne 0 ]]; then
NOTFOUND="${needle},${NOTFOUND}"
fi
done <<EOF
${NEEDLES}
EOF
if [[ -n "${NOTFOUND}" ]]; then NOTFOUND="$(strip_trailing_commas "${NOTFOUND}")"; fi
echo "${NOTFOUND}"
}
strip_trailing_commas() {
# shellcheck disable=SC2001
echo "${1}" | sed 's/,*$//g'
}
configure_kubectl(){
if [[ "${KUBECONFIG_SUPPLIED}" -eq 0 ]]; then
info "Fetching/writing GCP credentials to kubeconfig file..."
KUBECONFIG="${KUBECONFIG}" retry 2 gcloud container clusters get-credentials "${CLUSTER_NAME}" \
--project="${PROJECT_ID}" \
--zone="${CLUSTER_LOCATION}"
fi
if ! hash nc 2>/dev/null; then
warn "nc not found, skipping k8s connection verification"
warn "(Installation will continue normally.)"
return
fi
info "Verifying connectivity (10s)..."
local ADDR
ADDR="$(kubectl config view --minify=true -ojson | \
jq .clusters[0].cluster.server -r)"
local RETVAL; RETVAL=0;
run nc -zvw 10 "${ADDR:8:${#ADDR}}" 443 || RETVAL=$?
if [[ "${RETVAL}" -ne 0 ]]; then
{ read -r -d '' MSG; fatal "${MSG}"; } <<EOF || true
Couldn't connect to ${CLUSTER_NAME}.
If this is a private cluster, verify that the correct firewall rules are applied.
https://cloud.google.com/service-mesh/docs/gke-install-overview#requirements
EOF
fi
info "kubeconfig set to ${KUBECONFIG}"
info "context set to $(kubectl config current-context)"
}
warn() {
info "[WARNING]: ${1}" >&2
}
warn_pause() {
warn "${1}"
sleep 2
}
error() {
info "[ERROR]: ${1}" >&2
}
info() {
if [[ "${VERBOSE}" -eq 1 && "${HAS_TS}" -eq 1 ]]; then
echo "${SCRIPT_NAME}: ${1}" | TZ=utc ts '%Y-%m-%dT%.T' >&2
else
echo "${SCRIPT_NAME}: ${1}" >&2
fi
}
fatal() {
error "${1}"
exit 2
}
fatal_with_usage() {
error "${1}"
usage_short >&2
exit 2
}
starline() {
echo "*****************************"
}
is_managed() {
if [[ "${MANAGED}" -ne 1 ]]; then false; fi
}
is_mcp_cni() {
if [[ "${USE_MCP_CNI}" -ne 1 ]]; then false; fi
}
is_sa() {
if [[ -z "${SERVICE_ACCOUNT}" ]]; then false; fi
}
should_validate() {
if [[ "${PRINT_CONFIG}" -eq 1 || "${_CI_NO_VALIDATE}" -eq 1 ]] || only_enable; then false; fi
}
only_enable() {
if [[ "${ONLY_ENABLE}" -eq 0 ]]; then false; fi
}
can_modify_at_all() {
if [[ "${ONLY_VALIDATE}" -eq 1 || "${PRINT_CONFIG}" -eq 1 ]]; then false; fi
}
can_modify_cluster_roles() {
if ! can_modify_at_all; then false; return; fi
if is_managed || [[ "${ENABLE_ALL}" -eq 1 || "${ENABLE_CLUSTER_ROLES}" -eq 1 ]]; then
true
else
false
fi
}
can_modify_cluster_labels() {
if ! can_modify_at_all; then false; return; fi
if [[ "${ENABLE_ALL}" -eq 1 || "${ENABLE_CLUSTER_LABELS}" -eq 1 ]]; then
true
else
false
fi
}
can_modify_gcp_apis() {
if ! can_modify_at_all; then false; return; fi
if [[ "${ENABLE_ALL}" -eq 1 || "${ENABLE_GCP_APIS}" -eq 1 ]]; then
true
else
false
fi
}
can_modify_gcp_iam_roles() {
if ! can_modify_at_all; then false; return; fi
if is_managed || [[ "${ENABLE_ALL}" -eq 1 || "${ENABLE_GCP_IAM_ROLES}" -eq 1 ]]; then
true
else
false
fi
}
can_modify_gcp_components() {
if ! can_modify_at_all; then false; return; fi
if is_managed || [[ "${ENABLE_ALL}" -eq 1 || "${ENABLE_GCP_COMPONENTS}" -eq 1 ]]; then
true
else
false
fi
}
can_register_cluster() {
if ! can_modify_at_all; then false; return; fi
if [[ "${ENABLE_ALL}" -eq 1 && ("${USE_VM}" -eq 1 || "${USE_HUB_WIP}" -eq 1) ]] \
|| [[ "${ENABLE_REGISTRATION}" -eq 1 ]]; then
true
else
false
fi
}
can_create_namespace() {
if ! can_modify_at_all; then false; return; fi
}
needs_asm() {
if only_enable; then false; return; fi
if [[ "${PRINT_CONFIG}" -eq 1 ]] || can_modify_at_all || should_validate; then
true
else
false
fi
}
needs_kpt() {
if [[ -z "${AKPT}" ]]; then return; fi
KPT_VER="$(kpt version)"
if [[ "${KPT_VER:0:1}" != "0" ]]; then return; fi
false
}
add_trust_domain_alias() {
if [[ "${TRUST_DOMAIN_ALIASES}" == *${1}* ]]; then
return
fi
TRUST_DOMAIN_ALIASES="${TRUST_DOMAIN_ALIASES} ${1}"
}
enable_common_message() {
echo "Alternatively, use --enable_all|-e to allow this tool to handle all dependencies."
}
version_message() {
local VER; VER="${MAJOR}.${MINOR}.${POINT}-asm.${REV}+config${CONFIG_VER}";
if [[ "${_CI_CRC_VERSION}" -eq 1 ]]; then
VER="${VER}-$(crc32 "$0")"
fi
echo "${VER}"
}
### CLI/initial setup functions ###
usage_short() {
cat << EOF
${SCRIPT_NAME} $(version_message)
usage: ${SCRIPT_NAME} [OPTION]...
Set up, validate, and install ASM in a Google Cloud environment.
Use -h|--help with -v|--verbose to show detailed descriptions.
OPTIONS:
-l|--cluster_location <LOCATION>
-n|--cluster_name <NAME>
-p|--project_id <ID>
--kc|--kubeconfig <KUBECONFIG_FILE>
--ctx|--context <CONTEXT>
-m|--mode <MODE>
-c|--ca <CA>
-o|--option <FILE NAME>
-s|--service_account <ACCOUNT>
-k|--key_file <FILE PATH>
-D|--output_dir <DIR PATH>
--co|--custom_overlay <FILE NAME>
--ca_cert <FILE PATH>
--ca_key <FILE PATH>
--root_cert <FILE PATH>
--cert_chain <FILE PATH>
--ca_name <CA NAME>
-r|--revision_name <REVISION NAME>
FLAGS:
-e|--enable_all
--enable_cluster_roles
--enable_cluster_labels
--enable_gcp_apis
--enable_gcp_iam_roles
--enable_gcp_components
--enable_registration
--managed
--print_config
--disable_canonical_service
-v|--verbose
--dry_run
--only_validate
--only_enable
-h|--help
--version
EOF
}
usage() {
cat << EOF
${SCRIPT_NAME} $(version_message)
usage: ${SCRIPT_NAME} [OPTION]...
Set up, validate, and install ASM in a Google Cloud environment.
Single argument options can also be passed via environment variables by using
the ALL_CAPS name. Options specified via flags take precedence over environment
variables.
OPTIONS:
-l|--cluster_location <LOCATION> The GCP location of the target cluster.
-n|--cluster_name <NAME> The name of the target cluster.
-p|--project_id <ID> The GCP project ID.
--kc|--kubeconfig <KUBECONFIG> Path to the kubeconfig file to use for CLI requests.
Required if not supplying --cluster_location,
--cluster_name, --project_id in order to locate
and connect to the intended cluster.
--ctx|--context <CONTEXT> The name of the kubeconfig context to use.
-m|--mode <MODE> The type of installation to perform.
Passing --mode install will attempt a
new ASM installation. Passing --mode
migrate will attempt to migrate an Istio
installation to ASM. Passing --mode
upgrade will attempt to upgrade an
existing ASM installation to a newer
version. Allowed values for
<MODE> are {install|migrate|upgrade}.
-c|--ca <CA> The type of certificate authority to be
used. Defaults to "mesh_ca" for --mode
install. Specifying the CA is required
for --mode migrate. Allowed values for
<CA> are {citadel|mesh_ca|gcp_cas}.
-o|--option <FILE NAME> The name of a YAML file in the kpt pkg to
apply. For options, see the
anthos-service-mesh-package GitHub
repo under GoogleCloudPlatform. Files
should be in "asm/istio/options" folder,
and shouldn't include the .yaml extension.
(See https://git.io/JTDdi for options.)
To add multiple files, specify them with
multiple options one at a time.
-s|--service_account <ACCOUNT> The name of a service account used to
install ASM. If not specified, the gcloud
user currently configured will be used.
-k|--key_file <FILE PATH> The key file for a service account. This
option can be omitted if not using a
service account.
-D|--output_dir <DIR PATH> The directory where this script will place
downloaded ASM packages and configuration.
If not specified, a temporary directory
will be created. If specified and the
directory already contains the necessary
files, they will be used instead of
downloading them again.
--co|--custom_overlay <FILE PATH> The location of a YAML file to overlay on
the ASM IstioOperator. This option can be
omitted if not installing optional
features. To add multiple files, specify
them with multiple options one at a time.
--ca_name <CA NAME> Required only if --ca option is gcp_cas.
Name of the ca in the GCP CAS service used to
sign certificates in the format
'projects/project_name/locations/ \
ca_region/certificateAuthorities/ca_name'.
-r|--revision_name <REVISION NAME> Custom revision label. Label needs to follow DNS
label formats (re: RFC 1123). Not supported if
control plane is managed. Prefixing the revision
name with 'asm' is recommended.
The following four options must be passed together and are only necessary
for using a custom certificate for Citadel. Users that aren't sure whether
they need this probably don't.
--ca_cert <FILE PATH> The intermediate certificate
--ca_key <FILE PATH> The key for the intermediate certificate
--root_cert <FILE PATH> The root certificate
--cert_chain <FILE PATH> The certificate chain
FLAGS:
The following several flags all relate to allowing the script to create, set,
or enable required APIs, roles, or services. These can all be performed
manually before running the script if desired. To allow the script to perform
every necessary action, pass the -e|--enable_all flag. All of these flags
are incompatible with --only_validate.
-e|--enable_all Allow the script to perform all of the
individual enable actions below. (Environ
registration won't happen unless necessary
for a selected option.)
--enable_cluster_roles Allow the script to attempt to set
the necessary cluster roles.
--enable_cluster_labels Allow the script to attempt to set
necessary cluster labels.
--enable_gcp_apis Allow the script to enable GCP APIs on
the user's behalf
--enable_gcp_iam_roles Allow the script to set the required GCP
IAM permissions
--enable_gcp_components Allow the script to enable required GCP
managed services and components
--enable_registration Allow the script to register the cluster
to an environ
--managed Provision a remote, managed control plane
instead of installing one in-cluster.
--print_config Instead of installing ASM, print all of
the compiled YAML to stdout. All other
output will be written to stderr, even if
it would normally go to stdout. Skip all
validations and setup.
--disable_canonical_service Do not install the CanonicalService
controller. This is required for ASM UI to
support various features.
-v|--verbose Print commands before and after execution.
--dry_run Print commands, but don't execute them.
--only_validate Run validation, but don't install.
--only_enable Perform the specified steps to set up the
current user/cluster but don't install
anything.
-h|--help Show this message and exit.
--version Print the version of this tool and exit.
EXAMPLE:
The following invocation will install ASM to a cluster named "my_cluster" in
project "my_project" in region "us-central1-c" using the default "mesh_ca" as
the certificate authority:
$> ${SCRIPT_NAME} \\
-n my_cluster \\
-p my_project \\
-l us-central1-c \\
-m install
or
$> ${SCRIPT_NAME} \\
--kubeconfig kubeconfig_file \\
--context kube context \\
-m install
EOF
}
arg_required() {
if [[ ! "${2:-}" || "${2:0:1}" = '-' ]]; then
fatal "Option ${1} requires an argument."
fi
}
parse_args() {
if [[ "${*}" = '' ]]; then
usage_short >&2
exit 2
fi
# shellcheck disable=SC2064
trap "$(shopt -p nocasematch)" RETURN
shopt -s nocasematch
while [[ $# != 0 ]]; do
case "${1}" in
-l | --cluster_location | --cluster-location)
arg_required "${@}"
CLUSTER_LOCATION="${2}"
shift 2
;;
-n | --cluster_name | --cluster-name)
arg_required "${@}"
CLUSTER_NAME="${2}"
shift 2
;;
--kc | --kubeconfig)
arg_required "${@}"
KUBECONFIG="${2}"
KUBECONFIG_SUPPLIED=1
shift 2
;;
--ctx | --context)
arg_required "${@}"
CONTEXT="${2}"
shift 2
;;
-p | --project_id | --project-id)
arg_required "${@}"
PROJECT_ID="${2}"
shift 2
;;
-m | --mode)
arg_required "${@}"
MODE="$(echo "${2}" | tr '[:upper:]' '[:lower:]')"
shift 2
;;
-c | --ca)
arg_required "${@}"
CA="$(echo "${2}" | tr '[:upper:]' '[:lower:]')"
shift 2
;;
--ca_name | --ca-name)
arg_required "${@}"
CA_NAME="${2}"
shift 2
;;
-o | --option)
arg_required "${@}"
if [[ "${2}" == "cni-managed" ]]; then
USE_MCP_CNI=1
shift 2
continue
fi
OPTIONAL_OVERLAY="${2},${OPTIONAL_OVERLAY}"
if [[ "${2}" == "hub-meshca" ]]; then
USE_HUB_WIP=1
fi
if [[ "${2}" == "vm" ]]; then
USE_VM=1
fi
shift 2
;;
--co | --custom_overlay | --custom-overlay)
arg_required "${@}"
CUSTOM_OVERLAY="${2},${CUSTOM_OVERLAY}"
shift 2
;;
-e | --enable_all | --enable-all)
ENABLE_ALL=1
shift 1
;;
--enable_cluster_roles | --enable-cluster-roles)
ENABLE_CLUSTER_ROLES=1
shift 1
;;
--enable_cluster_labels | --enable-cluster-labels)
ENABLE_CLUSTER_LABELS=1
shift 1
;;
--enable_gcp_apis | --enable-gcp-apis)
ENABLE_GCP_APIS=1
shift 1
;;
--enable_gcp_iam_roles | --enable-gcp-iam-roles)
ENABLE_GCP_IAM_ROLES=1
shift 1
;;
--enable_gcp_components | --enable-gcp-components)
ENABLE_GCP_COMPONENTS=1
shift 1
;;
--enable_registration | --enable-registration)
ENABLE_REGISTRATION=1
shift 1
;;
--managed)
MANAGED=1
REVISION_LABEL="asm-managed"
shift 1
;;
--disable_canonical_service | --disable-canonical-service)
DISABLE_CANONICAL_SERVICE=1
shift 1
;;
--print_config | --print-config)
PRINT_CONFIG=1
shift 1
;;
-s | --service_account | --service-account)
arg_required "${@}"
SERVICE_ACCOUNT="${2}"
shift 2
;;
-k | --key_file | --key-file)
arg_required "${@}"
KEY_FILE="${2}"
shift 2
;;
-D | --output_dir | --output-dir)
arg_required "${@}"
OUTPUT_DIR="${2}"
shift 2
;;
--dry_run | --dry-run)
DRY_RUN=1
shift 1
;;
--only_validate | --only-validate)
ONLY_VALIDATE=1
shift 1
;;
--only_enable | --only-enable)
ONLY_ENABLE=1
shift 1
;;
--ca_cert | --ca-cert)
arg_required "${@}"
CA_CERT="${2}"
CUSTOM_CA=1
shift 2
;;
--ca_key | --ca-key)
arg_required "${@}"
CA_KEY="${2}"
CUSTOM_CA=1
shift 2
;;
--root_cert | --root-cert)
arg_required "${@}"
CA_ROOT="${2}"