forked from apigee/devrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteps.sh
executable file
Β·876 lines (727 loc) Β· 31.9 KB
/
steps.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
#!/bin/bash
# Copyright 2020 Google LLC
#
# 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.
set_config_params() {
echo "π Setting Config Parameters (Provide your own or defaults will be applied)"
echo "π§ Configuring GCP Project"
PROJECT_ID=${PROJECT_ID:-$(gcloud config get-value "project")}
export PROJECT_ID
echo "- Project ID $PROJECT_ID"
gcloud config set project "$PROJECT_ID"
export AX_REGION=${AX_REGION:-'europe-west1'}
echo "- Analytics Region $AX_REGION"
export REGION=${REGION:-'europe-west1'}
gcloud config set compute/region "$REGION"
export ZONE=${ZONE:-'europe-west1-b,europe-west1-c,europe-west1-d'}
echo "- Compute Location $REGION/$ZONE"
export NETWORK=${NETWORK:-'apigee-hybrid'}
export SUBNET=${SUBNET:-"apigee-$REGION"}
export SUBNET_RANGE=${SUBNET_RANGE:-"10.200.0.0/20"}
echo "- Network $NETWORK/$SUBNET - $SUBNET_RANGE"
export PRIVATE_CLUSTER=${PRIVATE_CLUSTER:-'true'}
echo "- Private Cluster $PRIVATE_CLUSTER"
export CONTROL_PLANE_CIDR=${CONTROL_PLANE_CIDR:-'172.16.0.16/28'}
echo "- GKE control plane CIDR (private cluster only) $CONTROL_PLANE_CIDR"
printf "\nπ§ Apigee hybrid Configuration:\n"
export ENV_NAME=${ENV_NAME:="test1"}
echo "- Environment Name $ENV_NAME"
export ENV_GROUP_NAME=${ENV_GROUP_NAME:="test"}
echo "- Environment Group Name $ENV_GROUP_NAME"
export INGRESS_IP_NAME=${INGRESS_IP_NAME:-'ingress-ip'} # internal|external
echo "- Ingress address name $INGRESS_IP_NAME"
export INGRESS_TYPE=${INGRESS_TYPE:-'external'} # internal|external
echo "- Ingress type $INGRESS_TYPE"
export CERT_TYPE=${CERT_TYPE:-google-managed}
echo "- TLS Certificate $CERT_TYPE"
if [ "$CERT_TYPE" == "google-managed" ] && [ "$INGRESS_TYPE" != "external" ]; then
echo "Google Managed Certificates can only be used with an external ingress. SET CERT_TYPE to 'self-signed' (for the script to create one for you) or 'skip' if you want to provide your own as a k8s secret later."
exit 1
fi
export GKE_CLUSTER_NAME=${GKE_CLUSTER_NAME:-apigee-hybrid}
export GKE_CLUSTER_MACHINE_TYPE=${GKE_CLUSTER_MACHINE_TYPE:-e2-standard-4}
echo "- GKE Node Type $GKE_CLUSTER_MACHINE_TYPE"
export APIGEE_HYBRID_VERSION='1.7.1'
echo "- Apigee hybrid version $APIGEE_HYBRID_VERSION"
export KPT_VERSION='v0.34.0'
echo "- kpt version $KPT_VERSION"
export CERT_MANAGER_VERSION='v1.7.2'
echo "- Cert Manager version $CERT_MANAGER_VERSION"
export ASM_VERSION='1.12'
echo "- ASM version $ASM_VERSION"
OS_NAME=$(uname -s)
if [[ "$OS_NAME" == "Linux" ]]; then
echo "- π§ Using Linux binaries"
export APIGEE_CTL='apigeectl_linux_64.tar.gz'
export JQ_VERSION='jq-1.6/jq-linux64'
export KPT_BINARY='kpt_linux_amd64-0.34.0.tar.gz'
elif [[ "$OS_NAME" == "Darwin" ]]; then
echo "- π Using macOS binaries"
export APIGEE_CTL='apigeectl_mac_64.tar.gz'
export JQ_VERSION='jq-1.6/jq-osx-amd64'
export KPT_BINARY='kpt_darwin_amd64-0.34.0.tar.gz'
if ! [ -x "$(command -v timeout)" ]; then
echo "Please install the timeout command for macOS. E.g. 'brew install coreutils'"
exit 2
fi
else
echo "π£ Only Linux and macOS are supported at this time. You seem to be running on $OS_NAME."
exit 2
fi
printf "\nπ§ Derived config parameters\n"
echo "- GCP Project $PROJECT_ID"
PROJECT_NUMBER=$(gcloud projects describe "${PROJECT_ID}" --format="value(projectNumber)")
export PROJECT_NUMBER
export WORKLOAD_POOL="${PROJECT_ID}.svc.id.goog"
echo "- Workload Pool $WORKLOAD_POOL"
export MESH_ID="proj-${PROJECT_NUMBER}"
echo "- Mesh ID $MESH_ID"
# these will be set if the steps are run in order
INGRESS_IP=$(gcloud compute addresses list --format json --filter "name=$INGRESS_IP_NAME" --format="get(address)" || echo "")
export INGRESS_IP
echo "- Ingress IP ${INGRESS_IP:-N/A}"
NAME_SERVER=$(gcloud dns managed-zones describe apigee-dns-zone --format="json" --format="get(nameServers[0])" 2>/dev/null || echo "")
export NAME_SERVER
echo "- Nameserver ${NAME_SERVER:-N/A}"
export QUICKSTART_ROOT="${QUICKSTART_ROOT:-$PWD}"
export QUICKSTART_TOOLS="$QUICKSTART_ROOT/tools"
export APIGEECTL_HOME=$QUICKSTART_TOOLS/apigeectl/apigeectl_$APIGEE_HYBRID_VERSION
export HYBRID_HOME=$QUICKSTART_ROOT/hybrid-files
echo "- Script root from: $QUICKSTART_ROOT"
}
token() { echo -n "$(gcloud config config-helper --force-auth-refresh | grep access_token | grep -o -E '[^ ]+$')" ; }
function wait_for_ready(){
local expected_output=$1
local action=$2
local message=$3
local max_iterations=120 # 10min
local iterations=0
local actual_out
# shellcheck disable=SC2001
sanitized="$(echo "$action" | sed 's/Bearer [^\"]*/TOKEN/gi')"
echo -e "Waiting for $sanitized to return output $expected_output"
echo -e "Start: $(date)\n"
while true; do
iterations="$((iterations+1))"
actual_out=$(bash -c "$action" || echo "error code $?")
if [ "$expected_output" = "$actual_out" ]; then
echo -e "\n$message"
break
fi
if [ "$iterations" -ge "$max_iterations" ]; then
echo "Wait timed out"
exit 1
fi
echo -n "."
sleep 5
done
}
ask_confirm() {
if [ ! "$QUIET_INSTALL" = "true" ]; then
printf "\n\n"
read -p "Do you want to continue with the config above? [Y/n]: " -n 1 -r REPLY; printf "\n"
REPLY=${REPLY:-Y}
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
echo "continuing"
else
exit 1
fi
fi
}
check_existing_apigee_resource() {
RESOURCE_URI=$1
echo "π€ Checking if the Apigee resource '$RESOURCE_URI' already exists".
RESPONSE=$(curl -H "Authorization: Bearer $(token)" --silent "$RESOURCE_URI")
if [[ $RESPONSE == *"error"* ]]; then
echo "π€·ββοΈ Apigee resource '$RESOURCE_URI' does not exist yet"
return 1
else
echo "π Apigee resource '$RESOURCE_URI' already exists"
return 0
fi
}
enable_all_apis() {
PROJECT_ID=${PROJECT_ID:-$(gcloud config get-value "project")}
echo "π Enabling all required APIs in GCP project \"$PROJECT_ID\""
echo -n "β³ Waiting for APIs to be enabled"
gcloud services enable \
anthos.googleapis.com \
apigee.googleapis.com \
apigeeconnect.googleapis.com \
cloudresourcemanager.googleapis.com \
cloudtrace.googleapis.com \
compute.googleapis.com \
container.googleapis.com \
dns.googleapis.com \
gkeconnect.googleapis.com \
gkehub.googleapis.com \
iamcredentials.googleapis.com \
logging.googleapis.com \
meshca.googleapis.com \
meshconfig.googleapis.com \
meshtelemetry.googleapis.com \
monitoring.googleapis.com \
pubsub.googleapis.com \
stackdriver.googleapis.com \
--project "$PROJECT_ID"
}
create_apigee_org() {
echo "π Create Apigee ORG - $PROJECT_ID"
if check_existing_apigee_resource "https://apigee.googleapis.com/v1/organizations/$PROJECT_ID" ; then
echo "(skipping org creation, already exists)"
return 0
fi
curl -X POST --fail -H "Authorization: Bearer $(token)" -H "content-type:application/json" \
-d "{
\"name\":\"$PROJECT_ID\",
\"displayName\":\"$PROJECT_ID\",
\"description\":\"Apigee hybrid Org\",
\"analyticsRegion\":\"$AX_REGION\",
\"runtimeType\":\"HYBRID\",
\"properties\" : {
\"property\" : [ {
\"name\" : \"features.hybrid.enabled\",
\"value\" : \"true\"
}, {
\"name\" : \"features.mart.connect.enabled\",
\"value\" : \"true\"
} ]
}
}" \
"https://apigee.googleapis.com/v1/organizations?parent=projects/$PROJECT_ID"
echo -n "β³ Waiting for Apigeectl Org Creation "
wait_for_ready "0" "curl --silent -H \"Authorization: Bearer $(token)\" -H \"Content-Type: application/json\" https://apigee.googleapis.com/v1/organizations/$PROJECT_ID | grep -q \"subscriptionType\"; echo \$?" "Organization $PROJECT_ID is created."
echo "β
Created Org '$PROJECT_ID'"
}
create_apigee_env() {
ENV_NAME=$1
echo "π Create Apigee Env - $ENV_NAME"
if check_existing_apigee_resource "https://apigee.googleapis.com/v1/organizations/$PROJECT_ID/environments/$ENV_NAME"; then
echo "(skipping, env already exists)"
return
fi
curl -X POST --fail -H "Authorization: Bearer $(token)" -H "content-type:application/json" \
-d "{\"name\":\"$ENV_NAME\"}" \
"https://apigee.googleapis.com/v1/organizations/$PROJECT_ID/environments"
echo -n "β³ Waiting for Apigeectl Env Creation "
wait_for_ready "0" "curl --silent -H \"Authorization: Bearer $(token)\" -H \"Content-Type: application/json\" https://apigee.googleapis.com/v1/organizations/$PROJECT_ID/environments/$ENV_NAME | grep -q \"$ENV_NAME\"; echo \$?" "Environment $ENV_NAME of Organization $PROJECT_ID is created."
echo "β
Created Env '$ENV_NAME'"
}
create_apigee_envgroup() {
ENV_GROUP_NAME=$1
echo "π Create Apigee Env Group - $ENV_GROUP_NAME"
if check_existing_apigee_resource "https://apigee.googleapis.com/v1/organizations/$PROJECT_ID/envgroups/$ENV_GROUP_NAME"; then
echo "(skipping, envgroup already exists)"
return
fi
curl -X POST --fail -H "Authorization: Bearer $(token)" -H "content-type:application/json" \
-d "{
\"name\":\"$ENV_GROUP_NAME\",
\"hostnames\":[\"$ENV_GROUP_NAME.${DNS_NAME:-$PROJECT_ID.apigee.com}\"],
}" \
"https://apigee.googleapis.com/v1/organizations/$PROJECT_ID/envgroups"
echo -n "β³ Waiting for Apigeectl Env Creation "
wait_for_ready "0" "curl --silent -H \"Authorization: Bearer $(token)\" -H \"Content-Type: application/json\" https://apigee.googleapis.com/v1/organizations/$PROJECT_ID/envgroups/$ENV_GROUP_NAME | grep -q $ENV_GROUP_NAME; echo \$?" "Environment Group $ENV_GROUP_NAME of Organization $PROJECT_ID is created."
echo "β
Created Env Group '$ENV_GROUP_NAME'"
}
add_env_to_envgroup() {
ENV_NAME=$1
ENV_GROUP_NAME=$2
echo "π Adding Env $ENV_NAME to Env Group $ENV_GROUP_NAME"
local ENV_GROUPS_ATTACHMENT_URI
ENV_GROUPS_ATTACHMENT_URI="https://apigee.googleapis.com/v1/organizations/$PROJECT_ID/envgroups/$ENV_GROUP_NAME/attachments"
if curl --fail --silent -H "Authorization: Bearer $(token)" -H "content-type:application/json" "$ENV_GROUPS_ATTACHMENT_URI" | grep -q "\"environment\": \"$ENV_NAME\""; then
echo "(skipping, envgroup assignment already exists)"
return
else
curl -X POST --fail -q -H "Authorization: Bearer $(token)" -H "content-type:application/json" \
-d '{ "environment": "'"$ENV_NAME"'" }' "$ENV_GROUPS_ATTACHMENT_URI"
fi
echo "β
Added Env $ENV_NAME to Env Group $ENV_GROUP_NAME"
}
configure_network() {
echo "π Setup Networking"
ENV_GROUP_NAME="$1"
if [ -z "$(gcloud compute networks list --format json --filter "name=$NETWORK" --format='get(name)')" ]; then
gcloud compute networks create "$NETWORK" --subnet-mode=custom
fi
if [ -z "$(gcloud compute networks subnets list --network "$NETWORK" --format json --filter "name=$SUBNET" --format='get(name)')" ]; then
gcloud compute networks subnets create "$SUBNET" --network "$NETWORK" --range "$SUBNET_RANGE" --region "$REGION"
fi
if [ -z "$(gcloud compute routers list --format json --filter "name=rt-$REGION" --format='get(name)')" ]; then
gcloud compute routers create "rt-$REGION" --network "$NETWORK"
fi
if [ -z "$(gcloud compute routers nats list --router "rt-$REGION" --format json --format='get(name)')" ]; then
gcloud compute routers nats create "apigee-nat-$REGION" --router "rt-$REGION" --region "$REGION" --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges
fi
if [ -z "$(gcloud compute addresses list --format json --filter "name=$INGRESS_IP_NAME" --format='get(address)')" ]; then
if [[ "$INGRESS_TYPE" == "external" && "$CERT_TYPE" == "google-managed" ]]; then
gcloud compute addresses create "$INGRESS_IP_NAME" --global
elif [[ "$INGRESS_TYPE" == "external" && "$CERT_TYPE" == "self-signed" ]]; then
gcloud compute addresses create "$INGRESS_IP_NAME" --region "$REGION"
else
gcloud compute addresses create "$INGRESS_IP_NAME" --region "$REGION" --subnet "$SUBNET" --purpose SHARED_LOADBALANCER_VIP
fi
fi
INGRESS_IP=$(gcloud compute addresses list --format json --filter "name=$INGRESS_IP_NAME" --format="get(address)")
export INGRESS_IP
export DNS_NAME=${DNS_NAME:="$(echo "$INGRESS_IP" | tr '.' '-').nip.io"}
echo "setting hostname on env group to $ENV_GROUP_NAME.$DNS_NAME"
curl -X PATCH --silent -H "Authorization: Bearer $(token)" \
-H "Content-Type:application/json" https://apigee.googleapis.com/v1/organizations/"$PROJECT_ID"/envgroups/"$ENV_GROUP_NAME" \
-d "{\"hostnames\": [\"$ENV_GROUP_NAME.$DNS_NAME\"]}"
if [ -z "$(gcloud dns managed-zones list --filter 'name=apigee-dns-zone' --format='get(name)')" ]; then
if [[ "$INGRESS_TYPE" == "external" ]]; then
gcloud dns managed-zones create apigee-dns-zone --dns-name="$DNS_NAME" --description=apigee-dns-zone
else
gcloud dns managed-zones create apigee-dns-zone --dns-name="$DNS_NAME" --description=apigee-dns-zone --visibility="private" --networks="$NETWORK"
fi
rm -f transaction.yaml
gcloud dns record-sets transaction start --zone=apigee-dns-zone
gcloud dns record-sets transaction add "$INGRESS_IP" \
--name="$ENV_GROUP_NAME.$DNS_NAME." --ttl=600 \
--type=A --zone=apigee-dns-zone
gcloud dns record-sets transaction describe --zone=apigee-dns-zone
gcloud dns record-sets transaction execute --zone=apigee-dns-zone
fi
if [[ "$INGRESS_TYPE" == "external" ]]; then
NAME_SERVER=$(gcloud dns managed-zones describe apigee-dns-zone --format="json" --format="get(nameServers[0])")
export NAME_SERVER
echo "π Add this as an NS record for $DNS_NAME: $NAME_SERVER"
fi
echo "β
Networking set up"
}
create_gke_cluster() {
echo "π Create GKE cluster"
if [ -z "$(gcloud container clusters list --filter "name=$GKE_CLUSTER_NAME" --format='get(name)')" ]; then
if [ "$PRIVATE_CLUSTER" = "true" ];then
if [ -z "$(gcloud compute firewall-rules list --format json --filter "name=allow-master-$GKE_CLUSTER_NAME" --format='get(name)')" ]; then
gcloud compute firewall-rules create "allow-master-$GKE_CLUSTER_NAME" --allow tcp:9443,tcp:15017 --target-tags hybrid-quickstart --network "$NETWORK" --source-ranges "$CONTROL_PLANE_CIDR"
fi
gcloud container clusters create "$GKE_CLUSTER_NAME" \
--no-enable-master-authorized-networks \
--enable-private-nodes \
--master-ipv4-cidr="$CONTROL_PLANE_CIDR" \
--tags="hybrid-quickstart,private-cluster" \
--region "$REGION" \
--node-locations "$ZONE" \
--release-channel stable \
--enable-ip-alias \
--enable-shielded-nodes \
--shielded-secure-boot \
--shielded-integrity-monitoring \
--network "$NETWORK" \
--subnetwork "$SUBNET" \
--default-max-pods-per-node "110" \
--enable-ip-alias \
--machine-type "$GKE_CLUSTER_MACHINE_TYPE" \
--num-nodes "1" \
--enable-autoscaling \
--min-nodes "1" \
--max-nodes "3" \
--tags=hybrid-quickstart \
--labels mesh_id="$MESH_ID" \
--workload-pool "$WORKLOAD_POOL" \
--logging SYSTEM,WORKLOAD \
--monitoring SYSTEM
else
gcloud container clusters create "$GKE_CLUSTER_NAME" \
--tags="hybrid-quickstart" \
--region "$REGION" \
--node-locations "$ZONE" \
--release-channel stable \
--enable-ip-alias \
--enable-shielded-nodes \
--shielded-secure-boot \
--shielded-integrity-monitoring \
--network "$NETWORK" \
--subnetwork "$SUBNET" \
--default-max-pods-per-node "110" \
--enable-ip-alias \
--machine-type "$GKE_CLUSTER_MACHINE_TYPE" \
--num-nodes "1" \
--enable-autoscaling \
--min-nodes "1" \
--max-nodes "3" \
--tags=hybrid-quickstart \
--labels mesh_id="$MESH_ID" \
--workload-pool "$WORKLOAD_POOL" \
--logging SYSTEM,WORKLOAD \
--monitoring SYSTEM
fi
fi
gcloud container clusters get-credentials "$GKE_CLUSTER_NAME" --region "$REGION"
kubectl create clusterrolebinding cluster-admin-binding \
--clusterrole cluster-admin --user "$(gcloud config get-value account)" || true
echo "β
GKE set up"
}
install_certmanager() {
echo "π©π½βπΌ Creating Cert Manager"
kubectl apply --validate=false -f https://github.com/jetstack/cert-manager/releases/download/$CERT_MANAGER_VERSION/cert-manager.yaml
}
install_asm() {
echo "ποΈ Preparing ASM install requirements"
mkdir -p "$QUICKSTART_TOOLS"/kpt
curl --fail -L -o "$QUICKSTART_TOOLS/kpt/kpt.tar.gz" "https://github.com/GoogleContainerTools/kpt/releases/download/${KPT_VERSION}/${KPT_BINARY}"
tar xzf "$QUICKSTART_TOOLS/kpt/kpt.tar.gz" -C "$QUICKSTART_TOOLS/kpt"
export PATH="$QUICKSTART_TOOLS"/kpt:$PATH # prevents asm from installing the wrong kpt version for macOS
mkdir -p "$QUICKSTART_TOOLS"/jq
curl --fail -L -o "$QUICKSTART_TOOLS"/jq/jq "https://github.com/stedolan/jq/releases/download/$JQ_VERSION"
chmod +x "$QUICKSTART_TOOLS"/jq/jq
export PATH=$PATH:"$QUICKSTART_TOOLS"/jq
echo "ποΈ Installing Anthos Service Mesh"
mkdir -p "$QUICKSTART_TOOLS"/istio-asm
curl --fail https://storage.googleapis.com/csm-artifacts/asm/asmcli_$ASM_VERSION > "$QUICKSTART_TOOLS"/istio-asm/asmcli
chmod +x "$QUICKSTART_TOOLS"/istio-asm/asmcli
if [ "$CERT_TYPE" = "google-managed" ];then
cat << EOF > "$QUICKSTART_TOOLS"/istio-asm/istio-operator-patch.yaml
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
components:
ingressGateways:
- name: istio-ingressgateway
enabled: true
k8s:
resources:
requests:
cpu: 1000m
serviceAnnotations:
cloud.google.com/neg: '{"ingress": true}'
cloud.google.com/backend-config: '{"default": "ingress-backendconfig"}'
cloud.google.com/app-protocols: '{"https":"HTTPS"}'
service:
type: ClusterIP
ports:
- name: status-port
port: 15021
targetPort: 15021
- name: https
port: 443
targetPort: 8443
EOF
else
cat << EOF > "$QUICKSTART_TOOLS"/istio-asm/istio-operator-patch.yaml
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
components:
ingressGateways:
- name: istio-ingressgateway
enabled: true
k8s:
resources:
requests:
cpu: 1000m
serviceAnnotations:
networking.gke.io/load-balancer-type: $INGRESS_TYPE
service:
type: LoadBalancer
loadBalancerIP: $INGRESS_IP
ports:
- name: status-port
port: 15021
targetPort: 15021
- name: https
port: 443
targetPort: 8443
EOF
fi
rm -rf "$QUICKSTART_TOOLS"/istio-asm/install-out
mkdir -p "$QUICKSTART_TOOLS"/istio-asm/install-out
yes | "$QUICKSTART_TOOLS"/istio-asm/asmcli install \
--project_id "$PROJECT_ID" \
--fleet_id "$PROJECT_ID" \
--cluster_name "$GKE_CLUSTER_NAME" \
--cluster_location "$REGION" \
--output_dir "$QUICKSTART_TOOLS"/istio-asm/install-out \
--custom_overlay "$QUICKSTART_TOOLS"/istio-asm/istio-operator-patch.yaml \
--ca mesh_ca \
--enable_all
echo "β
ASM installed"
}
download_apigee_ctl() {
echo "π₯ Setup Apigeectl"
APIGEECTL_ROOT="$QUICKSTART_TOOLS/apigeectl"
# Remove existing apigeectl
if [ -d "$APIGEECTL_ROOT" ]; then rm -rf "$APIGEECTL_ROOT"; fi
mkdir -p "$APIGEECTL_ROOT"
curl --fail -L \
-o "$APIGEECTL_ROOT/apigeectl.tar.gz" \
"https://storage.googleapis.com/apigee-release/hybrid/apigee-hybrid-setup/$APIGEE_HYBRID_VERSION/$APIGEE_CTL"
tar xvzf "$APIGEECTL_ROOT/apigeectl.tar.gz" -C "$APIGEECTL_ROOT"
rm "$APIGEECTL_ROOT/apigeectl.tar.gz"
mv "$APIGEECTL_ROOT"/apigeectl_*_64 "$APIGEECTL_HOME"
echo "β
Apigeectl set up in $APIGEECTL_HOME"
}
prepare_resources() {
echo "π οΈ Configure Apigee hybrid"
if [ -d "$HYBRID_HOME" ]; then rm -rf "$HYBRID_HOME"; fi
mkdir -p "$HYBRID_HOME"
mkdir -p "$HYBRID_HOME/overrides"
mkdir -p "$HYBRID_HOME/service-accounts"
ln -s "$APIGEECTL_HOME/tools" "$HYBRID_HOME/tools"
ln -s "$APIGEECTL_HOME/config" "$HYBRID_HOME/config"
ln -s "$APIGEECTL_HOME/templates" "$HYBRID_HOME/templates"
ln -s "$APIGEECTL_HOME/plugins" "$HYBRID_HOME/plugins"
echo "β
Hybrid Config Setup"
}
create_cert() {
ENV_GROUP_NAME=$1
if [ "$CERT_TYPE" = "skip" ];then
return
fi
echo "π Creating self-signed cert - $ENV_GROUP_NAME"
mkdir -p "$HYBRID_HOME/certs"
CA_CERT_NAME="quickstart-ca"
# create CA cert if not exist
if [ -f "$HYBRID_HOME/certs/$CA_CERT_NAME.crt" ]; then
echo "CA already exists! Reusing that one."
else
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj "/CN=$DNS_NAME/O=Apigee Quickstart" -keyout "$HYBRID_HOME/certs/$CA_CERT_NAME.key" -out "$HYBRID_HOME/certs/$CA_CERT_NAME.crt"
fi
openssl req -out "$HYBRID_HOME/certs/$ENV_GROUP_NAME.csr" -newkey rsa:2048 -nodes -keyout "$HYBRID_HOME/certs/$ENV_GROUP_NAME.key" -subj "/CN=$ENV_GROUP_NAME.$DNS_NAME/O=Apigee Quickstart"
openssl x509 -req -days 365 -CA "$HYBRID_HOME/certs/$CA_CERT_NAME.crt" -CAkey "$HYBRID_HOME/certs/$CA_CERT_NAME.key" -set_serial 0 -in "$HYBRID_HOME/certs/$ENV_GROUP_NAME.csr" -out "$HYBRID_HOME/certs/$ENV_GROUP_NAME.crt"
cat "$HYBRID_HOME/certs/$ENV_GROUP_NAME.crt" "$HYBRID_HOME/certs/$CA_CERT_NAME.crt" > "$HYBRID_HOME/certs/$ENV_GROUP_NAME.fullchain.crt"
kubectl create secret tls tls-hybrid-ingress \
--cert="$HYBRID_HOME/certs/$ENV_GROUP_NAME.fullchain.crt" \
--key="$HYBRID_HOME/certs/$ENV_GROUP_NAME.key" \
-n istio-system --dry-run=client -o yaml | kubectl apply -f -
if [ "$CERT_TYPE" = "google-managed" ];then
echo "π’ Letting Google manage the cert - $ENV_GROUP_NAME"
cat <<EOF | kubectl apply -f -
apiVersion: networking.gke.io/v1
kind: ManagedCertificate
metadata:
name: "apigee-cert-$ENV_GROUP_NAME"
namespace: istio-system
spec:
domains:
- "$ENV_GROUP_NAME.$DNS_NAME"
---
apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
name: ingress-backendconfig
namespace: istio-system
spec:
healthCheck:
requestPath: /healthz/ready
port: 15021
type: HTTP
logging:
enable: false
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
networking.gke.io/managed-certificates: "apigee-cert-$ENV_GROUP_NAME"
kubernetes.io/ingress.global-static-ip-name: $INGRESS_IP_NAME
kubernetes.io/ingress.allow-http: "false"
name: xlb-apigee-$ENV_GROUP_NAME
namespace: istio-system
spec:
defaultBackend:
service:
name: istio-ingressgateway
port:
number: 443
---
apiVersion: v1
kind: Namespace
metadata:
name: apigee
---
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
name: apigee-wildcard
namespace: apigee
spec:
selector:
app: istio-ingressgateway
servers:
- hosts:
- '*'
port:
name: https-apigee-443
number: 443
protocol: HTTPS
tls:
credentialName: tls-hybrid-ingress
mode: SIMPLE
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: health
namespace: apigee
spec:
gateways:
- apigee-wildcard
hosts:
- '*'
http:
- match:
- headers:
user-agent:
prefix: GoogleHC
method:
exact: GET
uri:
exact: /
rewrite:
authority: istio-ingressgateway.istio-system.svc.cluster.local:15021
uri: /healthz/ready
route:
- destination:
host: istio-ingressgateway.istio-system.svc.cluster.local
port:
number: 15021
EOF
else
kubectl create ns apigee
fi
}
create_k8s_sa_workload() {
K8S_SA=$1
GCP_SA=$2
kubectl create sa -n apigee "$K8S_SA" || echo "$K8S_SA exists"
kubectl annotate sa --overwrite -n apigee "$K8S_SA" "iam.gke.io/gcp-service-account=$GCP_SA"
gcloud iam service-accounts add-iam-policy-binding "$GCP_SA" \
--role roles/iam.workloadIdentityUser \
--member "serviceAccount:$PROJECT_ID.svc.id.goog[apigee/$K8S_SA]" --project "$PROJECT_ID"
}
create_sa() {
# make sure we don't download the service account keys
sed -i -e 's/download_key_file="y"/download_key_file="n"/g' "$APIGEECTL_HOME/tools/create-service-account"
sed -i -e 's/read -r download_key_file/# key download is not necessary for workload ID/g' "$APIGEECTL_HOME/tools/create-service-account"
yes | "$APIGEECTL_HOME"/tools/create-service-account -e prod -d "$HYBRID_HOME/service-accounts"
APIGEE_ORG_HASH="$("$APIGEECTL_HOME"/apigeectl encode --org "$PROJECT_ID" 2>&1 | tail -1 | xargs | sed -e "s/^apigee-udca-//")"
APIGEE_ORG_ENV_HASH="$("$APIGEECTL_HOME"/apigeectl encode --org "$PROJECT_ID" --env "$ENV_NAME" 2>&1 | tail -1 | xargs | sed -e "s/^apigee-udca-//")"
create_k8s_sa_workload "apigee-cassandra-schema-setup-$APIGEE_ORG_HASH-sa" "apigee-cassandra@$PROJECT_ID.iam.gserviceaccount.com"
create_k8s_sa_workload "apigee-cassandra-user-setup-$APIGEE_ORG_HASH-sa" "apigee-cassandra@$PROJECT_ID.iam.gserviceaccount.com"
create_k8s_sa_workload "apigee-mart-$APIGEE_ORG_HASH-sa" "apigee-mart@$PROJECT_ID.iam.gserviceaccount.com"
create_k8s_sa_workload "apigee-connect-agent-$APIGEE_ORG_HASH-sa" "apigee-mart@$PROJECT_ID.iam.gserviceaccount.com"
create_k8s_sa_workload "apigee-watcher-$APIGEE_ORG_HASH-sa" "apigee-watcher@$PROJECT_ID.iam.gserviceaccount.com"
create_k8s_sa_workload "apigee-runtime-$APIGEE_ORG_ENV_HASH-sa" "apigee-runtime@$PROJECT_ID.iam.gserviceaccount.com"
create_k8s_sa_workload "apigee-udca-$APIGEE_ORG_ENV_HASH-sa" "apigee-udca@$PROJECT_ID.iam.gserviceaccount.com"
create_k8s_sa_workload "apigee-synchronizer-$APIGEE_ORG_ENV_HASH-sa" "apigee-synchronizer@$PROJECT_ID.iam.gserviceaccount.com"
echo -n "π Enabling runtime synchronizer"
curl --fail -X POST -H "Authorization: Bearer $(token)" \
-H "Content-Type:application/json" \
"https://apigee.googleapis.com/v1/organizations/${PROJECT_ID}:setSyncAuthorization" \
-d "{\"identities\":[\"serviceAccount:apigee-synchronizer@${PROJECT_ID}.iam.gserviceaccount.com\"]}"
}
configure_runtime() {
ENV_NAME=$1
ENV_GROUP_NAME=$2
echo "Configure Overrides"
cat << EOF > "$HYBRID_HOME"/overrides/overrides.yaml
gcp:
projectID: $PROJECT_ID
region: "$AX_REGION" # Analytics Region
workloadIdentityEnabled: true
# Apigee org name.
org: $PROJECT_ID
# Kubernetes cluster name details
k8sCluster:
name: $GKE_CLUSTER_NAME
region: "$REGION"
virtualhosts:
- name: $ENV_GROUP_NAME
sslSecret: tls-hybrid-ingress
additionalGateways: ["apigee-wildcard"]
instanceID: "$PROJECT_ID-$(date +%s)"
envs:
- name: $ENV_NAME
logger:
enabled: false
EOF
}
apigeectl_init() {
echo -n "π Running apigeectl init"
"$APIGEECTL_HOME"/apigeectl init -f "$HYBRID_HOME"/overrides/overrides.yaml --print-yaml > "$HYBRID_HOME"/generated/apigee-init.yaml
}
apigeectl_apply() {
echo -n "π Running apigeectl apply"
timeout 300 bash -c 'until kubectl wait --for=condition=ready --timeout 60s pod -l app=apigee-controller -n apigee-system; do sleep 10; done'
"$APIGEECTL_HOME"/apigeectl apply -f "$HYBRID_HOME"/overrides/overrides.yaml --print-yaml > "$HYBRID_HOME"/generated/apigee-runtime.yaml
}
install_runtime() {
pushd "$HYBRID_HOME" || return # because apigeectl uses pwd-relative paths
mkdir -p "$HYBRID_HOME"/generated
export -f apigeectl_init
timeout 20m bash -c 'until apigeectl_init; do sleep 30; done'
echo -n "β³ Waiting for Apigeectl init "
timeout 600 bash -c 'until kubectl wait --for=condition=ready --timeout 60s pod -l app=apigee-controller -n apigee-system; do sleep 10; done'
timeout 600 bash -c 'until kubectl wait --for=condition=ready --timeout 60s issuer apigee-selfsigned-issuer -n apigee-system; do sleep 10; done'
timeout 600 bash -c 'until kubectl wait --for=condition=ready --timeout 60s certificate apigee-serving-cert -n apigee-system; do sleep 10; done'
timeout 600 bash -c 'until kubectl wait --for=condition=complete --timeout 60s job/apigee-resources-install -n apigee-system; do sleep 10; done'
export -f apigeectl_apply
timeout 720 bash -c 'until apigeectl_apply; do sleep 20; done'
echo -n "β³ Waiting for Apigeectl apply "
timeout 1800 bash -c 'until kubectl wait --for=condition=ready --timeout 60s pod -l app=apigee-runtime -n apigee; do sleep 10; done'
popd || return
echo "πππ Hybrid installation completed!"
}
enable_trace() {
ENV_NAME=$1
echo -n "π΅οΈββοΈ Turn on trace logs"
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
--member "serviceAccount:apigee-runtime@${PROJECT_ID}.iam.gserviceaccount.com" \
--role=roles/cloudtrace.agent --project "$PROJECT_ID"
curl --fail -X PATCH -H "Authorization: Bearer $(token)" \
-H "Content-Type:application/json" \
"https://apigee.googleapis.com/v1/organizations/${PROJECT_ID}/environments/$ENV_NAME/traceConfig" \
-d "{\"exporter\":\"CLOUD_TRACE\",\"endpoint\":\"${PROJECT_ID}\",\"sampling_config\":{\"sampler\":\"PROBABILITY\",\"sampling_rate\":0.5}}"
}
deploy_example_proxy() {
echo "π¦ Deploy Sample Proxy"
ENV_NAME=$1
ENV_GROUP_NAME=$2
(cd "$QUICKSTART_ROOT/example-proxy" && zip -r apiproxy.zip apiproxy/*)
PROXY_REV=$(curl -X POST \
"https://apigee.googleapis.com/v1/organizations/${PROJECT_ID}/apis?action=import&name=httpbin-v0&validate=true" \
-H "Authorization: Bearer $(token)" \
-H "Content-Type: multipart/form-data" \
-F "zipFile=@$QUICKSTART_ROOT/example-proxy/apiproxy.zip" | grep '"revision": "[^"]*' | cut -d'"' -f4)
rm "$QUICKSTART_ROOT/example-proxy/apiproxy.zip"
curl -X POST \
"https://apigee.googleapis.com/v1/organizations/${PROJECT_ID}/environments/$ENV_NAME/apis/httpbin-v0/revisions/${PROXY_REV}/deployments?override=true" \
-H "Authorization: Bearer $(token)" \
-H "Content-Length: 0"
echo "β
Sample Proxy Deployed"
echo "π€ Try without DNS (first deployment takes a few seconds. Relax and breathe!):"
if [ "$CERT_TYPE" = "self-signed" ];then
echo "curl --cacert $QUICKSTART_ROOT/hybrid-files/certs/quickstart-ca.crt https://$ENV_GROUP_NAME.$DNS_NAME/httpbin/v0/anything"
else
echo "curl https://$ENV_GROUP_NAME.$DNS_NAME/httpbin/v0/anything"
fi
echo "π To reach your API via the FQDN: Make sure you add a DNS record for your FQDN or an NS record for $DNS_NAME: $NAME_SERVER"
echo "π During development you can also use --resolve $ENV_GROUP_NAME.$DNS_NAME:443:$INGRESS_IP to resolve the hostname for your curl command"
}
delete_apigee_keys() {
for SA in mart cassandra udca metrics synchronizer logger watcher distributed-trace runtime
do
delete_sa_keys "apigee-${SA}"
done
}
delete_sa_keys() {
SA=$1
for SA_KEY_NAME in $(gcloud iam service-accounts keys list --iam-account="${SA}@${PROJECT_ID}.iam.gserviceaccount.com" --format="get(name)" --filter="keyType=USER_MANAGED")
do
gcloud iam service-accounts keys delete "$SA_KEY_NAME" --iam-account="$SA@$PROJECT_ID.iam.gserviceaccount.com" -q
done
}