diff --git a/.github/scripts/purge-aws-eks-clusters.sh b/.github/scripts/purge-aws-eks-clusters.sh new file mode 100755 index 00000000..1161a5a5 --- /dev/null +++ b/.github/scripts/purge-aws-eks-clusters.sh @@ -0,0 +1,42 @@ +# ------------------------------------------------------------ +# Copyright 2023 The Radius Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ------------------------------------------------------------ + +#!/bin/bash +set -xe + +# Get the current time in seconds since 1970-01-01 00:00:00 UTC +now=$(date +%s) + +# Define the prefix for the EKS clusters to delete +EKS_PREFIX="eks-samplestest-" + +# List all EKS clusters +clusters=$(aws eks list-clusters --query 'clusters' --output text | tr '\t' '\n' | grep "^$EKS_PREFIX") + +# Loop through each cluster +for cluster in $clusters; do + # Get the creation time of the cluster in seconds since 1970-01-01 00:00:00 UTC + creation_time=$(aws eks describe-cluster --name $cluster --query 'cluster.createdAt' --output text) + creation_time=$(date -d"$creation_time" +%s) + + # Calculate the age of the cluster in hours + age_hours=$(((now - creation_time) / 3600)) + + # If the cluster is more than 24 hours old, delete it + if ((age_hours > 24)); then + aws eks delete-cluster --name $cluster + fi +done diff --git a/.github/scripts/purge-aws-rds-snapshots.sh b/.github/scripts/purge-aws-rds-snapshots.sh index f9efcc03..8068770f 100755 --- a/.github/scripts/purge-aws-rds-snapshots.sh +++ b/.github/scripts/purge-aws-rds-snapshots.sh @@ -4,7 +4,7 @@ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software @@ -16,8 +16,7 @@ set -xe -aws rds describe-db-snapshots --query 'DBSnapshots[].DBSnapshotIdentifier' --output text > snapshots.txt -for rds_snapshot_identifier in $(cat ./snapshots.txt) -do +aws rds describe-db-snapshots --query 'DBSnapshots[].DBSnapshotIdentifier' --output text >snapshots.txt +for rds_snapshot_identifier in $(cat ./snapshots.txt); do aws rds delete-db-snapshot --db-snapshot-identifier $rds_snapshot_identifier done diff --git a/.github/workflows/purge-aws-eks-clusters.yaml b/.github/workflows/purge-aws-eks-clusters.yaml new file mode 100644 index 00000000..a6466357 --- /dev/null +++ b/.github/workflows/purge-aws-eks-clusters.yaml @@ -0,0 +1,30 @@ +name: Purge AWS EKS Clusters +on: + schedule: + # Runs every 6 hours + - cron: "0 0,6 * * *" +env: + GH_TOKEN: ${{ github.token }} + AWS_REGION: us-west-2 +jobs: + purge_rds_snapshots: + name: Purge AWS EKS Clusters + runs-on: [ubuntu-latest] + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Login to AWS + run: | + aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }} + aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws configure set region ${{ env.AWS_REGION }} + aws configure set output json + - name: Purge AWS EKS Clusters + run: | + ./.github/scripts/purge-aws-eks-clusters.sh + - name: Create GitHub issue on failure + if: ${{ failure() }} + run: | + gh issue create --title "Purge AWS EKS Clusters workflow failed" \ + --body "Test failed on ${{ github.repository }}. See [workflow logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for more details." \ + --repo ${{ github.repository }} diff --git a/.github/workflows/purge-aws-rds-snapshots.yaml b/.github/workflows/purge-aws-rds-snapshots.yaml index 257dd7ba..53a8ddce 100644 --- a/.github/workflows/purge-aws-rds-snapshots.yaml +++ b/.github/workflows/purge-aws-rds-snapshots.yaml @@ -4,8 +4,8 @@ on: # Runs at 00:30 and 12:30 - cron: "30 0,12 * * *" env: - GH_TOKEN: ${{ github.token }} - AWS_REGION: us-west-2 + GH_TOKEN: ${{ github.token }} + AWS_REGION: us-west-2 jobs: purge_rds_snapshots: name: Purge AWS RDS DBInstance snapshots