-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a job that will purge AWS EKS clusters every 6 hours
Signed-off-by: ytimocin <[email protected]>
- Loading branch information
Showing
4 changed files
with
116 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import boto3 | ||
import os | ||
from datetime import datetime, timezone | ||
|
||
|
||
def delete_node_groups(eks, cluster_name): | ||
node_groups = eks.list_nodegroups(clusterName=cluster_name)['nodegroups'] | ||
for node_group in node_groups: | ||
print(f"Deleting node group {node_group} in cluster {cluster_name}") | ||
eks.delete_nodegroup(clusterName=cluster_name, | ||
nodegroupName=node_group) | ||
|
||
|
||
def delete_fargate_profiles(eks, cluster_name): | ||
fargate_profiles = eks.list_fargate_profiles(clusterName=cluster_name)[ | ||
'fargateProfileNames'] | ||
for profile in fargate_profiles: | ||
print(f"Deleting Fargate profile {profile} in cluster {cluster_name}") | ||
eks.delete_fargate_profile( | ||
clusterName=cluster_name, fargateProfileName=profile) | ||
|
||
|
||
def delete_old_clusters(prefix, older_than_hours=24): | ||
# Read the region from environment variable | ||
aws_region = os.getenv('AWS_REGION', 'us-west-2') | ||
|
||
# Create an EKS client | ||
eks = boto3.client('eks', region_name=aws_region) | ||
|
||
# List all EKS clusters | ||
clusters = eks.list_clusters()['clusters'] | ||
|
||
# Get the current time | ||
now = datetime.now(timezone.utc) | ||
|
||
# Loop through the clusters | ||
for cluster_name in clusters: | ||
if cluster_name.startswith(prefix): | ||
# Get detailed information about the cluster | ||
cluster_info = eks.describe_cluster(name=cluster_name) | ||
creation_time = cluster_info['cluster']['createdAt'] | ||
|
||
# Calculate the age of the cluster in hours | ||
age = (now - creation_time).total_seconds() / 3600 | ||
|
||
# Delete the cluster if it's older than the specified time | ||
if age > older_than_hours: | ||
print( | ||
f"Deleting EKS cluster {cluster_name}, age: {age:.2f} hours") | ||
|
||
# Delete associated node groups | ||
delete_node_groups(eks, cluster_name) | ||
|
||
# Delete associated Fargate profiles | ||
delete_fargate_profiles(eks, cluster_name) | ||
|
||
# Now delete the cluster | ||
try: | ||
eks.delete_cluster(name=cluster_name) | ||
print(f"Deleted EKS cluster {cluster_name}") | ||
except Exception as e: | ||
print( | ||
f"Failed to delete EKS cluster {cluster_name}: {str(e)}") | ||
|
||
|
||
if __name__ == "__main__": | ||
# Specify the prefix of the clusters you want to target | ||
delete_old_clusters(prefix="eks-samplestest-") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
name: Purge AWS EKS Clusters | ||
on: | ||
schedule: | ||
# Runs every 6 hours | ||
- cron: "0 0,6 * * *" | ||
pull_request: | ||
types: [opened, synchronize, reopened] | ||
branches: | ||
- v*.* | ||
- edge | ||
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: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: "3.9" | ||
|
||
- name: Install dependencies | ||
run: | | ||
pip install boto3 | ||
- 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_REGION: ${{ env.AWS_REGION }} | ||
run: python .github/scripts/delete-old-eks-clusters.py | ||
|
||
- 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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters