A Kubernetes operator that deletes namespaces without resources.
Operator iterates over all namespaced api-resources in every namespace. If there are no resources, it annotates namespace as a candidate for deletion. The namespace will be deleted after specified time interval if there will be no resources still.
So operator doesn't delete namespace instantly: first time it marks namespace and after interval
operator deletes ns if it's still empty.
helm repo add remove-empty-ns-operator https://rgeraskin.github.io/remove-empty-ns-operator/
helm upgrade --install --create-namespace -n remove-empty-ns-operator remove-empty-ns-operator/remove-empty-ns-operator
See settings
in helm/values.yaml
as example
interval: "18000" # 5h
initialDelay: "300" # 5m
ignoredResouces:
- apiGroup: ""
kind: ConfigMap
nameRegExp: kube-root-ca.crt
- apiGroup: ""
kind: ConfigMap
nameRegExp: werf-synchronization
- apiGroup: ""
kind: Secret
nameRegExp: default-token-\w+$
- apiGroup: ""
kind: ServiceAccount
nameRegExp: default
protectedNamespaces:
- default
- kube-public
- kube-system
cleanupFinalizers: true
dryRun: false
-
interval
- interval between namespaces check -
initialDelay
- 'grace period' before new namespace will be checked -
ignoredResouces
- namespace will be treated as empty if it contains only 'ignored resources' -
protectedNamespaces
- these namespaces will not be deleated dispite of emptinessUsually there is no need to add kubernetes default namespaces (
default
,kube-public
, andkube-system
) toprotectedNamespaces
because they have some resources inside in the most cases. -
cleanupFinalizers
- cleanup kopf finalizers from all namespaces during operator shutdown (motivation)If the finalizers cleanup takes longer than that in total (e.g. due to retries), the activity will not be finished in full, as the pod will be SIGKILL’ed by Kubernetes.
So adjust the value of
terminationGracePeriodSeconds
if you have a lot of namespaces to cleanup. -
dryRun
- dry run mode: don't delete namespaces, just log what would be deleted
- Prepare local dev env with mise:
mise install
- Install pre-commit:
pre-commit install
- Use
mise tasks
for common tasks - Use tilt for a development process, e.g.
tilt up
- Tests:
mise run test