This commit is contained in:
Danijel Simeunovic
2026-03-06 08:58:04 +01:00
parent 2da39f7c67
commit 1f772ef7f5
2 changed files with 63 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
apiVersion: kyverno.io/v2
kind: ClusterCleanupPolicy
metadata:
name: cleanup-empty-replicasets
annotations:
policies.kyverno.io/title: Cleanup Empty ReplicaSets
policies.kyverno.io/category: Other
policies.kyverno.io/severity: medium
policies.kyverno.io/subject: ReplicaSet
kyverno.io/kyverno-version: 1.11.1
policies.kyverno.io/minversion: 1.9.0
kyverno.io/kubernetes-version: "1.27"
policies.kyverno.io/description: ReplicaSets serve as an intermediate controller for various Pod controllers like Deployments. When a new version of a Deployment is initiated, it generates a new ReplicaSet with the specified number of replicas and scales down the current one to zero. Consequently, numerous empty ReplicaSets may accumulate in the cluster, leading to clutter and potential false positives in policy reports if enabled. This cleanup policy is designed to remove empty ReplicaSets across the cluster within a specified timeframe, for instance, ReplicaSets created 7 days ago, ensuring the ability to rollback to previous ReplicaSets in case of deployment issues
spec:
match:
any:
- resources:
kinds:
- ReplicaSet
exclude:
any:
- resources:
namespaces:
- kube-system
conditions:
all:
- key: "{{ target.spec.replicas }}"
operator: Equals
value: 0
- key: "{{ time_diff('{{target.metadata.creationTimestamp}}','{{ time_now_utc() }}') }}"
operator: GreaterThan
value: 0h0m30s
schedule: "*/7 * * * *"
cleanup:
message: "This ReplicaSet has no replicas and has been removed by the cleanup-empty-replicasets ClusterCleanupPolicy."