Back to Policies

Cleanup Expired ConfigMaps

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.

View on GitHub

Policy Definition

apiVersion: policies.kyverno.io/v1
kind: DeletingPolicy
metadata:
name: cleanup-expired-configmaps
annotations:
policies.kyverno.io/title: Cleanup Expired ConfigMaps
policies.kyverno.io/category: Cleanup
policies.kyverno.io/severity: medium
policies.kyverno.io/subject: ConfigMap
policies.kyverno.io/minversion: 1.20.0
policies.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:
- v1
resources:
- configmaps
objectSelector:
matchLabels:
cleanup.example.com/enabled: "true"
conditions:
- name: has-expiration
expression: has(object.metadata.annotations) && 'cleanup.example.com/expires-at' in object.metadata.annotations
- name: expired
expression: timestamp(object.metadata.annotations['cleanup.example.com/expires-at']) < time.now()

Related Policies

CleanupMedium

Cleanup Bare Pods

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.

Pod
CleanupMedium

Cleanup ConfigMaps with a Namespace Switch

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.

ConfigMap
CleanupMedium

Cleanup Empty ReplicaSets

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.

ReplicaSet