@@ -23,6 +23,8 @@ import (
23
23
"k8s.io/apimachinery/pkg/labels"
24
24
"k8s.io/apimachinery/pkg/runtime/schema"
25
25
"k8s.io/client-go/kubernetes"
26
+
27
+ ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
26
28
)
27
29
28
30
// ResourceStatus describes the Kubernetes resource status we are evaluating
@@ -54,10 +56,10 @@ type StrategyConfig struct {
54
56
55
57
// StrategyFunc function allows a means to specify
56
58
// custom prune strategies
57
- type StrategyFunc func (cfg Config , resources []ResourceInfo ) ([]ResourceInfo , error )
59
+ type StrategyFunc func (ctx context. Context , cfg Config , resources []ResourceInfo ) ([]ResourceInfo , error )
58
60
59
61
// PreDelete function is called before a resource is pruned
60
- type PreDelete func (cfg Config , something ResourceInfo ) error
62
+ type PreDelete func (ctx context. Context , cfg Config , something ResourceInfo ) error
61
63
62
64
// Config defines a pruning configuration and ultimately
63
65
// determines what will get pruned
@@ -70,13 +72,13 @@ type Config struct {
70
72
Strategy StrategyConfig //strategy for pruning, either age or max
71
73
CustomStrategy StrategyFunc //custom strategy
72
74
PreDeleteHook PreDelete //called before resource is deleteds
73
- log logr.Logger
75
+ Log logr.Logger //optional: to overwrite the logger set at context level
74
76
}
75
77
76
78
// Execute causes the pruning work to be executed based on its configuration
77
79
func (config Config ) Execute (ctx context.Context ) error {
78
-
79
- config . log .V (1 ).Info ("Execute Prune" )
80
+ log := Logger ( ctx , config )
81
+ log .V (1 ).Info ("Execute Prune" )
80
82
81
83
err := config .validate ()
82
84
if err != nil {
@@ -92,13 +94,13 @@ func (config Config) Execute(ctx context.Context) error {
92
94
if err != nil {
93
95
return err
94
96
}
95
- config . log .V (1 ).Info ("pods " , "count" , len (resourceList ))
97
+ log .V (1 ).Info ("pods " , "count" , len (resourceList ))
96
98
} else if config .Resources [i ].Kind == JobKind {
97
99
resourceList , err = config .getCompletedJobs (ctx )
98
100
if err != nil {
99
101
return err
100
102
}
101
- config . log .V (1 ).Info ("jobs " , "count" , len (resourceList ))
103
+ log .V (1 ).Info ("jobs " , "count" , len (resourceList ))
102
104
}
103
105
104
106
var resourcesToRemove []ResourceInfo
@@ -109,7 +111,7 @@ func (config Config) Execute(ctx context.Context) error {
109
111
case MaxCountStrategy :
110
112
resourcesToRemove , err = pruneByMaxCount (ctx , config , resourceList )
111
113
case CustomStrategy :
112
- resourcesToRemove , err = config .CustomStrategy (config , resourceList )
114
+ resourcesToRemove , err = config .CustomStrategy (ctx , config , resourceList )
113
115
default :
114
116
return fmt .Errorf ("unknown strategy" )
115
117
}
@@ -123,7 +125,7 @@ func (config Config) Execute(ctx context.Context) error {
123
125
}
124
126
}
125
127
126
- config . log .V (1 ).Info ("Prune completed" )
128
+ log .V (1 ).Info ("Prune completed" )
127
129
128
130
return nil
129
131
}
@@ -181,3 +183,17 @@ func (config Config) validate() (err error) {
181
183
}
182
184
return nil
183
185
}
186
+
187
+ // Logger returns a logger from the context using logr method or Config.Log if none is found
188
+ // controller-runtime automatically provides a logger in context.Context during Reconcile calls.
189
+ // Note that there is no compile time check whether a logger can be retrieved by either way.
190
+ // keysAndValues allow to add fields to the logs, cf logr documentation.
191
+ func Logger (ctx context.Context , cfg Config , keysAndValues ... interface {}) logr.Logger {
192
+ var log logr.Logger
193
+ if cfg .Log != (logr.Logger {}) {
194
+ log = cfg .Log
195
+ } else {
196
+ log = ctrllog .FromContext (ctx )
197
+ }
198
+ return log .WithValues (keysAndValues ... )
199
+ }
0 commit comments