Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added documentation for DeletionPropagationPolicy for cleanupPolicy a… #1426

Merged
68 changes: 68 additions & 0 deletions content/en/docs/writing-policies/cleanup.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,71 @@ spec:
Although labeled resources are watched by Kyverno, the cleanup interval (the time resolution at which any cleanup can be performed) is controlled by a flag passed to the cleanup controller called `ttlReconciliationInterval`. This value is set to `1m` by default and can be changed if a longer resolution is required.

Because this is a label, there is opportunity to chain other Kyverno functionality around it. For example, it is possible to use a Kyverno mutate rule to assign this label to matching resources. A validate rule could be written prohibiting, for example, users from the `infra-ops` group from assigning the label to resources in certain Namespaces. Or, Kyverno could generate a new resource with this label as part of the resource definition.

## DeletionPropagationPolicy (Common to both)
ShivamJha2436 marked this conversation as resolved.
Show resolved Hide resolved

The deletionPropagationPolicy field is an optional setting available in both CleanupPolicy and TTL-based cleanup configurations. It determines how Kubernetes handles the deletion of dependent resources when the primary resource is deleted.
ShivamJha2436 marked this conversation as resolved.
Show resolved Hide resolved

Supported values:

- **Foreground**: Ensures dependent resources are deleted before the primary resource is removed.
- **Background**: Deletes the primary resource first, while dependents are removed asynchronously.
- **Orphan**: Deletes the primary resource but leaves its dependents untouched.

{{% alert title="Note" color="info" %}}
If deletionPropagationPolicy is not set, Kyverno defers to the Kubernetes API server's default behavior, which typically handles dependents based on cluster settings.
{{% /alert %}}

### Cleanup Policy Example with deletionPropagationPolicy ###
ShivamJha2436 marked this conversation as resolved.
Show resolved Hide resolved

A ClusterCleanupPolicy can include deletionPropagationPolicy to control the cleanup of dependents. Here's an example:

```yaml
apiVersion: kyverno.io/v2
kind: ClusterCleanupPolicy
metadata:
name: cleandeploy
spec:
match:
any:
- resources:
kinds:
- Deployment
selector:
matchLabels:
canremove: "true"
conditions:
any:
- key: "{{ target.spec.replicas }}"
operator: LessThan
value: 2
schedule: "*/5 * * * *"
deletionPropagationPolicy: "Foreground"
```
This policy schedules the deletion of Deployments labeled canremove: "true" with fewer than two replicas every 5 minutes, ensuring dependent resources are deleted before the Deployment itself.
ShivamJha2436 marked this conversation as resolved.
Show resolved Hide resolved

### TTL-Based Cleanup Example with deletionPropagationPolicy ###
ShivamJha2436 marked this conversation as resolved.
Show resolved Hide resolved

Resources with a cleanup.kyverno.io/ttl label can also use the deletionPropagationPolicy to manage dependent resources:
ShivamJha2436 marked this conversation as resolved.
Show resolved Hide resolved

```yaml
apiVersion: v1
kind: CleanupPolicy
ShivamJha2436 marked this conversation as resolved.
Show resolved Hide resolved
metadata:
labels:
cleanup.kyverno.io/ttl: 2m
annotations:
deletionPropagationPolicy: "Orphan"
ShivamJha2436 marked this conversation as resolved.
Show resolved Hide resolved
name: foo
spec:
containers:
- args:
- sleep
- 1d
image: busybox:1.35
name: foo
```
In this example:
ShivamJha2436 marked this conversation as resolved.
Show resolved Hide resolved

The TTL label specifies that the Pod will be deleted 2 minutes after creation.
The deletionPropagationPolicy: "Orphan" ensures that any dependents remain in the cluster after the Pod is deleted.