0

In our company multiple stakeholders create test systems in k8s that they forget to remove. The aim is to remove namespaces that are older than one week automatically. Is there a certain operator that is able to do this?

1

I recommend that you have an expire-date as label or annotation on the namespaces used for test. Then you can have a Job to clean-up expired namespaces.

New contributor
Jonas is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
0

While I don't know of any operator that allows for this directly, it can be done with a bash one liner with awk, xargs, sed, and gnu date. I've taken it from this post and modified it to do namespaces, and to not delete the default or kube-* namespaces.

kubectl get namespaces -o go-template --template '{{range .items}}{{.metadata.name}} {{.metadata.creationTimestamp}}{{"\n"}}{{end}}' | awk '$2 <= "'$(date -d '1 week ago' -Ins --utc | sed 's/+0000/Z/')'" && $1 !~ /(default|kube.*)/ { print $1 }' | xargs --no-run-if-empty kubectl delete namespace

Be careful using this though, it's worth running it without the final xargs call to verify that it is selecting the namespaces you are interested in.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.