diff --git a/controllers/keystoneapi_controller.go b/controllers/keystoneapi_controller.go index cde808257..60bb7a1e9 100644 --- a/controllers/keystoneapi_controller.go +++ b/controllers/keystoneapi_controller.go @@ -78,9 +78,9 @@ func (r *KeystoneAPIReconciler) GetScheme() *runtime.Scheme { return r.Scheme } -// getlog returns a logger object with a prefix of "conroller.name" and aditional controller context fields -func GetLog(ctx context.Context, controller string) logr.Logger { - return log.FromContext(ctx).WithName("Controllers").WithName(controller) +// getLog returns a logger object with a logging prefix of "conrollers.name" and aditional controller context fields +func (r *KeystoneAPIReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("KeystoneAPI") } // KeystoneAPIReconciler reconciles a KeystoneAPI object @@ -115,6 +115,7 @@ type KeystoneAPIReconciler struct { // Reconcile reconcile keystone API requests func (r *KeystoneAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { + Log := r.GetLogger(ctx) // Fetch the KeystoneAPI instance instance := &keystonev1.KeystoneAPI{} err := r.Client.Get(ctx, req.NamespacedName, instance) @@ -134,7 +135,7 @@ func (r *KeystoneAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) r.Client, r.Kclient, r.Scheme, - GetLog(ctx, "KeystoneAPI"), + Log, ) if err != nil { return ctrl.Result{}, err @@ -142,7 +143,7 @@ func (r *KeystoneAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) // Always patch the instance status when exiting this function so we can persist any changes. defer func() { - // update the Ready condition based on the sub conditions + // updatehe Ready condition based on the sub conditions if instance.Status.Conditions.AllSubConditionIsTrue() { instance.Status.Conditions.MarkTrue( condition.ReadyCondition, condition.ReadyMessage) @@ -214,8 +215,8 @@ func (r *KeystoneAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) } // SetupWithManager - -func (r *KeystoneAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { - logger := mgr.GetLogger() +func (r *KeystoneAPIReconciler) SetupWithManager(mgr ctrl.Manager, ctx context.Context) error { + Log := r.GetLogger(ctx) memcachedFn := func(o client.Object) []reconcile.Request { result := []reconcile.Request{} @@ -225,8 +226,8 @@ func (r *KeystoneAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { listOpts := []client.ListOption{ client.InNamespace(o.GetNamespace()), } - if err := r.Client.List(context.Background(), keystoneAPIs, listOpts...); err != nil { - logger.Error(err, "Unable to retrieve KeystoneAPI CRs %w") + if err := r.Client.List(ctx, keystoneAPIs, listOpts...); err != nil { + Log.Error(err, "Unable to retrieve KeystoneAPI CRs %w") return nil } @@ -236,7 +237,7 @@ func (r *KeystoneAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: o.GetNamespace(), Name: cr.Name, } - logger.Info(fmt.Sprintf("Memcached %s is used by KeystoneAPI CR %s", o.GetName(), cr.Name)) + Log.Info(fmt.Sprintf("Memcached %s is used by KeystoneAPI CR %s", o.GetName(), cr.Name)) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -265,8 +266,8 @@ func (r *KeystoneAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *KeystoneAPIReconciler) reconcileDelete(ctx context.Context, instance *keystonev1.KeystoneAPI, helper *helper.Helper) (ctrl.Result, error) { - log := GetLog(ctx, "KeystoneAPI") - log.Info("Reconciling Service delete") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service delete") // We need to allow all KeystoneEndpoint and KeystoneService processing to finish // in the case of a delete before we remove the finalizers. For instance, in the @@ -314,7 +315,7 @@ func (r *KeystoneAPIReconciler) reconcileDelete(ctx context.Context, instance *k // Service is deleted so remove the finalizer. controllerutil.RemoveFinalizer(instance, helper.GetFinalizer()) - log.Info("Reconciled Service delete successfully") + Log.Info("Reconciled Service delete successfully") return ctrl.Result{}, nil } @@ -326,8 +327,8 @@ func (r *KeystoneAPIReconciler) reconcileInit( serviceLabels map[string]string, serviceAnnotations map[string]string, ) (ctrl.Result, error) { - log := GetLog(ctx, "KeystoneAPI") - log.Info("Reconciling Service init") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service init") // // Service account, role, binding // @@ -445,7 +446,7 @@ func (r *KeystoneAPIReconciler) reconcileInit( } if dbSyncjob.HasChanged() { instance.Status.Hash[keystonev1.DbSyncHash] = dbSyncjob.GetHash() - log.Info(fmt.Sprintf("Job %s hash added - %s", jobDef.Name, instance.Status.Hash[keystonev1.DbSyncHash])) + Log.Info(fmt.Sprintf("Job %s hash added - %s", jobDef.Name, instance.Status.Hash[keystonev1.DbSyncHash])) } instance.Status.Conditions.MarkTrue(condition.DBSyncReadyCondition, condition.DBSyncReadyMessage) @@ -543,41 +544,41 @@ func (r *KeystoneAPIReconciler) reconcileInit( } if bootstrapjob.HasChanged() { instance.Status.Hash[keystonev1.BootstrapHash] = bootstrapjob.GetHash() - log.Info(fmt.Sprintf("Job %s hash added - %s", jobDef.Name, instance.Status.Hash[keystonev1.BootstrapHash])) + Log.Info(fmt.Sprintf("Job %s hash added - %s", jobDef.Name, instance.Status.Hash[keystonev1.BootstrapHash])) } instance.Status.Conditions.MarkTrue(condition.BootstrapReadyCondition, condition.BootstrapReadyMessage) // run keystone bootstrap - end - log.Info("Reconciled Service init successfully") + Log.Info("Reconciled Service init successfully") return ctrl.Result{}, nil } func (r *KeystoneAPIReconciler) reconcileUpdate(ctx context.Context, instance *keystonev1.KeystoneAPI, helper *helper.Helper) (ctrl.Result, error) { - log := GetLog(ctx, "KeystoneAPI") - log.Info("Reconciling Service update") + 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? - log.Info("Reconciled Service update successfully") + Log.Info("Reconciled Service update successfully") return ctrl.Result{}, nil } func (r *KeystoneAPIReconciler) reconcileUpgrade(ctx context.Context, instance *keystonev1.KeystoneAPI, helper *helper.Helper) (ctrl.Result, error) { - log := GetLog(ctx, "KeystoneAPI") - log.Info("Reconciling Service upgrade") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service upgrade") // TODO: should have major version upgrade tasks // -delete dbsync hash from status to rerun it? - log.Info("Reconciled Service upgrade successfully") + Log.Info("Reconciled Service upgrade successfully") return ctrl.Result{}, nil } func (r *KeystoneAPIReconciler) reconcileNormal(ctx context.Context, instance *keystonev1.KeystoneAPI, helper *helper.Helper) (ctrl.Result, error) { - log := GetLog(ctx, "KeystoneAPI") - log.Info("Reconciling Service") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service") // ConfigMap configMapVars := make(map[string]env.Setter) @@ -864,7 +865,7 @@ func (r *KeystoneAPIReconciler) reconcileNormal(ctx context.Context, instance *k return ctrl.Result{}, err } - log.Info("Reconciled Service successfully") + Log.Info("Reconciled Service successfully") return ctrl.Result{}, nil } @@ -1067,7 +1068,7 @@ func (r *KeystoneAPIReconciler) createHashOfInputHashes( instance *keystonev1.KeystoneAPI, envVars map[string]env.Setter, ) (string, bool, error) { - log := GetLog(ctx, "KeystoneAPI") + Log := r.GetLogger(ctx) var hashMap map[string]string changed := false mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars) @@ -1077,7 +1078,7 @@ func (r *KeystoneAPIReconciler) createHashOfInputHashes( } if hashMap, changed = util.SetHash(instance.Status.Hash, common.InputHashName, hash); changed { instance.Status.Hash = hashMap - 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/keystoneendpoint_controller.go b/controllers/keystoneendpoint_controller.go index 43b3dcfe3..1c5d5d5ce 100644 --- a/controllers/keystoneendpoint_controller.go +++ b/controllers/keystoneendpoint_controller.go @@ -26,7 +26,9 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/log" + "github.com/go-logr/logr" keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1" condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition" helper "github.com/openstack-k8s-operators/lib-common/modules/common/helper" @@ -42,6 +44,11 @@ type KeystoneEndpointReconciler struct { Scheme *runtime.Scheme } +// getLog returns a logger object with a logging prefix of "conrollers.name" and aditional controller context fields +func (r *KeystoneEndpointReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("KeystoneAPI") +} + //+kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneendpoints,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneendpoints/status,verbs=get;update;patch //+kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneendpoints/finalizers,verbs=update @@ -52,7 +59,7 @@ type KeystoneEndpointReconciler struct { // Reconcile keystone endpoint requests func (r *KeystoneEndpointReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - log := GetLog(ctx, "KeystoneEndpoint") + Log := r.GetLogger(ctx) // Fetch the KeystoneEndpoint instance instance := &keystonev1.KeystoneEndpoint{} @@ -74,7 +81,7 @@ func (r *KeystoneEndpointReconciler) Reconcile(ctx context.Context, req ctrl.Req r.Kclient, r.Scheme, //TODO remove later, log used here as to not break the helper struct signiture. - log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -144,7 +151,7 @@ func (r *KeystoneEndpointReconciler) Reconcile(ctx context.Context, req ctrl.Req condition.SeverityWarning, keystonev1.KeystoneAPIReadyNotFoundMessage, )) - log.Info("KeystoneAPI not found!") + Log.Info("KeystoneAPI not found!") return ctrl.Result{RequeueAfter: time.Second * 5}, nil } @@ -185,7 +192,7 @@ func (r *KeystoneEndpointReconciler) Reconcile(ctx context.Context, req ctrl.Req condition.RequestedReason, condition.SeverityInfo, keystonev1.KeystoneAPIReadyWaitingMessage)) - log.Info("KeystoneAPI not yet ready!") + Log.Info("KeystoneAPI not yet ready!") return ctrl.Result{RequeueAfter: time.Second * 5}, nil } @@ -228,7 +235,7 @@ func (r *KeystoneEndpointReconciler) Reconcile(ctx context.Context, req ctrl.Req } // SetupWithManager sets up the controller with the Manager. -func (r *KeystoneEndpointReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *KeystoneEndpointReconciler) SetupWithManager(mgr ctrl.Manager, ctx context.Context) error { return ctrl.NewControllerManagedBy(mgr). For(&keystonev1.KeystoneEndpoint{}). Complete(r) @@ -241,9 +248,9 @@ func (r *KeystoneEndpointReconciler) reconcileDelete( os *openstack.OpenStack, keystoneAPI *keystonev1.KeystoneAPI, ) (ctrl.Result, error) { - log := GetLog(ctx, "KeystoneEndpoint") + Log := r.GetLogger(ctx) - log.Info("Reconciling Endpoint delete") + Log.Info("Reconciling Endpoint delete") // We might not have an OpenStack backend to use in certain situations if os != nil { @@ -257,7 +264,7 @@ func (r *KeystoneEndpointReconciler) reconcileDelete( } err = os.DeleteEndpoint( - log, + Log, openstack.Endpoint{ Name: instance.Spec.ServiceName, ServiceID: instance.Status.ServiceID, @@ -298,7 +305,7 @@ func (r *KeystoneEndpointReconciler) reconcileDelete( // Endpoints are deleted so remove the finalizer. controllerutil.RemoveFinalizer(instance, helper.GetFinalizer()) - log.Info("Reconciled Endpoint delete successfully") + Log.Info("Reconciled Endpoint delete successfully") return ctrl.Result{}, nil } @@ -309,8 +316,8 @@ func (r *KeystoneEndpointReconciler) reconcileNormal( helper *helper.Helper, os *openstack.OpenStack, ) (ctrl.Result, error) { - log := GetLog(ctx, "KeystoneEndpoint") - log.Info("Reconciling Endpoint normal") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Endpoint normal") // // Wait for KeystoneService is Ready and get the ServiceID from the object @@ -318,7 +325,7 @@ func (r *KeystoneEndpointReconciler) reconcileNormal( ksSvc, err := keystonev1.GetKeystoneServiceWithName(ctx, helper, instance.Spec.ServiceName, instance.Namespace) if err != nil { if k8s_errors.IsNotFound(err) { - log.Info("KeystoneService not found", "KeystoneService", instance.Spec.ServiceName) + Log.Info("KeystoneService not found", "KeystoneService", instance.Spec.ServiceName) return ctrl.Result{RequeueAfter: time.Second * 5}, nil } @@ -332,7 +339,7 @@ func (r *KeystoneEndpointReconciler) reconcileNormal( } if !ksSvc.IsReady() { - log.Info("KeystoneService not ready, waiting to create endpoints", "KeystoneService", instance.Spec.ServiceName) + Log.Info("KeystoneService not ready, waiting to create endpoints", "KeystoneService", instance.Spec.ServiceName) return ctrl.Result{RequeueAfter: 10 * time.Second}, nil } @@ -374,7 +381,7 @@ func (r *KeystoneEndpointReconciler) reconcileNormal( instance.Spec.Endpoints, ) - log.Info("Reconciled Endpoint normal successfully") + Log.Info("Reconciled Endpoint normal successfully") return ctrl.Result{}, nil } @@ -385,8 +392,8 @@ func (r *KeystoneEndpointReconciler) reconcileEndpoints( helper *helper.Helper, os *openstack.OpenStack, ) error { - log := GetLog(ctx, "KeystoneEndpoint") - log.Info("Reconciling Endpoints") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Endpoints") // delete endpoint if it does no longer exist in Spec.Endpoints // but has a reference in Status.EndpointIDs @@ -400,7 +407,7 @@ func (r *KeystoneEndpointReconciler) reconcileEndpoints( } err = os.DeleteEndpoint( - log, + Log, openstack.Endpoint{ Name: instance.Spec.ServiceName, ServiceID: instance.Status.ServiceID, @@ -428,7 +435,7 @@ func (r *KeystoneEndpointReconciler) reconcileEndpoints( // get registered endpoints for the service and endpointType allEndpoints, err := os.GetEndpoints( - log, + Log, instance.Status.ServiceID, endpointType) if err != nil { @@ -439,7 +446,7 @@ func (r *KeystoneEndpointReconciler) reconcileEndpoints( if len(allEndpoints) == 0 { // Create the endpoint endpointID, err = os.CreateEndpoint( - log, + Log, openstack.Endpoint{ Name: instance.Spec.ServiceName, ServiceID: instance.Status.ServiceID, @@ -455,7 +462,7 @@ func (r *KeystoneEndpointReconciler) reconcileEndpoints( endpoint := allEndpoints[0] if endpointURL != endpoint.URL { endpointID, err = os.UpdateEndpoint( - log, + Log, openstack.Endpoint{ Name: endpoint.Name, ServiceID: endpoint.ServiceID, @@ -485,7 +492,7 @@ func (r *KeystoneEndpointReconciler) reconcileEndpoints( } } - log.Info("Reconciled Endpoints successfully") + Log.Info("Reconciled Endpoints successfully") return nil } diff --git a/controllers/keystoneservice_controller.go b/controllers/keystoneservice_controller.go index 789cb986e..38f4faed8 100644 --- a/controllers/keystoneservice_controller.go +++ b/controllers/keystoneservice_controller.go @@ -22,6 +22,7 @@ import ( "strings" "time" + "github.com/go-logr/logr" keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1" condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition" helper "github.com/openstack-k8s-operators/lib-common/modules/common/helper" @@ -34,6 +35,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/reconcile" ) @@ -59,6 +61,11 @@ type KeystoneServiceReconciler struct { Scheme *runtime.Scheme } +// GetLogger returns a logger object with a logging prefix of "conrollers.name" and aditional controller context fields +func (r *KeystoneServiceReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("KeystoneAPI") +} + // +kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneservices,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneservices/status,verbs=get;update;patch // +kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneservices/finalizers,verbs=update @@ -68,7 +75,7 @@ type KeystoneServiceReconciler struct { // Reconcile keystone service requests func (r *KeystoneServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - log := GetLog(ctx, "KeystoneService") + log := r.GetLogger(ctx) // Fetch the KeystoneService instance instance := &keystonev1.KeystoneService{} @@ -241,7 +248,7 @@ func (r *KeystoneServiceReconciler) Reconcile(ctx context.Context, req ctrl.Requ } // SetupWithManager x -func (r *KeystoneServiceReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *KeystoneServiceReconciler) SetupWithManager(mgr ctrl.Manager, ctx context.Context) error { return ctrl.NewControllerManagedBy(mgr). For(&keystonev1.KeystoneService{}). Complete(r) @@ -254,7 +261,7 @@ func (r *KeystoneServiceReconciler) reconcileDelete( os *openstack.OpenStack, keystoneAPI *keystonev1.KeystoneAPI, ) (ctrl.Result, error) { - log := GetLog(ctx, "KeystoneService") + log := r.GetLogger(ctx) log.Info("Reconciling Service delete") // only cleanup the service if there is the ServiceID reference in the @@ -307,7 +314,7 @@ func (r *KeystoneServiceReconciler) reconcileNormal( helper *helper.Helper, os *openstack.OpenStack, ) (ctrl.Result, error) { - log := GetLog(ctx, "KeystoneService") + log := r.GetLogger(ctx) log.Info("Reconciling Service") // @@ -369,7 +376,7 @@ func (r *KeystoneServiceReconciler) reconcileService( instance *keystonev1.KeystoneService, os *openstack.OpenStack, ) error { - log := GetLog(ctx, "KeystoneService") + log := r.GetLogger(ctx) log.Info("Reconciling Service ", "KeystoneService", instance.Spec.ServiceName) // verify if there is already a service in keystone for the type and name @@ -431,7 +438,7 @@ func (r *KeystoneServiceReconciler) reconcileUser( instance *keystonev1.KeystoneService, os *openstack.OpenStack, ) (reconcile.Result, error) { - log := GetLog(ctx, "KeystoneService") + log := r.GetLogger(ctx) log.Info("Reconciling User", "User", instance.Spec.ServiceUser) roleNames := []string{"admin", "service"} diff --git a/main.go b/main.go index 655fe2426..e76c0af79 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "context" "flag" "os" "strings" @@ -104,7 +105,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(mgr, context.Background()); err != nil { setupLog.Error(err, "unable to create controller", "controller", "KeystoneAPI") os.Exit(1) } @@ -113,7 +114,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(mgr, context.Background()); err != nil { setupLog.Error(err, "unable to create controller", "controller", "KeystoneService") os.Exit(1) } @@ -122,7 +123,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(mgr, context.Background()); err != nil { setupLog.Error(err, "unable to create controller", "controller", "KeystoneEndpoint") os.Exit(1) }