Skip to content

Commit

Permalink
switch to structured logging
Browse files Browse the repository at this point in the history
update logging

update logging

update logging

update logging

update logging

update logging fixup lint
  • Loading branch information
pinikomarov committed Oct 28, 2023
1 parent a96a326 commit 08cacc3
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 173 deletions.
85 changes: 49 additions & 36 deletions controllers/designate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand All @@ -81,7 +81,6 @@ func (r *DesignateReconciler) GetScheme() *runtime.Scheme {
type DesignateReconciler struct {
client.Client
Kclient kubernetes.Interface
Log logr.Logger
Scheme *runtime.Scheme
}

Expand Down Expand Up @@ -121,7 +120,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{}
Expand All @@ -142,7 +141,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
Expand Down Expand Up @@ -212,7 +211,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
Expand All @@ -224,6 +223,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{}

Expand All @@ -233,7 +234,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
}

Expand All @@ -246,7 +247,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})
}
}
Expand Down Expand Up @@ -279,7 +280,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)
Expand All @@ -298,7 +301,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
}
Expand All @@ -310,7 +313,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
Expand Down Expand Up @@ -405,7 +410,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)

Expand All @@ -414,12 +419,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{
Expand Down Expand Up @@ -466,13 +473,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,
Expand Down Expand Up @@ -522,7 +529,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 {
Expand All @@ -534,7 +541,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
Expand Down Expand Up @@ -618,7 +625,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)
Expand All @@ -632,7 +639,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
Expand All @@ -643,7 +650,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)
Expand All @@ -657,7 +664,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
Expand All @@ -668,7 +675,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)
Expand All @@ -682,7 +689,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
Expand All @@ -693,7 +700,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)
Expand All @@ -707,7 +714,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
Expand All @@ -718,7 +725,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)
Expand All @@ -732,7 +739,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
Expand All @@ -743,29 +750,33 @@ 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")

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
}

Expand Down Expand Up @@ -848,6 +859,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)
Expand All @@ -857,7 +870,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
}
Expand Down
Loading

0 comments on commit 08cacc3

Please sign in to comment.