From eed1bf3e187933ed32434c11f4e8d083872e8090 Mon Sep 17 00:00:00 2001 From: ytimocin Date: Mon, 29 Apr 2024 13:56:13 -0700 Subject: [PATCH] Adding a job that will purge AWS EKS clusters every 6 hours Signed-off-by: ytimocin --- .github/scripts/purge-aws-eks-clusters.sh | 45 ++++++++++++++++++ .github/scripts/purge-aws-rds-snapshots.sh | 7 ++- .github/workflows/purge-aws-eks-clusters.yaml | 47 +++++++++++++++++++ .../workflows/purge-aws-rds-snapshots.yaml | 4 +- 4 files changed, 97 insertions(+), 6 deletions(-) create mode 100755 .github/scripts/purge-aws-eks-clusters.sh create mode 100644 .github/workflows/purge-aws-eks-clusters.yaml diff --git a/.github/scripts/purge-aws-eks-clusters.sh b/.github/scripts/purge-aws-eks-clusters.sh new file mode 100755 index 00000000..e3952eb5 --- /dev/null +++ b/.github/scripts/purge-aws-eks-clusters.sh @@ -0,0 +1,45 @@ +# ------------------------------------------------------------ +# 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 + +# Current time in seconds since epoch +current_time=$(date +%s) + +# Age limit in seconds (4 hours * 3600 seconds/hour) +age_limit=$((4 * 3600)) + +# List clusters and their creation times, filter and delete those older than 2 hours +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##*:}" + + # Convert creation time to seconds + created_at_seconds=$(date -d "$formatted_created_at" +%s) + + # Calculate age in seconds + age=$((current_time - created_at_seconds)) + + # Check if age is greater than age limit + if [ "$age" -gt "$age_limit" ]; then + echo "Deleting cluster $name older than 2 hours." + eksctl delete cluster --name "$name" --approve + else + echo "Cluster $name is not older than 2 hours." + 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..0b0403d6 --- /dev/null +++ b/.github/workflows/purge-aws-eks-clusters.yaml @@ -0,0 +1,47 @@ +name: Purge AWS EKS Clusters + +on: + schedule: + # Runs every 6 hours + - cron: "0 */6 * * *" + pull_request: + types: [opened, synchronize, reopened] + branches: + - v*.* + - edge + +env: + GH_TOKEN: ${{ github.token }} + AWS_REGION: us-west-2 + +jobs: + purge_eks_clusters: + name: Purge AWS EKS Clusters + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install AWS CLI + run: | + sudo apt-get update + sudo apt-get install -y awscli + + - name: Install eksctl + run: | + curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp + sudo mv /tmp/eksctl /usr/local/bin + + - name: Delete old EKS clusters + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_DEFAULT_REGION: ${{ env.AWS_REGION }} + run: bash .github/scripts/purge-aws-eks-clusters.sh + + - name: Create GitHub issue on failure + if: failure() && github.event_name != 'pull_request' + 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