Resource pruning

    Operators can create Jobs or Pods as part of their normal operation, and when those Jobs or Pods complete, they can remain on the Kubernetes cluster if not specifically removed. These resources can consume valuable cluster resources like disk storage (e.g. etcd). These resources are not tied to a Custom Resource using an .

    Operator authors have traditionally had two pruning options:

    • leave the resource cleanup to a system admin to perform

    For our purposes when we say, prune, we mean to remove a resource (e.g. kubectl delete) from a Kubernetes cluster for a given namespace.

    This documentation describes the pattern and library useful for implementing a solution within an operator.

    A simple pruning implementation can be found in the . This package is written in Go and is meant to be used within Go-based operators. This package was developed to include common pruning strategies as found in common operators. The package also allow for customization of hooks and strategies.

    Pruning Execution

    Users can invoke the pruning by running the Execute function on the pruning configuration as follows:

    Users might want to implement pruning execution by means of a cron package or simply call the prune library based on some other triggering event.

    If a logger has been configured in the Config structure it takes precedence on the one provided through ctx. Adding a logger.Logger to the context can be done with .

    A strategy of leaving a finite set of resources is implemented called maxcount. This strategy seeks to leave a specific number of resources, sorted by latest, on your cluster. For example, if you have 10 resources that would be pruned, and you specified a maxcount value of 4, then 6 resources would be pruned (removed) from your cluster starting with the oldest resources.

    maxage Strategy

    A strategy of removing resources greater than a specific time is called maxage. This strategy seeks to remove resources older than a specified maxage duration. For example, a library user might specify a value of 48h to indicate that any resource older than 48 hours would be pruned. Durations are specified using golang’s (e.g. 48h).

    Here is an example of a preDelete hook:

    Note if your custom hook returns an error, then the resource will not be removed by the prune library.

    Custom Strategy

    Library users can also write their own custom pruning strategy function to support advanced cases. Custom strategy functions are passed in the prune configuration and a list of resources selected by the library. The custom strategy builds up a list of resources to be removed, returning the list to the prune library which performs the actual resource removal. Here is an example custom strategy:

    To have your custom strategy invoked, you will specify your function within the prune configuration as follows:

    Notice that you can optionally pass in settings to your custom function as a map using the field.