Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions website/content/en/docs/best-practices/resource-pruning.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ for customization of hooks and strategies.
Users can configure the pruning library by creating code similar to this example:
```golang
cfg = Config{
log: logf.Log.WithName("prune"),
Log: logf.Log.WithName("prune"),
DryRun: false,
Clientset: client,
LabelSelector: "app=churro",
Expand All @@ -53,7 +53,7 @@ cfg = Config{

| Config Field | Description
| ------------ | -----------
| log | a logger to handle library log messages
| Log | a logr.Logger. It is optional if a logger is provided through the context to the Execute method, which is the case with the context of the Reconcile function of operator-sdk and controller-runtime
| DryRun | a boolean determines whether to actually remove resources; `true` means to execute but not to remove resources
| Clientset | a client-go Kubernetes ClientSet that will be used for Kube API calls by the library
| LabelSelector| Kubernetes label selector expression used to find resources to prune
Expand All @@ -79,6 +79,9 @@ err := cfg.Execute(ctx)
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 [logr.NewContext][logr-newcontext].

## Pruning Strategies

### maxcount Strategy
Expand Down Expand Up @@ -106,10 +109,11 @@ from the cluster.

Here is an example of a *preDelete* hook:
```golang
func myhook(cfg Config, x ResourceInfo) error {
fmt.Println("myhook is called ")
if x.GVK.Kind == PodKind {
req := cfg.Clientset.CoreV1().Pods(x.Namespace).GetLogs(x.Name, &v1.PodLogOptions{})
func myhook(ctx context.Context, cfg Config, res ResourceInfo) error {
log := prune.Logger(ctx, cfg)
log.V(4).Info("pre-deletion", "GVK", res.GVK, "namespace", res.Namespace, "name", res.Name)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we not update here to use the log from the context either
@fgiloux ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@camilamacedo86 as soon as the code fix has been merged I will update this doc PR. There are a couple of places that would need to be changed to align to the current version of the code fix and this is one of them.

if res.GVK.Kind == PodKind {
req := cfg.Clientset.CoreV1().Pods(res.Namespace).GetLogs(res.Name, &v1.PodLogOptions{})
podLogs, err := req.Stream(context.Background())
if err != nil {
return err
Expand All @@ -122,7 +126,7 @@ func myhook(cfg Config, x ResourceInfo) error {
return err
}

fmt.Printf("pod log before removing is %s\n", buf.String())
log.V(4).Info("pod log before removing is", "log", buf.String())
}
return nil
}
Expand All @@ -138,8 +142,9 @@ cases. Custom strategy functions are passed in the prune configuration and a lis
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:
```golang
func myStrategy(cfg Config, resources []ResourceInfo) (resourcesToRemove []ResourceInfo, err error) {
fmt.Printf("myStrategy is called with resources %v config %v\n", resources, cfg)
func myStrategy(ctx context.Context, cfg Config, resources []ResourceInfo) (resourcesToRemove []ResourceInfo, err error) {
log := Logger(ctx, cfg)
log.V(4).Info("myStrategy called", "resources", resources, "config", cfg)
if len(resources) != 3 {
return resourcesToRemove, fmt.Errorf("count of resources did not equal our expectation")
}
Expand All @@ -163,3 +168,4 @@ Notice that you can optionally pass in settings to your custom function as a map
[operator-lib-prune]: https://github.com/operator-framework/operator-lib/tree/main/prune
[jobs]: https://kubernetes.io/docs/concepts/workloads/controllers/job/
[time.Duration formatting]: https://pkg.go.dev/time#Duration
[logr-newcontext]: https://pkg.go.dev/github.com/go-logr/logr#NewContext