Skip to content

Commit

Permalink
update structured logging
Browse files Browse the repository at this point in the history
Structured logging
  • Loading branch information
pinikomarov authored and openshift-merge-bot[bot] committed Nov 15, 2023
1 parent c15a245 commit c45baee
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
47 changes: 27 additions & 20 deletions controllers/placementapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ import (
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
)

// GetLog returns a logger object with a prefix of "controller.name" and additional controller context fields
func (r *PlacementAPIReconciler) GetLogger(ctx context.Context) logr.Logger {
return log.FromContext(ctx).WithName("Controllers").WithName("PlacementAPI")
}

// PlacementAPIReconciler reconciles a PlacementAPI object
type PlacementAPIReconciler struct {
client.Client
Kclient kubernetes.Interface
Log logr.Logger
Scheme *runtime.Scheme
}

Expand Down Expand Up @@ -91,7 +95,7 @@ type PlacementAPIReconciler struct {

// Reconcile reconcile placement API requests
func (r *PlacementAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
_ = log.FromContext(ctx)
Log := r.GetLogger(ctx)

// Fetch the PlacementAPI instance
instance := &placementv1.PlacementAPI{}
Expand All @@ -112,7 +116,7 @@ func (r *PlacementAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request
r.Client,
r.Kclient,
r.Scheme,
r.Log,
Log,
)
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -207,7 +211,8 @@ func (r *PlacementAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

func (r *PlacementAPIReconciler) reconcileDelete(ctx context.Context, instance *placementv1.PlacementAPI, helper *helper.Helper) (ctrl.Result, error) {
util.LogForObject(helper, "Reconciling Service delete", instance)
Log := r.GetLogger(ctx)
Log.Info("Reconciling Service delete")

// remove db finalizer before the placement one
db, err := mariadbv1.GetDatabaseByName(ctx, helper, instance.Name)
Expand All @@ -233,7 +238,7 @@ func (r *PlacementAPIReconciler) reconcileDelete(ctx context.Context, instance *
if err != nil && !k8s_errors.IsNotFound(err) {
return ctrl.Result{}, err
}
util.LogForObject(helper, "Removed finalizer from our KeystoneEndpoint", instance)
Log.Info("Removed finalizer from our KeystoneEndpoint")
}
}

Expand All @@ -249,14 +254,14 @@ func (r *PlacementAPIReconciler) reconcileDelete(ctx context.Context, instance *
if err != nil && !k8s_errors.IsNotFound(err) {
return ctrl.Result{}, err
}
util.LogForObject(helper, "Removed finalizer from our KeystoneService", instance)
Log.Info("Removed finalizer from our KeystoneService")
}
}

// 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())
util.LogForObject(helper, "Reconciled Service delete successfully", instance)
Log.Info("Reconciled Service delete successfully")
return ctrl.Result{}, nil
}

Expand All @@ -267,8 +272,8 @@ func (r *PlacementAPIReconciler) reconcileInit(
serviceLabels map[string]string,
serviceAnnotations map[string]string,
) (ctrl.Result, error) {
r.Log.Info("Reconciling Service init")

Log := r.GetLogger(ctx)
Log.Info("Reconciling Service init")
// Service account, role, binding
rbacRules := []rbacv1.PolicyRule{
{
Expand Down Expand Up @@ -546,39 +551,40 @@ func (r *PlacementAPIReconciler) reconcileInit(
}
if dbSyncjob.HasChanged() {
instance.Status.Hash[placementv1.DbSyncHash] = dbSyncjob.GetHash()
r.Log.Info(fmt.Sprintf("Job %s hash added - %s", jobDef.Name, instance.Status.Hash[placementv1.DbSyncHash]))
Log.Info(fmt.Sprintf("Job %s hash added - %s", jobDef.Name, instance.Status.Hash[placementv1.DbSyncHash]))
}
instance.Status.Conditions.MarkTrue(condition.DBSyncReadyCondition, condition.DBSyncReadyMessage)

// run placement db sync - end

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

func (r *PlacementAPIReconciler) reconcileUpdate(ctx context.Context, instance *placementv1.PlacementAPI, helper *helper.Helper) (ctrl.Result, error) {
r.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?

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

func (r *PlacementAPIReconciler) reconcileUpgrade(ctx context.Context, instance *placementv1.PlacementAPI, helper *helper.Helper) (ctrl.Result, error) {
r.Log.Info("Reconciling Service upgrade")

Log := r.GetLogger(ctx)
Log.Info("Reconciling Service update")
// TODO: should have major version upgrade tasks
// -delete dbsync hash from status to rerun it?

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

func (r *PlacementAPIReconciler) reconcileNormal(ctx context.Context, instance *placementv1.PlacementAPI, 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)

Expand Down Expand Up @@ -763,7 +769,7 @@ func (r *PlacementAPIReconciler) reconcileNormal(ctx context.Context, instance *
}
// create Deployment - end

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

Expand Down Expand Up @@ -843,6 +849,7 @@ func (r *PlacementAPIReconciler) createHashOfInputHashes(
instance *placementv1.PlacementAPI,
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 @@ -852,7 +859,7 @@ func (r *PlacementAPIReconciler) 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
}
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Kclient: kclient,
Log: ctrl.Log.WithName("controllers").WithName("PlacementAPI"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "PlacementAPI")
os.Exit(1)
Expand Down
1 change: 0 additions & 1 deletion tests/functional/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ var _ = BeforeSuite(func() {
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
Kclient: kclient,
Log: ctrl.Log.WithName("controllers").WithName("PlacementAPI"),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

Expand Down

0 comments on commit c45baee

Please sign in to comment.