|
| 1 | +name: "Cleanup Old Kind Clusters on s390x" |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: '0 * * * *' |
| 6 | + workflow_dispatch: |
| 7 | +jobs: |
| 8 | + cleanup: |
| 9 | + name: Delete Stale Clusters |
| 10 | + runs-on: s390x |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Identify and Delete Old Clusters |
| 14 | + run: | |
| 15 | + # The time threshold in seconds (default: 3600 seconds = 1 hour) |
| 16 | + THRESHOLD=3600 |
| 17 | + # The current time in seconds since the Epoch |
| 18 | + CURRENT_TIME=$(date +%s) |
| 19 | + |
| 20 | + echo "🔍 Searching for Kind clusters..." |
| 21 | + |
| 22 | + # Get the list of Kind clusters. Redirects error to /dev/null and |
| 23 | + # uses '|| echo ""' to ensure CLUSTERS is empty if none are found. |
| 24 | + CLUSTERS=$(kind get clusters 2>/dev/null || echo "") |
| 25 | + |
| 26 | + # Check if any clusters were found |
| 27 | + if [ -z "$CLUSTERS" ]; then |
| 28 | + echo "✅ No Kind clusters found." |
| 29 | + exit 0 |
| 30 | + fi |
| 31 | + |
| 32 | + # Iterate over each found cluster |
| 33 | + for cluster in $CLUSTERS; do |
| 34 | + # The control plane node in Kind is always named <cluster-name>-control-plane |
| 35 | + NODE_NAME="${cluster}-control-plane" |
| 36 | + |
| 37 | + # Get the creation timestamp of the Docker container |
| 38 | + # The output format is '{{.Created}}', which is Docker's ISO timestamp |
| 39 | + CREATE_DATE=$(docker inspect -f '{{.Created}}' "$NODE_NAME" 2>/dev/null) |
| 40 | + |
| 41 | + # Check if reading the container info was successful |
| 42 | + if [ -z "$CREATE_DATE" ]; then |
| 43 | + echo "⚠️ Error reading node '$NODE_NAME' information for cluster: $cluster" |
| 44 | + continue |
| 45 | + fi |
| 46 | + |
| 47 | + # Convert the creation timestamp to seconds since the epoch |
| 48 | + # Note: The 'date -d' command usually recognizes Docker's ISO 8601 format. |
| 49 | + CREATE_SECONDS=$(date -d "$CREATE_DATE" +%s 2>/dev/null) |
| 50 | + |
| 51 | + if [ $? -ne 0 ] || [ -z "$CREATE_SECONDS" ]; then |
| 52 | + echo "⚠️ Error converting date '$CREATE_DATE' to seconds for cluster: $cluster" |
| 53 | + continue |
| 54 | + fi |
| 55 | + |
| 56 | + # Calculate the cluster's age in seconds |
| 57 | + AGE=$((CURRENT_TIME - CREATE_SECONDS)) |
| 58 | + |
| 59 | + # Evaluate if the age exceeds the threshold |
| 60 | + if [ $AGE -gt $THRESHOLD ]; then |
| 61 | + # Delete the cluster |
| 62 | + echo "🗑️ Removing cluster '$cluster' (Age: $((AGE / 60)) minutes)" |
| 63 | + kind delete cluster --name "$cluster" |
| 64 | + else |
| 65 | + # Keep the cluster |
| 66 | + echo "✨ Keeping cluster '$cluster' (Age: $((AGE / 60)) minutes)" |
| 67 | + fi |
| 68 | + done |
| 69 | + |
| 70 | + echo "✅ Cleanup process finished." |
0 commit comments