-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdk-cli-wrapper-dev.sh
executable file
·48 lines (39 loc) · 2.29 KB
/
cdk-cli-wrapper-dev.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
#!/bin/bash
# Get script location.
SHELL_PATH=$(cd "$(dirname "$0")";pwd)
CDK_CMD=$1
CDK_ACC="$(aws sts get-caller-identity --output text --query 'Account')"
CDK_REGION="$(aws configure get region)"
cdk bootstrap aws://${CDK_ACC}/${CDK_REGION}
$SHELL_PATH/cdk-cli-wrapper.sh ${CDK_ACC} ${CDK_REGION} "$@"
# Deployment post process.
eks_ca_yaml=$SHELL_PATH/cdk.out/cluster-autoscaler-autodiscover.yaml
if [ "$CDK_CMD" == "deploy" ] && [ ! -f "$eks_ca_yaml" ]; then
stack_name="$(cdk ls)"
eks_cluster_name="$(aws cloudformation describe-stacks \
--stack-name ${stack_name} \
--query "Stacks[0].Outputs" \
--output json | jq -rc '.[] | select(.OutputKey=="EksClusterName") | .OutputValue '
)"
eks_ca_role_arn="$(aws cloudformation describe-stacks \
--stack-name ${stack_name} \
--query "Stacks[0].Outputs" \
--output json | jq -rc '.[] | select(.OutputKey=="EksCARoleArn") | .OutputValue '
)"
# Update kubeconfig
aws eks update-kubeconfig --region ${CDK_REGION} --name ${eks_cluster_name}
# Install Cluster Autoscaler
cluster_autoscaler_version="1.21.2"
curl -o ${eks_ca_yaml}.origin https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yaml
sed -i '/<YOUR CLUSTER NAME>/a \ - --skip-nodes-with-system-pods=false' ${eks_ca_yaml}.origin
sed -i '/<YOUR CLUSTER NAME>/a \ - --balance-similar-node-groups' ${eks_ca_yaml}.origin
sed "s/<YOUR CLUSTER NAME>/${eks_cluster_name}/g" ${eks_ca_yaml}.origin > ${eks_ca_yaml}
kubectl apply -f ${eks_ca_yaml}
kubectl annotate --overwrite serviceaccount cluster-autoscaler -n kube-system eks.amazonaws.com/role-arn=${eks_ca_role_arn}
kubectl patch deployment cluster-autoscaler -n kube-system -p '{"spec":{"template":{"metadata":{"annotations":{"cluster-autoscaler.kubernetes.io/safe-to-evict": "false"}}}}}'
kubectl set image deployment cluster-autoscaler -n kube-system cluster-autoscaler=k8s.gcr.io/autoscaling/cluster-autoscaler:v${cluster_autoscaler_version}
fi
# Destroy post process.
if [ "$CDK_CMD" == "destroy" ]; then
rm -rf $SHELL_PATH/cdk.out/
fi