|
| 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