Skip to content

Commit 2447484

Browse files
committed
🏃 Cleanup controller internal logger
Signed-off-by: Vince Prignano <[email protected]>
1 parent 5362611 commit 2447484

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

pkg/controller/controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"k8s.io/client-go/util/workqueue"
2323
"sigs.k8s.io/controller-runtime/pkg/handler"
2424
"sigs.k8s.io/controller-runtime/pkg/internal/controller"
25+
"sigs.k8s.io/controller-runtime/pkg/internal/log"
2526
"sigs.k8s.io/controller-runtime/pkg/manager"
2627
"sigs.k8s.io/controller-runtime/pkg/predicate"
2728
"sigs.k8s.io/controller-runtime/pkg/ratelimiter"
@@ -109,6 +110,7 @@ func NewUnmanaged(name string, mgr manager.Manager, options Options) (Controller
109110
MaxConcurrentReconciles: options.MaxConcurrentReconciles,
110111
SetFields: mgr.SetFields,
111112
Name: name,
113+
Log: log.RuntimeLog.WithName("controller").WithName(name),
112114
}
113115

114116
return c, nil

pkg/internal/controller/controller.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,18 @@ import (
2121
"sync"
2222
"time"
2323

24+
"github.com/go-logr/logr"
2425
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2526
"k8s.io/apimachinery/pkg/util/wait"
2627
"k8s.io/client-go/util/workqueue"
2728
"sigs.k8s.io/controller-runtime/pkg/handler"
2829
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/internal/controller/metrics"
29-
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
3030
"sigs.k8s.io/controller-runtime/pkg/predicate"
3131
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3232
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
3333
"sigs.k8s.io/controller-runtime/pkg/source"
3434
)
3535

36-
var log = logf.RuntimeLog.WithName("controller")
37-
3836
var _ inject.Injector = &Controller{}
3937

4038
// Controller implements controller.Controller
@@ -75,6 +73,9 @@ type Controller struct {
7573

7674
// watches maintains a list of sources, handlers, and predicates to start when the controller is started.
7775
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
7879
}
7980

8081
// 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
109110

110111
c.watches = append(c.watches, watchDescription{src: src, handler: evthdler, predicates: prct})
111112
if c.Started {
112-
log.Info("Starting EventSource", "controller", c.Name, "source", src)
113+
c.Log.Info("Starting EventSource", "source", src)
113114
return src.Start(evthdler, c.Queue, prct...)
114115
}
115116

@@ -135,14 +136,14 @@ func (c *Controller) Start(stop <-chan struct{}) error {
135136
// caches to sync so that they have a chance to register their intendeded
136137
// caches.
137138
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)
139140
if err := watch.src.Start(watch.handler, c.Queue, watch.predicates...); err != nil {
140141
return err
141142
}
142143
}
143144

144145
// Start the SharedIndexInformer factories to begin populating the SharedIndexInformer caches
145-
log.Info("Starting Controller", "controller", c.Name)
146+
c.Log.Info("Starting Controller")
146147

147148
for _, watch := range c.watches {
148149
syncingSource, ok := watch.src.(source.SyncingSource)
@@ -153,7 +154,7 @@ func (c *Controller) Start(stop <-chan struct{}) error {
153154
// This code is unreachable in case of kube watches since WaitForCacheSync will never return an error
154155
// Leaving it here because that could happen in the future
155156
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")
157158
return err
158159
}
159160
}
@@ -163,7 +164,7 @@ func (c *Controller) Start(stop <-chan struct{}) error {
163164
}
164165

165166
// 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)
167168
for i := 0; i < c.MaxConcurrentReconciles; i++ {
168169
// Process work items
169170
go wait.Until(c.worker, c.JitterPeriod, stop)
@@ -177,7 +178,7 @@ func (c *Controller) Start(stop <-chan struct{}) error {
177178
}
178179

179180
<-stop
180-
log.Info("Stopping workers", "controller", c.Name)
181+
c.Log.Info("Stopping workers")
181182
return nil
182183
}
183184

@@ -215,23 +216,23 @@ func (c *Controller) reconcileHandler(obj interface{}) bool {
215216
c.updateMetrics(time.Since(reconcileStartTS))
216217
}()
217218

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 {
221222
// As the item in the workqueue is actually invalid, we call
222223
// Forget here else we'd go into a loop of attempting to
223224
// process a work item that is invalid.
224225
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)
227227
// Return true, don't take a break
228228
return true
229229
}
230+
230231
// RunInformersAndControllers the syncHandler, passing it the namespace/Name string of the
231232
// resource to be synced.
232233
if result, err := c.Do.Reconcile(req); err != nil {
233234
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)
235236
ctrlmetrics.ReconcileErrors.WithLabelValues(c.Name).Inc()
236237
ctrlmetrics.ReconcileTotal.WithLabelValues(c.Name, "error").Inc()
237238
return false
@@ -255,7 +256,7 @@ func (c *Controller) reconcileHandler(obj interface{}) bool {
255256
c.Queue.Forget(obj)
256257

257258
// 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)
259260

260261
ctrlmetrics.ReconcileTotal.WithLabelValues(c.Name, "success").Inc()
261262
// Return true, don't take a break

pkg/internal/controller/controller_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"sigs.k8s.io/controller-runtime/pkg/controller/controllertest"
3636
"sigs.k8s.io/controller-runtime/pkg/handler"
3737
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/internal/controller/metrics"
38+
"sigs.k8s.io/controller-runtime/pkg/internal/log"
3839
"sigs.k8s.io/controller-runtime/pkg/predicate"
3940
"sigs.k8s.io/controller-runtime/pkg/reconcile"
4041
"sigs.k8s.io/controller-runtime/pkg/source"
@@ -66,6 +67,7 @@ var _ = Describe("controller", func() {
6667
MaxConcurrentReconciles: 1,
6768
Do: fakeReconcile,
6869
MakeQueue: func() workqueue.RateLimitingInterface { return queue },
70+
Log: log.RuntimeLog.WithName("controller").WithName("test"),
6971
}
7072
Expect(ctrl.InjectFunc(func(interface{}) error { return nil })).To(Succeed())
7173
})

0 commit comments

Comments
 (0)