@@ -21,20 +21,18 @@ import (
21
21
"sync"
22
22
"time"
23
23
24
+ "github.com/go-logr/logr"
24
25
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
25
26
"k8s.io/apimachinery/pkg/util/wait"
26
27
"k8s.io/client-go/util/workqueue"
27
28
"sigs.k8s.io/controller-runtime/pkg/handler"
28
29
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/internal/controller/metrics"
29
- logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
30
30
"sigs.k8s.io/controller-runtime/pkg/predicate"
31
31
"sigs.k8s.io/controller-runtime/pkg/reconcile"
32
32
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
33
33
"sigs.k8s.io/controller-runtime/pkg/source"
34
34
)
35
35
36
- var log = logf .RuntimeLog .WithName ("controller" )
37
-
38
36
var _ inject.Injector = & Controller {}
39
37
40
38
// Controller implements controller.Controller
@@ -75,6 +73,9 @@ type Controller struct {
75
73
76
74
// watches maintains a list of sources, handlers, and predicates to start when the controller is started.
77
75
watches []watchDescription
76
+
77
+ // Log is used to log messages to users during reconciliation, or for example when a watch is started.
78
+ Log logr.Logger
78
79
}
79
80
80
81
// watchDescription contains all the information necessary to start a watch.
@@ -109,7 +110,7 @@ func (c *Controller) Watch(src source.Source, evthdler handler.EventHandler, prc
109
110
110
111
c .watches = append (c .watches , watchDescription {src : src , handler : evthdler , predicates : prct })
111
112
if c .Started {
112
- log . Info ("Starting EventSource" , "controller" , c . Name , "source" , src )
113
+ c . Log . Info ("Starting EventSource" , "source" , src )
113
114
return src .Start (evthdler , c .Queue , prct ... )
114
115
}
115
116
@@ -135,14 +136,14 @@ func (c *Controller) Start(stop <-chan struct{}) error {
135
136
// caches to sync so that they have a chance to register their intendeded
136
137
// caches.
137
138
for _ , watch := range c .watches {
138
- log . Info ("Starting EventSource" , "controller" , c . Name , "source" , watch .src )
139
+ c . Log . Info ("Starting EventSource" , "source" , watch .src )
139
140
if err := watch .src .Start (watch .handler , c .Queue , watch .predicates ... ); err != nil {
140
141
return err
141
142
}
142
143
}
143
144
144
145
// Start the SharedIndexInformer factories to begin populating the SharedIndexInformer caches
145
- log . Info ("Starting Controller" , "controller" , c . Name )
146
+ c . Log . Info ("Starting Controller" )
146
147
147
148
for _ , watch := range c .watches {
148
149
syncingSource , ok := watch .src .(source.SyncingSource )
@@ -153,7 +154,7 @@ func (c *Controller) Start(stop <-chan struct{}) error {
153
154
// This code is unreachable in case of kube watches since WaitForCacheSync will never return an error
154
155
// Leaving it here because that could happen in the future
155
156
err := fmt .Errorf ("failed to wait for %s caches to sync: %w" , c .Name , err )
156
- log . Error (err , "Could not wait for Cache to sync" , "controller" , c . Name )
157
+ c . Log . Error (err , "Could not wait for Cache to sync" )
157
158
return err
158
159
}
159
160
}
@@ -163,7 +164,7 @@ func (c *Controller) Start(stop <-chan struct{}) error {
163
164
}
164
165
165
166
// Launch workers to process resources
166
- log . Info ("Starting workers" , "controller" , c . Name , "worker count" , c .MaxConcurrentReconciles )
167
+ c . Log . Info ("Starting workers" , "worker count" , c .MaxConcurrentReconciles )
167
168
for i := 0 ; i < c .MaxConcurrentReconciles ; i ++ {
168
169
// Process work items
169
170
go wait .Until (c .worker , c .JitterPeriod , stop )
@@ -177,7 +178,7 @@ func (c *Controller) Start(stop <-chan struct{}) error {
177
178
}
178
179
179
180
<- stop
180
- log . Info ("Stopping workers" , "controller" , c . Name )
181
+ c . Log . Info ("Stopping workers" )
181
182
return nil
182
183
}
183
184
@@ -215,23 +216,23 @@ func (c *Controller) reconcileHandler(obj interface{}) bool {
215
216
c .updateMetrics (time .Since (reconcileStartTS ))
216
217
}()
217
218
218
- var req reconcile. Request
219
- var ok bool
220
- if req , ok = obj .(reconcile. Request ); ! ok {
219
+ // Make sure that the the object is a valid request.
220
+ req , ok := obj .(reconcile. Request )
221
+ if ! ok {
221
222
// As the item in the workqueue is actually invalid, we call
222
223
// Forget here else we'd go into a loop of attempting to
223
224
// process a work item that is invalid.
224
225
c .Queue .Forget (obj )
225
- log .Error (nil , "Queue item was not a Request" ,
226
- "controller" , c .Name , "type" , fmt .Sprintf ("%T" , obj ), "value" , obj )
226
+ c .Log .Error (nil , "Queue item was not a Request" , "type" , fmt .Sprintf ("%T" , obj ), "value" , obj )
227
227
// Return true, don't take a break
228
228
return true
229
229
}
230
+
230
231
// RunInformersAndControllers the syncHandler, passing it the namespace/Name string of the
231
232
// resource to be synced.
232
233
if result , err := c .Do .Reconcile (req ); err != nil {
233
234
c .Queue .AddRateLimited (req )
234
- log . Error (err , "Reconciler error" , "controller " , c .Name , "request " , req )
235
+ c . Log . Error (err , "Reconciler error" , "name " , req .Name , "namespace " , req . Namespace )
235
236
ctrlmetrics .ReconcileErrors .WithLabelValues (c .Name ).Inc ()
236
237
ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , "error" ).Inc ()
237
238
return false
@@ -255,7 +256,7 @@ func (c *Controller) reconcileHandler(obj interface{}) bool {
255
256
c .Queue .Forget (obj )
256
257
257
258
// TODO(directxman12): What does 1 mean? Do we want level constants? Do we want levels at all?
258
- log . V (1 ).Info ("Successfully Reconciled" , "controller " , c .Name , "request " , req )
259
+ c . Log . V (1 ).Info ("Successfully Reconciled" , "name " , req .Name , "namespace " , req . Namespace )
259
260
260
261
ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , "success" ).Inc ()
261
262
// Return true, don't take a break
0 commit comments