1+ ---
2+ apiVersion : tekton.dev/v1beta1
3+ kind : Task
4+ metadata :
5+ name : awscli-eks-cfn-launch-template-al2023
6+ namespace : scalability
7+ spec :
8+ description : |
9+ Create an EKS CFN stack to output a launch template for AL2023-based nodes.
10+ This Task can be used to create an EKS CFN stack that outputs a launch template.
11+ The launch template may be used for a managed nodegroup with or without a custom AMI.
12+ params :
13+ - name : cluster-name
14+ description : EKS cluster you want to create CFN stack for.
15+ - name : stack-name
16+ description : Stack name you want to spin.
17+ - name : region
18+ default : " us-west-2"
19+ description : The region where the cluster is in.
20+ - name : kubernetes-version
21+ default : " 1.28"
22+ description : The EKS version to install.
23+ - name : ng-cfn-url
24+ description : The url of the CFN YAML/JSON to create CFN stack for NG launch template
25+ - name : endpoint
26+ default : " "
27+ - name : kubelet-config
28+ default : " {}"
29+ - name : ami
30+ default : " "
31+ description : The AMI ID (or SSM parameter) to use for the launch template. If not provided, the launch template will not specify an AMI.
32+ workspaces :
33+ - name : config
34+ mountPath : /config/
35+ stepTemplate :
36+ env :
37+ - name : KUBECONFIG
38+ value : /config/kubeconfig
39+ steps :
40+ - name : create-launch-template
41+ image : alpine/k8s:1.23.7
42+ script : |
43+ set -o xtrace
44+ set -o errexit
45+ set -o pipefail
46+
47+ ENDPOINT_FLAG=""
48+ if [ -n "$(params.endpoint)" ]; then
49+ ENDPOINT_FLAG="--endpoint $(params.endpoint)"
50+ fi
51+
52+ curl -s $(params.ng-cfn-url) -o ./amazon-ng-cfn
53+
54+ SSH_KEY_NAME=scaletest-nodegroups-ssh-key
55+ if [[ "$(aws ec2 --region "$(params.region)" describe-key-pairs --key-names "$SSH_KEY_NAME" --query 'KeyPairs[0].KeyName' --output text)" == "$SSH_KEY_NAME" ]]; then
56+ echo "KeyPair '$SSH_KEY_NAME' already exists."
57+ else
58+ echo "KeyPair not found. Creating a new keypair."
59+ # Given these are temp nodes, outputting key for devs to copy it to use for debugging
60+ #ToDo - store it in s3 for devs to download it.
61+ aws ec2 create-key-pair --region $(params.region) --key-name $SSH_KEY_NAME --query 'KeyMaterial' --output text
62+ fi
63+
64+ aws eks describe-cluster --name $(params.cluster-name) --region $(params.region) --output json > cluster.json
65+
66+ launch_template_name=$(params.cluster-name)-launchTemplate
67+ STACK_NAME=$(params.stack-name)
68+ STACK_STATUS=$(aws cloudformation describe-stacks --query 'Stacks[?StackName==`'${STACK_NAME}'`].StackStatus' --output text --region $(params.region))
69+
70+ # assemble the stack parameters as a JSON file
71+ # the AWS CLI can't handle a JSON string as a ParameterValue in the flag representation
72+ # and we need that for kubelet-config
73+ jq --null-input \
74+ --arg LaunchTemplateName "${launch_template_name}" \
75+ --arg ClusterName "$(params.cluster-name)" \
76+ --arg SSHKeyName "${SSH_KEY_NAME}" \
77+ --arg APIServerEndpoint "$(jq -r .cluster.endpoint cluster.json)" \
78+ --arg ClusterCIDR "$(jq -r .cluster.kubernetesNetworkConfig.serviceIpv4Cidr cluster.json)" \
79+ --arg CertificateAuthority "$(jq -r .cluster.certificateAuthority.data cluster.json)" \
80+ --arg KubeletConfig '$(params.kubelet-config)' \
81+ --arg AMI "$(params.ami)" \
82+ '$ARGS.named | to_entries | map({"ParameterKey": .key, "ParameterValue": .value})' \
83+ > parameters.json
84+
85+ if [[ "$STACK_STATUS" == "" ]]; then
86+ aws cloudformation create-stack \
87+ --stack-name $STACK_NAME \
88+ --template-body file://$(pwd)/amazon-ng-cfn \
89+ --parameters file://$(pwd)/parameters.json \
90+ --region $(params.region)
91+
92+ aws cloudformation wait stack-create-complete --stack-name $STACK_NAME --region $(params.region)
93+ echo "CREATED_CFN_STACK=$STACK_NAME"
94+ else
95+ echo "$STACK_NAME Already exists"
96+ fi
0 commit comments