-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitbucket-pipelines.yml
706 lines (665 loc) · 37.7 KB
/
bitbucket-pipelines.yml
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
pipelines:
default: # Steps below will kick off every time a code commit is detected on any branch.
- step:
name: ECR - eks-demo
script:
- pipe: atlassian/aws-cloudformation-deploy:0.9.1
variables:
AWS_DEFAULT_REGION: "us-east-1"
STACK_NAME: "ecr-eks-demo"
TEMPLATE: "./ecr.yml"
WAIT: 'false'
STACK_PARAMETERS: >
[{
"ParameterKey": "ApplicationName",
"ParameterValue": "eks-demo"
}]
- step:
name: IAM - EKS admin
script:
- pipe: atlassian/aws-cloudformation-deploy:0.9.1
variables:
AWS_DEFAULT_REGION: "us-east-1"
STACK_NAME: "eks-iam"
TEMPLATE: "./iam.yml"
WAIT: 'true'
CAPABILITIES: ['CAPABILITY_IAM', 'CAPABILITY_AUTO_EXPAND', 'CAPABILITY_NAMED_IAM']
branches: # Pipelines below will automatically kick off with a code commit to the particular branch
master:
# Left blank for branch naming
##############################
#### DEVELOPMENT SECTION #####
##############################
development:
- step:
name: Network stack
script:
- export PARAMETERS=$(cat ./vpc-parm-$BITBUCKET_BRANCH.json)
- pipe: atlassian/aws-cloudformation-deploy:0.9.2
variables:
AWS_DEFAULT_REGION: "us-east-1"
STACK_NAME: "$BITBUCKET_BRANCH-vpc"
TEMPLATE: "./vpc.yml"
WAIT: 'true'
STACK_PARAMETERS: $PARAMETERS
condition:
changesets:
includePaths:
- "vpc.yml"
- "vpc-parm-$BITBUCKET_BRANCH.json"
- step:
name: EKS cluster
script:
- export PARAMETERS=$(cat ./eks-cluster-parm-$BITBUCKET_BRANCH.json)
- pipe: atlassian/aws-cloudformation-deploy:0.9.2
variables:
AWS_DEFAULT_REGION: "us-east-1"
STACK_NAME: "$BITBUCKET_BRANCH-eks"
TEMPLATE: "./eks-cluster.yml"
CAPABILITIES: ['CAPABILITY_NAMED_IAM']
WAIT: 'true'
STACK_PARAMETERS: $PARAMETERS
condition:
changesets:
includePaths:
- "eks-cluster.yml"
- "eks-cluster-parm-$BITBUCKET_BRANCH.json"
- step:
name: Update API endpoint from public to public/private
image: atlassian/pipelines-awscli:1.16.302
script:
- apk add jq
- aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}"
- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"
- aws eks describe-cluster --region us-east-1 --name eks-cluster-$BITBUCKET_BRANCH > describeCluster.json
- check=$(jq -r .cluster.resourcesVpcConfig.endpointPrivateAccess describeCluster.json)
- if [ "$check" == "true" ]; then
- echo endpointPrivateAccess already set
- else
- aws eks update-cluster-config --region us-east-1 --name eks-cluster-$BITBUCKET_BRANCH --resources-vpc-config endpointPublicAccess=true,endpointPrivateAccess=true
- sleep 30
- aws eks wait cluster-active --region us-east-1 --name eks-cluster-$BITBUCKET_BRANCH
- fi
condition:
changesets:
includePaths:
- "eks-cluster.yml"
- "eks-cluster-parm-$BITBUCKET_BRANCH.json"
- step:
name: EKS node group 1
script:
- export PARAMETERS=$(cat ./eks-nodegroup1-parm-$BITBUCKET_BRANCH.json)
- pipe: atlassian/aws-cloudformation-deploy:0.9.2
variables:
AWS_DEFAULT_REGION: "us-east-1"
STACK_NAME: "$BITBUCKET_BRANCH-eks-nodegroup-1"
TEMPLATE: "./eks-nodegroup.yml"
CAPABILITIES: ['CAPABILITY_NAMED_IAM']
WAIT: 'true'
STACK_PARAMETERS: $PARAMETERS
condition:
changesets:
includePaths:
- "eks-nodegroup.yml"
- "eks-nodegroup1-parm-$BITBUCKET_BRANCH.json"
- step:
# limitation with CF that you cant use LatestVersion in node group with Launch templates so needed to move this outside of CF
# Exporting the latest version to a variable then modifying the launch template and trigger a node group update on EKS
# Also checking to make sure the Cluster K8s version matches the node group AMI version
name: Update Node Group 1
image: atlassian/pipelines-awscli:1.16.302
script:
- # Install jq to parse json
- apk add jq
- # Read credentials from Bitbucket variables
- aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}"
- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"
- # Get the node group label from CloudFormation template parameter file
- export NODE_LABEL=$(cat ./eks-nodegroup1-parm-$BITBUCKET_BRANCH.json | jq -r '.[] | select(.ParameterKey=="Label") | .ParameterValue')
- # Connect to the node group and set variables for the default version and latest version
- export DEFAULT_VER=$(aws ec2 describe-launch-templates --launch-template-names $BITBUCKET_BRANCH-eks-$NODE_LABEL-launch-template | jq -r .LaunchTemplates[].DefaultVersionNumber)
- export LATEST_VER=$(aws ec2 describe-launch-templates --launch-template-names $BITBUCKET_BRANCH-eks-$NODE_LABEL-launch-template | jq -r .LaunchTemplates[].LatestVersionNumber)
- # If statement to compare version and modify the node group if they dont match
- if [ "$DEFAULT_VER" == "$LATEST_VER" ]; then
- echo DefaultVersion matches LatestVersion
- else
- aws ec2 modify-launch-template --launch-template-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-launch-template --default-version $LATEST_VER
- aws eks update-nodegroup-version --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group
- sleep 30
- # This waits for the node group to update before moving on in the pipeline
- aws eks wait nodegroup-active --region us-east-1 --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group
- fi
- # Connect to the cluster and node group to match sure the Kubernetes versions match. If using custom AMIs then comment out the next two lines
- export NODE_VER=$(aws eks describe-nodegroup --region us-east-1 --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group | jq -r .nodegroup.version)
- export CLUSTER_VER=$(aws eks describe-cluster --region us-east-1 --name eks-cluster-$BITBUCKET_BRANCH | jq -r .cluster.version)
- # If they dont match then force an update on the node group to update the AMI. If using custom AMIs then comment out the IF STATEMENT below
- if [ "$NODE_VER" == "$CLUSTER_VER" ]; then
- echo Node group version matches cluster version
- else
- aws eks update-nodegroup-version --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group
- sleep 30
- aws eks wait nodegroup-active --region us-east-1 --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group
- # end the script
- fi
condition:
changesets:
includePaths:
- "eks-nodegroup.yml"
- "eks-nodegroup1-parm-$BITBUCKET_BRANCH.json"
- step:
name: EKS node group 2
script:
- export PARAMETERS=$(cat ./eks-nodegroup2-parm-$BITBUCKET_BRANCH.json)
- pipe: atlassian/aws-cloudformation-deploy:0.9.2
variables:
AWS_DEFAULT_REGION: "us-east-1"
STACK_NAME: "$BITBUCKET_BRANCH-eks-nodegroup-2"
TEMPLATE: "./eks-nodegroup.yml"
CAPABILITIES: ['CAPABILITY_NAMED_IAM']
WAIT: 'true'
STACK_PARAMETERS: $PARAMETERS
condition:
changesets:
includePaths:
- "eks-nodegroup.yml"
- "eks-nodegroup2-parm-$BITBUCKET_BRANCH.json"
- step:
# limitation with CF that you cant use LatestVersion in node group with Launch templates so needed to move this outside of CF
# Exporting the latest version to a variable then modifying the launch template and trigger a node group update on EKS
# Also checking to make sure the Cluster K8s version matches the node group AMI version
name: Update Node Group 2
image: atlassian/pipelines-awscli:1.16.302
script:
- # Install jq to parse json
- apk add jq
- # Read credentials from Bitbucket variables
- aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}"
- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"
- # Get the node group label from CloudFormation template parameter file
- export NODE_LABEL=$(cat ./eks-nodegroup2-parm-$BITBUCKET_BRANCH.json | jq -r '.[] | select(.ParameterKey=="Label") | .ParameterValue')
- # Connect to the node group and set variables for the default version and latest version
- export DEFAULT_VER=$(aws ec2 describe-launch-templates --launch-template-names $BITBUCKET_BRANCH-eks-$NODE_LABEL-launch-template | jq -r .LaunchTemplates[].DefaultVersionNumber)
- export LATEST_VER=$(aws ec2 describe-launch-templates --launch-template-names $BITBUCKET_BRANCH-eks-$NODE_LABEL-launch-template | jq -r .LaunchTemplates[].LatestVersionNumber)
- # If statement to compare version and modify the node group if they dont match
- if [ "$DEFAULT_VER" == "$LATEST_VER" ]; then
- echo DefaultVersion matches LatestVersion
- else
- aws ec2 modify-launch-template --launch-template-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-launch-template --default-version $LATEST_VER
- aws eks update-nodegroup-version --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group
- sleep 30
- # This waits for the node group to update before moving on in the pipeline
- aws eks wait nodegroup-active --region us-east-1 --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group
- fi
- # Connect to the cluster and node group to match sure the Kubernetes versions match. If using custom AMIs then comment out the next two lines
- export NODE_VER=$(aws eks describe-nodegroup --region us-east-1 --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group | jq -r .nodegroup.version)
- export CLUSTER_VER=$(aws eks describe-cluster --region us-east-1 --name eks-cluster-$BITBUCKET_BRANCH | jq -r .cluster.version)
- # If they dont match then force an update on the node group to update the AMI. If using custom AMIs then comment out the IF STATEMENT below
- if [ "$NODE_VER" == "$CLUSTER_VER" ]; then
- echo Node group version matches cluster version
- else
- aws eks update-nodegroup-version --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group
- sleep 30
- aws eks wait nodegroup-active --region us-east-1 --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group
- # end the script
- fi
condition:
changesets:
includePaths:
- "eks-nodegroup.yml"
- "eks-nodegroup1-parm-$BITBUCKET_BRANCH.json"
- step:
name: K8s monitoring
image: alpine/k8s:1.18.2
script:
- aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}"
- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"
- aws --region us-east-1 eks update-kubeconfig --name eks-cluster-$BITBUCKET_BRANCH
- kubectl apply -f ./monitoring/k8s-metrics-server.yaml
- helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
- helm repo add grafana https://grafana.github.io/helm-charts
- helm repo update
- kubectl apply -f ./monitoring/monitoring_namespace.yaml
- helm upgrade --install prometheus prometheus-community/prometheus -n monitoring --values ./monitoring/prometheus-values.yaml
- helm upgrade --install loki-stack grafana/loki-stack -n monitoring --values ./monitoring/loki-values.yaml
- helm upgrade --install grafana grafana/grafana -n monitoring --values ./monitoring/grafana-values-$DEV_ENVIRONMENT.yaml
#condition:
# changesets:
# includePaths:
# - "k8s-metrics-server.yaml"
# - "prometheus-values.yaml"
# - "loki-values.yaml"
# - "grafana-values.yaml"
# Add the example command below to each pod that needs hpa enabled on
#kubectl autoscale deployment DEPLOYMENTNAME --cpu-percent=50 --min=1 --max=10
# Below will check to see if the HPA is enabled and details
#kubectl describe hpa
- step:
name: Cluster Autoscaler
image: alpine/k8s:1.18.2
script:
- aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}"
- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"
- aws --region us-east-1 eks update-kubeconfig --name eks-cluster-$BITBUCKET_BRANCH
- kubectl apply -f cluster-autoscaler-autodiscover_$BITBUCKET_BRANCH.yaml
- kubectl -n kube-system annotate --overwrite deployment.apps/cluster-autoscaler cluster-autoscaler.kubernetes.io/safe-to-evict="false"
- kubectl -n kube-system set image deployment.apps/cluster-autoscaler cluster-autoscaler=gcr.io/k8s-artifacts-prod/autoscaling/cluster-autoscaler:v1.17.4
condition:
changesets:
includePaths:
- "eks-cluster.yml"
- "eks-cluster-parm-$BITBUCKET_BRANCH.json"
- step:
name: Update ConfigMap
image: alpine/k8s:1.18.2
script:
- aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}"
- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"
- aws --region us-east-1 eks update-kubeconfig --name eks-cluster-$BITBUCKET_BRANCH
- 'SSOROLE=" - rolearn: arn:aws:iam::ACCOUNTID:role/ROLENAME\n username: build\n groups:\n - system:masters"'
- 'kubectl get -n kube-system configmap/aws-auth -o yaml | awk "/mapRoles: \|/{print;print \"$SSOROLE\";next}1" > /tmp/aws-auth-patch.yml'
- kubectl patch configmap/aws-auth -n kube-system --patch "$(cat /tmp/aws-auth-patch.yml)"
- 'IAMUSER=" mapUsers: \|\n - userarn: arn:aws:iam::ACCOUNTID:user/bitbucket-eks-admin\n username: bitbucket-eks-admin\n groups:\n - system:masters"'
- 'kubectl get -n kube-system configmap/aws-auth -o yaml | awk "/data:/{print;print \"$IAMUSER\";next}1" > /tmp/aws-auth-patch.yml'
- kubectl patch configmap/aws-auth -n kube-system --patch "$(cat /tmp/aws-auth-patch.yml)"
condition:
changesets:
includePaths:
- "sponsor-portal-eks-cluster.yml"
- "sponsor-portal-eks-cluster-parm-$DEV_ENVIRONMENT.json"
- step:
name: OpenID connect for Ingress Controller
image: alpine/k8s:1.18.2
script:
- aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}"
- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"
- aws --region us-east-1 eks update-kubeconfig --name eks-cluster-$BITBUCKET_BRANCH
- eksctl utils associate-iam-oidc-provider --cluster=eks-cluster-$BITBUCKET_BRANCH --approve
condition:
changesets:
includePaths:
- "eks-cluster.yml"
- "eks-cluster-parm-$BITBUCKET_BRANCH.json"
- step:
name: Ingress Controller IAM role
script:
- pipe: atlassian/aws-cloudformation-deploy:0.9.2
variables:
AWS_DEFAULT_REGION: "us-east-1"
STACK_NAME: "$BITBUCKET_BRANCH-ingress-controller"
TEMPLATE: "./eks-ingress-controller.yml"
WAIT: 'true'
CAPABILITIES: ['CAPABILITY_IAM', 'CAPABILITY_AUTO_EXPAND', 'CAPABILITY_NAMED_IAM']
STACK_PARAMETERS: >
[{
"ParameterKey": "EnvironmentName",
"ParameterValue": $BITBUCKET_BRANCH
}]
condition:
changesets:
includePaths:
- "eks-ingress-controller.yml"
- "eks-cluster.yml"
- "eks-cluster-parm-$BITBUCKET_BRANCH.json"
- step:
name: RBAC policy for Ingress controller
script:
- pipe: atlassian/aws-eks-kubectl-run:1.2.0
variables:
AWS_DEFAULT_REGION: "us-east-1"
CLUSTER_NAME: eks-cluster-$BITBUCKET_BRANCH
KUBECTL_COMMAND: 'apply'
RESOURCE_PATH: 'alb-ingress-rbac.yml'
condition:
changesets:
includePaths:
- "alb-ingress-rbac.yml"
- "eks-cluster.yml"
- "eks-cluster-parm-$BITBUCKET_BRANCH.json"
- step:
name: Associate IAM role to EKS Service account for ingress controller
image: alpine/k8s:1.18.2
script:
- aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}"
- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"
- aws --region us-east-1 eks update-kubeconfig --name eks-cluster-$BITBUCKET_BRANCH
- kubectl annotate serviceaccount -n kube-system alb-ingress-controller eks.amazonaws.com/role-arn=arn:aws:iam::ACCOUNTID:role/eks-alb-ingress-controller-$BITBUCKET_BRANCH --overwrite
condition:
changesets:
includePaths:
- "alb-ingress-rbac.yml"
- "eks-cluster.yml"
- "eks-cluster-parm-$BITBUCKET_BRANCH.json"
- step:
name: Deploy Ingress controller
script:
- pipe: atlassian/aws-eks-kubectl-run:1.2.0
variables:
AWS_DEFAULT_REGION: "us-east-1"
CLUSTER_NAME: eks-cluster-$BITBUCKET_BRANCH
KUBECTL_COMMAND: 'apply'
RESOURCE_PATH: 'alb-ingress-controller-$BITBUCKET_BRANCH.yml'
condition:
changesets:
includePaths:
- "alb-ingress-controller-$BITBUCKET_BRANCH.yml"
- "alb-ingress-rbac.yml"
- "eks-cluster.yml"
- "eks-cluster-parm-$BITBUCKET_BRANCH.json"
##############################
##### PRODUCTION SECTION #####
##############################
production:
- step:
name: Network stack
script:
- export PARAMETERS=$(cat ./vpc-parm-$BITBUCKET_BRANCH.json)
- pipe: atlassian/aws-cloudformation-deploy:0.9.2
variables:
AWS_DEFAULT_REGION: "us-east-1"
STACK_NAME: "$BITBUCKET_BRANCH-vpc"
TEMPLATE: "./vpc.yml"
WAIT: 'true'
STACK_PARAMETERS: $PARAMETERS
condition:
changesets:
includePaths:
- "vpc.yml"
- "vpc-parm-$BITBUCKET_BRANCH.json"
- step:
name: EKS cluster
script:
- export PARAMETERS=$(cat ./eks-cluster-parm-$BITBUCKET_BRANCH.json)
- pipe: atlassian/aws-cloudformation-deploy:0.9.2
variables:
AWS_DEFAULT_REGION: "us-east-1"
STACK_NAME: "$BITBUCKET_BRANCH-eks"
TEMPLATE: "./eks-cluster.yml"
CAPABILITIES: ['CAPABILITY_NAMED_IAM']
WAIT: 'true'
STACK_PARAMETERS: $PARAMETERS
condition:
changesets:
includePaths:
- "eks-cluster.yml"
- "eks-cluster-parm-$BITBUCKET_BRANCH.json"
- step:
name: Update API endpoint from public to public/private
image: atlassian/pipelines-awscli:1.16.302
script:
- apk add jq
- aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}"
- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"
- aws eks describe-cluster --region us-east-1 --name eks-cluster-$BITBUCKET_BRANCH > describeCluster.json
- check=$(jq -r .cluster.resourcesVpcConfig.endpointPrivateAccess describeCluster.json)
- if [ "$check" == "true" ]; then
- echo endpointPrivateAccess already set
- else
- aws eks update-cluster-config --region us-east-1 --name eks-cluster-$BITBUCKET_BRANCH --resources-vpc-config endpointPublicAccess=true,endpointPrivateAccess=true
- sleep 30
- aws eks wait cluster-active --region us-east-1 --name eks-cluster-$BITBUCKET_BRANCH
- fi
condition:
changesets:
includePaths:
- "eks-cluster.yml"
- "eks-cluster-parm-$BITBUCKET_BRANCH.json"
- step:
name: EKS node group 1
script:
- export PARAMETERS=$(cat ./eks-nodegroup1-parm-$BITBUCKET_BRANCH.json)
- pipe: atlassian/aws-cloudformation-deploy:0.9.2
variables:
AWS_DEFAULT_REGION: "us-east-1"
STACK_NAME: "$BITBUCKET_BRANCH-eks-nodegroup-1"
TEMPLATE: "./eks-nodegroup.yml"
CAPABILITIES: ['CAPABILITY_NAMED_IAM']
WAIT: 'true'
STACK_PARAMETERS: $PARAMETERS
condition:
changesets:
includePaths:
- "eks-nodegroup.yml"
- "eks-nodegroup1-parm-$BITBUCKET_BRANCH.json"
- step:
# limitation with CF that you cant use LatestVersion in node group with Launch templates so needed to move this outside of CF
# Exporting the latest version to a variable then modifying the launch template and trigger a node group update on EKS
# Also checking to make sure the Cluster K8s version matches the node group AMI version
name: Update Node Group 1
image: atlassian/pipelines-awscli:1.16.302
script:
- # Install jq to parse json
- apk add jq
- # Read credentials from Bitbucket variables
- aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}"
- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"
- # Get the node group label from CloudFormation template parameter file
- export NODE_LABEL=$(cat ./eks-nodegroup1-parm-$BITBUCKET_BRANCH.json | jq -r '.[] | select(.ParameterKey=="Label") | .ParameterValue')
- # Connect to the node group and set variables for the default version and latest version
- export DEFAULT_VER=$(aws ec2 describe-launch-templates --launch-template-names $BITBUCKET_BRANCH-eks-$NODE_LABEL-launch-template | jq -r .LaunchTemplates[].DefaultVersionNumber)
- export LATEST_VER=$(aws ec2 describe-launch-templates --launch-template-names $BITBUCKET_BRANCH-eks-$NODE_LABEL-launch-template | jq -r .LaunchTemplates[].LatestVersionNumber)
- # If statement to compare version and modify the node group if they dont match
- if [ "$DEFAULT_VER" == "$LATEST_VER" ]; then
- echo DefaultVersion matches LatestVersion
- else
- aws ec2 modify-launch-template --launch-template-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-launch-template --default-version $LATEST_VER
- aws eks update-nodegroup-version --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group
- sleep 30
- # This waits for the node group to update before moving on in the pipeline
- aws eks wait nodegroup-active --region us-east-1 --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group
- fi
- # Connect to the cluster and node group to match sure the Kubernetes versions match. If using custom AMIs then comment out the next two lines
- export NODE_VER=$(aws eks describe-nodegroup --region us-east-1 --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group | jq -r .nodegroup.version)
- export CLUSTER_VER=$(aws eks describe-cluster --region us-east-1 --name eks-cluster-$BITBUCKET_BRANCH | jq -r .cluster.version)
- # If they dont match then force an update on the node group to update the AMI. If using custom AMIs then comment out the IF STATEMENT below
- if [ "$NODE_VER" == "$CLUSTER_VER" ]; then
- echo Node group version matches cluster version
- else
- aws eks update-nodegroup-version --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group
- sleep 30
- aws eks wait nodegroup-active --region us-east-1 --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group
- # end the script
- fi
condition:
changesets:
includePaths:
- "eks-nodegroup.yml"
- "eks-nodegroup1-parm-$BITBUCKET_BRANCH.json"
- step:
name: EKS node group 2
script:
- export PARAMETERS=$(cat ./eks-nodegroup2-parm-$BITBUCKET_BRANCH.json)
- pipe: atlassian/aws-cloudformation-deploy:0.9.2
variables:
AWS_DEFAULT_REGION: "us-east-1"
STACK_NAME: "$BITBUCKET_BRANCH-eks-nodegroup-2"
TEMPLATE: "./eks-nodegroup.yml"
CAPABILITIES: ['CAPABILITY_NAMED_IAM']
WAIT: 'true'
STACK_PARAMETERS: $PARAMETERS
condition:
changesets:
includePaths:
- "eks-nodegroup.yml"
- "eks-nodegroup2-parm-$BITBUCKET_BRANCH.json"
- step:
# limitation with CF that you cant use LatestVersion in node group with Launch templates so needed to move this outside of CF
# Exporting the latest version to a variable then modifying the launch template and trigger a node group update on EKS
# Also checking to make sure the Cluster K8s version matches the node group AMI version
name: Update Node Group 2
image: atlassian/pipelines-awscli:1.16.302
script:
- # Install jq to parse json
- apk add jq
- # Read credentials from Bitbucket variables
- aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}"
- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"
- # Get the node group label from CloudFormation template parameter file
- export NODE_LABEL=$(cat ./eks-nodegroup2-parm-$BITBUCKET_BRANCH.json | jq -r '.[] | select(.ParameterKey=="Label") | .ParameterValue')
- # Connect to the node group and set variables for the default version and latest version
- export DEFAULT_VER=$(aws ec2 describe-launch-templates --launch-template-names $BITBUCKET_BRANCH-eks-$NODE_LABEL-launch-template | jq -r .LaunchTemplates[].DefaultVersionNumber)
- export LATEST_VER=$(aws ec2 describe-launch-templates --launch-template-names $BITBUCKET_BRANCH-eks-$NODE_LABEL-launch-template | jq -r .LaunchTemplates[].LatestVersionNumber)
- # If statement to compare version and modify the node group if they dont match
- if [ "$DEFAULT_VER" == "$LATEST_VER" ]; then
- echo DefaultVersion matches LatestVersion
- else
- aws ec2 modify-launch-template --launch-template-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-launch-template --default-version $LATEST_VER
- aws eks update-nodegroup-version --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group
- sleep 30
- # This waits for the node group to update before moving on in the pipeline
- aws eks wait nodegroup-active --region us-east-1 --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group
- fi
- # Connect to the cluster and node group to match sure the Kubernetes versions match. If using custom AMIs then comment out the next two lines
- export NODE_VER=$(aws eks describe-nodegroup --region us-east-1 --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group | jq -r .nodegroup.version)
- export CLUSTER_VER=$(aws eks describe-cluster --region us-east-1 --name eks-cluster-$BITBUCKET_BRANCH | jq -r .cluster.version)
- # If they dont match then force an update on the node group to update the AMI. If using custom AMIs then comment out the IF STATEMENT below
- if [ "$NODE_VER" == "$CLUSTER_VER" ]; then
- echo Node group version matches cluster version
- else
- aws eks update-nodegroup-version --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group
- sleep 30
- aws eks wait nodegroup-active --region us-east-1 --cluster-name eks-cluster-$BITBUCKET_BRANCH --nodegroup-name $BITBUCKET_BRANCH-eks-$NODE_LABEL-node-group
- # end the script
- fi
condition:
changesets:
includePaths:
- "eks-nodegroup.yml"
- "eks-nodegroup1-parm-$BITBUCKET_BRANCH.json"
- step:
name: K8s monitoring
image: alpine/k8s:1.18.2
script:
- aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}"
- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"
- aws --region us-east-1 eks update-kubeconfig --name eks-cluster-$BITBUCKET_BRANCH
- kubectl apply -f ./monitoring/k8s-metrics-server.yaml
- helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
- helm repo add grafana https://grafana.github.io/helm-charts
- helm repo update
- kubectl apply -f ./monitoring/monitoring_namespace.yaml
- helm upgrade --install prometheus prometheus-community/prometheus -n monitoring --values ./monitoring/prometheus-values.yaml
- helm upgrade --install loki-stack grafana/loki-stack -n monitoring --values ./monitoring/loki-values.yaml
- helm upgrade --install grafana grafana/grafana -n monitoring --values ./monitoring/grafana-values-$DEV_ENVIRONMENT.yaml
#condition:
# changesets:
# includePaths:
# - "k8s-metrics-server.yaml"
# - "prometheus-values.yaml"
# - "loki-values.yaml"
# - "grafana-values.yaml"
# Add the example command below to each pod that needs hpa enabled on
#kubectl autoscale deployment DEPLOYMENTNAME --cpu-percent=50 --min=1 --max=10
# Below will check to see if the HPA is enabled and details
#kubectl describe hpa
- step:
name: Cluster Autoscaler
image: alpine/k8s:1.18.2
script:
- aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}"
- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"
- aws --region us-east-1 eks update-kubeconfig --name eks-cluster-$BITBUCKET_BRANCH
- kubectl apply -f cluster-autoscaler-autodiscover_$BITBUCKET_BRANCH.yaml
- kubectl -n kube-system annotate --overwrite deployment.apps/cluster-autoscaler cluster-autoscaler.kubernetes.io/safe-to-evict="false"
- kubectl -n kube-system set image deployment.apps/cluster-autoscaler cluster-autoscaler=gcr.io/k8s-artifacts-prod/autoscaling/cluster-autoscaler:v1.17.4
condition:
changesets:
includePaths:
- "eks-cluster.yml"
- "eks-cluster-parm-$BITBUCKET_BRANCH.json"
- step:
name: Update ConfigMap
image: alpine/k8s:1.18.2
script:
- aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}"
- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"
- aws --region us-east-1 eks update-kubeconfig --name eks-cluster-$BITBUCKET_BRANCH
- 'SSOROLE=" - rolearn: arn:aws:iam::ACCOUNTID:role/ROLENAME\n username: build\n groups:\n - system:masters"'
- 'kubectl get -n kube-system configmap/aws-auth -o yaml | awk "/mapRoles: \|/{print;print \"$SSOROLE\";next}1" > /tmp/aws-auth-patch.yml'
- kubectl patch configmap/aws-auth -n kube-system --patch "$(cat /tmp/aws-auth-patch.yml)"
- 'IAMUSER=" mapUsers: \|\n - userarn: arn:aws:iam::ACCOUNTID:user/bitbucket-eks-admin\n username: bitbucket-eks-admin\n groups:\n - system:masters"'
- 'kubectl get -n kube-system configmap/aws-auth -o yaml | awk "/data:/{print;print \"$IAMUSER\";next}1" > /tmp/aws-auth-patch.yml'
- kubectl patch configmap/aws-auth -n kube-system --patch "$(cat /tmp/aws-auth-patch.yml)"
condition:
changesets:
includePaths:
- "sponsor-portal-eks-cluster.yml"
- "sponsor-portal-eks-cluster-parm-$DEV_ENVIRONMENT.json"
- step:
name: OpenID connect for Ingress Controller
image: alpine/k8s:1.18.2
script:
- aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}"
- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"
- aws --region us-east-1 eks update-kubeconfig --name eks-cluster-$BITBUCKET_BRANCH
- eksctl utils associate-iam-oidc-provider --cluster=eks-cluster-$BITBUCKET_BRANCH --approve
condition:
changesets:
includePaths:
- "eks-cluster.yml"
- "eks-cluster-parm-$BITBUCKET_BRANCH.json"
- step:
name: Ingress Controller IAM role
script:
- pipe: atlassian/aws-cloudformation-deploy:0.9.2
variables:
AWS_DEFAULT_REGION: "us-east-1"
STACK_NAME: "$BITBUCKET_BRANCH-ingress-controller"
TEMPLATE: "./eks-ingress-controller.yml"
WAIT: 'true'
CAPABILITIES: ['CAPABILITY_IAM', 'CAPABILITY_AUTO_EXPAND', 'CAPABILITY_NAMED_IAM']
STACK_PARAMETERS: >
[{
"ParameterKey": "EnvironmentName",
"ParameterValue": $BITBUCKET_BRANCH
}]
condition:
changesets:
includePaths:
- "eks-ingress-controller.yml"
- "eks-cluster.yml"
- "eks-cluster-parm-$BITBUCKET_BRANCH.json"
- step:
name: RBAC policy for Ingress controller
script:
- pipe: atlassian/aws-eks-kubectl-run:1.2.0
variables:
AWS_DEFAULT_REGION: "us-east-1"
CLUSTER_NAME: eks-cluster-$BITBUCKET_BRANCH
KUBECTL_COMMAND: 'apply'
RESOURCE_PATH: 'alb-ingress-rbac.yml'
condition:
changesets:
includePaths:
- "alb-ingress-rbac.yml"
- "eks-cluster.yml"
- "eks-cluster-parm-$BITBUCKET_BRANCH.json"
- step:
name: Associate IAM role to EKS Service account for ingress controller
image: alpine/k8s:1.18.2
script:
- aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}"
- aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}"
- aws --region us-east-1 eks update-kubeconfig --name eks-cluster-$BITBUCKET_BRANCH
- kubectl annotate serviceaccount -n kube-system alb-ingress-controller eks.amazonaws.com/role-arn=arn:aws:iam::ACCOUNTID:role/eks-alb-ingress-controller-$BITBUCKET_BRANCH --overwrite
condition:
changesets:
includePaths:
- "alb-ingress-rbac.yml"
- "eks-cluster.yml"
- "eks-cluster-parm-$BITBUCKET_BRANCH.json"
- step:
name: Deploy Ingress controller
script:
- pipe: atlassian/aws-eks-kubectl-run:1.2.0
variables:
AWS_DEFAULT_REGION: "us-east-1"
CLUSTER_NAME: eks-cluster-$BITBUCKET_BRANCH
KUBECTL_COMMAND: 'apply'
RESOURCE_PATH: 'alb-ingress-controller-$BITBUCKET_BRANCH.yml'
condition:
changesets:
includePaths:
- "alb-ingress-controller-$BITBUCKET_BRANCH.yml"
- "alb-ingress-rbac.yml"
- "eks-cluster.yml"
- "eks-cluster-parm-$BITBUCKET_BRANCH.json"