Skip to content

Commit cb3ee5c

Browse files
cartermckinnonhakuna-matatah
authored andcommitted
Add AL2023 launch template task
1 parent 627e023 commit cb3ee5c

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
AWSTemplateFormatVersion: '2010-09-09'
3+
Description: Create a launch template for use in an autoscaling group of EKS nodes
4+
(Amazon Linux 2023)
5+
Parameters:
6+
LaunchTemplateName:
7+
Type: String
8+
Description: Name of the Launch Template
9+
ClusterName:
10+
Type: String
11+
Description: Name of the Cluster
12+
SSHKeyName:
13+
Type: String
14+
Description: SSH Key Name for EC2 instances
15+
APIServerEndpoint:
16+
Type: String
17+
Description: Kubernetes API Server Endpoint
18+
CertificateAuthority:
19+
Type: String
20+
Description: Certificate Authority data (base64 encoded)
21+
ClusterCIDR:
22+
Type: String
23+
Description: CIDR for cluster (IP range for pods)
24+
KubeletConfig:
25+
Type: String
26+
Description: Kubelet config JSON (will be merged with default config)
27+
Default: '{}'
28+
AMI:
29+
Type: String
30+
Description: Launch template ImageId value, which may be an AMI ID or resolve:ssm reference.
31+
Default: ''
32+
Conditions:
33+
AMIProvided:
34+
!Not [!Equals [!Ref AMI, '']]
35+
Resources:
36+
LaunchTemplate:
37+
Type: AWS::EC2::LaunchTemplate
38+
Properties:
39+
LaunchTemplateName:
40+
Ref: LaunchTemplateName
41+
LaunchTemplateData:
42+
KeyName:
43+
Ref: SSHKeyName
44+
BlockDeviceMappings:
45+
- DeviceName: "/dev/xvda"
46+
Ebs:
47+
VolumeSize: 40
48+
VolumeType: gp3
49+
MetadataOptions:
50+
HttpPutResponseHopLimit: 2
51+
HttpEndpoint: enabled
52+
HttpTokens: required
53+
ImageId:
54+
!If
55+
- AMIProvided
56+
- !Ref AMI
57+
- !Ref "AWS::NoValue"
58+
UserData:
59+
Fn::Base64:
60+
Fn::Sub: |
61+
Content-Type: multipart/mixed; boundary="BOUNDARY"
62+
MIME-Version: 1.0
63+
64+
--BOUNDARY
65+
Content-Type: application/node.eks.aws
66+
MIME-Version: 1.0
67+
68+
---
69+
apiVersion: node.eks.aws/v1alpha1
70+
kind: NodeConfig
71+
spec:
72+
cluster:
73+
name: ${ClusterName}
74+
apiServerEndpoint: ${APIServerEndpoint}
75+
certificateAuthority: ${CertificateAuthority}
76+
cidr: ${ClusterCIDR}
77+
kubelet:
78+
config: ${KubeletConfig}
79+
80+
--BOUNDARY--
81+
Outputs:
82+
LaunchTemplateName:
83+
Description: Name of the Node Group Launch Template
84+
Value:
85+
Ref: LaunchTemplate
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.32"
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+
description: "Kubelet config JSON (will be merged with default config)"
30+
- name: ami
31+
default: ""
32+
description: The AMI ID (or SSM parameter) to use for the launch template. If not provided, the launch template will not specify an AMI.
33+
workspaces:
34+
- name: config
35+
mountPath: /config/
36+
stepTemplate:
37+
env:
38+
- name: KUBECONFIG
39+
value: /config/kubeconfig
40+
steps:
41+
- name: create-launch-template
42+
image: alpine/k8s:1.23.7
43+
script: |
44+
set -o xtrace
45+
set -o errexit
46+
set -o pipefail
47+
48+
ENDPOINT_FLAG=""
49+
if [ -n "$(params.endpoint)" ]; then
50+
ENDPOINT_FLAG="--endpoint $(params.endpoint)"
51+
fi
52+
53+
curl -s $(params.ng-cfn-url) -o ./amazon-ng-cfn
54+
55+
SSH_KEY_NAME=scaletest-nodegroups-ssh-key
56+
if [[ "$(aws ec2 --region "$(params.region)" describe-key-pairs --key-names "$SSH_KEY_NAME" --query 'KeyPairs[0].KeyName' --output text)" == "$SSH_KEY_NAME" ]]; then
57+
echo "KeyPair '$SSH_KEY_NAME' already exists."
58+
else
59+
echo "KeyPair not found. Creating a new keypair."
60+
# Given these are temp nodes, outputting key for devs to copy it to use for debugging
61+
#ToDo - store it in s3 for devs to download it.
62+
aws ec2 create-key-pair --region $(params.region) --key-name $SSH_KEY_NAME --query 'KeyMaterial' --output text
63+
fi
64+
65+
aws eks describe-cluster --name $(params.cluster-name) --region $(params.region) --output json > cluster.json
66+
67+
launch_template_name=$(params.cluster-name)-launchTemplate
68+
STACK_NAME=$(params.stack-name)
69+
STACK_STATUS=$(aws cloudformation describe-stacks --query 'Stacks[?StackName==`'${STACK_NAME}'`].StackStatus' --output text --region $(params.region))
70+
71+
# assemble the stack parameters as a JSON file
72+
# the AWS CLI can't handle a JSON string as a ParameterValue in the flag representation
73+
# and we need that for kubelet-config
74+
jq --null-input \
75+
--arg LaunchTemplateName "${launch_template_name}" \
76+
--arg ClusterName "$(params.cluster-name)" \
77+
--arg SSHKeyName "${SSH_KEY_NAME}" \
78+
--arg APIServerEndpoint "$(jq -r .cluster.endpoint cluster.json)" \
79+
--arg ClusterCIDR "$(jq -r .cluster.kubernetesNetworkConfig.serviceIpv4Cidr cluster.json)" \
80+
--arg CertificateAuthority "$(jq -r .cluster.certificateAuthority.data cluster.json)" \
81+
--arg KubeletConfig '$(params.kubelet-config)' \
82+
--arg AMI "$(params.ami)" \
83+
'$ARGS.named | to_entries | map({"ParameterKey": .key, "ParameterValue": .value})' \
84+
> parameters.json
85+
86+
if [[ "$STACK_STATUS" == "" ]]; then
87+
aws cloudformation create-stack \
88+
--stack-name $STACK_NAME \
89+
--template-body file://$(pwd)/amazon-ng-cfn \
90+
--parameters file://$(pwd)/parameters.json \
91+
--region $(params.region)
92+
93+
aws cloudformation wait stack-create-complete --stack-name $STACK_NAME --region $(params.region)
94+
echo "CREATED_CFN_STACK=$STACK_NAME"
95+
else
96+
echo "$STACK_NAME Already exists"
97+
fi

0 commit comments

Comments
 (0)