Skip to content

Commit 472b451

Browse files
authored
Documents operator-lib PR #100. (#5515)
log could not be set as it was unexported. Signed-off-by: Frederic Giloux <fgiloux@redhat.com>
1 parent 4ccfe84 commit 472b451

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

website/content/en/docs/best-practices/resource-pruning.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ for customization of hooks and strategies.
3434
Users can configure the pruning library by creating code similar to this example:
3535
```golang
3636
cfg = Config{
37-
log: logf.Log.WithName("prune"),
37+
Log: logf.Log.WithName("prune"),
3838
DryRun: false,
3939
Clientset: client,
4040
LabelSelector: "app=churro",
@@ -53,7 +53,7 @@ cfg = Config{
5353

5454
| Config Field | Description
5555
| ------------ | -----------
56-
| log | a logger to handle library log messages
56+
| 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
5757
| DryRun | a boolean determines whether to actually remove resources; `true` means to execute but not to remove resources
5858
| Clientset | a client-go Kubernetes ClientSet that will be used for Kube API calls by the library
5959
| LabelSelector| Kubernetes label selector expression used to find resources to prune
@@ -79,6 +79,9 @@ err := cfg.Execute(ctx)
7979
Users might want to implement pruning execution by means of a cron package or simply call the prune
8080
library based on some other triggering event.
8181

82+
If a logger has been configured in the Config structure it takes precedence on the one provided through ctx.
83+
Adding a logger.Logger to the context can be done with [logr.NewContext][logr-newcontext].
84+
8285
## Pruning Strategies
8386

8487
### maxcount Strategy
@@ -106,10 +109,11 @@ from the cluster.
106109

107110
Here is an example of a *preDelete* hook:
108111
```golang
109-
func myhook(cfg Config, x ResourceInfo) error {
110-
fmt.Println("myhook is called ")
111-
if x.GVK.Kind == PodKind {
112-
req := cfg.Clientset.CoreV1().Pods(x.Namespace).GetLogs(x.Name, &v1.PodLogOptions{})
112+
func myhook(ctx context.Context, cfg Config, res ResourceInfo) error {
113+
log := prune.Logger(ctx, cfg)
114+
log.V(4).Info("pre-deletion", "GVK", res.GVK, "namespace", res.Namespace, "name", res.Name)
115+
if res.GVK.Kind == PodKind {
116+
req := cfg.Clientset.CoreV1().Pods(res.Namespace).GetLogs(res.Name, &v1.PodLogOptions{})
113117
podLogs, err := req.Stream(context.Background())
114118
if err != nil {
115119
return err
@@ -122,7 +126,7 @@ func myhook(cfg Config, x ResourceInfo) error {
122126
return err
123127
}
124128

125-
fmt.Printf("pod log before removing is %s\n", buf.String())
129+
log.V(4).Info("pod log before removing is", "log", buf.String())
126130
}
127131
return nil
128132
}
@@ -138,8 +142,9 @@ cases. Custom strategy functions are passed in the prune configuration and a lis
138142
the library. The custom strategy builds up a list of resources to be removed, returning the list to the prune library which
139143
performs the actual resource removal. Here is an example custom strategy:
140144
```golang
141-
func myStrategy(cfg Config, resources []ResourceInfo) (resourcesToRemove []ResourceInfo, err error) {
142-
fmt.Printf("myStrategy is called with resources %v config %v\n", resources, cfg)
145+
func myStrategy(ctx context.Context, cfg Config, resources []ResourceInfo) (resourcesToRemove []ResourceInfo, err error) {
146+
log := Logger(ctx, cfg)
147+
log.V(4).Info("myStrategy called", "resources", resources, "config", cfg)
143148
if len(resources) != 3 {
144149
return resourcesToRemove, fmt.Errorf("count of resources did not equal our expectation")
145150
}
@@ -163,3 +168,4 @@ Notice that you can optionally pass in settings to your custom function as a map
163168
[operator-lib-prune]: https://github.com/operator-framework/operator-lib/tree/main/prune
164169
[jobs]: https://kubernetes.io/docs/concepts/workloads/controllers/job/
165170
[time.Duration formatting]: https://pkg.go.dev/time#Duration
171+
[logr-newcontext]: https://pkg.go.dev/github.com/go-logr/logr#NewContext

0 commit comments

Comments
 (0)