Skip to content

Commit

Permalink
update logging naming
Browse files Browse the repository at this point in the history
  • Loading branch information
pinikomarov committed Oct 5, 2023
1 parent 87a8215 commit 22b6221
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 59 deletions.
59 changes: 30 additions & 29 deletions controllers/keystoneapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -134,15 +135,15 @@ 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
}

// 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)
Expand Down Expand Up @@ -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{}
Expand All @@ -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
}

Expand All @@ -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})
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
//
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
49 changes: 28 additions & 21 deletions controllers/keystoneendpoint_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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{}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -257,7 +264,7 @@ func (r *KeystoneEndpointReconciler) reconcileDelete(
}

err = os.DeleteEndpoint(
log,
Log,
openstack.Endpoint{
Name: instance.Spec.ServiceName,
ServiceID: instance.Status.ServiceID,
Expand Down Expand Up @@ -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
}
Expand All @@ -309,16 +316,16 @@ 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
//
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
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -400,7 +407,7 @@ func (r *KeystoneEndpointReconciler) reconcileEndpoints(
}

err = os.DeleteEndpoint(
log,
Log,
openstack.Endpoint{
Name: instance.Spec.ServiceName,
ServiceID: instance.Status.ServiceID,
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -485,7 +492,7 @@ func (r *KeystoneEndpointReconciler) reconcileEndpoints(
}
}

log.Info("Reconciled Endpoints successfully")
Log.Info("Reconciled Endpoints successfully")

return nil
}
Loading

0 comments on commit 22b6221

Please sign in to comment.