Sign inSign up

maktouch/namespace-cleaner

By maktouch

•Updated over 3 years ago

Prune Kubernetes namespaces based on open Github PR

Image
0

1.3K

maktouch/namespace-cleaner repository overview

⁠maktouch/namespace-cleaner

Prunes Kubernetes namespaces based on a repo's Github PR

Github Repo: https://github.com/maktouch/namespace-cleaner⁠

⁠Why?

When a PR opens, we start the build, which takes a few minutes. After that, it gets deployed to our review cluster, in a dedicated namespace. Once the PR is merged or closed, an action triggers the deletion.

Unfortunately, some people sometimes merges a PR before the builds has completed; so the Delete namespace action triggers, and then a few minutes later, the build and deploy finishes, and we find ourselves with a zombie namespace that never gets killed, and takes resources for nothing.

This solves that by comparing namespaces with PRs. If it's not in the open PR list, it deletes the namespace in Kubernetes.

⁠How to run

Since it's made to interface with Kubernetes, it's only logical that you run this in Kubernetes as a CronJob.

apiVersion: batch/v1
kind: CronJob
metadata:
  name: namespace-cleaner
spec:
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 1
  schedule: '0 */2 * * *'
  concurrencyPolicy: Forbid 
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: namespace-cleaner
        spec:
          restartPolicy: OnFailure
          containers:
            - name: namespace-cleaner
              image: 'maktouch/namespace-cleaner:latest'
              imagePullPolicy: Always
              env:
                - name: GITHUB_REPOSITORY
                  value: 'org/repo'
                - name: GITHUB_TOKEN
                  value: '<secret>'
                - name: K8S_CREDENTIALS
                  value: '<secret>'
              resources:
                requests:
                  cpu: 50m
                  memory: 64M

⁠Configuration

envdescriptionrequireddefault
GITHUB_REPOSITORYThe Github Repo in ORGANIZATION/REPO formatyes-
GITHUB_TOKENThe Github Personal Tokenyes-
K8S_CREDENTIALSThe Kubernetes credentials in YAML or JSON. If you don't provide this, it'll use the default service account, and you'll have to configure RBAC yourself.probably yes-
NAMESPACE_REGEXThe namespace format. By default, it looks for namespaces that looks like rev-{some-digits}. This is how you change the pattern. Make sure you escape the regex properly.norev-(\\d+)
NAMESPACE_BLACKLISTMaybe you want to project a PR. This is how you do it. It's comma-separated, like rev-1,rev-3no-

Tag summary

Content type

Image

Digest

sha256:55495e870…

Size

56.5 MB

Last updated

over 3 years ago

docker pull maktouch/namespace-cleaner