Skip to content

Commit

Permalink
Ensure to have kops_ and eksctl_ prefixes in bash functions for CI (
Browse files Browse the repository at this point in the history
#394)

By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice.

---------

Signed-off-by: Burak Varlı <burakvar@amazon.co.uk>
  • Loading branch information
unexge authored Feb 25, 2025
1 parent 3f790e0 commit 930f8a8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
1 change: 1 addition & 0 deletions .github/workflows/delete-cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ jobs:
- name: Delete cluster
env:
ACTION: "delete_cluster"
FORCE: "true"
run: |
tests/e2e-kubernetes/scripts/run.sh
19 changes: 10 additions & 9 deletions tests/e2e-kubernetes/scripts/eksctl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function eksctl_install() {
fi
}

function is_cluster_too_old() {
function eksctl_is_cluster_too_old() {
CLUSTER_NAME=${1}
REGION=${2}

Expand All @@ -27,7 +27,7 @@ function is_cluster_too_old() {
return $?
}

function compute_cluster_spec_hash() {
function eksctl_compute_cluster_spec_hash() {
NODE_TYPE=${1}
ZONES=${2}
EKSCTL_PATCH_SELINUX_ENFORCING_FILE=${3}
Expand All @@ -36,7 +36,7 @@ function compute_cluster_spec_hash() {
}

# Checks whether existing cluster matches with expected specs to decide whether to re-use it.
function cluster_matches_specs() {
function eksctl_cluster_matches_specs() {
CLUSTER_NAME=${1}
REGION=${2}
DESIRED_HASH=${3}
Expand All @@ -61,18 +61,18 @@ function eksctl_create_cluster() {
K8S_VERSION=${12}
EKSCTL_PATCH_SELINUX_ENFORCING_FILE=${13}

CLUSTER_SPEC_HASH=$(compute_cluster_spec_hash "${NODE_TYPE}" "${ZONES}" "${EKSCTL_PATCH_SELINUX_ENFORCING_FILE}")
CLUSTER_SPEC_HASH=$(eksctl_compute_cluster_spec_hash "${NODE_TYPE}" "${ZONES}" "${EKSCTL_PATCH_SELINUX_ENFORCING_FILE}")

# Check if cluster exists and matches our specs
if eksctl_cluster_exists "${BIN}" "${CLUSTER_NAME}"; then
if ! is_cluster_too_old "${CLUSTER_NAME}" "${REGION}" && \
cluster_matches_specs "${CLUSTER_NAME}" "${REGION}" "${CLUSTER_SPEC_HASH}"; then
if ! eksctl_is_cluster_too_old "${CLUSTER_NAME}" "${REGION}" && \
eksctl_cluster_matches_specs "${CLUSTER_NAME}" "${REGION}" "${CLUSTER_SPEC_HASH}"; then
echo "Reusing existing cluster ${CLUSTER_NAME} as it matches specifications and it is not too old"
return 0
fi

echo "Existing cluster ${CLUSTER_NAME} is either too old or doesn't match specifications. Re-creating..."
eksctl_delete_cluster "$BIN" "$CLUSTER_NAME" "$REGION"
eksctl_delete_cluster "$BIN" "$CLUSTER_NAME" "$REGION" "true"
fi

# CAUTION: this may fail with "the targeted availability zone, does not currently have sufficient capacity to support the cluster" error, we may require a fix for that
Expand Down Expand Up @@ -109,13 +109,14 @@ function eksctl_delete_cluster() {
BIN=${1}
CLUSTER_NAME=${2}
REGION=${3}
FORCE=${4:-false}

if ! eksctl_cluster_exists "${BIN}" "${CLUSTER_NAME}"; then
return 0
fi

# Skip deletion if cluster is not too old, so we can re-use it
if ! is_cluster_too_old "${CLUSTER_NAME}" "${REGION}"; then
# Skip deletion if cluster is not too old and force flag is not set
if [ "${FORCE}" != "true" ] && ! eksctl_is_cluster_too_old "${CLUSTER_NAME}" "${REGION}"; then
echo "Skipping deletion of cluster ${CLUSTER_NAME} to re-use it"
return 0
fi
Expand Down
19 changes: 10 additions & 9 deletions tests/e2e-kubernetes/scripts/kops.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function kops_install() {
chmod +x "${INSTALL_PATH}"/kops
}

function is_cluster_too_old() {
function kops_is_cluster_too_old() {
CLUSTER_NAME=${1}
BIN=${2}
KOPS_STATE_FILE=${3}
Expand All @@ -35,7 +35,7 @@ function is_cluster_too_old() {
return $?
}

function compute_cluster_spec_hash() {
function kops_compute_cluster_spec_hash() {
INSTANCE_TYPE=${1}
ZONES=${2}
AMI_ID=${3}
Expand All @@ -45,7 +45,7 @@ function compute_cluster_spec_hash() {
}

# Checks whether existing cluster matches with expected specs to decide whether to re-use it.
function cluster_matches_specs() {
function kops_cluster_matches_specs() {
CLUSTER_NAME=${1}
BIN=${2}
KOPS_STATE_FILE=${3}
Expand All @@ -72,18 +72,18 @@ function kops_create_cluster() {
SSH_KEY=${13}
KOPS_PATCH_NODE_SELINUX_ENFORCING_FILE=${14}

CLUSTER_SPEC_HASH=$(compute_cluster_spec_hash "${INSTANCE_TYPE}" "${ZONES}" "${AMI_ID}" "${KOPS_PATCH_NODE_SELINUX_ENFORCING_FILE}")
CLUSTER_SPEC_HASH=$(kops_compute_cluster_spec_hash "${INSTANCE_TYPE}" "${ZONES}" "${AMI_ID}" "${KOPS_PATCH_NODE_SELINUX_ENFORCING_FILE}")

# Check if cluster exists and matches our specs
if kops_cluster_exists "${CLUSTER_NAME}" "${BIN}" "${KOPS_STATE_FILE}"; then
if ! is_cluster_too_old "${CLUSTER_NAME}" "${BIN}" "${KOPS_STATE_FILE}" && \
cluster_matches_specs "${CLUSTER_NAME}" "${BIN}" "${KOPS_STATE_FILE}" "${CLUSTER_SPEC_HASH}"; then
if ! kops_is_cluster_too_old "${CLUSTER_NAME}" "${BIN}" "${KOPS_STATE_FILE}" && \
kops_cluster_matches_specs "${CLUSTER_NAME}" "${BIN}" "${KOPS_STATE_FILE}" "${CLUSTER_SPEC_HASH}"; then
echo "Reusing existing cluster ${CLUSTER_NAME} as it matches specifications and it is not too old"
return 0
fi

echo "Existing cluster ${CLUSTER_NAME} is either too old or doesn't match specifications. Re-creating..."
kops_delete_cluster "$BIN" "$CLUSTER_NAME" "$KOPS_STATE_FILE"
kops_delete_cluster "$BIN" "$CLUSTER_NAME" "$KOPS_STATE_FILE" "true"
fi

ARGS=()
Expand Down Expand Up @@ -135,13 +135,14 @@ function kops_delete_cluster() {
BIN=${1}
CLUSTER_NAME=${2}
KOPS_STATE_FILE=${3}
FORCE=${4:-false}

if ! kops_cluster_exists "${CLUSTER_NAME}" "${BIN}" "${KOPS_STATE_FILE}"; then
return 0
fi

# Skip deletion if cluster is not too old, so we can re-use it
if ! is_cluster_too_old "${CLUSTER_NAME}" "${BIN}" "${KOPS_STATE_FILE}"; then
# Skip deletion if cluster is not too old and force flag is not set
if [ "${FORCE}" != "true" ] && ! kops_is_cluster_too_old "${CLUSTER_NAME}" "${BIN}" "${KOPS_STATE_FILE}"; then
echo "Skipping deletion of cluster ${CLUSTER_NAME} to re-use it"
return 0
fi
Expand Down
6 changes: 4 additions & 2 deletions tests/e2e-kubernetes/scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,14 @@ function delete_cluster() {
kops_delete_cluster \
"${KOPS_BIN}" \
"${CLUSTER_NAME}" \
"${KOPS_STATE_FILE}"
"${KOPS_STATE_FILE}" \
"${FORCE:-}"
elif [[ "${CLUSTER_TYPE}" == "eksctl" ]]; then
eksctl_delete_cluster \
"$EKSCTL_BIN" \
"$CLUSTER_NAME" \
"$REGION"
"$REGION" \
"${FORCE:-}"
fi
}

Expand Down

0 comments on commit 930f8a8

Please # to comment.