Cleanup GCE kops clusters #20
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
name: Cleanup GCE kops clusters | |
on: | |
# Run every 3 hours | |
# In case we leak kops cluster, we want to cleanup | |
# 100 node cluster pretty fast | |
schedule: | |
- cron: '0 */3 * * *' | |
permissions: | |
# To be able to access the repository with actions/checkout | |
contents: read | |
# To be able to request the JWT from GitHub's OIDC provider | |
id-token: write | |
# To allow retrieving information from the PR API | |
pull-requests: read | |
concurrency: | |
# Structure: | |
# - Workflow name | |
group: | | |
${{ github.workflow }} | |
cancel-in-progress: true | |
env: | |
# renovate: datasource=golang-version depName=go | |
go_version: 1.23.3 | |
# renovate: datasource=docker depName=google/cloud-sdk | |
gcloud_version: 502.0.0 | |
jobs: | |
cleanup-kops-clusters: | |
runs-on: ubuntu-latest | |
name: Cleanup kops clusters | |
timeout-minutes: 30 | |
steps: | |
- name: Checkout context ref (trusted) | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
with: | |
ref: ${{ github.sha }} | |
persist-credentials: false | |
- name: Set Environment Variables | |
uses: ./.github/actions/set-env-variables | |
- name: Install Kops | |
uses: cilium/scale-tests-action/install-kops@d3ecfd83003f3e9c98ba125ca14933401d44918f # main | |
- name: Setup gcloud credentials | |
uses: google-github-actions/auth@6fc4af4b145ae7821d527454aa9bd537d1f2dc5f # v2.1.7 | |
with: | |
workload_identity_provider: ${{ secrets.GCP_PERF_WORKLOAD_IDENTITY_PROVIDER }} | |
service_account: ${{ secrets.GCP_PERF_SA }} | |
create_credentials_file: true | |
export_environment_variables: true | |
- name: Setup gcloud CLI | |
uses: google-github-actions/setup-gcloud@6189d56e4096ee891640bb02ac264be376592d6a # v2.1.2 | |
with: | |
project_id: ${{ secrets.GCP_PERF_PROJECT_ID }} | |
version: ${{ env.gcloud_version }} | |
- name: Cleanup stale clusters | |
shell: bash | |
timeout-minutes: 25 | |
run: | | |
if ./kops get clusters --state ${{ secrets.GCP_PERF_KOPS_STATE_STORE }} -o json > /tmp/clusters.json | |
then | |
echo "Clusters list fetched successfully" | |
date=`date -u +%Y-%m-%d'T'%H:%M'Z' -d "3 hour ago"` | |
cat /tmp/clusters.json | jq -r --arg date "$date" '.[] | select(.metadata.creationTimestamp < $date) | .metadata.name' > /tmp/stale-clusters.txt | |
# iterate through list of cluster names in /tmp/stale-clusters.txt | |
while IFS= read -r cluster; do | |
./kops delete cluster --state ${{ secrets.GCP_PERF_KOPS_STATE_STORE }} $cluster --yes | |
done < /tmp/stale-clusters.txt | |
else | |
echo "Failed to fetch clusters list, probably no clusters present" | |
fi |