@@ -21,6 +21,7 @@ import (
2121	"sync" 
2222	"time" 
2323
24+ 	"github.com/go-logr/logr" 
2425	"k8s.io/apimachinery/pkg/runtime" 
2526	utilruntime "k8s.io/apimachinery/pkg/util/runtime" 
2627	"k8s.io/apimachinery/pkg/util/wait" 
@@ -29,15 +30,12 @@ import (
2930	"sigs.k8s.io/controller-runtime/pkg/client" 
3031	"sigs.k8s.io/controller-runtime/pkg/handler" 
3132	ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/internal/controller/metrics" 
32- 	logf "sigs.k8s.io/controller-runtime/pkg/internal/log" 
3333	"sigs.k8s.io/controller-runtime/pkg/predicate" 
3434	"sigs.k8s.io/controller-runtime/pkg/reconcile" 
3535	"sigs.k8s.io/controller-runtime/pkg/runtime/inject" 
3636	"sigs.k8s.io/controller-runtime/pkg/source" 
3737)
3838
39- var  log  =  logf .RuntimeLog .WithName ("controller" )
40- 
4139var  _  inject.Injector  =  & Controller {}
4240
4341// Controller implements controller.Controller 
@@ -88,6 +86,9 @@ type Controller struct {
8886
8987	// watches maintains a list of sources, handlers, and predicates to start when the controller is started. 
9088	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 
9192}
9293
9394// 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
122123
123124	c .watches  =  append (c .watches , watchDescription {src : src , handler : evthdler , predicates : prct })
124125	if  c .Started  {
125- 		log . Info ("Starting EventSource" ,  "controller" ,  c . Name , "source" , src )
126+ 		c . Log . Info ("Starting EventSource" , "source" , src )
126127		return  src .Start (evthdler , c .Queue , prct ... )
127128	}
128129
@@ -148,14 +149,14 @@ func (c *Controller) Start(stop <-chan struct{}) error {
148149		// caches to sync so that they have a chance to register their intendeded 
149150		// caches. 
150151		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 )
152153			if  err  :=  watch .src .Start (watch .handler , c .Queue , watch .predicates ... ); err  !=  nil  {
153154				return  err 
154155			}
155156		}
156157
157158		// Start the SharedIndexInformer factories to begin populating the SharedIndexInformer caches 
158- 		log . Info ("Starting Controller" ,  "controller" ,  c . Name )
159+ 		c . Log . Info ("Starting Controller" )
159160
160161		for  _ , watch  :=  range  c .watches  {
161162			syncingSource , ok  :=  watch .src .(source.SyncingSource )
@@ -166,7 +167,7 @@ func (c *Controller) Start(stop <-chan struct{}) error {
166167				// This code is unreachable in case of kube watches since WaitForCacheSync will never return an error 
167168				// Leaving it here because that could happen in the future 
168169				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" )
170171				return  err 
171172			}
172173		}
@@ -176,7 +177,7 @@ func (c *Controller) Start(stop <-chan struct{}) error {
176177		}
177178
178179		// 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 )
180181		for  i  :=  0 ; i  <  c .MaxConcurrentReconciles ; i ++  {
181182			// Process work items 
182183			go  wait .Until (c .worker , c .JitterPeriod , stop )
@@ -190,7 +191,7 @@ func (c *Controller) Start(stop <-chan struct{}) error {
190191	}
191192
192193	<- stop 
193- 	log . Info ("Stopping workers" ,  "controller" ,  c . Name )
194+ 	c . Log . Info ("Stopping workers" )
194195	return  nil 
195196}
196197
@@ -228,23 +229,23 @@ func (c *Controller) reconcileHandler(obj interface{}) bool {
228229		c .updateMetrics (time .Since (reconcileStartTS ))
229230	}()
230231
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  {
234235		// As the item in the workqueue is actually invalid, we call 
235236		// Forget here else we'd go into a loop of attempting to 
236237		// process a work item that is invalid. 
237238		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 )
240240		// Return true, don't take a break 
241241		return  true 
242242	}
243+ 
243244	// RunInformersAndControllers the syncHandler, passing it the namespace/Name string of the 
244245	// resource to be synced. 
245246	if  result , err  :=  c .Do .Reconcile (req ); err  !=  nil  {
246247		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 )
248249		ctrlmetrics .ReconcileErrors .WithLabelValues (c .Name ).Inc ()
249250		ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , "error" ).Inc ()
250251		return  false 
@@ -268,7 +269,7 @@ func (c *Controller) reconcileHandler(obj interface{}) bool {
268269	c .Queue .Forget (obj )
269270
270271	// 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 )
272273
273274	ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , "success" ).Inc ()
274275	// Return true, don't take a break 
0 commit comments