Skip to content

Commit

Permalink
Merge pull request #169 from pinikomarov/structured_logging
Browse files Browse the repository at this point in the history
switch to structured logging
  • Loading branch information
openshift-merge-bot[bot] authored Nov 8, 2023
2 parents 8d59a90 + 03c3087 commit c3657ff
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 55 deletions.
35 changes: 22 additions & 13 deletions controllers/amphoracontroller_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ 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"
)

// OctaviaAmphoraControllerReconciler reconciles an OctaviaAmmphoraController object
Expand All @@ -56,6 +57,11 @@ type OctaviaAmphoraControllerReconciler struct {
Scheme *runtime.Scheme
}

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

//+kubebuilder:rbac:groups=octavia.openstack.org,resources=octaviaamphoracontrollers,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=octavia.openstack.org,resources=octaviaamphoracontrollers/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=octavia.openstack.org,resources=octaviaamphoracontrollers/finalizers,verbs=update
Expand All @@ -65,7 +71,7 @@ type OctaviaAmphoraControllerReconciler struct {
// controllers like the octavia housekeeper, worker and health manager
// services
func (r *OctaviaAmphoraControllerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("amphoracontroller", req.NamespacedName)
Log := r.GetLogger(ctx)

instance := &octaviav1.OctaviaAmphoraController{}
err := r.Client.Get(ctx, req.NamespacedName, instance)
Expand Down Expand Up @@ -110,7 +116,7 @@ func (r *OctaviaAmphoraControllerReconciler) Reconcile(ctx context.Context, req
r.Client,
r.Kclient,
r.Scheme,
r.Log,
Log,
)
if err != nil {
return ctrl.Result{}, err
Expand All @@ -124,14 +130,14 @@ func (r *OctaviaAmphoraControllerReconciler) Reconcile(ctx context.Context, req
}

if err := helper.SetAfter(instance); err != nil {
util.LogErrorForObject(helper, err, "Set after and calc patch/diff", instance)
Log.Error(err, "Set after and calc patch/diff")
}

if changed := helper.GetChanges()["status"]; changed {
patch := client.MergeFrom(helper.GetBeforeObject())

if err := r.Status().Patch(ctx, instance, patch); err != nil && !k8s_errors.IsNotFound(err) {
util.LogErrorForObject(helper, err, "Update status", instance)
Log.Error(err, "Update status")
}
}
}()
Expand All @@ -147,36 +153,39 @@ func (r *OctaviaAmphoraControllerReconciler) Reconcile(ctx context.Context, req

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

controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())

if err := r.Update(ctx, instance); err != nil && !k8s_errors.IsNotFound(err) {
return ctrl.Result{}, err
}

util.LogForObject(helper, "Reconciled Service delete successfully", instance)
Log.Info("Reconciled Service delete successfully")
return ctrl.Result{}, nil
}

func (r *OctaviaAmphoraControllerReconciler) reconcileUpdate(ctx context.Context, instance *octaviav1.OctaviaAmphoraController,
helper *helper.Helper) (ctrl.Result, error) {
util.LogForObject(helper, "Reconciling Service update", instance)
util.LogForObject(helper, "Reconciled Service update successfully", instance)
Log := r.GetLogger(ctx)
Log.Info("Reconciling Service update")
Log.Info("Reconciled Service update successfully")
return ctrl.Result{}, nil
}

func (r *OctaviaAmphoraControllerReconciler) reconcileUpgrade(ctx context.Context, instance *octaviav1.OctaviaAmphoraController,
helper *helper.Helper) (ctrl.Result, error) {
util.LogForObject(helper, "Reconciling Service upgrade", instance)
util.LogForObject(helper, "Reconciled Service upgrade successfully", instance)
Log := r.GetLogger(ctx)
Log.Info("Reconciling Service upgrade")
Log.Info("Reconciled Service upgrade successfully")
return ctrl.Result{}, nil
}

func (r *OctaviaAmphoraControllerReconciler) reconcileNormal(ctx context.Context, instance *octaviav1.OctaviaAmphoraController,
helper *helper.Helper) (ctrl.Result, error) {

util.LogForObject(helper, "Reconciling Service", instance)
Log := r.GetLogger(ctx)
Log.Info("Reconciling Service")
if !controllerutil.ContainsFinalizer(instance, helper.GetFinalizer()) {
// If the service object doesn't have our finalizer, add it.
controllerutil.AddFinalizer(instance, helper.GetFinalizer())
Expand Down Expand Up @@ -353,7 +362,7 @@ func (r *OctaviaAmphoraControllerReconciler) reconcileNormal(ctx context.Context

// create Deployment - end

util.LogForObject(helper, "Reconciled Service successfully", instance)
Log.Info("Reconciled Service successfully")
return ctrl.Result{}, nil
}

Expand Down
53 changes: 32 additions & 21 deletions controllers/octavia_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,21 @@ 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"
)

// OctaviaReconciler reconciles an Octavia object
type OctaviaReconciler struct {
client.Client
Kclient kubernetes.Interface
Log logr.Logger
Scheme *runtime.Scheme
}

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

// +kubebuilder:rbac:groups=octavia.openstack.org,resources=octavias,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=octavia.openstack.org,resources=octavias/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=octavia.openstack.org,resources=octavias/finalizers,verbs=update
Expand Down Expand Up @@ -96,7 +101,7 @@ type OctaviaReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *OctaviaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
_ = r.Log.WithValues("octavia", req.NamespacedName)
Log := r.GetLogger(ctx)

// Fetch the Octavia instance
instance := &octaviav1.Octavia{}
Expand All @@ -117,7 +122,7 @@ func (r *OctaviaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
r.Client,
r.Kclient,
r.Scheme,
r.Log,
Log,
)
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -200,6 +205,7 @@ func (r *OctaviaReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

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

// remove db finalizer first
Expand All @@ -217,7 +223,7 @@ func (r *OctaviaReconciler) reconcileDelete(ctx context.Context, instance *octav
// 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())
r.Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name))
Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name))

