Skip to content

Commit c0d8406

Browse files
authored
Remove unnecessary LogConstructor overrides (#4855)
Controller Runtime already does this by default.
1 parent ba2e768 commit c0d8406

File tree

3 files changed

+17
-24
lines changed

3 files changed

+17
-24
lines changed

v2/cmd/controller/app/setup.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ func initializeClients(cfg config.Values, mgr ctrl.Manager) (*clients, error) {
431431
// Register the evaluator for use by webhooks
432432
asocel.RegisterEvaluator(expressionEvaluator)
433433

434-
options := makeControllerOptions(log, cfg)
434+
options := makeControllerOptions(cfg)
435435

436436
return &clients{
437437
positiveConditions: positiveConditions,
@@ -485,7 +485,7 @@ func initializeWatchers(readyResources map[string]apiextensions.CustomResourceDe
485485
return nil
486486
}
487487

488-
func makeControllerOptions(log logr.Logger, cfg config.Values) generic.Options {
488+
func makeControllerOptions(cfg config.Values) generic.Options {
489489
var additionalRateLimiters []workqueue.TypedRateLimiter[reconcile.Request]
490490
if cfg.RateLimit.Mode == config.RateLimitModeBucket {
491491
additionalRateLimiters = append(
@@ -506,14 +506,6 @@ func makeControllerOptions(log logr.Logger, cfg config.Values) generic.Options {
506506
Config: cfg,
507507
Options: controller.Options{
508508
MaxConcurrentReconciles: cfg.MaxConcurrentReconciles,
509-
LogConstructor: func(req *reconcile.Request) logr.Logger {
510-
// refer to https://github.com/kubernetes-sigs/controller-runtime/pull/1827/files
511-
if req == nil {
512-
return log
513-
}
514-
// TODO: do we need GVK here too?
515-
return log.WithValues("namespace", req.Namespace, "name", req.Name)
516-
},
517509
// These rate limits are used for happy-path backoffs (for example polling async operation IDs for PUT/DELETE)
518510
RateLimiter: generic.NewRateLimiter(1*time.Second, 1*time.Minute, additionalRateLimiters...),
519511
},

v2/internal/reconcilers/generic/register.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
2727
"sigs.k8s.io/controller-runtime/pkg/controller"
2828
"sigs.k8s.io/controller-runtime/pkg/handler"
29+
"sigs.k8s.io/controller-runtime/pkg/log"
2930
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3031

3132
"github.com/Azure/azure-service-operator/v2/internal/config"
@@ -91,7 +92,7 @@ func RegisterAll(
9192
// pre-register any indexes we need
9293
for _, obj := range objs {
9394
for _, indexer := range obj.Indexes {
94-
options.LogConstructor(nil).V(Info).Info("Registering indexer for type", "type", fmt.Sprintf("%T", obj.Obj), "key", indexer.Key)
95+
mgr.GetLogger().V(Info).Info("Registering indexer for type", "type", fmt.Sprintf("%T", obj.Obj), "key", indexer.Key)
9596
err := fieldIndexer.IndexField(context.Background(), obj.Obj, indexer.Key, indexer.Func)
9697
if err != nil {
9798
return eris.Wrapf(err, "failed to register indexer for %T, Key: %q", obj.Obj, indexer.Key)
@@ -101,7 +102,6 @@ func RegisterAll(
101102

102103
var errs []error
103104
for _, obj := range objs {
104-
options.LogConstructor(nil).V(Info).Info("Registering", "objectName", obj.Name)
105105
// TODO: Consider pulling some of the construction of things out of register (gvk, etc), so that we can pass in just
106106
// TODO: the applicable extensions rather than a map of all of them
107107
if err := register(mgr, kubeClient, positiveConditions, obj, options); err != nil {
@@ -126,7 +126,7 @@ func register(
126126
}
127127

128128
loggerFactory := func(mo genruntime.MetaObject) logr.Logger {
129-
result := options.LogConstructor(nil)
129+
result := mgr.GetLogger()
130130
if options.LoggerFactory != nil {
131131
if factoryResult := options.LoggerFactory(mo); factoryResult != (logr.Logger{}) && factoryResult != logr.Discard() {
132132
result = factoryResult
@@ -137,7 +137,7 @@ func register(
137137
}
138138
eventRecorder := mgr.GetEventRecorderFor(info.Name)
139139

140-
options.LogConstructor(nil).V(Status).Info("Registering", "GVK", gvk)
140+
mgr.GetLogger().V(Status).Info("Registering", "GVK", gvk)
141141

142142
reconciler := &GenericReconciler{
143143
Reconciler: info.Reconciler,
@@ -156,10 +156,15 @@ func register(
156156
WithOptions(options.Options)
157157
builder.Named(info.Name)
158158

159-
builder = builder.Watches(&corev1.Namespace{}, handler.EnqueueRequestsFromMapFunc(registerNamespaceWatcher(options, kubeClient, info.Obj, gvk)), ctrlbuilder.WithPredicates(reconcilers.ARMReconcilerAnnotationChangedPredicate()))
159+
// All resources watch namespace for the reconcile-policy annotation
160+
builder = builder.Watches(
161+
&corev1.Namespace{},
162+
handler.EnqueueRequestsFromMapFunc(registerNamespaceWatcher(kubeClient, gvk)),
163+
ctrlbuilder.WithPredicates(reconcilers.ARMReconcilerAnnotationChangedPredicate()),
164+
)
160165

161166
for _, watch := range info.Watches {
162-
builder = builder.Watches(watch.Type, watch.MakeEventHandler(kubeClient, options.LogConstructor(nil).WithName(info.Name)))
167+
builder = builder.Watches(watch.Type, watch.MakeEventHandler(kubeClient, mgr.GetLogger().WithName(info.Name)))
163168
}
164169

165170
err = builder.Complete(reconciler)
@@ -171,18 +176,19 @@ func register(
171176
}
172177

173178
// This registers a watcher on the namespace for the watched resource
174-
func registerNamespaceWatcher(options Options, kubeclient kubeclient.Client, object client.Object, gvk schema.GroupVersionKind) func(ctx context.Context, obj client.Object) []reconcile.Request {
175-
options.LogConstructor(nil).V(Status).Info("Handler for resource object", "name", object.GetName(), "object.namespace", object.GetNamespace(), "object.kind", gvk.Kind)
179+
func registerNamespaceWatcher(kubeclient kubeclient.Client, gvk schema.GroupVersionKind) func(ctx context.Context, obj client.Object) []reconcile.Request {
176180
return func(ctx context.Context, obj client.Object) []reconcile.Request {
177181
reconcileRequests := []reconcile.Request{}
182+
log := log.FromContext(ctx)
178183

179184
aList := &unstructured.UnstructuredList{}
180185
aList.SetGroupVersionKind(gvk)
181186
if err := kubeclient.List(ctx, aList, &client.ListOptions{Namespace: obj.GetName()}); err != nil {
182187
return []reconcile.Request{}
183188
}
184189
// list the objects for the current kind
185-
options.LogConstructor(nil).V(Verbose).Info("Detected namespace reconcile-policy annotation")
190+
191+
log.V(Verbose).Info("Detected namespace reconcile-policy annotation", "namespace", obj.GetName())
186192
for _, el := range aList.Items {
187193
if _, ok := el.GetAnnotations()["serviceoperator.azure.com/reconcile-policy"]; ok {
188194
// If the annotation is defined for the object, there's no need to reconcile it and we skip the object

v2/internal/testcommon/kube_test_context_envtest.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"sigs.k8s.io/controller-runtime/pkg/controller"
3434
"sigs.k8s.io/controller-runtime/pkg/envtest"
3535
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
36-
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3736
"sigs.k8s.io/controller-runtime/pkg/webhook"
3837

3938
"github.com/Azure/azure-service-operator/v2/internal/config"
@@ -233,10 +232,6 @@ func createSharedEnvTest(cfg testConfig, namespaceResources *namespaceResources)
233232

234233
// Use appropriate backoff for mode.
235234
RateLimiter: generic.NewRateLimiter(minBackoff, maxBackoff),
236-
237-
LogConstructor: func(request *reconcile.Request) logr.Logger {
238-
return ctrl.Log
239-
},
240235
},
241236
// Specified here because usually controller-runtime logging would detect panics and log them for us
242237
// but in the case of envtest we disable those logs because they're too verbose.

0 commit comments

Comments
 (0)