|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Available env vars: |
| 5 | +# $TMP_DIR |
| 6 | +# $CLUSTER_NAME |
| 7 | +# $KUBECONFIG |
| 8 | +# $NODE_TERMINATION_HANDLER_DOCKER_REPO |
| 9 | +# $NODE_TERMINATION_HANDLER_DOCKER_TAG |
| 10 | +# $WEBHOOK_DOCKER_REPO |
| 11 | +# $WEBHOOK_DOCKER_TAG |
| 12 | +# $AEMM_URL |
| 13 | +# $AEMM_VERSION |
| 14 | + |
| 15 | +function fail_and_exit { |
| 16 | + echo "❌ Rebalance Recommendation SQS Test failed $CLUSTER_NAME ❌" |
| 17 | + exit "${1:-1}" |
| 18 | +} |
| 19 | + |
| 20 | +echo "Starting Rebalance Recommendation SQS Test for Node Termination Handler" |
| 21 | +START_TIME=$(date -u +"%Y-%m-%dT%TZ") |
| 22 | + |
| 23 | +SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" |
| 24 | + |
| 25 | +common_helm_args=() |
| 26 | + |
| 27 | +localstack_helm_args=( |
| 28 | + upgrade |
| 29 | + --install |
| 30 | + --namespace default |
| 31 | + "$CLUSTER_NAME-localstack" |
| 32 | + "$SCRIPTPATH/../../config/helm/localstack/" |
| 33 | + --set nodeSelector."${NTH_CONTROL_LABEL}" |
| 34 | + --set defaultRegion="${AWS_REGION}" |
| 35 | + --wait |
| 36 | +) |
| 37 | + |
| 38 | +set -x |
| 39 | +helm "${localstack_helm_args[@]}" |
| 40 | +set +x |
| 41 | + |
| 42 | +sleep 10 |
| 43 | + |
| 44 | +localstack_pod=$(kubectl get pods --selector app=localstack --field-selector="status.phase=Running" \ |
| 45 | + -o go-template --template '{{range .items}}{{.metadata.name}} {{.metadata.creationTimestamp}}{{"\n"}}{{end}}' \ |
| 46 | + | awk '$2 >= "'"${START_TIME//+0000/Z}"'" { print $1 }') |
| 47 | +echo "🥑 Using localstack pod ${localstack_pod}" |
| 48 | + |
| 49 | +run_instances_resp=$(kubectl exec -i "${localstack_pod}" -- bash -c "awslocal ec2 run-instances --private-ip-address 192.168.0.4 --region ${AWS_REGION}") |
| 50 | +instance_id=$(echo "${run_instances_resp}" | jq -r '.Instances[] .InstanceId') |
| 51 | +echo "🥑 Created awslocal EC2 instance (${instance_id}) not backed by a node" |
| 52 | + |
| 53 | +CREATE_SQS_CMD="awslocal sqs create-queue --queue-name "${CLUSTER_NAME}-queue" --attributes MessageRetentionPeriod=300 --region ${AWS_REGION}" |
| 54 | +queue_url=$(kubectl exec -i "${localstack_pod}" -- bash -c "${CREATE_SQS_CMD}" | jq -r .QueueUrl) |
| 55 | + |
| 56 | +echo "🥑 Created SQS Queue ${queue_url}" |
| 57 | + |
| 58 | +anth_helm_args=( |
| 59 | + upgrade |
| 60 | + --install |
| 61 | + --namespace kube-system |
| 62 | + "$CLUSTER_NAME-acth" |
| 63 | + "$SCRIPTPATH/../../config/helm/aws-node-termination-handler/" |
| 64 | + --set image.repository="$NODE_TERMINATION_HANDLER_DOCKER_REPO" |
| 65 | + --set image.tag="$NODE_TERMINATION_HANDLER_DOCKER_TAG" |
| 66 | + --set nodeSelector."${NTH_CONTROL_LABEL}" |
| 67 | + --set tolerations[0].operator=Exists |
| 68 | + --set awsAccessKeyID=foo |
| 69 | + --set awsSecretAccessKey=bar |
| 70 | + --set awsRegion="${AWS_REGION}" |
| 71 | + --set awsEndpoint="http://localstack.default" |
| 72 | + --set checkTagBeforeDraining=false |
| 73 | + --set enableSqsTerminationDraining=true |
| 74 | + --set deleteSqsMsgIfNodeNotFound=true |
| 75 | + --set taintNode="true" |
| 76 | + --set "queueURL=${queue_url}" |
| 77 | + --wait |
| 78 | +) |
| 79 | +[[ -n "${NODE_TERMINATION_HANDLER_DOCKER_PULL_POLICY-}" ]] && |
| 80 | + anth_helm_args+=(--set image.pullPolicy="$NODE_TERMINATION_HANDLER_DOCKER_PULL_POLICY") |
| 81 | +[[ ${#common_helm_args[@]} -gt 0 ]] && |
| 82 | + anth_helm_args+=("${common_helm_args[@]}") |
| 83 | + |
| 84 | +set -x |
| 85 | +helm "${anth_helm_args[@]}" |
| 86 | +set +x |
| 87 | + |
| 88 | +emtp_helm_args=( |
| 89 | + upgrade |
| 90 | + --install |
| 91 | + --namespace default |
| 92 | + "$CLUSTER_NAME-emtp" |
| 93 | + "$SCRIPTPATH/../../config/helm/webhook-test-proxy/" |
| 94 | + --set webhookTestProxy.image.repository="$WEBHOOK_DOCKER_REPO" |
| 95 | + --set webhookTestProxy.image.tag="$WEBHOOK_DOCKER_TAG" |
| 96 | + --wait |
| 97 | +) |
| 98 | +[[ -n "${WEBHOOK_DOCKER_PULL_POLICY-}" ]] && |
| 99 | + emtp_helm_args+=(--set webhookTestProxy.image.pullPolicy="$WEBHOOK_DOCKER_PULL_POLICY") |
| 100 | +[[ ${#common_helm_args[@]} -gt 0 ]] && |
| 101 | + emtp_helm_args+=("${common_helm_args[@]}") |
| 102 | + |
| 103 | +set -x |
| 104 | +helm "${emtp_helm_args[@]}" |
| 105 | +set +x |
| 106 | + |
| 107 | +CHECK_CYCLES=15 |
| 108 | +CHECK_SLEEP=15 |
| 109 | + |
| 110 | +DEPLOYED=0 |
| 111 | + |
| 112 | +for i in $(seq 1 $CHECK_CYCLES); do |
| 113 | + if [[ $(kubectl get deployments regular-pod-test -o jsonpath='{.status.unavailableReplicas}') -eq 0 ]]; then |
| 114 | + echo "✅ Verified regular-pod-test pod was scheduled and started!" |
| 115 | + DEPLOYED=1 |
| 116 | + break |
| 117 | + fi |
| 118 | + echo "Setup Loop $i/$CHECK_CYCLES, sleeping for $CHECK_SLEEP seconds" |
| 119 | + sleep $CHECK_SLEEP |
| 120 | +done |
| 121 | + |
| 122 | +if [[ $DEPLOYED -eq 0 ]]; then |
| 123 | + echo "❌ regular-pod-test pod deployment failed" |
| 124 | + fail_and_exit 2 |
| 125 | +fi |
| 126 | + |
| 127 | +REBALANCE_EVENT=$(cat <<EOF |
| 128 | +{ |
| 129 | + "version": "0", |
| 130 | + "id": "5d5555d5-dd55-5555-5555-5555dd55d55d", |
| 131 | + "detail-type": "EC2 Instance Rebalance Recommendation", |
| 132 | + "source": "aws.ec2", |
| 133 | + "account": "123456789012", |
| 134 | + "time": "$(date -u +"%Y-%m-%dT%TZ")", |
| 135 | + "region": "us-east-1", |
| 136 | + "resources": [ |
| 137 | + "arn:aws:ec2:us-east-1b:instance/${instance_id}" |
| 138 | + ], |
| 139 | + "detail": { |
| 140 | + "instance-id": "${instance_id}" |
| 141 | + } |
| 142 | +} |
| 143 | +EOF |
| 144 | +) |
| 145 | + |
| 146 | +REBALANCE_EVENT_ONE_LINE=$(echo "${REBALANCE_EVENT}" | tr -d '\n' |sed 's/\"/\\"/g') |
| 147 | +SEND_SQS_CMD="awslocal sqs send-message --queue-url ${queue_url} --message-body \"${REBALANCE_EVENT_ONE_LINE}\" --region ${AWS_REGION}" |
| 148 | +kubectl exec -i "${localstack_pod}" -- bash -c "${SEND_SQS_CMD}" |
| 149 | +echo "✅ Sent Rebalance Recommendation to SQS queue: ${queue_url}" |
| 150 | + |
| 151 | +GET_ATTRS_SQS_CMD="awslocal sqs get-queue-attributes --queue-url ${queue_url} --attribute-names All --region ${AWS_REGION}" |
| 152 | + |
| 153 | +for i in $(seq 1 $CHECK_CYCLES); do |
| 154 | + if [[ $(kubectl exec -i "${localstack_pod}" -- bash -c "${GET_ATTRS_SQS_CMD}" | jq '(.Attributes.ApproximateNumberOfMessagesNotVisible|tonumber) + (.Attributes.ApproximateNumberOfMessages|tonumber)' ) -eq 0 ]]; then |
| 155 | + kubectl exec -i "${localstack_pod}" -- bash -c "${GET_ATTRS_SQS_CMD}" |
| 156 | + echo "✅ Verified the message was deleted from the queue after processing!" |
| 157 | + echo "✅ Rebalance Recommendation SQS Test Passed $CLUSTER_NAME! ✅" |
| 158 | + exit 0 |
| 159 | + fi |
| 160 | + |
| 161 | + echo "Assertion Loop $i/$CHECK_CYCLES, sleeping for $CHECK_SLEEP seconds" |
| 162 | + sleep $CHECK_SLEEP |
| 163 | +done |
| 164 | + |
| 165 | +echo "❌ message was not removed from the queue after processing" |
| 166 | +fail_and_exit 3 |
0 commit comments