@@ -21,6 +21,7 @@ import (
21
21
"sync"
22
22
"time"
23
23
24
+ "github.com/go-logr/logr"
24
25
"k8s.io/apimachinery/pkg/runtime"
25
26
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
26
27
"k8s.io/apimachinery/pkg/util/wait"
@@ -29,15 +30,12 @@ import (
29
30
"sigs.k8s.io/controller-runtime/pkg/client"
30
31
"sigs.k8s.io/controller-runtime/pkg/handler"
31
32
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/internal/controller/metrics"
32
- logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
33
33
"sigs.k8s.io/controller-runtime/pkg/predicate"
34
34
"sigs.k8s.io/controller-runtime/pkg/reconcile"
35
35
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
36
36
"sigs.k8s.io/controller-runtime/pkg/source"
37
37
)
38
38
39
- var log = logf .RuntimeLog .WithName ("controller" )
40
-
41
39
var _ inject.Injector = & Controller {}
42
40
43
41
// Controller implements controller.Controller
@@ -88,6 +86,9 @@ type Controller struct {
88
86
89
87
// watches maintains a list of sources, handlers, and predicates to start when the controller is started.
90
88
watches []watchDescription
89
+
90
+ // Log is used to log messages to users during reconciliation, or for example when a watch is started.
91
+ Log logr.Logger
91
92
}
92
93
93
94
// watchDescription contains all the information necessary to start a watch.
@@ -122,7 +123,7 @@ func (c *Controller) Watch(src source.Source, evthdler handler.EventHandler, prc
122
123
123
124
c .watches = append (c .watches , watchDescription {src : src , handler : evthdler , predicates : prct })
124
125
if c .Started {
125
- log . Info ("Starting EventSource" , "controller" , c . Name , "source" , src )
126
+ c . Log . Info ("Starting EventSource" , "source" , src )
126
127
return src .Start (evthdler , c .Queue , prct ... )
127
128
}
128
129
@@ -148,14 +149,14 @@ func (c *Controller) Start(stop <-chan struct{}) error {
148
149
// caches to sync so that they have a chance to register their intendeded
149
150
// caches.
150
151
for _ , watch := range c .watches {
151
- log . Info ("Starting EventSource" , "controller" , c . Name , "source" , watch .src )
152
+ c . Log . Info ("Starting EventSource" , "source" , watch .src )
152
153
if err := watch .src .Start (watch .handler , c .Queue , watch .predicates ... ); err != nil {
153
154
return err
154
155
}
155
156
}
156
157
157
158
// Start the SharedIndexInformer factories to begin populating the SharedIndexInformer caches
158
- log . Info ("Starting Controller" , "controller" , c . Name )
159
+ c . Log . Info ("Starting Controller" )
159
160
160
161
for _ , watch := range c .watches {
161
162
syncingSource , ok := watch .src .(source.SyncingSource )
@@ -166,7 +167,7 @@ func (c *Controller) Start(stop <-chan struct{}) error {
166
167
// This code is unreachable in case of kube watches since WaitForCacheSync will never return an error
167
168
// Leaving it here because that could happen in the future
168
169
err := fmt .Errorf ("failed to wait for %s caches to sync: %w" , c .Name , err )
169
- log . Error (err , "Could not wait for Cache to sync" , "controller" , c . Name )
170
+ c . Log . Error (err , "Could not wait for Cache to sync" )
170
171
return err
171
172
}
172
173
}
@@ -176,7 +177,7 @@ func (c *Controller) Start(stop <-chan struct{}) error {
176
177
}
177
178
178
179
// Launch workers to process resources
179
- log . Info ("Starting workers" , "controller" , c . Name , "worker count" , c .MaxConcurrentReconciles )
180
+ c . Log . Info ("Starting workers" , "worker count" , c .MaxConcurrentReconciles )
180
181
for i := 0 ; i < c .MaxConcurrentReconciles ; i ++ {
181
182
// Process work items
182
183
go wait .Until (c .worker , c .JitterPeriod , stop )
@@ -190,7 +191,7 @@ func (c *Controller) Start(stop <-chan struct{}) error {
190
191
}
191
192
192
193
<- stop
193
- log . Info ("Stopping workers" , "controller" , c . Name )
194
+ c . Log . Info ("Stopping workers" )
194
195
return nil
195
196
}
196
197
@@ -228,23 +229,23 @@ func (c *Controller) reconcileHandler(obj interface{}) bool {
228
229
c .updateMetrics (time .Since (reconcileStartTS ))
229
230
}()
230
231
231
- var req reconcile. Request
232
- var ok bool
233
- if req , ok = obj .(reconcile. Request ); ! ok {
232
+ // Make sure that the the object is a valid request.
233
+ req , ok := obj .(reconcile. Request )
234
+ if ! ok {
234
235
// As the item in the workqueue is actually invalid, we call
235
236
// Forget here else we'd go into a loop of attempting to
236
237
// process a work item that is invalid.
237
238
c .Queue .Forget (obj )
238
- log .Error (nil , "Queue item was not a Request" ,
239
- "controller" , c .Name , "type" , fmt .Sprintf ("%T" , obj ), "value" , obj )
239
+ c .Log .Error (nil , "Queue item was not a Request" , "type" , fmt .Sprintf ("%T" , obj ), "value" , obj )
240
240
// Return true, don't take a break
241
241
return true
242
242
}
243
+
243
244
// RunInformersAndControllers the syncHandler, passing it the namespace/Name string of the
244
245
// resource to be synced.
245
246
if result , err := c .Do .Reconcile (req ); err != nil {
246
247
c .Queue .AddRateLimited (req )
247
- log . Error (err , "Reconciler error" , "controller " , c .Name , "request " , req )
248
+ c . Log . Error (err , "Reconciler error" , "name " , req .Name , "namespace " , req . Namespace )
248
249
ctrlmetrics .ReconcileErrors .WithLabelValues (c .Name ).Inc ()
249
250
ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , "error" ).Inc ()
250
251
return false
@@ -268,7 +269,7 @@ func (c *Controller) reconcileHandler(obj interface{}) bool {
268
269
c .Queue .Forget (obj )
269
270
270
271
// TODO(directxman12): What does 1 mean? Do we want level constants? Do we want levels at all?
271
- log . V (1 ).Info ("Successfully Reconciled" , "controller " , c .Name , "request " , req )
272
+ c . Log . V (1 ).Info ("Successfully Reconciled" , "name " , req .Name , "namespace " , req . Namespace )
272
273
273
274
ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , "success" ).Inc ()
274
275
// Return true, don't take a break
0 commit comments