From 304a33148a4647c1fbad71d5522f2c4c525ef252 Mon Sep 17 00:00:00 2001 From: r Date: Sun, 3 Sep 2023 11:32:55 +0300 Subject: [PATCH] switch to structured logging update logging update logging update logging update logging update logging update logging fixup lint --- controllers/designate_controller.go | 89 ++++++++++++--------- controllers/designateapi_controller.go | 62 ++++++++------ controllers/designatecentral_controller.go | 64 +++++++++------ controllers/designatemdns_controller.go | 65 +++++++++------ controllers/designateproducer_controller.go | 64 +++++++++------ controllers/designateworker_controller.go | 57 +++++++------ main.go | 19 ++--- 7 files changed, 245 insertions(+), 175 deletions(-) diff --git a/controllers/designate_controller.go b/controllers/designate_controller.go index 50089d43..fb8b8501 100644 --- a/controllers/designate_controller.go +++ b/controllers/designate_controller.go @@ -67,9 +67,9 @@ func (r *DesignateReconciler) GetKClient() kubernetes.Interface { return r.Kclient } -// GetLogger - -func (r *DesignateReconciler) GetLogger() logr.Logger { - return r.Log +// GetLogger returns a logger object with a prefix of "conroller.name" and aditional controller context fields +func (r *DesignateReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("Designate") } // GetScheme - @@ -81,7 +81,6 @@ func (r *DesignateReconciler) GetScheme() *runtime.Scheme { type DesignateReconciler struct { client.Client Kclient kubernetes.Interface - Log logr.Logger Scheme *runtime.Scheme } @@ -124,7 +123,7 @@ type DesignateReconciler struct { // Reconcile - func (r *DesignateReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = log.FromContext(ctx) + Log := r.GetLogger(ctx) // Fetch the Designate instance instance := &designatev1beta1.Designate{} @@ -145,7 +144,7 @@ func (r *DesignateReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -215,7 +214,7 @@ func (r *DesignateReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( } // SetupWithManager sets up the controller with the Manager. -func (r *DesignateReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *DesignateReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { // transportURLSecretFn - Watch for changes made to the secret associated with the RabbitMQ // TransportURL created and used by Designate CRs. Watch functions return a list of namespace-scoped // CRs that then get fed to the reconciler. Hence, in this case, we need to know the name of the @@ -227,6 +226,8 @@ func (r *DesignateReconciler) SetupWithManager(mgr ctrl.Manager) error { // reconciliation for a Designate CR that does not need it. // // TODO: We also need a watch func to monitor for changes to the secret referenced by Designate.Spec.Secret + Log := r.GetLogger(ctx) + transportURLSecretFn := func(o client.Object) []reconcile.Request { result := []reconcile.Request{} @@ -236,7 +237,7 @@ func (r *DesignateReconciler) SetupWithManager(mgr ctrl.Manager) error { client.InNamespace(o.GetNamespace()), } if err := r.Client.List(context.Background(), designates, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve Designate CRs %v") + Log.Error(err, "Unable to retrieve Designate CRs %v") return nil } @@ -249,7 +250,7 @@ func (r *DesignateReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: o.GetNamespace(), Name: cr.Name, } - r.Log.Info(fmt.Sprintf("TransportURL Secret %s belongs to TransportURL belonging to Designate CR %s", o.GetName(), cr.Name)) + Log.Info(fmt.Sprintf("TransportURL Secret %s belongs to TransportURL belonging to Designate CR %s", o.GetName(), cr.Name)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -283,7 +284,9 @@ func (r *DesignateReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *DesignateReconciler) reconcileDelete(ctx context.Context, instance *designatev1beta1.Designate, 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) @@ -302,7 +305,7 @@ func (r *DesignateReconciler) reconcileDelete(ctx context.Context, instance *des // 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 } @@ -314,7 +317,9 @@ func (r *DesignateReconciler) 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 @@ -409,7 +414,7 @@ func (r *DesignateReconciler) reconcileInit( } if dbSyncjob.HasChanged() { instance.Status.Hash[designatev1beta1.DbSyncHash] = dbSyncjob.GetHash() - r.Log.Info(fmt.Sprintf("Service '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[designatev1beta1.DbSyncHash])) + Log.Info(fmt.Sprintf("Service '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[designatev1beta1.DbSyncHash])) } instance.Status.Conditions.MarkTrue(condition.DBSyncReadyCondition, condition.DBSyncReadyMessage) @@ -418,12 +423,14 @@ func (r *DesignateReconciler) reconcileInit( // run Designate 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 *DesignateReconciler) reconcileNormal(ctx context.Context, instance *designatev1beta1.Designate, 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{ @@ -470,13 +477,13 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des } if op != controllerutil.OperationResultNone { - r.Log.Info(fmt.Sprintf("TransportURL %s successfully reconciled - operation: %s", transportURL.Name, string(op))) + Log.Info(fmt.Sprintf("TransportURL %s successfully reconciled - operation: %s", transportURL.Name, string(op))) } instance.Status.TransportURLSecret = transportURL.Status.SecretName if instance.Status.TransportURLSecret == "" { - r.Log.Info(fmt.Sprintf("Waiting for TransportURL %s secret to be created", transportURL.Name)) + Log.Info(fmt.Sprintf("Waiting for TransportURL %s secret to be created", transportURL.Name)) instance.Status.Conditions.Set(condition.FalseCondition( designatev1beta1.DesignateRabbitMqTransportURLReadyCondition, condition.RequestedReason, @@ -526,7 +533,7 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des // - %-config configmap holding minimal designate config required to get the service up, user can add additional files to be added to the service // - parameters which has passwords gets added from the OpenStack secret via the init container // - r.Log.Info("pre generateConfigMap ....") + Log.Info("pre generateConfigMap ....") err = r.generateServiceConfigMaps(ctx, helper, instance, &configMapVars, serviceLabels) if err != nil { @@ -538,7 +545,7 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des err.Error())) return ctrl.Result{}, err } - r.Log.Info("post generateConfigMap ....") + Log.Info("post generateConfigMap ....") // // create hash over all the different input resources to identify if any those changed @@ -622,7 +629,7 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des // // normal reconcile tasks // - r.Log.Info("Reconcile tasks starting....") + Log.Info("Reconcile tasks starting....") // deploy designate-api designateAPI, op, err := r.apiDeploymentCreateOrUpdate(ctx, instance) @@ -636,7 +643,7 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des 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 DesignateAPI status' ReadyCount to this parent CR @@ -647,7 +654,7 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des if c != nil { instance.Status.Conditions.Set(c) } - r.Log.Info("Deployment API task reconciled") + Log.Info("Deployment API task reconciled") // deploy designate-central designateCentral, op, err := r.centralDeploymentCreateOrUpdate(ctx, instance) @@ -661,7 +668,7 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des 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 DesignateCentral status' ReadyCount to this parent CR @@ -672,7 +679,7 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des if c != nil { instance.Status.Conditions.Set(c) } - r.Log.Info("Deployment Central task reconciled") + Log.Info("Deployment Central task reconciled") // deploy designate-worker designateWorker, op, err := r.workerDeploymentCreateOrUpdate(ctx, instance) @@ -686,7 +693,7 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des 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 DesignateWorker status' ReadyCount to this parent CR @@ -697,7 +704,7 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des if c != nil { instance.Status.Conditions.Set(c) } - r.Log.Info("Deployment Worker task reconciled") + Log.Info("Deployment Worker task reconciled") // deploy designate-mdns designateMdns, op, err := r.mdnsDeploymentCreateOrUpdate(ctx, instance) @@ -711,7 +718,7 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des 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 DesignateMdns status' ReadyCount to this parent CR @@ -722,7 +729,7 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des if c != nil { instance.Status.Conditions.Set(c) } - r.Log.Info("Deployment Mdns task reconciled") + Log.Info("Deployment Mdns task reconciled") // deploy designate-producer designateProducer, op, err := r.producerDeploymentCreateOrUpdate(ctx, instance) @@ -736,7 +743,7 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des 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 DesignateProducer status' ReadyCount to this parent CR @@ -747,7 +754,7 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des if c != nil { instance.Status.Conditions.Set(c) } - r.Log.Info("Deployment Producer task reconciled") + Log.Info("Deployment Producer task reconciled") // deploy designate-backendbind9 designateBackendbind9, op, err := r.backendbind9DeploymentCreateOrUpdate(ctx, instance) @@ -761,7 +768,7 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des 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 DesignateBackendbind9 status' ReadyCount to this parent CR @@ -772,29 +779,33 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des if c != nil { instance.Status.Conditions.Set(c) } - r.Log.Info("Deployment Backendbind9 task reconciled") + Log.Info("Deployment Backendbind9 task reconciled") - 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 } func (r *DesignateReconciler) reconcileUpdate(ctx context.Context, instance *designatev1beta1.Designate, 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 *DesignateReconciler) reconcileUpgrade(ctx context.Context, instance *designatev1beta1.Designate, 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 } @@ -877,6 +888,8 @@ func (r *DesignateReconciler) createHashOfInputHashes( instance *designatev1beta1.Designate, 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) @@ -886,7 +899,7 @@ func (r *DesignateReconciler) 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/controllers/designateapi_controller.go b/controllers/designateapi_controller.go index da9776d7..0885416f 100644 --- a/controllers/designateapi_controller.go +++ b/controllers/designateapi_controller.go @@ -31,6 +31,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" @@ -64,11 +65,6 @@ func (r *DesignateAPIReconciler) GetKClient() kubernetes.Interface { return r.Kclient } -// GetLogger - -func (r *DesignateAPIReconciler) GetLogger() logr.Logger { - return r.Log -} - // GetScheme - func (r *DesignateAPIReconciler) GetScheme() *runtime.Scheme { return r.Scheme @@ -78,10 +74,14 @@ func (r *DesignateAPIReconciler) GetScheme() *runtime.Scheme { type DesignateAPIReconciler 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 *DesignateAPIReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("DesignateAPI") +} + var ( keystoneServices = []map[string]string{ { @@ -116,7 +116,7 @@ var ( // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile func (r *DesignateAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = r.Log.WithValues("designateapi", req.NamespacedName) + Log := r.GetLogger(ctx) // Fetch the DesignateAPI instance instance := &designatev1beta1.DesignateAPI{} @@ -137,7 +137,7 @@ func (r *DesignateAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -206,9 +206,11 @@ func (r *DesignateAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request } // SetupWithManager sets up the controller with the Manager. -func (r *DesignateAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *DesignateAPIReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { // Watch for changes to any CustomServiceConfigSecrets. Global secrets // (e.g. TransportURLSecret) are handled by the top designate controller. + Log := r.GetLogger(ctx) + svcSecretFn := func(o client.Object) []reconcile.Request { var namespace string = o.GetNamespace() var secretName string = o.GetName() @@ -220,7 +222,7 @@ func (r *DesignateAPIReconciler) 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") return nil } for _, cr := range apis.Items { @@ -230,7 +232,7 @@ func (r *DesignateAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: namespace, Name: cr.Name, } - r.Log.Info(fmt.Sprintf("Secret %s is used by Designate CR %s", secretName, cr.Name)) + Log.Info(fmt.Sprintf("Secret %s is used by Designate CR %s", secretName, cr.Name)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -255,7 +257,7 @@ func (r *DesignateAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { client.InNamespace(o.GetNamespace()), } if err := r.Client.List(context.Background(), designateAPIs, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve DesignateAPI CRs %v") + Log.Error(err, "Unable to retrieve DesignateAPI CRs %v") return nil } @@ -265,7 +267,7 @@ func (r *DesignateAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: cr.GetNamespace(), Name: cr.GetName(), } - r.Log.Info(fmt.Sprintf("NAD %s is used by DesignateAPI CR %s", o.GetName(), cr.GetName())) + Log.Info(fmt.Sprintf("NAD %s is used by DesignateAPI CR %s", o.GetName(), cr.GetName())) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -290,7 +292,9 @@ func (r *DesignateAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *DesignateAPIReconciler) reconcileDelete(ctx context.Context, instance *designatev1beta1.DesignateAPI, 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)) for _, ksSvc := range keystoneServices { @@ -326,7 +330,7 @@ func (r *DesignateAPIReconciler) reconcileDelete(ctx context.Context, instance * // We did all the cleanup on the objects we created so we can remove the // finalizer from ourselves to allow the deletion 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 } @@ -337,7 +341,9 @@ func (r *DesignateAPIReconciler) 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)) // // expose the service (create service, route and return the created endpoint URLs) @@ -523,12 +529,14 @@ func (r *DesignateAPIReconciler) reconcileInit( } } - 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 *DesignateAPIReconciler) reconcileNormal(ctx context.Context, instance *designatev1beta1.DesignateAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Service") + Log := r.GetLogger(ctx) + + Log.Info("Reconciling Service") // ConfigMap configMapVars := make(map[string]env.Setter) @@ -765,27 +773,31 @@ func (r *DesignateAPIReconciler) reconcileNormal(ctx context.Context, instance * } // create Deployment - end - r.Log.Info("Reconciled Service successfully") + Log.Info("Reconciled Service successfully") return ctrl.Result{}, nil } func (r *DesignateAPIReconciler) reconcileUpdate(ctx context.Context, instance *designatev1beta1.DesignateAPI, 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 *DesignateAPIReconciler) reconcileUpgrade(ctx context.Context, instance *designatev1beta1.DesignateAPI, 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 } @@ -874,6 +886,8 @@ func (r *DesignateAPIReconciler) createHashOfInputHashes( instance *designatev1beta1.DesignateAPI, 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) @@ -883,7 +897,7 @@ func (r *DesignateAPIReconciler) 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/controllers/designatecentral_controller.go b/controllers/designatecentral_controller.go index 2a6799e8..1502b2d3 100644 --- a/controllers/designatecentral_controller.go +++ b/controllers/designatecentral_controller.go @@ -31,6 +31,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" @@ -60,11 +61,6 @@ func (r *DesignateCentralReconciler) GetKClient() kubernetes.Interface { return r.Kclient } -// GetLogger - -func (r *DesignateCentralReconciler) GetLogger() logr.Logger { - return r.Log -} - // GetScheme - func (r *DesignateCentralReconciler) GetScheme() *runtime.Scheme { return r.Scheme @@ -74,10 +70,14 @@ func (r *DesignateCentralReconciler) GetScheme() *runtime.Scheme { type DesignateCentralReconciler 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 *DesignateCentralReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("DesignateCentral") +} + //+kubebuilder:rbac:groups=designate.openstack.org,resources=designatecentrals,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=designate.openstack.org,resources=designatecentrals/status,verbs=get;update;patch //+kubebuilder:rbac:groups=designate.openstack.org,resources=designatecentrals/finalizers,verbs=update @@ -98,7 +98,7 @@ type DesignateCentralReconciler struct { // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile func (r *DesignateCentralReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = r.Log.WithValues("designatecentral", req.NamespacedName) + Log := r.GetLogger(ctx) // Fetch the DesignateCentral instance instance := &designatev1beta1.DesignateCentral{} @@ -119,7 +119,7 @@ func (r *DesignateCentralReconciler) Reconcile(ctx context.Context, req ctrl.Req r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -179,9 +179,11 @@ func (r *DesignateCentralReconciler) Reconcile(ctx context.Context, req ctrl.Req } // SetupWithManager sets up the controller with the Manager. -func (r *DesignateCentralReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *DesignateCentralReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { // Watch for changes to any CustomServiceConfigSecrets. Global secrets // (e.g. TransportURLSecret) are handled by the top designate controller. + Log := r.GetLogger(ctx) + svcSecretFn := func(o client.Object) []reconcile.Request { var namespace string = o.GetNamespace() var secretName string = o.GetName() @@ -193,7 +195,7 @@ func (r *DesignateCentralReconciler) 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 Central CRs %v") + Log.Error(err, "Unable to retrieve Central CRs %v") return nil } for _, cr := range apis.Items { @@ -203,7 +205,7 @@ func (r *DesignateCentralReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: namespace, Name: cr.Name, } - r.Log.Info(fmt.Sprintf("Secret %s is used by Designate CR %s", secretName, cr.Name)) + Log.Info(fmt.Sprintf("Secret %s is used by Designate CR %s", secretName, cr.Name)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -224,7 +226,7 @@ func (r *DesignateCentralReconciler) SetupWithManager(mgr ctrl.Manager) error { client.InNamespace(o.GetNamespace()), } if err := r.Client.List(context.Background(), apis, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve Central CRs %v") + Log.Error(err, "Unable to retrieve Central CRs %v") return nil } @@ -240,7 +242,7 @@ func (r *DesignateCentralReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: o.GetNamespace(), Name: cr.Name, } - r.Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) + Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -265,12 +267,14 @@ func (r *DesignateCentralReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *DesignateCentralReconciler) reconcileDelete(ctx context.Context, instance *designatev1beta1.DesignateCentral, 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)) // We did all the cleanup on the objects we created so we can remove the // finalizer from ourselves to allow the deletion 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 } @@ -281,14 +285,18 @@ func (r *DesignateCentralReconciler) 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) - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name)) + + Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name)) return ctrl.Result{}, nil } func (r *DesignateCentralReconciler) reconcileNormal(ctx context.Context, instance *designatev1beta1.DesignateCentral, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Service") + Log := r.GetLogger(ctx) + + Log.Info("Reconciling Service") // ConfigMap configMapVars := make(map[string]env.Setter) @@ -327,7 +335,7 @@ func (r *DesignateCentralReconciler) reconcileNormal(ctx context.Context, instan // parentDesignateName := designate.GetOwningDesignateName(instance) - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' init: parent name: %s", instance.Name, parentDesignateName)) + Log.Info(fmt.Sprintf("Reconciling Service '%s' init: parent name: %s", instance.Name, parentDesignateName)) configMaps := []string{ fmt.Sprintf("%s-scripts", parentDesignateName), //ScriptsConfigMap @@ -526,27 +534,31 @@ func (r *DesignateCentralReconciler) reconcileNormal(ctx context.Context, instan } // create Deployment - end - r.Log.Info("Reconciled Service successfully") + Log.Info("Reconciled Service successfully") return ctrl.Result{}, nil } func (r *DesignateCentralReconciler) reconcileUpdate(ctx context.Context, instance *designatev1beta1.DesignateCentral, 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 *DesignateCentralReconciler) reconcileUpgrade(ctx context.Context, instance *designatev1beta1.DesignateCentral, 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 } @@ -635,6 +647,8 @@ func (r *DesignateCentralReconciler) createHashOfInputHashes( instance *designatev1beta1.DesignateCentral, 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) @@ -644,7 +658,7 @@ func (r *DesignateCentralReconciler) 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/controllers/designatemdns_controller.go b/controllers/designatemdns_controller.go index 1629dd52..1e5e44fc 100644 --- a/controllers/designatemdns_controller.go +++ b/controllers/designatemdns_controller.go @@ -30,6 +30,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" @@ -59,11 +60,6 @@ func (r *DesignateMdnsReconciler) GetKClient() kubernetes.Interface { return r.Kclient } -// GetLogger - -func (r *DesignateMdnsReconciler) GetLogger() logr.Logger { - return r.Log -} - // GetScheme - func (r *DesignateMdnsReconciler) GetScheme() *runtime.Scheme { return r.Scheme @@ -73,10 +69,14 @@ func (r *DesignateMdnsReconciler) GetScheme() *runtime.Scheme { type DesignateMdnsReconciler 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 *DesignateMdnsReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("DesignateMdns") +} + //+kubebuilder:rbac:groups=designate.openstack.org,resources=designatemdnses,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=designate.openstack.org,resources=designatemdnses/status,verbs=get;update;patch //+kubebuilder:rbac:groups=designate.openstack.org,resources=designatemdnses/finalizers,verbs=update @@ -97,8 +97,7 @@ type DesignateMdnsReconciler struct { // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile func (r *DesignateMdnsReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = r.Log.WithValues("designatemdns", req.NamespacedName) - + Log := r.GetLogger(ctx) // Fetch the DesignateMdns instance instance := &designatev1beta1.DesignateMdns{} err := r.Client.Get(ctx, req.NamespacedName, instance) @@ -118,7 +117,7 @@ func (r *DesignateMdnsReconciler) Reconcile(ctx context.Context, req ctrl.Reques r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -178,9 +177,11 @@ func (r *DesignateMdnsReconciler) Reconcile(ctx context.Context, req ctrl.Reques } // SetupWithManager sets up the controller with the Manager. -func (r *DesignateMdnsReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *DesignateMdnsReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { // Watch for changes to any CustomServiceConfigSecrets. Global secrets // (e.g. TransportURLSecret) are handled by the top designate controller. + Log := r.GetLogger(ctx) + svcSecretFn := func(o client.Object) []reconcile.Request { var namespace string = o.GetNamespace() var secretName string = o.GetName() @@ -192,7 +193,7 @@ func (r *DesignateMdnsReconciler) 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 Mdns CRs %v") + Log.Error(err, "Unable to retrieve Mdns CRs %v") return nil } for _, cr := range apis.Items { @@ -202,7 +203,7 @@ func (r *DesignateMdnsReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: namespace, Name: cr.Name, } - r.Log.Info(fmt.Sprintf("Secret %s is used by Designate CR %s", secretName, cr.Name)) + Log.Info(fmt.Sprintf("Secret %s is used by Designate CR %s", secretName, cr.Name)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -223,7 +224,7 @@ func (r *DesignateMdnsReconciler) SetupWithManager(mgr ctrl.Manager) error { client.InNamespace(o.GetNamespace()), } if err := r.Client.List(context.Background(), apis, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve Mdns CRs %v") + Log.Error(err, "Unable to retrieve Mdns CRs %v") return nil } @@ -239,7 +240,7 @@ func (r *DesignateMdnsReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: o.GetNamespace(), Name: cr.Name, } - r.Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) + Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -264,12 +265,14 @@ func (r *DesignateMdnsReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *DesignateMdnsReconciler) reconcileDelete(ctx context.Context, instance *designatev1beta1.DesignateMdns, 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)) // We did all the cleanup on the objects we created so we can remove the // finalizer from ourselves to allow the deletion 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 } @@ -280,14 +283,18 @@ func (r *DesignateMdnsReconciler) 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)) - 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 *DesignateMdnsReconciler) reconcileNormal(ctx context.Context, instance *designatev1beta1.DesignateMdns, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Service") + Log := r.GetLogger(ctx) + + Log.Info("Reconciling Service") // ConfigMap configMapVars := make(map[string]env.Setter) @@ -326,7 +333,7 @@ func (r *DesignateMdnsReconciler) reconcileNormal(ctx context.Context, instance // parentDesignateName := designate.GetOwningDesignateName(instance) - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' init: parent name: %s", instance.Name, parentDesignateName)) + Log.Info(fmt.Sprintf("Reconciling Service '%s' init: parent name: %s", instance.Name, parentDesignateName)) configMaps := []string{ fmt.Sprintf("%s-scripts", parentDesignateName), //ScriptsConfigMap @@ -525,27 +532,31 @@ func (r *DesignateMdnsReconciler) reconcileNormal(ctx context.Context, instance } // create Deployment - end - r.Log.Info("Reconciled Service successfully") + Log.Info("Reconciled Service successfully") return ctrl.Result{}, nil } func (r *DesignateMdnsReconciler) reconcileUpdate(ctx context.Context, instance *designatev1beta1.DesignateMdns, 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 *DesignateMdnsReconciler) reconcileUpgrade(ctx context.Context, instance *designatev1beta1.DesignateMdns, 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 } @@ -634,6 +645,8 @@ func (r *DesignateMdnsReconciler) createHashOfInputHashes( instance *designatev1beta1.DesignateMdns, 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) @@ -643,7 +656,7 @@ func (r *DesignateMdnsReconciler) 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/controllers/designateproducer_controller.go b/controllers/designateproducer_controller.go index 3749b0a2..0ee24eea 100644 --- a/controllers/designateproducer_controller.go +++ b/controllers/designateproducer_controller.go @@ -31,6 +31,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" @@ -60,11 +61,6 @@ func (r *DesignateProducerReconciler) GetKClient() kubernetes.Interface { return r.Kclient } -// GetLogger - -func (r *DesignateProducerReconciler) GetLogger() logr.Logger { - return r.Log -} - // GetScheme - func (r *DesignateProducerReconciler) GetScheme() *runtime.Scheme { return r.Scheme @@ -74,10 +70,14 @@ func (r *DesignateProducerReconciler) GetScheme() *runtime.Scheme { type DesignateProducerReconciler 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 *DesignateProducerReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("DesignateProducer") +} + //+kubebuilder:rbac:groups=designate.openstack.org,resources=designateproducers,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=designate.openstack.org,resources=designateproducers/status,verbs=get;update;patch //+kubebuilder:rbac:groups=designate.openstack.org,resources=designateproducers/finalizers,verbs=update @@ -98,7 +98,7 @@ type DesignateProducerReconciler struct { // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile func (r *DesignateProducerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = r.Log.WithValues("designateproducer", req.NamespacedName) + Log := r.GetLogger(ctx) // Fetch the DesignateProducer instance instance := &designatev1beta1.DesignateProducer{} @@ -119,7 +119,7 @@ func (r *DesignateProducerReconciler) Reconcile(ctx context.Context, req ctrl.Re r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -179,9 +179,11 @@ func (r *DesignateProducerReconciler) Reconcile(ctx context.Context, req ctrl.Re } // SetupWithManager sets up the controller with the Manager. -func (r *DesignateProducerReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *DesignateProducerReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { // Watch for changes to any CustomServiceConfigSecrets. Global secrets // (e.g. TransportURLSecret) are handled by the top designate controller. + Log := r.GetLogger(ctx) + svcSecretFn := func(o client.Object) []reconcile.Request { var namespace string = o.GetNamespace() var secretName string = o.GetName() @@ -193,7 +195,7 @@ func (r *DesignateProducerReconciler) 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 Producer CRs %v") + Log.Error(err, "Unable to retrieve Producer CRs %v") return nil } for _, cr := range apis.Items { @@ -203,7 +205,7 @@ func (r *DesignateProducerReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: namespace, Name: cr.Name, } - r.Log.Info(fmt.Sprintf("Secret %s is used by Designate CR %s", secretName, cr.Name)) + Log.Info(fmt.Sprintf("Secret %s is used by Designate CR %s", secretName, cr.Name)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -224,7 +226,7 @@ func (r *DesignateProducerReconciler) SetupWithManager(mgr ctrl.Manager) error { client.InNamespace(o.GetNamespace()), } if err := r.Client.List(context.Background(), apis, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve Producer CRs %v") + Log.Error(err, "Unable to retrieve Producer CRs %v") return nil } @@ -240,7 +242,7 @@ func (r *DesignateProducerReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: o.GetNamespace(), Name: cr.Name, } - r.Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) + Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -265,12 +267,14 @@ func (r *DesignateProducerReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *DesignateProducerReconciler) reconcileDelete(ctx context.Context, instance *designatev1beta1.DesignateProducer, 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)) // We did all the cleanup on the objects we created so we can remove the // finalizer from ourselves to allow the deletion 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 } @@ -281,14 +285,18 @@ func (r *DesignateProducerReconciler) 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) - r.Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name)) + Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name)) + + Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name)) return ctrl.Result{}, nil } func (r *DesignateProducerReconciler) reconcileNormal(ctx context.Context, instance *designatev1beta1.DesignateProducer, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Service") + Log := r.GetLogger(ctx) + + Log.Info("Reconciling Service") // ConfigMap configMapVars := make(map[string]env.Setter) @@ -327,7 +335,7 @@ func (r *DesignateProducerReconciler) reconcileNormal(ctx context.Context, insta // parentDesignateName := designate.GetOwningDesignateName(instance) - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' init: parent name: %s", instance.Name, parentDesignateName)) + Log.Info(fmt.Sprintf("Reconciling Service '%s' init: parent name: %s", instance.Name, parentDesignateName)) configMaps := []string{ fmt.Sprintf("%s-scripts", parentDesignateName), //ScriptsConfigMap @@ -526,27 +534,31 @@ func (r *DesignateProducerReconciler) reconcileNormal(ctx context.Context, insta } // create Deployment - end - r.Log.Info("Reconciled Service successfully") + Log.Info("Reconciled Service successfully") return ctrl.Result{}, nil } func (r *DesignateProducerReconciler) reconcileUpdate(ctx context.Context, instance *designatev1beta1.DesignateProducer, 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 *DesignateProducerReconciler) reconcileUpgrade(ctx context.Context, instance *designatev1beta1.DesignateProducer, 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 } @@ -635,6 +647,8 @@ func (r *DesignateProducerReconciler) createHashOfInputHashes( instance *designatev1beta1.DesignateProducer, 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) @@ -644,7 +658,7 @@ func (r *DesignateProducerReconciler) 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/controllers/designateworker_controller.go b/controllers/designateworker_controller.go index 7300c8f2..e2aede01 100644 --- a/controllers/designateworker_controller.go +++ b/controllers/designateworker_controller.go @@ -30,6 +30,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" @@ -59,11 +60,6 @@ func (r *DesignateWorkerReconciler) GetKClient() kubernetes.Interface { return r.Kclient } -// GetLogger - -func (r *DesignateWorkerReconciler) GetLogger() logr.Logger { - return r.Log -} - // GetScheme - func (r *DesignateWorkerReconciler) GetScheme() *runtime.Scheme { return r.Scheme @@ -73,10 +69,14 @@ func (r *DesignateWorkerReconciler) GetScheme() *runtime.Scheme { type DesignateWorkerReconciler 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 *DesignateWorkerReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("DesignateWorker") +} + //+kubebuilder:rbac:groups=designate.openstack.org,resources=designateworkers,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=designate.openstack.org,resources=designateworkers/status,verbs=get;update;patch //+kubebuilder:rbac:groups=designate.openstack.org,resources=designateworkers/finalizers,verbs=update @@ -97,7 +97,7 @@ type DesignateWorkerReconciler struct { // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile func (r *DesignateWorkerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = r.Log.WithValues("designateworker", req.NamespacedName) + Log := r.GetLogger(ctx) // Fetch the DesignateWorker instance instance := &designatev1beta1.DesignateWorker{} @@ -118,7 +118,7 @@ func (r *DesignateWorkerReconciler) Reconcile(ctx context.Context, req ctrl.Requ r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -178,9 +178,10 @@ func (r *DesignateWorkerReconciler) Reconcile(ctx context.Context, req ctrl.Requ } // SetupWithManager sets up the controller with the Manager. -func (r *DesignateWorkerReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *DesignateWorkerReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { // Watch for changes to any CustomServiceConfigSecrets. Global secrets // (e.g. TransportURLSecret) are handled by the top designate controller. + Log := r.GetLogger(ctx) svcSecretFn := func(o client.Object) []reconcile.Request { var namespace string = o.GetNamespace() var secretName string = o.GetName() @@ -192,7 +193,7 @@ func (r *DesignateWorkerReconciler) 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 Worker CRs %v") + Log.Error(err, "Unable to retrieve Worker CRs %v") return nil } for _, cr := range apis.Items { @@ -202,7 +203,7 @@ func (r *DesignateWorkerReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: namespace, Name: cr.Name, } - r.Log.Info(fmt.Sprintf("Secret %s is used by Designate CR %s", secretName, cr.Name)) + Log.Info(fmt.Sprintf("Secret %s is used by Designate CR %s", secretName, cr.Name)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -223,7 +224,7 @@ func (r *DesignateWorkerReconciler) SetupWithManager(mgr ctrl.Manager) error { client.InNamespace(o.GetNamespace()), } if err := r.Client.List(context.Background(), apis, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve Worker CRs %v") + Log.Error(err, "Unable to retrieve Worker CRs %v") return nil } @@ -239,7 +240,7 @@ func (r *DesignateWorkerReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: o.GetNamespace(), Name: cr.Name, } - r.Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) + Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -264,12 +265,13 @@ func (r *DesignateWorkerReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *DesignateWorkerReconciler) reconcileDelete(ctx context.Context, instance *designatev1beta1.DesignateWorker, 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)) // We did all the cleanup on the objects we created so we can remove the // finalizer from ourselves to allow the deletion 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 } @@ -280,14 +282,16 @@ func (r *DesignateWorkerReconciler) 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)) - 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 *DesignateWorkerReconciler) reconcileNormal(ctx context.Context, instance *designatev1beta1.DesignateWorker, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Service") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service") // ConfigMap configMapVars := make(map[string]env.Setter) @@ -326,7 +330,7 @@ func (r *DesignateWorkerReconciler) reconcileNormal(ctx context.Context, instanc // parentDesignateName := designate.GetOwningDesignateName(instance) - r.Log.Info(fmt.Sprintf("Reconciling Service '%s' init: parent name: %s", instance.Name, parentDesignateName)) + Log.Info(fmt.Sprintf("Reconciling Service '%s' init: parent name: %s", instance.Name, parentDesignateName)) configMaps := []string{ fmt.Sprintf("%s-scripts", parentDesignateName), //ScriptsConfigMap @@ -525,27 +529,29 @@ func (r *DesignateWorkerReconciler) reconcileNormal(ctx context.Context, instanc } // create Deployment - end - r.Log.Info("Reconciled Service successfully") + Log.Info("Reconciled Service successfully") return ctrl.Result{}, nil } func (r *DesignateWorkerReconciler) reconcileUpdate(ctx context.Context, instance *designatev1beta1.DesignateWorker, 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 *DesignateWorkerReconciler) reconcileUpgrade(ctx context.Context, instance *designatev1beta1.DesignateWorker, 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 } @@ -634,6 +640,7 @@ func (r *DesignateWorkerReconciler) createHashOfInputHashes( instance *designatev1beta1.DesignateWorker, 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) @@ -643,7 +650,7 @@ func (r *DesignateWorkerReconciler) 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 9efb81bb..c6efb69d 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "context" "crypto/tls" "flag" "os" @@ -127,8 +128,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("Designate"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(context.Background(), mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Designate") os.Exit(1) } @@ -138,8 +138,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("DesignateAPI"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(context.Background(), mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "DesignateAPI") os.Exit(1) } @@ -149,8 +148,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("DesignateCentral"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(context.Background(), mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "DesignateCentral") os.Exit(1) } @@ -160,8 +158,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("DesignateProducer"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(context.Background(), mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "DesignateProducer") os.Exit(1) } @@ -171,8 +168,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("DesignateWork"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(context.Background(), mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "DesignateWork") os.Exit(1) } @@ -182,8 +178,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("DesignateMdns"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(context.Background(), mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "DesignateMdns") os.Exit(1) }