From f8efb00ff3b8d8beb8d0af00f419eb217ce1a34d Mon Sep 17 00:00:00 2001 From: Brandon Wagner Date: Wed, 18 Aug 2021 10:47:00 -0500 Subject: [PATCH] fix k8s event patch permission and add test (#480) --- .../templates/clusterrole.yaml | 1 + test/e2e/emit-events-test | 123 ++++++++++++++++++ test/k8s-local-cluster-test/run-test | 1 + 3 files changed, 125 insertions(+) create mode 100755 test/e2e/emit-events-test diff --git a/config/helm/aws-node-termination-handler/templates/clusterrole.yaml b/config/helm/aws-node-termination-handler/templates/clusterrole.yaml index 32a385db..8418ff3e 100644 --- a/config/helm/aws-node-termination-handler/templates/clusterrole.yaml +++ b/config/helm/aws-node-termination-handler/templates/clusterrole.yaml @@ -44,4 +44,5 @@ rules: - events verbs: - create + - patch {{- end }} diff --git a/test/e2e/emit-events-test b/test/e2e/emit-events-test new file mode 100755 index 00000000..c0611fc5 --- /dev/null +++ b/test/e2e/emit-events-test @@ -0,0 +1,123 @@ +#!/bin/bash +set -euo pipefail + +# Available env vars: +# $TMP_DIR +# $CLUSTER_NAME +# $KUBECONFIG +# $NODE_TERMINATION_HANDLER_DOCKER_REPO +# $NODE_TERMINATION_HANDLER_DOCKER_TAG +# $WEBHOOK_DOCKER_REPO +# $WEBHOOK_DOCKER_TAG +# $AEMM_URL +# $AEMM_VERSION + +function fail_and_exit { + echo "❌ K8s Emit Events Test failed $CLUSTER_NAME ❌" + exit ${1:-1} +} + +echo "Starting K8s Emit Events Test for Node Termination Handler" + +SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" + +common_helm_args=() +[[ "${TEST_WINDOWS-}" == "true" ]] && common_helm_args+=(--set targetNodeOs="windows") +[[ -n "${NTH_WORKER_LABEL-}" ]] && common_helm_args+=(--set nodeSelector."$NTH_WORKER_LABEL") + +anth_helm_args=( + upgrade + --install + "$CLUSTER_NAME-anth" + "$SCRIPTPATH/../../config/helm/aws-node-termination-handler/" + --force + --namespace kube-system + --set instanceMetadataURL="${INSTANCE_METADATA_URL:-"http://$AEMM_URL:$IMDS_PORT"}" + --set image.repository="$NODE_TERMINATION_HANDLER_DOCKER_REPO" + --set image.tag="$NODE_TERMINATION_HANDLER_DOCKER_TAG" + --set enableSpotInterruptionDraining="true" + --set enableScheduledEventDraining="true" + --set emitKubernetesEvents="true" +) +[[ -n "${NODE_TERMINATION_HANDLER_DOCKER_PULL_POLICY-}" ]] && + anth_helm_args+=(--set image.pullPolicy="$NODE_TERMINATION_HANDLER_DOCKER_PULL_POLICY") +[[ ${#common_helm_args[@]} -gt 0 ]] && + anth_helm_args+=("${common_helm_args[@]}") + +set -x +helm "${anth_helm_args[@]}" +set +x + +emtp_helm_args=( + upgrade + --install + "$CLUSTER_NAME-emtp" + "$SCRIPTPATH/../../config/helm/webhook-test-proxy/" + --wait + --namespace default + --set webhookTestProxy.image.repository="$WEBHOOK_DOCKER_REPO" + --set webhookTestProxy.image.tag="$WEBHOOK_DOCKER_TAG" +) +[[ -n "${WEBHOOK_DOCKER_PULL_POLICY-}" ]] && + emtp_helm_args+=(--set webhookTestProxy.image.pullPolicy="$WEBHOOK_DOCKER_PULL_POLICY") +[[ ${#common_helm_args[@]} -gt 0 ]] && + emtp_helm_args+=("${common_helm_args[@]}") + +set -x +helm "${emtp_helm_args[@]}" +set +x + +aemm_helm_args=( + upgrade + --install + "$CLUSTER_NAME-aemm" + "$AEMM_DL_URL" + --wait + --namespace default + --set aemm.IMDSv2="true" + --set servicePort="$IMDS_PORT" +) +[[ ${#common_helm_args[@]} -gt 0 ]] && + aemm_helm_args+=("${common_helm_args[@]}") + +set -x +retry 5 helm "${aemm_helm_args[@]}" +set +x + +TAINT_CHECK_CYCLES=15 +TAINT_CHECK_SLEEP=15 + +DEPLOYED=0 + +for i in `seq 1 $TAINT_CHECK_CYCLES`; do + if [[ $(kubectl get deployments regular-pod-test -o jsonpath='{.status.unavailableReplicas}') -eq 0 ]]; then + echo "✅ Verified regular-pod-test pod was scheduled and started!" + DEPLOYED=1 + break + fi + echo "Setup Loop $i/$TAINT_CHECK_CYCLES, sleeping for $TAINT_CHECK_SLEEP seconds" + sleep $TAINT_CHECK_SLEEP +done + +if [[ $DEPLOYED -eq 0 ]]; then + echo "❌ regular-pod-test pod deployment failed" + fail_and_exit 2 +fi + +test_node="${TEST_NODE:-$CLUSTER_NAME-worker}" +for i in `seq 1 $TAINT_CHECK_CYCLES`; do + if kubectl get events | tr -s " " | grep "CordonAndDrain node/${test_node} Node successfully cordoned and drained" >/dev/null; then + echo "✅ Verified CordonAndDrain was emitted as a k8s event!" + echo "✅ K8s Emit Events Test Passed $CLUSTER_NAME! ✅"s + exit 0 + fi + + echo "Assertion Loop $i/$TAINT_CHECK_CYCLES, sleeping for $TAINT_CHECK_SLEEP seconds" + sleep $TAINT_CHECK_SLEEP +done + + +echo "❌ k8s CordonAndDrain event was not emitted to k8s" + +echo "❌ K8s Emit Events Test failed $CLUSTER_NAME ❌" +fail_and_exit 1 diff --git a/test/k8s-local-cluster-test/run-test b/test/k8s-local-cluster-test/run-test index 942f6803..ddd02d01 100755 --- a/test/k8s-local-cluster-test/run-test +++ b/test/k8s-local-cluster-test/run-test @@ -91,6 +91,7 @@ function reset_cluster { kubectl taint node "$node" aws-node-termination-handler/rebalance-recommendation- || true done remove_labels || : + kubectl delete events --all sleep 2 }