diff --git a/controllers/glance_controller.go b/controllers/glance_controller.go index aa4933cd..ba573271 100644 --- a/controllers/glance_controller.go +++ b/controllers/glance_controller.go @@ -60,10 +60,14 @@ import ( type GlanceReconciler struct { client.Client Kclient kubernetes.Interface - Log logr.Logger Scheme *runtime.Scheme } +// GetLogger returns a logger object with a logging prefix of "controller.name" and additional controller context fields +func (r *GlanceReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName(string(glance.Glance)) +} + //+kubebuilder:rbac:groups=glance.openstack.org,resources=glances,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=glance.openstack.org,resources=glances/status,verbs=get;update;patch //+kubebuilder:rbac:groups=glance.openstack.org,resources=glances/finalizers,verbs=update @@ -87,7 +91,7 @@ type GlanceReconciler struct { // Reconcile reconcile Glance requests func (r *GlanceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = log.FromContext(ctx) + Log := r.GetLogger(ctx) // Fetch the Glance instance instance := &glancev1.Glance{} @@ -108,7 +112,7 @@ func (r *GlanceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -204,7 +208,8 @@ func (r *GlanceReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *GlanceReconciler) reconcileDelete(ctx context.Context, instance *glancev1.Glance, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name)) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name)) // remove db finalizer first db, err := mariadbv1.GetDatabaseByName(ctx, helper, instance.Name) @@ -260,7 +265,7 @@ func (r *GlanceReconciler) reconcileDelete(ctx context.Context, instance *glance // Service is deleted so remove the finalizer. controllerutil.RemoveFinalizer(instance, helper.GetFinalizer()) - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name)) return ctrl.Result{}, nil } @@ -303,7 +308,9 @@ func (r *GlanceReconciler) reconcileInit( serviceLabels map[string]string, serviceAnnotations map[string]string, ) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name)) // // create service DB instance @@ -441,7 +448,7 @@ func (r *GlanceReconciler) reconcileInit( } if dbSyncjob.HasChanged() { instance.Status.Hash[glancev1.DbSyncHash] = dbSyncjob.GetHash() - r.Log.Info(fmt.Sprintf("Service '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[glancev1.DbSyncHash])) + Log.Info(fmt.Sprintf("Service '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[glancev1.DbSyncHash])) } instance.Status.Conditions.MarkTrue(condition.DBSyncReadyCondition, condition.DBSyncReadyMessage) @@ -450,32 +457,38 @@ func (r *GlanceReconciler) reconcileInit( // run Glance db sync - end - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name)) return ctrl.Result{}, nil } func (r *GlanceReconciler) reconcileUpdate(ctx context.Context, instance *glancev1.Glance, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name)) // TODO: should have minor update tasks if required // - delete dbsync hash from status to rerun it? - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name)) return ctrl.Result{}, nil } func (r *GlanceReconciler) reconcileUpgrade(ctx context.Context, instance *glancev1.Glance, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name)) // TODO: should have major version upgrade tasks // -delete dbsync hash from status to rerun it? - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name)) return ctrl.Result{}, nil } func (r *GlanceReconciler) reconcileNormal(ctx context.Context, instance *glancev1.Glance, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s'", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s'", instance.Name)) // Service account, role, binding rbacRules := []rbacv1.PolicyRule{ @@ -629,6 +642,8 @@ func (r *GlanceReconciler) apiDeployment( serviceLabels map[string]string, ) error { + Log := r.GetLogger(ctx) + // By default internal and external points to diff instances, but we might // want to override "external" with "single" in case APIType == "single": // in this case we only deploy the External instance and skip the internal @@ -662,7 +677,7 @@ func (r *GlanceReconciler) apiDeployment( return err } if op != controllerutil.OperationResultNone { - r.Log.Info(fmt.Sprintf("StatefulSet %s successfully reconciled - operation: %s", instance.Name, string(op))) + Log.Info(fmt.Sprintf("StatefulSet %s successfully reconciled - operation: %s", instance.Name, string(op))) } if instance.Status.GlanceAPIReadyCounts == nil { instance.Status.GlanceAPIReadyCounts = map[string]int32{} @@ -707,7 +722,7 @@ func (r *GlanceReconciler) apiDeployment( return err } if op != controllerutil.OperationResultNone { - r.Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op))) + Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op))) } // It is possible that an earlier call to update the status has also set @@ -731,7 +746,7 @@ func (r *GlanceReconciler) apiDeployment( instance.Status.Conditions.Set(apiCondition) } - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' successfully", instance.Name)) return nil } @@ -874,6 +889,7 @@ func (r *GlanceReconciler) ensureRegisteredLimits( instance *glancev1.Glance, quota map[string]int, ) error { + Log := r.GetLogger(ctx) // get admin var err error @@ -895,7 +911,7 @@ func (r *GlanceReconciler) ensureRegisteredLimits( ResourceName: lName, DefaultLimit: lValue, } - _, err = o.CreateOrUpdateRegisteredLimit(r.Log, m) + _, err = o.CreateOrUpdateRegisteredLimit(Log, m) if err != nil { return err } @@ -953,6 +969,7 @@ func (r *GlanceReconciler) registeredLimitsDelete( instance *glancev1.Glance, quota map[string]int, ) error { + Log := r.GetLogger(ctx) // get admin var err error @@ -965,12 +982,12 @@ func (r *GlanceReconciler) registeredLimitsDelete( if err != nil { return err } - fetchRegLimits, err := o.ListRegisteredLimitsByServiceID(r.Log, instance.Status.ServiceID) + fetchRegLimits, err := o.ListRegisteredLimitsByServiceID(Log, instance.Status.ServiceID) if err != nil { return err } for _, l := range fetchRegLimits { - err = o.DeleteRegisteredLimit(r.Log, l.ID) + err = o.DeleteRegisteredLimit(Log, l.ID) if err != nil { return err } @@ -981,13 +998,15 @@ func (r *GlanceReconciler) registeredLimitsDelete( // GlanceAPICleanup - Delete the glanceAPI instance if it no longer appears // in the spec. func (r *GlanceReconciler) glanceAPICleanup(ctx context.Context, instance *glancev1.Glance) error { + Log := r.GetLogger(ctx) + // Generate a list of GlanceAPI CRs apis := &glancev1.GlanceAPIList{} listOpts := []client.ListOption{ client.InNamespace(instance.Namespace), } if err := r.Client.List(ctx, apis, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve GlanceAPI CRs %v") + Log.Error(err, "Unable to retrieve GlanceAPI CRs %v") return nil } for _, glanceAPI := range apis.Items { @@ -999,7 +1018,7 @@ func (r *GlanceReconciler) glanceAPICleanup(ctx context.Context, instance *glanc // Simply return if the apiName doesn't match the existing pattern, log but do not // raise an error if apiName == "" { - r.Log.Info(fmt.Sprintf("GlanceAPI %s does not match the pattern", glanceAPI.Name)) + Log.Info(fmt.Sprintf("GlanceAPI %s does not match the pattern", glanceAPI.Name)) return nil } _, exists := instance.Spec.GlanceAPIs[apiName] diff --git a/controllers/glanceapi_controller.go b/controllers/glanceapi_controller.go index 37e32692..38b2e73e 100644 --- a/controllers/glanceapi_controller.go +++ b/controllers/glanceapi_controller.go @@ -64,10 +64,14 @@ import ( type GlanceAPIReconciler struct { client.Client Kclient kubernetes.Interface - Log logr.Logger Scheme *runtime.Scheme } +// GetLogger returns a logger object with a logging prefix of "controller.name" and additional controller context fields +func (r *GlanceAPIReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName(string(glance.GlanceAPI)) +} + //+kubebuilder:rbac:groups=glance.openstack.org,resources=glanceapis,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=glance.openstack.org,resources=glanceapis/status,verbs=get;update;patch //+kubebuilder:rbac:groups=glance.openstack.org,resources=glanceapis/finalizers,verbs=update @@ -83,7 +87,7 @@ type GlanceAPIReconciler struct { // Reconcile reconcile Glance API requests func (r *GlanceAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = log.FromContext(ctx) + Log := r.GetLogger(ctx) // Fetch the GlanceAPI instance instance := &glancev1.GlanceAPI{} @@ -104,7 +108,7 @@ func (r *GlanceAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -177,7 +181,8 @@ func (r *GlanceAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( } // SetupWithManager sets up the controller with the Manager. -func (r *GlanceAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *GlanceAPIReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { + Log := r.GetLogger(ctx) // Watch for changes to any CustomServiceConfigSecrets. Global secrets svcSecretFn := func(o client.Object) []reconcile.Request { @@ -191,7 +196,7 @@ func (r *GlanceAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { client.InNamespace(namespace), } if err := r.Client.List(context.Background(), apis, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve API CRs %v") + Log.Error(err, "Unable to retrieve API CRs %v") return nil } for _, cr := range apis.Items { @@ -201,7 +206,7 @@ func (r *GlanceAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: namespace, Name: cr.Name, } - r.Log.Info(fmt.Sprintf("Secret %s is used by Glance CR %s", secretName, cr.Name)) + Log.Info(fmt.Sprintf("Secret %s is used by Glance CR %s", secretName, cr.Name)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -222,7 +227,7 @@ func (r *GlanceAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { client.InNamespace(o.GetNamespace()), } if err := r.Client.List(context.Background(), glanceAPIs, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve GlanceAPI CRs %w") + Log.Error(err, "Unable to retrieve GlanceAPI CRs %w") return nil } for _, cr := range glanceAPIs.Items { @@ -231,7 +236,7 @@ func (r *GlanceAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: cr.GetNamespace(), Name: cr.GetName(), } - r.Log.Info(fmt.Sprintf("NAD %s is used by GlanceAPI CR %s", o.GetName(), cr.GetName())) + Log.Info(fmt.Sprintf("NAD %s is used by GlanceAPI CR %s", o.GetName(), cr.GetName())) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -255,14 +260,16 @@ func (r *GlanceAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *GlanceAPIReconciler) reconcileDelete(ctx context.Context, instance *glancev1.GlanceAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info("Reconciling Service delete") if ctrlResult, err := r.ensureDeletedEndpoints(ctx, instance, helper); err != nil { return ctrlResult, err } // Endpoints are deleted so remove the finalizer. controllerutil.RemoveFinalizer(instance, helper.GetFinalizer()) - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name)) return ctrl.Result{}, nil } @@ -273,7 +280,9 @@ func (r *GlanceAPIReconciler) reconcileInit( helper *helper.Helper, serviceLabels map[string]string, ) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name)) // // create service/s @@ -405,32 +414,38 @@ func (r *GlanceAPIReconciler) reconcileInit( return ctrlResult, err } - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name)) + Log.Info("Reconciled Service init successfully") return ctrl.Result{}, nil } func (r *GlanceAPIReconciler) reconcileUpdate(ctx context.Context, instance *glancev1.GlanceAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info("Reconciling Service update") // TODO: should have minor update tasks if required // - delete dbsync hash from status to rerun it? - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name)) return ctrl.Result{}, nil } func (r *GlanceAPIReconciler) reconcileUpgrade(ctx context.Context, instance *glancev1.GlanceAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name)) // TODO: should have major version upgrade tasks // -delete dbsync hash from status to rerun it? - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name)) return ctrl.Result{}, nil } func (r *GlanceAPIReconciler) reconcileNormal(ctx context.Context, instance *glancev1.GlanceAPI, helper *helper.Helper, req ctrl.Request) (ctrl.Result, error) { - r.Log.Info(fmt.Sprintf("Reconciling Service '%s'", instance.Name)) + Log := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s'", instance.Name)) configVars := make(map[string]env.Setter) privileged := false @@ -519,7 +534,7 @@ func (r *GlanceAPIReconciler) reconcileNormal(ctx context.Context, instance *gla if err != nil { if errors.IsNotFound(err) { // Request object not found, can't run GlanceAPI with this config - r.Log.Info("Cinder resource not found. Waiting for it to be deployed") + Log.Info("Cinder resource not found. Waiting for it to be deployed") return ctrl.Result{RequeueAfter: time.Duration(10) * time.Second}, nil } } @@ -625,7 +640,7 @@ func (r *GlanceAPIReconciler) reconcileNormal(ctx context.Context, instance *gla } // create StatefulSet - end - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciled Service '%s' successfully", instance.Name)) return ctrl.Result{}, nil } @@ -731,6 +746,8 @@ func (r *GlanceAPIReconciler) createHashOfInputHashes( instance *glancev1.GlanceAPI, envVars map[string]env.Setter, ) (string, bool, error) { + Log := r.GetLogger(ctx) + var hashMap map[string]string changed := false mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars) @@ -740,7 +757,7 @@ func (r *GlanceAPIReconciler) createHashOfInputHashes( } if hashMap, changed = util.SetHash(instance.Status.Hash, common.InputHashName, hash); changed { instance.Status.Hash = hashMap - r.Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash)) + Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash)) } return hash, changed, nil } @@ -752,6 +769,7 @@ func (r *GlanceAPIReconciler) ensureKeystoneEndpoints( instance *glancev1.GlanceAPI, serviceLabels map[string]string, ) (ctrl.Result, error) { + Log := r.GetLogger(ctx) var ctrlResult ctrl.Result var err error @@ -771,7 +789,7 @@ func (r *GlanceAPIReconciler) ensureKeystoneEndpoints( // the registered endpoints with the new URL. err = keystonev1.DeleteKeystoneEndpointWithName(ctx, helper, instance.Name, instance.Namespace) if err != nil { - r.Log.Info(fmt.Sprintf("Endpoint %s not found", instance.Name)) + Log.Info("Endpoint not found") return ctrlResult, nil } return ctrlResult, nil diff --git a/main.go b/main.go index 51576ab3..44e8db70 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "context" "crypto/tls" "flag" "os" @@ -115,8 +116,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("GlanceAPI"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(context.Background(), mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "GlanceAPI") os.Exit(1) } @@ -124,7 +124,6 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("Glance"), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Glance") os.Exit(1) diff --git a/test/functional/suite_test.go b/test/functional/suite_test.go index 42d6a2b2..cdccaf13 100644 --- a/test/functional/suite_test.go +++ b/test/functional/suite_test.go @@ -178,7 +178,6 @@ var _ = BeforeSuite(func() { Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("Glance"), }).SetupWithManager(k8sManager) Expect(err).ToNot(HaveOccurred()) @@ -186,8 +185,7 @@ var _ = BeforeSuite(func() { Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("GlanceAPI"), - }).SetupWithManager(k8sManager) + }).SetupWithManager(context.Background(), k8sManager) Expect(err).ToNot(HaveOccurred()) // Acquire environmental defaults and initialize operator defaults with them