@@ -26,6 +26,7 @@ import (
26
26
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
27
27
"sigs.k8s.io/controller-runtime/pkg/controller"
28
28
"sigs.k8s.io/controller-runtime/pkg/handler"
29
+ "sigs.k8s.io/controller-runtime/pkg/log"
29
30
"sigs.k8s.io/controller-runtime/pkg/reconcile"
30
31
31
32
"github.com/Azure/azure-service-operator/v2/internal/config"
@@ -91,7 +92,7 @@ func RegisterAll(
91
92
// pre-register any indexes we need
92
93
for _ , obj := range objs {
93
94
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 )
95
96
err := fieldIndexer .IndexField (context .Background (), obj .Obj , indexer .Key , indexer .Func )
96
97
if err != nil {
97
98
return eris .Wrapf (err , "failed to register indexer for %T, Key: %q" , obj .Obj , indexer .Key )
@@ -101,7 +102,6 @@ func RegisterAll(
101
102
102
103
var errs []error
103
104
for _ , obj := range objs {
104
- options .LogConstructor (nil ).V (Info ).Info ("Registering" , "objectName" , obj .Name )
105
105
// TODO: Consider pulling some of the construction of things out of register (gvk, etc), so that we can pass in just
106
106
// TODO: the applicable extensions rather than a map of all of them
107
107
if err := register (mgr , kubeClient , positiveConditions , obj , options ); err != nil {
@@ -126,7 +126,7 @@ func register(
126
126
}
127
127
128
128
loggerFactory := func (mo genruntime.MetaObject ) logr.Logger {
129
- result := options . LogConstructor ( nil )
129
+ result := mgr . GetLogger ( )
130
130
if options .LoggerFactory != nil {
131
131
if factoryResult := options .LoggerFactory (mo ); factoryResult != (logr.Logger {}) && factoryResult != logr .Discard () {
132
132
result = factoryResult
@@ -137,7 +137,7 @@ func register(
137
137
}
138
138
eventRecorder := mgr .GetEventRecorderFor (info .Name )
139
139
140
- options . LogConstructor ( nil ).V (Status ).Info ("Registering" , "GVK" , gvk )
140
+ mgr . GetLogger ( ).V (Status ).Info ("Registering" , "GVK" , gvk )
141
141
142
142
reconciler := & GenericReconciler {
143
143
Reconciler : info .Reconciler ,
@@ -156,10 +156,15 @@ func register(
156
156
WithOptions (options .Options )
157
157
builder .Named (info .Name )
158
158
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
+ )
160
165
161
166
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 )))
163
168
}
164
169
165
170
err = builder .Complete (reconciler )
@@ -171,18 +176,19 @@ func register(
171
176
}
172
177
173
178
// 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 {
176
180
return func (ctx context.Context , obj client.Object ) []reconcile.Request {
177
181
reconcileRequests := []reconcile.Request {}
182
+ log := log .FromContext (ctx )
178
183
179
184
aList := & unstructured.UnstructuredList {}
180
185
aList .SetGroupVersionKind (gvk )
181
186
if err := kubeclient .List (ctx , aList , & client.ListOptions {Namespace : obj .GetName ()}); err != nil {
182
187
return []reconcile.Request {}
183
188
}
184
189
// 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 ())
186
192
for _ , el := range aList .Items {
187
193
if _ , ok := el .GetAnnotations ()["serviceoperator.azure.com/reconcile-policy" ]; ok {
188
194
// If the annotation is defined for the object, there's no need to reconcile it and we skip the object
0 commit comments