Skip to content

Commit

Permalink
update structured logging
Browse files Browse the repository at this point in the history
This automatically adds additional fields like reconcile_id etc.. from the controller context.

before :

2023-05-18T01:53:14+03:00 INFO  controllers.KeystoneAPI Reconciled Service init successfully

after:

2023-10-15T02:22:33+03:00       INFO    Controllers.KeystoneAPI Reconciling Service     {"controller": "k eystoneapi", "controllerGroup": "keystone.openstack.org", "controllerKind": "KeystoneAPI", "KeystoneAPI": {"name":"keystone","namespace":"openstack"}, "namespace": "openstack", "name": "keystone", "reconcileID" : "30b247eb-4538-47d1-89a6-b877816a1103"}

*by using per reconcile function respective logger objects, the intention is to lay the ground for parallel reconciliation in future and avoid race conditions as ctx objects are reconcile run specific.

update structured logging

fix log obj.

update logging naming

update logging naming

update logging in tests

update logging

update logging

Structured logging

Structured logging
  • Loading branch information
pinikomarov committed Feb 14, 2024
1 parent e40975f commit 80e1b34
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 91 deletions.
65 changes: 33 additions & 32 deletions controllers/keystoneapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ 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) logr.Logger {
// GetLog returns a logger object with a logging prefix of "controller.name" and additional controller context fields
func (r *KeystoneAPIReconciler) GetLogger(ctx context.Context) logr.Logger {
return log.FromContext(ctx).WithName("Controllers").WithName("KeystoneAPI")
}

Expand Down Expand Up @@ -124,6 +124,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 @@ -143,7 +144,7 @@ func (r *KeystoneAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request)
r.Client,
r.Kclient,
r.Scheme,
GetLog(ctx),
Log,
)
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -242,8 +243,8 @@ var (
)

// SetupWithManager -
func (r *KeystoneAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
logger := mgr.GetLogger()
func (r *KeystoneAPIReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
Log := r.GetLogger(ctx)

// index passwordSecretField
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &keystonev1.KeystoneAPI{}, passwordSecretField, func(rawObj client.Object) []string {
Expand Down Expand Up @@ -301,8 +302,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 @@ -312,7 +313,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 @@ -348,7 +349,7 @@ func (r *KeystoneAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *KeystoneAPIReconciler) findObjectsForSrc(src client.Object) []reconcile.Request {
requests := []reconcile.Request{}

l := log.FromContext(context.Background()).WithName("Controllers").WithName("KeystoneAPI")
Log := r.GetLogger(context.Background())

for _, field := range allWatchFields {
crList := &keystonev1.KeystoneAPIList{}
Expand All @@ -362,7 +363,7 @@ func (r *KeystoneAPIReconciler) findObjectsForSrc(src client.Object) []reconcile
}

for _, item := range crList.Items {
l.Info(fmt.Sprintf("input source %s changed, reconcile: %s - %s", src.GetName(), item.GetName(), item.GetNamespace()))
Log.Info(fmt.Sprintf("input source %s changed, reconcile: %s - %s", src.GetName(), item.GetName(), item.GetNamespace()))

requests = append(requests,
reconcile.Request{
Expand All @@ -379,8 +380,8 @@ func (r *KeystoneAPIReconciler) findObjectsForSrc(src client.Object) []reconcile
}

func (r *KeystoneAPIReconciler) reconcileDelete(ctx context.Context, instance *keystonev1.KeystoneAPI, helper *helper.Helper) (ctrl.Result, error) {
l := GetLog(ctx)
l.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 @@ -428,7 +429,7 @@ func (r *KeystoneAPIReconciler) reconcileDelete(ctx context.Context, instance *k

// Service is deleted so remove the finalizer.
controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())
l.Info("Reconciled Service delete successfully")
Log.Info("Reconciled Service delete successfully")

return ctrl.Result{}, nil
}
Expand All @@ -440,9 +441,8 @@ func (r *KeystoneAPIReconciler) reconcileInit(
serviceLabels map[string]string,
serviceAnnotations map[string]string,
) (ctrl.Result, error) {
l := GetLog(ctx)
l.Info("Reconciling Service init")

Log := r.GetLogger(ctx)
Log.Info("Reconciling Service init")
//
// Service account, role, binding
//
Expand Down Expand Up @@ -560,7 +560,7 @@ func (r *KeystoneAPIReconciler) reconcileInit(
}
if dbSyncjob.HasChanged() {
instance.Status.Hash[keystonev1.DbSyncHash] = dbSyncjob.GetHash()
l.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 @@ -718,35 +718,35 @@ func (r *KeystoneAPIReconciler) reconcileInit(
}
if bootstrapjob.HasChanged() {
instance.Status.Hash[keystonev1.BootstrapHash] = bootstrapjob.GetHash()
l.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

l.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) {
l := GetLog(ctx)
l.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?

l.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) {
l := GetLog(ctx)
l.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?

l.Info("Reconciled Service upgrade successfully")
Log.Info("Reconciled Service upgrade successfully")
return ctrl.Result{}, nil
}

Expand All @@ -755,8 +755,8 @@ func (r *KeystoneAPIReconciler) reconcileNormal(
instance *keystonev1.KeystoneAPI,
helper *helper.Helper,
) (ctrl.Result, error) {
l := GetLog(ctx)
l.Info("Reconciling Service")
Log := r.GetLogger(ctx)
Log.Info("Reconciling Service")

serviceLabels := map[string]string{
common.AppSelector: keystone.ServiceName,
Expand Down Expand Up @@ -808,21 +808,21 @@ func (r *KeystoneAPIReconciler) reconcileNormal(
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
l.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 == "" {
l.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(
condition.RabbitMqTransportURLReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.RabbitMqTransportURLReadyRunningMessage))
return ctrl.Result{RequeueAfter: time.Duration(10) * time.Second}, nil
}
l.Info(fmt.Sprintf("TransportURL secret name %s", transportURL.Status.SecretName))
Log.Info(fmt.Sprintf("TransportURL secret name %s", transportURL.Status.SecretName))
instance.Status.Conditions.MarkTrue(condition.RabbitMqTransportURLReadyCondition, condition.RabbitMqTransportURLReadyMessage)
// run check rabbitmq - end

Expand Down Expand Up @@ -1131,7 +1131,7 @@ func (r *KeystoneAPIReconciler) reconcileNormal(
return ctrl.Result{}, err
}

l.Info("Reconciled Service successfully")
Log.Info("Reconciled Service successfully")
return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -1386,6 +1386,7 @@ func (r *KeystoneAPIReconciler) createHashOfInputHashes(
instance *keystonev1.KeystoneAPI,
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)
Expand All @@ -1395,7 +1396,7 @@ func (r *KeystoneAPIReconciler) createHashOfInputHashes(
}
if hashMap, changed = util.SetHash(instance.Status.Hash, common.InputHashName, hash); changed {
instance.Status.Hash = hashMap
GetLog(ctx).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
Loading

0 comments on commit 80e1b34

Please sign in to comment.