Every minute, delete ConfigMaps explicitly labeled cleanup.example.com/enabled=true whose cleanup.example.com/expires-at annotation is an RFC3339 timestamp in the past. Unlabeled ConfigMaps and ConfigMaps without an expiration are retained. Invalid timestamps cause evaluation errors, not deletion. Grant the cleanup controller ConfigMap list, watch, get, and delete permissions using the accompanying RBAC.
apiVersion: policies.kyverno.io/v1kind: DeletingPolicymetadata:name: cleanup-expired-configmapsannotations:policies.kyverno.io/title: Cleanup Expired ConfigMapspolicies.kyverno.io/category: Cleanuppolicies.kyverno.io/severity: mediumpolicies.kyverno.io/subject: ConfigMappolicies.kyverno.io/minversion: 1.20.0policies.kyverno.io/description: Every minute, delete ConfigMaps explicitly labeled cleanup.example.com/enabled=true whose cleanup.example.com/expires-at annotation is an RFC3339 timestamp in the past. Unlabeled ConfigMaps and ConfigMaps without an expiration are retained. Invalid timestamps cause evaluation errors, not deletion. Grant the cleanup controller ConfigMap list, watch, get, and delete permissions using the accompanying RBAC.spec:schedule: "* * * * *"matchConstraints:resourceRules:- apiGroups:- ""apiVersions:- v1resources:- configmapsobjectSelector:matchLabels:cleanup.example.com/enabled: "true"conditions:- name: has-expirationexpression: has(object.metadata.annotations) && 'cleanup.example.com/expires-at' in object.metadata.annotations- name: expiredexpression: timestamp(object.metadata.annotations['cleanup.example.com/expires-at']) < time.now()
A bare Pod is any Pod created directly and not owned by a controller such as a Deployment or Job. Bare Pods are often create manually by users in an attempt to troubleshoot an issue. If left in the cluster, they create clutter, increase cost, and can be a security risk. Bare Pods can be cleaned up periodically through use of a policy. This policy finds and removes all bare Pods across the cluster.
Demonstrate a resource lookup during scheduled cleanup. Delete ConfigMaps labeled cleanup.example.com/temporary=true only if a cleanup-settings ConfigMap in the same namespace has data.enabled set to "true". The settings ConfigMap itself is always retained. A missing settings ConfigMap produces an evaluation error and does not authorize deletion. Grant the cleanup controller ConfigMap permissions using the accompanying RBAC.
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. Note: This simplified version cleans up all empty ReplicaSets without a time delay. The schedule runs every minute, so very recent ReplicaSets should survive at least one cycle before deletion.