Skip to content

Commit

Permalink
Merge pull request #211 from pinikomarov/structured_logging
Browse files Browse the repository at this point in the history
update logging
  • Loading branch information
openshift-ci[bot] authored Oct 26, 2023
2 parents e981d04 + 8ba9e05 commit 49dc221
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 68 deletions.
22 changes: 13 additions & 9 deletions controllers/aodh_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func (r *AutoscalingReconciler) reconcileDisabledAodh(
instance *telemetryv1.Autoscaling,
helper *helper.Helper,
) (ctrl.Result, error) {
r.Log.Info("Reconciling Service Aodh disabled")
Log := r.GetLogger(ctx)
Log.Info("Reconciling Service Aodh disabled")
serviceLabels := map[string]string{
common.AppSelector: autoscaling.ServiceName,
}
Expand Down Expand Up @@ -144,7 +145,7 @@ func (r *AutoscalingReconciler) reconcileDisabledAodh(
return ctrl.Result{}, err
}
instance.Status.Conditions = condition.Conditions{}
r.Log.Info(fmt.Sprintf("Reconciled Service Aodh '%s' disable successfully", autoscaling.ServiceName))
Log.Info(fmt.Sprintf("Reconciled Service Aodh '%s' disable successfully", autoscaling.ServiceName))
return ctrl.Result{}, nil
}

Expand All @@ -153,7 +154,8 @@ func (r *AutoscalingReconciler) reconcileDeleteAodh(
instance *telemetryv1.Autoscaling,
helper *helper.Helper,
) (ctrl.Result, error) {
r.Log.Info("Reconciling Service Aodh delete")
Log := r.GetLogger(ctx)
Log.Info("Reconciling Service Aodh delete")

// remove db finalizer first
db, err := mariadbv1.GetDatabaseByName(ctx, helper, autoscaling.ServiceName)
Expand Down Expand Up @@ -197,7 +199,7 @@ func (r *AutoscalingReconciler) reconcileDeleteAodh(
util.LogForObject(helper, "Removed finalizer from our KeystoneEndpoint", instance)
}
}
r.Log.Info(fmt.Sprintf("Reconciled Service Aodh '%s' delete successfully", autoscaling.ServiceName))
Log.Info(fmt.Sprintf("Reconciled Service Aodh '%s' delete successfully", autoscaling.ServiceName))

