Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix purge AWS resources workflow #1703

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions .github/scripts/purge-aws-eks-clusters.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#!/bin/bash

set -ex

# Current time in seconds since epoch
current_time=$(date +%s)

Expand All @@ -24,15 +26,31 @@ age_limit=$((6 * 3600))

echo "Starting cluster purge script."

# List clusters and their creation times, filter and delete those older than 6 hours and starting with 'eks-samplestest-'
aws eks list-clusters --query "clusters[]" --output text | xargs -I {} aws eks describe-cluster --name {} --query "cluster.{name: name, createdAt: createdAt}" --output text | while read -r created_at name; do
# Convert creation time to seconds since the epoch
# Remove milliseconds and adjust timezone format from "-07:00" to "-0700"
formatted_created_at="${created_at%.*}${created_at##*.}"
formatted_created_at="${formatted_created_at%:*}${formatted_created_at##*:}"
# List clusters
clusters=$(aws eks list-clusters --query "clusters[]" --output text)

# Loop through each cluster
for cluster in $clusters; do
# Get the creation time and name of the cluster
cluster_info=$(aws eks describe-cluster --name "$cluster" --query "cluster.{name: name, createdAt: createdAt}" --output text)
created_at=$(echo "$cluster_info" | awk '{print $1}')
name=$(echo "$cluster_info" | awk '{print $2}')

# Ensure created_at is in the correct format
created_at=$(echo "$created_at" | sed 's/\.[0-9]*Z//')

# Convert creation time to seconds
created_at_seconds=$(date -d "$formatted_created_at" +%s)
# Convert creation time to seconds since the epoch using date
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
if ! command -v gdate &> /dev/null; then
echo "gdate could not be found. Please install coreutils: brew install coreutils"
exit 1
fi
created_at_seconds=$(gdate -d "$created_at" +%s)
else
# Linux
created_at_seconds=$(date -d "$created_at" +%s)
fi

# Calculate age in seconds
age=$((current_time - created_at_seconds))
Expand Down
Loading