Skip to content

Commit

Permalink
Fix purge AWS resources workflow (#1703)
Browse files Browse the repository at this point in the history
* Upmerge fix (#1649)

* Fix error when no branch changes

Signed-off-by: Brooke Hamilton <[email protected]>

* Remove extra commit

Signed-off-by: Brooke Hamilton <[email protected]>

* Add comments

Signed-off-by: Brooke Hamilton <[email protected]>

* Fix branch name env var

Signed-off-by: Brooke Hamilton <[email protected]>

* Clarify comments

Signed-off-by: Brooke Hamilton <[email protected]>

* Add example

Signed-off-by: Brooke Hamilton <[email protected]>

---------

Signed-off-by: Brooke Hamilton <[email protected]>

* Fix purge aws resources workflow

Signed-off-by: willdavsmith <[email protected]>

---------

Signed-off-by: Brooke Hamilton <[email protected]>
Signed-off-by: willdavsmith <[email protected]>
Co-authored-by: Brooke Hamilton <[email protected]>
  • Loading branch information
willdavsmith and brooke-hamilton authored Sep 20, 2024
1 parent 5f29100 commit e76d8f2
Showing 1 changed file with 26 additions and 8 deletions.
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

0 comments on commit e76d8f2

Please sign in to comment.