return ctrl.Result{}, nil
}
Expand All @@ -208,7 +210,8 @@ func (r *AutoscalingReconciler) reconcileInitAodh(
helper *helper.Helper,
serviceLabels map[string]string,
) (ctrl.Result, error) {
r.Log.Info("Reconciling Service Aodh init")
Log := r.GetLogger(ctx)
Log.Info("Reconciling Service Aodh init")
_, _, err := secret.GetSecret(ctx, helper, instance.Spec.Aodh.Secret, instance.Namespace)
if err != nil {
if k8s_errors.IsNotFound(err) {
Expand Down Expand Up @@ -347,12 +350,12 @@ func (r *AutoscalingReconciler) reconcileInitAodh(
}
if dbSyncjob.HasChanged() {
instance.Status.Hash[telemetryv1.DbSyncHash] = dbSyncjob.GetHash()
r.Log.Info(fmt.Sprintf("Service '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[telemetryv1.DbSyncHash]))
Log.Info(fmt.Sprintf("Service '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[telemetryv1.DbSyncHash]))
}
instance.Status.Conditions.MarkTrue(condition.DBSyncReadyCondition, condition.DBSyncReadyMessage)

// run Aodh db sync - end
r.Log.Info("Reconciled Service Aodh init successfully")
Log.Info("Reconciled Service Aodh init successfully")
return ctrl.Result{}, nil
}

Expand All @@ -362,7 +365,8 @@ func (r *AutoscalingReconciler) reconcileNormalAodh(
helper *helper.Helper,
inputHash string,
) (ctrl.Result, error) {
r.Log.Info(fmt.Sprintf("Reconciling Service Aodh '%s'", autoscaling.ServiceName))
Log := r.GetLogger(ctx)
Log.Info(fmt.Sprintf("Reconciling Service Aodh '%s'", autoscaling.ServiceName))
serviceLabels := map[string]string{
common.AppSelector: autoscaling.ServiceName,
}
Expand Down Expand Up @@ -543,6 +547,6 @@ func (r *AutoscalingReconciler) reconcileNormalAodh(
return ctrlResult, nil
}

r.Log.Info("Reconciled Service Aodh successfully")
Log.Info("Reconciled Service Aodh successfully")
return ctrl.Result{}, nil
}
51 changes: 31 additions & 20 deletions controllers/autoscaling_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

Expand Down Expand Up @@ -64,10 +65,14 @@ import (
type AutoscalingReconciler struct {
client.Client
Kclient kubernetes.Interface
Log logr.Logger
Scheme *runtime.Scheme
}

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

// +kubebuilder:rbac:groups=telemetry.openstack.org,resources=autoscalings,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=telemetry.openstack.org,resources=autoscalings/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=telemetry.openstack.org,resources=autoscalings/finalizers,verbs=update
Expand Down Expand Up @@ -96,7 +101,7 @@ type AutoscalingReconciler struct {

// Reconcile reconciles an Autoscaling
func (r *AutoscalingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues(autoscaling.ServiceName, req.NamespacedName)
Log := r.GetLogger(ctx)

// Fetch the Autoscaling instance
instance := &telemetryv1.Autoscaling{}
Expand All @@ -117,7 +122,7 @@ func (r *AutoscalingReconciler) 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 @@ -215,7 +220,8 @@ func (r *AutoscalingReconciler) reconcileDisabled(
instance *telemetryv1.Autoscaling,
helper *helper.Helper,
) (ctrl.Result, error) {
r.Log.Info("Reconciling Service disabled")
Log := r.GetLogger(ctx)
Log.Info("Reconciling Service disabled")
ctrlResult, err := r.reconcileDisabledPrometheus(ctx, instance, helper)
if err != nil {
return ctrlResult, err
Expand All @@ -232,7 +238,7 @@ func (r *AutoscalingReconciler) reconcileDisabled(
for _, c := range instance.Status.Conditions {
instance.Status.Conditions.MarkTrue(c.Type, telemetryv1.AutoscalingReadyDisabledMessage)
}
r.Log.Info(fmt.Sprintf("Reconciled Service '%s' disable successfully", autoscaling.ServiceName))
Log.Info(fmt.Sprintf("Reconciled Service '%s' disable successfully", autoscaling.ServiceName))
return ctrl.Result{}, nil
}

Expand All @@ -241,7 +247,8 @@ func (r *AutoscalingReconciler) reconcileDelete(
instance *telemetryv1.Autoscaling,
helper *helper.Helper,
) (ctrl.Result, error) {
r.Log.Info("Reconciling Service delete")
Log := r.GetLogger(ctx)
Log.Info("Reconciling Service delete")

ctrlResult, err := r.reconcileDeletePrometheus(ctx, instance, helper)
if err != nil {
Expand All @@ -257,7 +264,7 @@ func (r *AutoscalingReconciler) reconcileDelete(
}
// Service is deleted so remove the finalizer.
controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())
r.Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", autoscaling.ServiceName))
Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", autoscaling.ServiceName))

return ctrl.Result{}, nil
}
Expand All @@ -268,7 +275,8 @@ func (r *AutoscalingReconciler) reconcileInit(
helper *helper.Helper,
serviceLabels map[string]string,
) (ctrl.Result, error) {
r.Log.Info("Reconciling Service init")
Log := r.GetLogger(ctx)
Log.Info("Reconciling Service init")
ctrlResult, err := r.reconcileInitPrometheus(ctx, instance, helper, serviceLabels)
if err != nil {
return ctrlResult, err
Expand All @@ -281,7 +289,7 @@ func (r *AutoscalingReconciler) reconcileInit(
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
r.Log.Info("Reconciled Service init successfully")
Log.Info("Reconciled Service init successfully")
return ctrl.Result{}, nil

}
Expand All @@ -291,7 +299,8 @@ func (r *AutoscalingReconciler) reconcileNormal(
instance *telemetryv1.Autoscaling,
helper *helper.Helper,
) (ctrl.Result, error) {
r.Log.Info(fmt.Sprintf("Reconciling Service '%s'", autoscaling.ServiceName))
Log := r.GetLogger(ctx)
Log.Info(fmt.Sprintf("Reconciling Service '%s'", autoscaling.ServiceName))

// Service account, role, binding
rbacRules := []rbacv1.PolicyRule{
Expand Down Expand Up @@ -326,7 +335,7 @@ func (r *AutoscalingReconciler) reconcileNormal(
transportURL, op, err := r.transportURLCreateOrUpdate(instance)

if err != nil {
r.Log.Info("Error getting transportURL. Setting error condition on status and returning")
Log.Info("Error getting transportURL. Setting error condition on status and returning")
instance.Status.Conditions.Set(condition.FalseCondition(
condition.RabbitMqTransportURLReadyCondition,
condition.ErrorReason,
Expand All @@ -337,13 +346,13 @@ func (r *AutoscalingReconciler) reconcileNormal(
}

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(
condition.RabbitMqTransportURLReadyCondition,
condition.RequestedReason,
Expand Down Expand Up @@ -500,7 +509,7 @@ func (r *AutoscalingReconciler) reconcileNormal(
if err != nil {
return ctrlResult, err
}
r.Log.Info("Reconciled Service successfully")
Log.Info("Reconciled Service successfully")
return ctrl.Result{}, nil
}

Expand All @@ -513,6 +522,7 @@ func (r *AutoscalingReconciler) createHashOfInputHashes(
instance *telemetryv1.Autoscaling,
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 @@ -522,7 +532,7 @@ func (r *AutoscalingReconciler) 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 Expand Up @@ -684,7 +694,7 @@ func (r *AutoscalingReconciler) transportURLCreateOrUpdate(instance *telemetryv1
}

// SetupWithManager sets up the controller with the Manager.
func (r *AutoscalingReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *AutoscalingReconciler) 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 Autoscaling 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 @@ -696,6 +706,7 @@ func (r *AutoscalingReconciler) SetupWithManager(mgr ctrl.Manager) error {
// reconciliation for a Autoscaling CR that does not need it.
//
// TODO: We also need a watch func to monitor for changes to the secret referenced by Autoscaling.Spec.Secret
Log := r.GetLogger(ctx)
transportURLSecretFn := func(o client.Object) []reconcile.Request {
result := []reconcile.Request{}

Expand All @@ -705,7 +716,7 @@ func (r *AutoscalingReconciler) SetupWithManager(mgr ctrl.Manager) error {
client.InNamespace(o.GetNamespace()),
}
if err := r.Client.List(context.Background(), autoscalings, listOpts...); err != nil {
r.Log.Error(err, "Unable to retrieve Autoscaling CRs %v")
Log.Error(err, "Unable to retrieve Autoscaling CRs %v")
return nil
}

Expand All @@ -718,7 +729,7 @@ func (r *AutoscalingReconciler) SetupWithManager(mgr ctrl.Manager) error {
Namespace: o.GetNamespace(),
Name: cr.Name,
}
r.Log.Info(fmt.Sprintf("TransportURL Secret %s belongs to TransportURL belonging to Autoscaling CR %s", o.GetName(), cr.Name))
Log.Info(fmt.Sprintf("TransportURL Secret %s belongs to TransportURL belonging to Autoscaling CR %s", o.GetName(), cr.Name))
result = append(result, reconcile.Request{NamespacedName: name})
}
}
Expand Down Expand Up @@ -748,7 +759,7 @@ func (r *AutoscalingReconciler) SetupWithManager(mgr ctrl.Manager) error {
client.InNamespace(o.GetNamespace()),
}
if err := r.Client.List(context.Background(), autoscalings, listOpts...); err != nil {
r.Log.Error(err, "Unable to retrieve Autoscaling CRs %w")
Log.Error(err, "Unable to retrieve Autoscaling CRs %w")
return nil
}

Expand All @@ -758,7 +769,7 @@ func (r *AutoscalingReconciler) SetupWithManager(mgr ctrl.Manager) error {
Namespace: o.GetNamespace(),
Name: cr.Name,
}
r.Log.Info(fmt.Sprintf("Memcached %s is used by Autoscaling CR %s", o.GetName(), cr.Name))
Log.Info(fmt.Sprintf("Memcached %s is used by Autoscaling CR %s", o.GetName(), cr.Name))
result = append(result, reconcile.Request{NamespacedName: name})
}
}
Expand Down
Loading

0 comments on commit 49dc221

Please sign in to comment.