util.LogForObject(helper, "Reconciled Service delete successfully", instance)
return ctrl.Result{}, nil
Expand All @@ -230,7 +236,8 @@ func (r *OctaviaReconciler) 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")

//
// create service DB instance
Expand Down Expand Up @@ -296,7 +303,7 @@ func (r *OctaviaReconciler) reconcileInit(
//
dbSyncHash := instance.Status.Hash[octaviav1.DbSyncHash]
jobDef := octavia.DbSyncJob(instance, serviceLabels, serviceAnnotations)
r.Log.Info("Initializing db sync job")
Log.Info("Initializing db sync job")
dbSyncjob := job.NewJob(
jobDef,
octaviav1.DbSyncHash,
Expand Down Expand Up @@ -332,33 +339,36 @@ func (r *OctaviaReconciler) reconcileInit(

// run octavia db sync - end

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

func (r *OctaviaReconciler) reconcileUpdate(ctx context.Context, instance *octaviav1.Octavia, 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 *OctaviaReconciler) reconcileUpgrade(ctx context.Context, instance *octaviav1.Octavia, helper *helper.Helper) (ctrl.Result, error) {
r.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?

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

// TODO(tweining): implement
func (r *OctaviaReconciler) reconcileNormal(ctx context.Context, instance *octaviav1.Octavia, helper *helper.Helper) (ctrl.Result, error) {
r.Log.Info("Reconciling Service")
Log := r.GetLogger(ctx)
Log.Info("Reconciling Service")

// Service account, role, binding
rbacRules := []rbacv1.PolicyRule{
Expand Down Expand Up @@ -395,13 +405,13 @@ func (r *OctaviaReconciler) reconcileNormal(ctx context.Context, instance *octav
}

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 the TransportURL %s secret to be created", transportURL.Name))
Log.Info(fmt.Sprintf("Waiting for the TransportURL %s secret to be created", transportURL.Name))
instance.Status.Conditions.Set(condition.FalseCondition(
condition.InputReadyCondition,
condition.RequestedReason,
Expand Down Expand Up @@ -550,7 +560,7 @@ func (r *OctaviaReconciler) reconcileNormal(ctx context.Context, instance *octav
return ctrlResult, nil
}

r.Log.Info(fmt.Sprintf("Calling for deploy for API with %s", instance.Status.DatabaseHostname))
Log.Info(fmt.Sprintf("Calling for deploy for API with %s", instance.Status.DatabaseHostname))

// TODO(beagles): look into adding condition types/messages in a common file
octaviaAPI, op, err := r.apiDeploymentCreateOrUpdate(instance)
Expand All @@ -564,7 +574,7 @@ func (r *OctaviaReconciler) reconcileNormal(ctx context.Context, instance *octav
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 OctaviaAPI status' ReadyCount to this parent CR
Expand All @@ -590,7 +600,7 @@ func (r *OctaviaReconciler) reconcileNormal(ctx context.Context, instance *octav
}

if op != controllerutil.OperationResultNone {
r.Log.Info(fmt.Sprintf("Deployment of OctaviaHousekeeping for %s successfully reconciled - operation: %s", instance.Name, string(op)))
Log.Info(fmt.Sprintf("Deployment of OctaviaHousekeeping for %s successfully reconciled - operation: %s", instance.Name, string(op)))
}

instance.Status.OctaviaHousekeepingReadyCount = octaviaHousekeeping.Status.ReadyCount
Expand All @@ -613,7 +623,7 @@ func (r *OctaviaReconciler) reconcileNormal(ctx context.Context, instance *octav
}

if op != controllerutil.OperationResultNone {
r.Log.Info(fmt.Sprintf("Deployment of OctaviaHealthManager for %s successfully reconciled - operation: %s", instance.Name, string(op)))
Log.Info(fmt.Sprintf("Deployment of OctaviaHealthManager for %s successfully reconciled - operation: %s", instance.Name, string(op)))
}

instance.Status.OctaviaHealthManagerReadyCount = octaviaHealthManager.Status.ReadyCount
Expand All @@ -636,7 +646,7 @@ func (r *OctaviaReconciler) reconcileNormal(ctx context.Context, instance *octav
}

if op != controllerutil.OperationResultNone {
r.Log.Info(fmt.Sprintf("Deployment of OctaviaWorker for %s successfully reconciled - operation: %s", instance.Name, string(op)))
Log.Info(fmt.Sprintf("Deployment of OctaviaWorker for %s successfully reconciled - operation: %s", instance.Name, string(op)))
}

instance.Status.OctaviaWorkerReadyCount = octaviaWorker.Status.ReadyCount
Expand All @@ -649,7 +659,7 @@ func (r *OctaviaReconciler) reconcileNormal(ctx context.Context, instance *octav

// create Deployment - end

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

Expand Down Expand Up @@ -720,6 +730,7 @@ func (r *OctaviaReconciler) createHashOfInputHashes(
instance *octaviav1.Octavia,
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 @@ -729,7 +740,7 @@ func (r *OctaviaReconciler) 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 c3657ff

Please sign in to comment.