-
Notifications
You must be signed in to change notification settings - Fork 162
/
install_asm
executable file
·1510 lines (1338 loc) · 44 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
#!/bin/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
### Internal variables ###
MAJOR="${MAJOR:=1}"
MINOR="${MINOR:=7}"
POINT="${POINT:=8}"
REV="${REV:=10}"
RELEASE="${MAJOR}.${MINOR}.${POINT}-asm.${REV}"
RELEASE_LINE="${MAJOR}.${MINOR}."
PREVIOUS_RELEASE_LINE="${MAJOR}.$(( MINOR - 1 ))."
REVISION_LABEL="asm-${MAJOR}${MINOR}${POINT}-${REV}"
KPT_BRANCH="release-${MAJOR}.${MINOR}-asm"
readonly RELEASE; readonly RELEASE_LINE; readonly REVISION_LABEL; readonly KPT_BRANCH;
CONFIG_VER="${CONFIG_VER:="2"}"; readonly CONFIG_VER;
K8S_MINOR=0
### 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_PKG_LOCATION="${_CI_ASM_PKG_LOCATION:=}"
_CI_CRC_VERSION="${_CI_CRC_VERSION:=0}"
### File related constants ###
ISTIO_FOLDER_NAME="istio-${RELEASE}"; readonly ISTIO_FOLDER_NAME;
ISTIOCTL_REL_PATH="${ISTIO_FOLDER_NAME}/bin/istioctl"; readonly ISTIOCTL_REL_PATH;
PACKAGE_DIRECTORY="asm/istio"; readonly PACKAGE_DIRECTORY;
VALIDATION_FIX_SERVICE="${PACKAGE_DIRECTORY}/istiod-service.yaml"; readonly VALIDATION_FIX_SERVICE;
OPTIONS_DIRECTORY="${PACKAGE_DIRECTORY}/options"; readonly OPTIONS_DIRECTORY;
OPERATOR_MANIFEST="${PACKAGE_DIRECTORY}/istio-operator.yaml"; readonly OPERATOR_MANIFEST;
MULTICLUSTER_MANIFEST="${OPTIONS_DIRECTORY}/multicluster.yaml"; readonly MULTICLUSTER_MANIFEST;
BETA_CRD_MANIFEST="${OPTIONS_DIRECTORY}/v1beta1-crds.yaml"; readonly BETA_CRD_MANIFEST;
SCRIPT_NAME="${0##*/}"
PROJECT_NUMBER=""
KPT_URL=""
KUBECONFIG=""
APATH=""
AKPT="$(which kpt || true)"
ABS_OVERLAYS=""
### Option variables ###
PROJECT_ID="${PROJECT_ID:=}"
CLUSTER_NAME="${CLUSTER_NAME:=}"
CLUSTER_LOCATION="${CLUSTER_LOCATION:=}"
MODE="${MODE:=}"
CA="${CA:=}"
CUSTOM_OVERLAY=""
OPTIONAL_OVERLAY=""
ENABLE_APIS="${ENABLE_APIS:=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:=}"
CUSTOM_CA=0
CA_CERT="${CA_CERT:=}"
CA_KEY="${CA_KEY:=}"
CA_ROOT="${CA_ROOT:=}"
CA_CHAIN="${CA_CHAIN:=}"
DRY_RUN="${DRY_RUN:=0}"
ONLY_VALIDATE="${ONLY_VALIDATE:=0}"
VERBOSE="${VERBOSE:=0}"
PRINT_HELP=0
PRINT_VERSION=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
validate_dependencies
info "Successfully validated all requirements to install ASM from this computer."
if [[ "${ONLY_VALIDATE}" -ne 0 ]]; then
return 0
fi
set_up_project
set_up_cluster
configure_package
handle_multi_yaml_bug
if [[ "${PRINT_CONFIG}" -eq 1 ]]; then
print_config >&3
return 0
else
if [[ "${CUSTOM_CA}" -eq 1 ]]; then
install_secrets
fi
install_asm
info "Successfully installed ASM."
fi
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
}
apath() {
"${APATH}" "${@}"
}
kpt() {
"${AKPT}" "${@}"
}
### 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}" -ne 0 ]]; then
warn "Would have executed: ${*}"
return
elif [[ "${VERBOSE}" -eq 0 ]]; then
"${@}" 2>/dev/null
return "$?"
fi
warn "Running: '${*}'"
warn "-------------"
local RETVAL;
{ "${@}"; RETVAL="$?"; } || true
return $RETVAL
}
#######
# 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
return 1
fi
{ "${@}" && return 0; } || true
warn "Failed, retrying...($((i+1)) of ${MAX_TRIES})"
sleep 2
if [[ "$1" == "kubectl" ]]; then
configure_kubectl
fi
done
return 1
}
configure_kubectl(){
info "Fetching/writing GCP credentials to kubeconfig file..."
retry 2 run gcloud container clusters get-credentials "${CLUSTER_NAME}" \
--project="${PROJECT_ID}" \
--zone="${CLUSTER_LOCATION}"
info "Verifying connectivity (20s)..."
local RETVAL; RETVAL=0;
kubectl cluster-info --request-timeout='20s' 1>/dev/null 2>/dev/null || 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 ${PROJECT_ID}/${CLUSTER_LOCATION}/${CLUSTER_NAME}..."
}
warn() {
info "[WARNING]: ${1}" >&2
}
warn_pause() {
warn "${1}"
sleep 2
}
error() {
info "[ERROR]: ${1}" >&2
}
info() {
echo "${SCRIPT_NAME}: ${1}"
}
fatal() {
warn "ERROR: ${1}"
exit 2
}
fatal_with_usage() {
warn "${1}"
usage_short >&2
exit 2
}
is_sa() {
[[ -n "${SERVICE_ACCOUNT}" ]] && return 0
return 1
}
needs_asm() {
if [[ "${PRINT_CONFIG}" -eq 0 ]] && ! can_modify && ! should_validate; then false; fi
}
needs_kpt() {
if [[ -z "${AKPT}" ]]; then return; fi
KPT_VER="$(kpt version)"
if [[ "${KPT_VER:0:1}" != "0" ]]; then return; fi
false
}
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>
-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>
FLAGS:
-e|--enable_apis
--print_config
--disable_canonical_service
-v|--verbose
--dry_run
--only_validate
-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.
-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}.
-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.
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:
-e|--enable_apis Allow this script to enable necessary APIs
on your behalf. Without this flag, it will
abort if APIs are not already enabled.
--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.
-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
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
;;
-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
;;
-o | --option)
arg_required "${@}"
OPTIONAL_OVERLAY="${2},${OPTIONAL_OVERLAY}"
shift 2
;;
--co | --custom_overlay | --custom-overlay)
arg_required "${@}"
CUSTOM_OVERLAY="${2},${CUSTOM_OVERLAY}"
shift 2
;;
-e | --enable_apis | --enable-apis)
ENABLE_APIS=1
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
;;
--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}"
CUSTOM_CA=1
shift 2
;;
--cert_chain | --cert-chain)
arg_required "${@}"
CA_CHAIN="${2}"
CUSTOM_CA=1
shift 2
;;
-v | --verbose)
VERBOSE=1
shift 1
;;
-h | --help)
PRINT_HELP=1
shift 1
;;
--version)
PRINT_VERSION=1
shift 1
;;
*)
fatal_with_usage "Unknown option ${1}"
;;
esac
done
if [[ "${PRINT_HELP}" -eq 1 || "${PRINT_VERSION}" -eq 1 ]]; then
if [[ "${PRINT_VERSION}" -eq 1 ]]; then
version_message
elif [[ "${VERBOSE}" -eq 1 ]]; then
usage
else
usage_short
fi
exit
fi
}
validate_args() {
if [[ "${MODE}" == "install" && -z "${CA}" ]]; then
CA="mesh_ca"
fi
local MISSING_ARGS=0
while read -r REQUIRED_ARG; do
if [[ -z "${!REQUIRED_ARG}" ]]; then
MISSING_ARGS=1
warn "Missing value for ${REQUIRED_ARG}"
fi
readonly "${REQUIRED_ARG}"
done <<EOF
CLUSTER_LOCATION
CLUSTER_NAME
PROJECT_ID
MODE
EOF
if [[ "${MODE}" != "upgrade" ]]; then
case "${CA}" in
citadel | mesh_ca);;
"")
MISSING_ARGS=1
warn "Missing value for CA"
;;
*) fatal "CA must be one of 'citadel', 'mesh_ca'";;
esac
fi
if [[ "${MISSING_ARGS}" -ne 0 ]]; then
fatal_with_usage "Missing one or more required options."
fi
if [[ "${MODE}" == "upgrade" && -n "${CA}" ]]; then
warn "The CA flag is ignored during an update operation."
warn "Update will proceed with the currently installed CA."
fi
# shellcheck disable=SC2064
case "${MODE}" in
install | migrate | upgrade);;
*) fatal "MODE must be one of 'install', 'migrate', 'upgrade'";;
esac
if [[ "${CUSTOM_CA}" -eq 1 ]]; then
validate_certs
fi
while read -r FLAG; do
if [[ "${!FLAG}" -ne 0 && "${!FLAG}" -ne 1 ]]; then
fatal "${FLAG} must be 0 (off) or 1 (on) if set via environment variables."
fi
readonly "${FLAG}"
done <<EOF
DRY_RUN
ENABLE_APIS
DISABLE_CANONICAL_SERVICE
ONLY_VALIDATE
VERBOSE
EOF
if [[ -n "$SERVICE_ACCOUNT" && -z "$KEY_FILE" || -z "$SERVICE_ACCOUNT" && -n "$KEY_FILE" ]]; then
fatal "Service account and key file must be used together."
fi
# since we cd to a tmp directory, we need the absolute path for the key file
# and yaml file
if [[ -f "${KEY_FILE}" ]]; then
KEY_FILE="$(apath -f "${KEY_FILE}")"
readonly KEY_FILE
elif [[ -n "${KEY_FILE}" ]]; then
fatal "Couldn't find key file ${KEY_FILE}."
fi
while read -d ',' -r yaml_file; do
if [[ -f "${yaml_file}" ]]; then
ABS_OVERLAYS="$(apath -f "${yaml_file}"),${ABS_OVERLAYS}"
elif [[ -n "${yaml_file}" ]]; then
fatal "Couldn't find yaml file ${yaml_file}."
fi
done <<EOF
${CUSTOM_OVERLAY}
EOF
if [[ "${CA}" = "citadel" ]]; then
ABS_OVERLAYS="${OPTIONS_DIRECTORY}/citadel-ca.yaml,${ABS_OVERLAYS}"
fi
CUSTOM_OVERLAY="${ABS_OVERLAYS}"
set_kpt_package_url
WORKLOAD_POOL="${PROJECT_ID}.svc.id.goog"
}
validate_certs() {
if [[ "${CA}" != "citadel" ]]; then
fatal "You must select Citadel as the CA in order to use custom certificates."
fi
if [[ -z "${CA_ROOT}" || -z "${CA_KEY}" || -z "${CA_CHAIN}" || -z "${CA_CERT}" ]]; then
fatal "All four certificate options must be present to use a custom cert."
fi
while read -r CERT_FILE; do
if ! [[ -f "${!CERT_FILE}" ]]; then
fatal "Couldn't find file ${!CERT_FILE}."
fi
done <<EOF
CA_CERT
CA_ROOT
CA_KEY
CA_CHAIN
EOF
CA_CERT="$(apath -f "${CA_CERT}")"; readonly CA_CERT;
CA_KEY="$(apath -f "${CA_KEY}")"; readonly CA_KEY;
CA_CHAIN="$(apath -f "${CA_CHAIN}")"; readonly CA_CHAIN;
CA_ROOT="$(apath -f "${CA_ROOT}")"; readonly CA_ROOT;
info "Checking certificate files for consistency..."
if ! openssl rsa -in "${CA_KEY}" -check >/dev/null 2>/dev/null; then
fatal "${CA_KEY} failed an openssl consistency check."
fi
if ! openssl x509 -in "${CA_CERT}" -text -noout >/dev/null; then
fatal "${CA_CERT} failed an openssl consistency check."
fi
if ! openssl x509 -in "${CA_CHAIN}" -text -noout >/dev/null; then
fatal "${CA_CHAIN} failed an openssl consistency check."
fi
if ! openssl x509 -in "${CA_ROOT}" -text -noout >/dev/null; then
fatal "${CA_ROOT} failed an openssl consistency check."
fi
info "Checking key matches certificate..."
local CERT_HASH; local KEY_HASH;
CERT_HASH="$(openssl x509 -noout -modulus -in "${CA_CERT}" | openssl md5)";
KEY_HASH="$(openssl rsa -noout -modulus -in "${CA_KEY}" | openssl md5)";
if [[ "${CERT_HASH}" != "${KEY_HASH}" ]]; then
fatal "Keyfile does not match the given certificate."
fatal "Cert: ${CA_CERT}"
fatal "Key: ${CA_KEY}"
fi
unset CERT_HASH; unset KEY_HASH;
info "Verifying certificate chain of trust..."
if ! openssl verify -trusted "${CA_ROOT}" -untrusted "${CA_CHAIN}" "${CA_CERT}"; then
fatal "Unable to verify chain of trust."
fi
}
set_kpt_package_url() {
KPT_URL="https://github.com/GoogleCloudPlatform/anthos-service-mesh-packages"
KPT_URL="${KPT_URL}.git/asm@${KPT_BRANCH}"
readonly KPT_URL;
}
auth_service_account() {
info "Authorizing ${SERVICE_ACCOUNT} with ${KEY_FILE}..."
run gcloud auth activate-service-account \
--project="${PROJECT_ID}" \
"${SERVICE_ACCOUNT}" \
--key-file="${KEY_FILE}"
}
#######
# set_up_local_workspace does everything that the script needs to avoid
# polluting the environment or current working directory
#######
set_up_local_workspace() {
info "Setting up necessary files..."
if [[ -z "${OUTPUT_DIR}" ]]; then
info "Creating temp directory..."
OUTPUT_DIR="$(mktemp -d)"
if [[ -z "${OUTPUT_DIR}" ]]; then
fatal "Encountered error when running mktemp -d!"
fi
else
OUTPUT_DIR="$(apath -f "${OUTPUT_DIR}")"
fi
if [[ -x "${OUTPUT_DIR}/kpt" ]]; then AKPT="$(apath -f "${OUTPUT_DIR}/kpt")"; fi
pushd "$OUTPUT_DIR" > /dev/null
info "Generating a new kubeconfig..."
KUBECONFIG="$(mktemp)"
if [[ -z "${KUBECONFIG}" ]]; then
fatal "Encountered error when running mktemp!"
fi
export KUBECONFIG
}
### Environment validation functions ###
validate_dependencies() {
validate_cli_dependencies
if [[ "${PRINT_CONFIG}" -eq 1 ]]; then
return 0
fi
validate_gcp_resources
# configure kubectl does have side effects but we've generated a temprorary
# kubeconfig so we're not breaking the promise that --only_validate gives
configure_kubectl
validate_k8s
validate_expected_control_plane
if [[ "${MODE}" = "migrate" ]]; then
validate_istio_version
elif [[ "${MODE}" = "upgrade" ]]; then
validate_asm_version
validate_ca_consistency
fi
if [[ "${ENABLE_APIS}" -eq 0 || "${ONLY_VALIDATE}" -eq 1 ]]; then
exit_if_apis_not_enabled
fi
}
# This is a workaround for https://github.com/istio/istio/issues/30632
# which doesn't handle files with multiple operator specs correctly.
# This will split all of the files based off of the yaml document separator.
handle_multi_yaml_bug() {
local CSPLIT_OUTPUT; CSPLIT_OUTPUT="";
local BASE_NAME
while read -d ',' -r yaml_file; do
BASE_NAME="$(basename "${yaml_file}")"
if [[ "$(csplit -f "overlay-${BASE_NAME}" "${yaml_file}" '/^---$/' '{*}' | wc -l)" -eq 1 ]]; then
CSPLIT_OUTPUT="${CSPLIT_OUTPUT},${yaml_file}"
else
for split_file in overlay-"${BASE_NAME}"*; do
if [[ -s "${split_file}" ]]; then
CSPLIT_OUTPUT="${CSPLIT_OUTPUT},$(apath -f "${split_file}")"
fi
done
fi
done <<EOF
${CUSTOM_OVERLAY}
EOF
if [[ -n "${CSPLIT_OUTPUT}" ]]; then
CUSTOM_OVERLAY="${CSPLIT_OUTPUT:1:${#CSPLIT_OUTPUT}},"
fi
}
validate_cli_dependencies() {
local NOTFOUND; NOTFOUND="";
local EXITCODE; EXITCODE=0;
info "Checking installation tool dependencies..."
while read -r dependency; do
EXITCODE=0
hash "${dependency}" 2>/dev/null || EXITCODE=$?
if [[ "${EXITCODE}" -ne 0 ]]; then
NOTFOUND="${dependency},${NOTFOUND}"
fi
done <<EOF
awk
gcloud
grep
jq
kubectl
sed
tr
head
csplit
EOF
if [[ "${CUSTOM_CA}" -eq 1 ]]; then
EXITCODE=0
hash openssl 2>/dev/null || EXITCODE=$?
if [[ "${EXITCODE}" -ne 0 ]]; then
NOTFOUND="openssl,${NOTFOUND}"
fi
fi
if [[ -n "${NOTFOUND}" ]]; then
NOTFOUND="${NOTFOUND::-1}"
for dep in $(echo "${NOTFOUND}" | tr ' ' '\n'); do
warn "Dependency not found: ${dep}"
done
fatal "One or more dependencies were not found. Please install them and retry."
fi
local OS
# shellcheck disable=SC2064
trap "$(shopt -p nocasematch)" RETURN
shopt -s nocasematch
if [[ "$(uname -m)" != "x86_64" ]]; then
fatal "Installation is only supported on x86_64."
fi
case "$(uname)" in
Linux ) OS="linux-amd64";;
Darwin) OS="osx";;
* ) fatal "$(uname) is not a supported OS.";;
esac
if is_sa; then
auth_service_account
fi
if ! necessary_files_exist; then
info "Downloading ASM.."
download_asm "${OS}"
if needs_kpt; then
download_kpt
fi
info "Downloading ASM kpt package..."
retry 3 run kpt pkg get --auto-set=false "${KPT_URL}" asm
fi
local ABS_YAML
while read -d ',' -r yaml_file; do
ABS_YAML="${OPTIONS_DIRECTORY}/${yaml_file}.yaml"
if [[ ! -f "${ABS_YAML}" ]]; then
{ read -r -d '' MSG; fatal "${MSG}"; } <<EOF || true
Couldn't find remote yaml file ${yaml_file}.
See directory $(apath -f "${OPTIONS_DIRECTORY}") for available options.
EOF
fi
CUSTOM_OVERLAY="${ABS_YAML},${CUSTOM_OVERLAY}"
done <<EOF
${OPTIONAL_OVERLAY}
EOF
unset OPTIONAL_OVERLAY
}
download_kpt() {
local OS
case "$(uname)" in
Linux ) OS="linux_amd64";;
Darwin) OS="darwin_arm64";;
* ) fatal "$(uname) is not a supported OS.";;
esac
local KPT_TGZ
KPT_TGZ="https://github.com/GoogleContainerTools/kpt/releases/download/v0.39.3/kpt_${OS}-0.39.3.tar.gz"
info "Downloading kpt.."
curl -L "${KPT_TGZ}" | tar xz
AKPT="$(apath -f kpt)"
}
download_asm() {
local OS; OS="${1}";
local TARBALL; TARBALL="istio-${RELEASE}-${OS}.tar.gz"
if [[ -z "${_CI_ASM_PKG_LOCATION}" ]]; then
curl -L "https://storage.googleapis.com/gke-release/asm/${TARBALL}" \
| tar xz
else
local TOKEN; TOKEN="$(retry 2 gcloud --project="${PROJECT_ID}" auth print-access-token)"
run curl -L "https://storage.googleapis.com/${_CI_ASM_PKG_LOCATION}/asm/${TARBALL}" \
--header @- <<EOF | tar xz
Authorization: Bearer ${TOKEN}
EOF
fi
}
necessary_files_exist() {
if [[ ! -f "${OUTPUT_DIR}/${ISTIOCTL_REL_PATH}" ]]; then
return 1
fi
if [[ ! -f "${OUTPUT_DIR}/${OPERATOR_MANIFEST}" ]]; then
return 1
fi
return 0
}
validate_gcp_resources() {
validate_project
PROJECT_NUMBER="$(gcloud projects describe "${PROJECT_ID}" --format="value(projectNumber)")"
validate_cluster
validate_node_pool
}
validate_project() {
local RESULT; RESULT=""
info "Checking for project ${PROJECT_ID}..."
RESULT=$(gcloud projects list \
--filter="project_id=${PROJECT_ID}" \
--format="value(project_id)" || true)
if [[ -z "${RESULT}" ]]; then
{ read -r -d '' MSG; fatal "${MSG}"; } <<EOF || true
Unable to find project ${PROJECT_ID}. Please verify the spelling and try
again. To see a list of your projects, run:
gcloud projects list --format='value(project_id)'
EOF
fi
}
validate_cluster() {
local RESULT; RESULT=""
info "Confirming cluster information for ${PROJECT_ID}/${CLUSTER_LOCATION}/${CLUSTER_NAME}..."
RESULT="$(gcloud container clusters list \
--project="${PROJECT_ID}" \
--filter="name = ${CLUSTER_NAME} and location = ${CLUSTER_LOCATION}" \
--format="value(name)" || true)"
if [[ -z "${RESULT}" ]]; then
{ read -r -d '' MSG; fatal "${MSG}"; } <<EOF || true
Unable to find cluster ${CLUSTER_LOCATION}/${CLUSTER_NAME}.
Please verify the spelling and try again. To see a list of your clusters, in
this project, run:
gcloud container clusters list --format='value(name,zone)' --project="${PROJECT_ID}"
EOF
fi
}
validate_k8s() {
K8S_MINOR="$(kubectl version -o json | jq .serverVersion.minor | sed 's/[^0-9]//g')"
if [[ "${K8S_MINOR}" -lt 15 ]]; then
fatal "ASM ${RELEASE} requires Kubernetes version 1.15+, found 1.${K8S_MINOR}"
fi
}
list_valid_pools() {
gcloud container node-pools list \
--project="${PROJECT_ID}" \
--region "${CLUSTER_LOCATION}" \
--cluster "${CLUSTER_NAME}" \
--filter "$(valid_pool_query "${1}")"\
--format=json
}
#######
# valid_pool_query takes an integer argument: the minimum vCPU requirement.
# It outputs to stdout a query for `gcloud container node-pools list`
#######
valid_pool_query() {
cat <<EOF | tr '\n' ' '
config.machineType.split(sep="-").slice(-1:) >= $1
EOF
}
#######
# validate_node_pool makes sure that the cluster meets ASM's minimum compute
# requirements
#######
validate_node_pool() {
local MACHINE_CPU_REQ; MACHINE_CPU_REQ=4; readonly MACHINE_CPU_REQ;
local TOTAL_CPU_REQ; TOTAL_CPU_REQ=8; readonly TOTAL_CPU_REQ;
info "Confirming node pool requirements for ${PROJECT_ID}/${CLUSTER_LOCATION}/${CLUSTER_NAME}..."
local ACTUAL_CPU
ACTUAL_CPU="$(list_valid_pools "${MACHINE_CPU_REQ}" | \
jq '.[] |
(if .autoscaling.enabled then .autoscaling.maxNodeCount else .initialNodeCount end)
*
(.config.machineType / "-" | .[-1] | tonumber)
' 2>/dev/null)" || true
local MAX_CPU; MAX_CPU=0;
for i in ${ACTUAL_CPU}; do
MAX_CPU="$(( i > MAX_CPU ? i : MAX_CPU))"
done
if [[ "$MAX_CPU" -lt "$TOTAL_CPU_REQ" ]]; then
{ read -r -d '' MSG; warn_pause "${MSG}"; } <<EOF || true
ASM requires you to have at least ${TOTAL_CPU_REQ} vCPUs in node pools whose
machine type is at least ${MACHINE_CPU_REQ} vCPUs.
${CLUSTER_LOCATION}/${CLUSTER_NAME} does not meet this requirement. ASM
may not function as expected.
EOF
fi
}
validate_expected_control_plane(){
info "Checking Istio installations..."
check_no_istiod_outside_of_istio_system_namespace
if [[ "${MODE}" = "migrate" || "${MODE}" = "upgrade" ]]; then
check_istio_deployed