From e01704831a8ae52d69b4413f97d9f79354004447 Mon Sep 17 00:00:00 2001 From: r Date: Sun, 3 Sep 2023 10:37:16 +0300 Subject: [PATCH] switch to structured logging fix gitlog func update logging update tests logging update logging --- controllers/glance_controller.go | 57 ++++++++++++++++---------- controllers/glanceapi_controller.go | 62 +++++++++++++++++++---------- main.go | 5 +-- test/functional/suite_test.go | 4 +- 4 files changed, 80 insertions(+), 48 deletions(-) diff --git a/controllers/glance_controller.go b/controllers/glance_controller.go index c75158052..d9889aa7b 100644 --- a/controllers/glance_controller.go +++ b/controllers/glance_controller.go @@ -62,10 +62,14 @@ import ( type GlanceReconciler struct { client.Client Kclient kubernetes.Interface - Log logr.Logger Scheme *runtime.Scheme } +// getlogger returns a logger object with a prefix of "conroller.name" and aditional controller context fields +func (r *GlanceReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("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 @@ -89,7 +93,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{} @@ -110,7 +114,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 @@ -203,7 +207,9 @@ 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 := r.GetLogger(ctx) + + Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name)) // remove db finalizer first db, err := mariadbv1.GetDatabaseByName(ctx, helper, instance.Name) @@ -270,7 +276,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 } @@ -282,7 +288,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)) // Define the PVCs objects required by Glance ctrlResult, err := r.ensurePVC(ctx, helper, instance, serviceLabels) @@ -429,7 +437,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) @@ -438,32 +446,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{ @@ -610,7 +624,7 @@ func (r *GlanceReconciler) reconcileNormal(ctx context.Context, instance *glance return ctrl.Result{}, 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 @@ -642,7 +656,7 @@ func (r *GlanceReconciler) reconcileNormal(ctx context.Context, instance *glance return ctrl.Result{}, 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))) } // Mirror external GlanceAPI status' APIEndpoints and ReadyCount to this parent CR @@ -675,7 +689,7 @@ func (r *GlanceReconciler) reconcileNormal(ctx context.Context, instance *glance instance.Status.Conditions.MarkTrue(condition.CronJobReadyCondition, condition.CronJobReadyMessage) // create CronJob - 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 } @@ -754,6 +768,7 @@ func (r *GlanceReconciler) generateServiceConfig( instance *glancev1.Glance, envVars *map[string]env.Setter, ) error { + Log := r.GetLogger(ctx) labels := labels.GetLabels(instance, labels.GetGroupLabel(glance.ServiceName), map[string]string{}) ospSecret, _, err := secret.GetSecret(ctx, h, instance.Spec.Secret, instance.Namespace) @@ -778,7 +793,7 @@ func (r *GlanceReconciler) generateServiceConfig( // error defer func() { if err := recover(); err != nil { - r.Log.Error(err.(error), "ImageCacheSize not valid: %s") + Log.Error(err.(error), "ImageCacheSize not valid: %s") } }() cacheSize := resource.MustParse(instance.Spec.ImageCacheSize) @@ -805,6 +820,7 @@ func (r *GlanceReconciler) ensureRegisteredLimits( instance *glancev1.Glance, quota map[string]int, ) error { + Log := r.GetLogger(ctx) // get admin var err error @@ -826,7 +842,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 } @@ -917,6 +933,7 @@ func (r *GlanceReconciler) registeredLimitsDelete( instance *glancev1.Glance, quota map[string]int, ) error { + Log := r.GetLogger(ctx) // get admin var err error @@ -929,12 +946,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 } diff --git a/controllers/glanceapi_controller.go b/controllers/glanceapi_controller.go index b2d6efc82..9aa0c0401 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 prefix of "conroller.name" and aditional controller context fields +func (r *GlanceAPIReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("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 @@ -82,7 +86,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{} @@ -103,7 +107,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 @@ -176,7 +180,9 @@ 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 { @@ -189,8 +195,8 @@ func (r *GlanceAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { listOpts := []client.ListOption{ client.InNamespace(namespace), } - if err := r.Client.List(context.Background(), apis, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve API CRs %v") + if err := r.Client.List(ctx, apis, listOpts...); err != nil { + Log.Error(err, "Unable to retrieve API CRs %v") return nil } for _, cr := range apis.Items { @@ -200,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}) } } @@ -220,8 +226,8 @@ func (r *GlanceAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { listOpts := []client.ListOption{ 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") + if err := r.Client.List(ctx, glanceAPIs, listOpts...); err != nil { + Log.Error(err, "Unable to retrieve GlanceAPI CRs %w") return nil } for _, cr := range glanceAPIs.Items { @@ -230,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}) } } @@ -254,7 +260,9 @@ 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(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name)) // Remove the finalizer from our KeystoneEndpoint CR keystoneEndpoint, err := keystonev1.GetKeystoneEndpointWithName(ctx, helper, instance.Name, instance.Namespace) @@ -274,7 +282,7 @@ func (r *GlanceAPIReconciler) reconcileDelete(ctx context.Context, instance *gla // 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 } @@ -285,7 +293,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 @@ -436,32 +446,38 @@ func (r *GlanceAPIReconciler) reconcileInit( // create keystone endpoints - 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 *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(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 *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 @@ -543,7 +559,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 } } @@ -672,7 +688,7 @@ func (r *GlanceAPIReconciler) reconcileNormal(ctx context.Context, instance *gla } // create Deployment - 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 } @@ -775,6 +791,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) @@ -784,7 +802,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 } diff --git a/main.go b/main.go index 775cea184..bdffb30c6 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "context" "flag" "os" "strings" @@ -105,8 +106,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("GlanceAPI"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(mgr, context.Background()); err != nil { setupLog.Error(err, "unable to create controller", "controller", "GlanceAPI") os.Exit(1) } @@ -114,7 +114,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 278cd8772..e9cc299c3 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(k8sManager, context.Background()) Expect(err).ToNot(HaveOccurred()) // Acquire environmental defaults and initialize operator defaults with them