@@ -31,6 +31,7 @@ import (
3131 ctrl "sigs.k8s.io/controller-runtime"
3232 "sigs.k8s.io/controller-runtime/pkg/client"
3333 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
34+ "sigs.k8s.io/controller-runtime/pkg/log"
3435
3536 memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1"
3637 "github.com/openstack-k8s-operators/lib-common/modules/common/condition"
@@ -47,10 +48,14 @@ import (
4748type SwiftReconciler struct {
4849 client.Client
4950 Scheme * runtime.Scheme
50- Log logr.Logger
5151 Kclient kubernetes.Interface
5252}
5353
54+ // GetLogger returns a logger object with a prefix of "controller.name" and additional controller context fields
55+ func (r * SwiftReconciler ) GetLogger (ctx context.Context ) logr.Logger {
56+ return log .FromContext (ctx ).WithName ("Controllers" ).WithName ("Swift" )
57+ }
58+
5459//+kubebuilder:rbac:groups=swift.openstack.org,resources=swifts,verbs=get;list;watch;create;update;patch;delete
5560//+kubebuilder:rbac:groups=swift.openstack.org,resources=swifts/status,verbs=get;update;patch
5661//+kubebuilder:rbac:groups=swift.openstack.org,resources=swifts/finalizers,verbs=update;patch
@@ -72,19 +77,19 @@ type SwiftReconciler struct {
7277// For more details, check Reconcile and its Result here:
7378// - https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/reconcile 7479func (r * SwiftReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (result ctrl.Result , _err error ) {
75- _ = r .Log . WithValues ( "swift" , req . NamespacedName )
80+ Log : = r .GetLogger ( ctx )
7681
7782 instance := & swiftv1.Swift {}
7883 err := r .Get (ctx , req .NamespacedName , instance )
7984 if err != nil {
8085 if apierrors .IsNotFound (err ) {
8186 // If the custom resource is not found then, it usually means that it was deleted or not created
8287 // In this way, we will stop the reconciliation
83- r . Log .Info ("Swift resource not found. Ignoring since object must be deleted" )
88+ Log .Info ("Swift resource not found. Ignoring since object must be deleted" )
8489 return ctrl.Result {}, nil
8590 }
8691 // Error reading the object - requeue the request.
87- r . Log .Error (err , "Failed to get Swift" )
92+ Log .Error (err , "Failed to get Swift" )
8893 return ctrl.Result {}, err
8994 }
9095
@@ -93,7 +98,7 @@ func (r *SwiftReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resu
9398 r .Client ,
9499 r .Kclient ,
95100 r .Scheme ,
96- r . Log ,
101+ Log ,
97102 )
98103 if err != nil {
99104 return ctrl.Result {}, err
@@ -115,7 +120,7 @@ func (r *SwiftReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resu
115120 defer func () {
116121 // Don't update the status, if Reconciler Panics
117122 if rc := recover (); rc != nil {
118- r . Log .Info (fmt .Sprintf ("Panic during reconcile %v\n " , rc ))
123+ Log .Info (fmt .Sprintf ("Panic during reconcile %v\n " , rc ))
119124 panic (rc )
120125 }
121126 condition .RestoreLastTransitionTimes (
@@ -162,15 +167,16 @@ func (r *SwiftReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resu
162167
163168 // Handle service delete
164169 if ! instance .DeletionTimestamp .IsZero () {
165- return r .reconcileDelete (instance , helper )
170+ return r .reconcileDelete (ctx , instance , helper )
166171 }
167172
168173 // Handle non-deleted clusters
169174 return r .reconcileNormal (ctx , instance , helper )
170175}
171176
172177func (r * SwiftReconciler ) reconcileNormal (ctx context.Context , instance * swiftv1.Swift , helper * helper.Helper ) (ctrl.Result , error ) {
173- r .Log .Info (fmt .Sprintf ("Reconciling Service '%s'" , instance .Name ))
178+ Log := r .GetLogger (ctx )
179+ Log .Info (fmt .Sprintf ("Reconciling Service '%s'" , instance .Name ))
174180
175181 // Service account, role, binding
176182 rbacRules := []rbacv1.PolicyRule {
@@ -243,7 +249,7 @@ func (r *SwiftReconciler) reconcileNormal(ctx context.Context, instance *swiftv1
243249 condition .RequestedReason ,
244250 condition .SeverityInfo ,
245251 condition .MemcachedReadyWaitingMessage ))
246- r . Log .Info (fmt .Sprintf ("%s... requeueing" , condition .MemcachedReadyWaitingMessage ))
252+ Log .Info (fmt .Sprintf ("%s... requeueing" , condition .MemcachedReadyWaitingMessage ))
247253 return ctrl.Result {RequeueAfter : time .Duration (10 ) * time .Second }, nil
248254
249255 }
@@ -262,7 +268,7 @@ func (r *SwiftReconciler) reconcileNormal(ctx context.Context, instance *swiftv1
262268 condition .RequestedReason ,
263269 condition .SeverityInfo ,
264270 condition .MemcachedReadyWaitingMessage ))
265- r . Log .Info (fmt .Sprintf ("%s... requeueing" , condition .MemcachedReadyWaitingMessage ))
271+ Log .Info (fmt .Sprintf ("%s... requeueing" , condition .MemcachedReadyWaitingMessage ))
266272 return ctrl.Result {RequeueAfter : time .Duration (10 ) * time .Second }, nil
267273 }
268274 // Mark the Memcached Service as Ready if we get to this point with no errors
@@ -282,7 +288,7 @@ func (r *SwiftReconciler) reconcileNormal(ctx context.Context, instance *swiftv1
282288 return ctrl.Result {}, err
283289 }
284290 // make sure the controller is watching the last generation of the subCR
285- stg , err := r .checkSwiftStorageGeneration (instance )
291+ stg , err := r .checkSwiftStorageGeneration (ctx , instance )
286292 if err != nil {
287293 instance .Status .Conditions .Set (condition .FalseCondition (
288294 swiftv1 .SwiftStorageReadyCondition ,
@@ -306,7 +312,7 @@ func (r *SwiftReconciler) reconcileNormal(ctx context.Context, instance *swiftv1
306312 }
307313 }
308314 if op != controllerutil .OperationResultNone && stg {
309- r . Log .Info (fmt .Sprintf ("Deployment %s successfully reconciled - operation: %s" , instance .Name , string (op )))
315+ Log .Info (fmt .Sprintf ("Deployment %s successfully reconciled - operation: %s" , instance .Name , string (op )))
310316 }
311317
312318 if instance .Spec .SwiftRing .Enabled {
@@ -323,7 +329,7 @@ func (r *SwiftReconciler) reconcileNormal(ctx context.Context, instance *swiftv1
323329 }
324330
325331 // make sure the controller is watching the last generation of the subCR
326- ring , err := r .checkSwiftRingGeneration (instance )
332+ ring , err := r .checkSwiftRingGeneration (ctx , instance )
327333 if err != nil {
328334 instance .Status .Conditions .Set (condition .FalseCondition (
329335 swiftv1 .SwiftRingReadyCondition ,
@@ -348,7 +354,7 @@ func (r *SwiftReconciler) reconcileNormal(ctx context.Context, instance *swiftv1
348354 }
349355
350356 if op != controllerutil .OperationResultNone && ring {
351- r . Log .Info (fmt .Sprintf ("Deployment %s successfully reconciled - operation: %s" , instance .Name , string (op )))
357+ Log .Info (fmt .Sprintf ("Deployment %s successfully reconciled - operation: %s" , instance .Name , string (op )))
352358 }
353359 } else {
354360 instance .Status .Conditions .Remove (swiftv1 .SwiftRingReadyCondition )
@@ -365,7 +371,7 @@ func (r *SwiftReconciler) reconcileNormal(ctx context.Context, instance *swiftv1
365371 err .Error ()))
366372 return ctrl.Result {}, err
367373 }
368- sst , err := r .checkSwiftProxyGeneration (instance )
374+ sst , err := r .checkSwiftProxyGeneration (ctx , instance )
369375 if err != nil {
370376 instance .Status .Conditions .Set (condition .FalseCondition (
371377 swiftv1 .SwiftProxyReadyCondition ,
@@ -389,7 +395,7 @@ func (r *SwiftReconciler) reconcileNormal(ctx context.Context, instance *swiftv1
389395 }
390396 }
391397 if op != controllerutil .OperationResultNone && sst {
392- r . Log .Info (fmt .Sprintf ("Deployment %s successfully reconciled - operation: %s" , instance .Name , string (op )))
398+ Log .Info (fmt .Sprintf ("Deployment %s successfully reconciled - operation: %s" , instance .Name , string (op )))
393399 }
394400
395401 // We reached the end of the Reconcile, update the Ready condition based on
@@ -398,7 +404,7 @@ func (r *SwiftReconciler) reconcileNormal(ctx context.Context, instance *swiftv1
398404 instance .Status .Conditions .MarkTrue (
399405 condition .ReadyCondition , condition .ReadyMessage )
400406 }
401- r . Log .Info (fmt .Sprintf ("Reconciled Service '%s' successfully" , instance .Name ))
407+ Log .Info (fmt .Sprintf ("Reconciled Service '%s' successfully" , instance .Name ))
402408 return ctrl.Result {}, nil
403409}
404410
@@ -415,12 +421,13 @@ func (r *SwiftReconciler) SetupWithManager(mgr ctrl.Manager) error {
415421 Complete (r )
416422}
417423
418- func (r * SwiftReconciler ) reconcileDelete (instance * swiftv1.Swift , helper * helper.Helper ) (ctrl.Result , error ) {
419- r .Log .Info (fmt .Sprintf ("Reconciling Service '%s' delete" , instance .Name ))
424+ func (r * SwiftReconciler ) reconcileDelete (ctx context.Context , instance * swiftv1.Swift , helper * helper.Helper ) (ctrl.Result , error ) {
425+ Log := r .GetLogger (ctx )
426+ Log .Info (fmt .Sprintf ("Reconciling Service '%s' delete" , instance .Name ))
420427
421428 // Service is deleted so remove the finalizer.
422429 controllerutil .RemoveFinalizer (instance , helper .GetFinalizer ())
423- r . Log .Info (fmt .Sprintf ("Reconciled Service '%s' delete successfully" , instance .Name ))
430+ Log .Info (fmt .Sprintf ("Reconciled Service '%s' delete successfully" , instance .Name ))
424431
425432 return ctrl.Result {}, nil
426433}
@@ -569,14 +576,16 @@ func (r *SwiftReconciler) proxyCreateOrUpdate(ctx context.Context, instance *swi
569576
570577// checkSwiftProxyGeneration -
571578func (r * SwiftReconciler ) checkSwiftProxyGeneration (
579+ ctx context.Context ,
572580 instance * swiftv1.Swift ,
573581) (bool , error ) {
582+ Log := r .GetLogger (ctx )
574583 proxy := & swiftv1.SwiftProxyList {}
575584 listOpts := []client.ListOption {
576585 client .InNamespace (instance .Namespace ),
577586 }
578587 if err := r .Client .List (context .Background (), proxy , listOpts ... ); err != nil {
579- r . Log .Error (err , "Unable to retrieve SwiftProxy %w" )
588+ Log .Error (err , "Unable to retrieve SwiftProxy %w" )
580589 return false , err
581590 }
582591 for _ , item := range proxy .Items {
@@ -589,14 +598,16 @@ func (r *SwiftReconciler) checkSwiftProxyGeneration(
589598
590599// checkSwiftStorageGeneration -
591600func (r * SwiftReconciler ) checkSwiftStorageGeneration (
601+ ctx context.Context ,
592602 instance * swiftv1.Swift ,
593603) (bool , error ) {
604+ Log := r .GetLogger (ctx )
594605 sst := & swiftv1.SwiftStorageList {}
595606 listOpts := []client.ListOption {
596607 client .InNamespace (instance .Namespace ),
597608 }
598609 if err := r .Client .List (context .Background (), sst , listOpts ... ); err != nil {
599- r . Log .Error (err , "Unable to retrieve SwiftStorage %w" )
610+ Log .Error (err , "Unable to retrieve SwiftStorage %w" )
600611 return false , err
601612 }
602613 for _ , item := range sst .Items {
@@ -609,14 +620,16 @@ func (r *SwiftReconciler) checkSwiftStorageGeneration(
609620
610621// checkSwiftRingGeneration -
611622func (r * SwiftReconciler ) checkSwiftRingGeneration (
623+ ctx context.Context ,
612624 instance * swiftv1.Swift ,
613625) (bool , error ) {
626+ Log := r .GetLogger (ctx )
614627 rings := & swiftv1.SwiftRingList {}
615628 listOpts := []client.ListOption {
616629 client .InNamespace (instance .Namespace ),
617630 }
618631 if err := r .Client .List (context .Background (), rings , listOpts ... ); err != nil {
619- r . Log .Error (err , "Unable to retrieve SwiftRing %w" )
632+ Log .Error (err , "Unable to retrieve SwiftRing %w" )
620633 return false , err
621634 }
622635 for _ , item := range rings .Items {
0 commit comments