Object pruning utility for Go-based Operators
The operator-lib
pruning utility lets Go-based Operators clean up, or prune, objects when they are no longer needed. Operator authors can also use the utility to create custom hooks and strategies.
About the operator-lib pruning utility
Objects, such as jobs or pods, are created as a normal part of the Operator life cycle. If the cluster administrator or the Operator does not remove these object, they can stay in the cluster and consume resources.
Previously, the following options were available for pruning unnecessary objects:
Operator authors had to create a unique pruning solution for their Operators.
Cluster administrators had to clean up objects on their own.
The operator-lib
pruning utility removes objects from a Kubernetes cluster for a given namespace. The library was added in version 0.9.0
of the operator-lib library as part of the Operator Framework.
Pruning utility configuration
The operator-lib
pruning utility is written in Go and includes common pruning strategies for Go-based Operators.
Example configuration
cfg = Config{
log: logf.Log.WithName("prune"),
DryRun: false,
Clientset: client,
LabelSelector: "app=<operator_name>",
Resources: []schema.GroupVersionKind{
{Group: "", Version: "", Kind: PodKind},
},
Namespaces: []string{"<operator_namespace>"},
Strategy: StrategyConfig{
Mode: MaxCountStrategy,
MaxCountSetting: 1,
},
PreDeleteHook: myhook,
}
The pruning utility configuration file defines pruning actions by using the following fields:
Configuration field | Description |
---|---|
| Logger used to handle library log messages. |
| Boolean that determines whether resources should be removed. If set to |
| Client-go Kubernetes ClientSet used for Kubernetes API calls. |
| Kubernetes label selector expression used to find resources to prune. |
| Kubernetes resource kinds. |
| List of Kubernetes namespaces to search for resources. |
| Pruning strategy to run. |
|
|
| Integer value for |
| Go |
| Go map of values that can be passed into a custom strategy function. |
| Optional: Go function to call before pruning a resource. |
| Optional: Go function that implements a custom pruning strategy. |
Pruning execution
You can call the pruning action by running the execute function on the pruning configuration.
err := cfg.Execute(ctx)
You can also call a pruning action by using a cron package or by calling the pruning utility with a triggering event.