Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
update logging
Browse files Browse the repository at this point in the history
update logging
  • Loading branch information
pinikomarov committed Oct 24, 2023
1 parent 7450e0f commit 523d07d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
36 changes: 20 additions & 16 deletions controllers/openstackdataplanedeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ type OpenStackDataPlaneDeploymentReconciler struct {
client.Client
Kclient kubernetes.Interface
Scheme *runtime.Scheme
Log logr.Logger
}

// Getlogger returns a logger object with a prefix of "conroller.name" and aditional controller context fields

Check failure on line 49 in controllers/openstackdataplanedeployment_controller.go

View workflow job for this annotation

GitHub Actions / github (govet, golint and gotest)

comment on exported method OpenStackDataPlaneDeploymentReconciler.GetLogger should be of the form "GetLogger ..."
func (r *OpenStackDataPlaneDeploymentReconciler) GetLogger(ctx context.Context) logr.Logger {
return log.FromContext(ctx).WithName("Controllers").WithName("OpenStackDataPlaneDeployment")
}

//+kubebuilder:rbac:groups=dataplane.openstack.org,resources=openstackdataplanedeployments,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -78,8 +82,8 @@ type OpenStackDataPlaneDeploymentReconciler struct {
// move the current state of the cluster closer to the desired state.
func (r *OpenStackDataPlaneDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {

logger := log.FromContext(ctx)
logger.Info("Reconciling Deployment")
Log := r.GetLogger(ctx)
Log.Info("Reconciling Deployment")

// Fetch the OpenStackDataPlaneDeployment instance
instance := &dataplanev1.OpenStackDataPlaneDeployment{}
Expand All @@ -100,7 +104,7 @@ func (r *OpenStackDataPlaneDeploymentReconciler) Reconcile(ctx context.Context,
r.Client,
r.Kclient,
r.Scheme,
logger,
Log,
)
if err != nil {
return ctrl.Result{}, err
Expand All @@ -121,15 +125,15 @@ func (r *OpenStackDataPlaneDeploymentReconciler) Reconcile(ctx context.Context,
}
err := helper.PatchInstance(ctx, instance)
if err != nil {
logger.Error(err, "Error updating instance status conditions")
Log.Error(err, "Error updating instance status conditions")
_err = err
return
}
}()

// If the deploy is already done, return immediately.
if instance.Status.Deployed {
logger.Info("Already deployed", "instance.Status.Deployed", instance.Status.Deployed)
Log.Info("Already deployed", "instance.Status.Deployed", instance.Status.Deployed)
return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -160,7 +164,7 @@ func (r *OpenStackDataPlaneDeploymentReconciler) Reconcile(ctx context.Context,
if err != nil {
// NodeSet not found, force a requeue
if k8s_errors.IsNotFound(err) {
logger.Info("NodeSet not found", "NodeSet", nodeSet)
Log.Info("NodeSet not found", "NodeSet", nodeSet)
return ctrl.Result{RequeueAfter: time.Second * 15}, nil
}
// Error reading the object - requeue the request.
Expand All @@ -172,7 +176,7 @@ func (r *OpenStackDataPlaneDeploymentReconciler) Reconcile(ctx context.Context,
// Check that all nodeSets are SetupReady
for _, nodeSet := range nodeSets.Items {
if !nodeSet.Status.Conditions.IsTrue(dataplanev1.SetupReadyCondition) {
logger.Info("NodeSet SetupReadyCondition is not True", "NodeSet", nodeSet.Name)
Log.Info("NodeSet SetupReadyCondition is not True", "NodeSet", nodeSet.Name)
return ctrl.Result{RequeueAfter: time.Second * 15}, nil
}
}
Expand All @@ -189,10 +193,10 @@ func (r *OpenStackDataPlaneDeploymentReconciler) Reconcile(ctx context.Context,
haveError := false
for _, nodeSet := range nodeSets.Items {

logger.Info(fmt.Sprintf("Deploying NodeSet: %s", nodeSet.Name))
logger.Info("Set Status.Deployed to false", "instance", instance)
Log.Info(fmt.Sprintf("Deploying NodeSet: %s", nodeSet.Name))
Log.Info("Set Status.Deployed to false", "instance", instance)
instance.Status.Deployed = false
logger.Info("Set DeploymentReadyCondition false", "instance", instance)
Log.Info("Set DeploymentReadyCondition false", "instance", instance)
instance.Status.Conditions.Set(condition.FalseCondition(
condition.DeploymentReadyCondition, condition.RequestedReason,
condition.SeverityInfo, condition.DeploymentReadyRunningMessage))
Expand Down Expand Up @@ -240,8 +244,8 @@ func (r *OpenStackDataPlaneDeploymentReconciler) Reconcile(ctx context.Context,
if deployResult != nil {
shouldRequeue = true
} else {
logger.Info("OpenStackDeployment succeeded for NodeSet", "NodeSet", nodeSet.Name)
logger.Info("Set NodeSetDeploymentReadyCondition true", "nodeSet", nodeSet.Name)
Log.Info("OpenStackDeployment succeeded for NodeSet", "NodeSet", nodeSet.Name)
Log.Info("Set NodeSetDeploymentReadyCondition true", "nodeSet", nodeSet.Name)
instance.Status.Conditions.Set(
condition.TrueCondition(
condition.Type(fmt.Sprintf(dataplanev1.NodeSetDeploymentReadyCondition, nodeSet.Name)),
Expand All @@ -254,14 +258,14 @@ func (r *OpenStackDataPlaneDeploymentReconciler) Reconcile(ctx context.Context,
}

if shouldRequeue {
logger.Info("Not all NodeSets done for OpenStackDeployment")
Log.Info("Not all NodeSets done for OpenStackDeployment")
return ctrl.Result{}, nil
}

logger.Info("Set DeploymentReadyCondition true", "instance", instance)
Log.Info("Set DeploymentReadyCondition true", "instance", instance)
instance.Status.Conditions.Set(condition.TrueCondition(condition.DeploymentReadyCondition, condition.DeploymentReadyMessage))
instance.Status.Deployed = true
logger.Info("Set status deploy true", "instance", instance)
Log.Info("Set status deploy true", "instance", instance)
return ctrl.Result{}, nil
}

Expand Down
29 changes: 17 additions & 12 deletions controllers/openstackdataplanenodeset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ type OpenStackDataPlaneNodeSetReconciler struct {
client.Client
Kclient kubernetes.Interface
Scheme *runtime.Scheme
Log logr.Logger
}

// Getlogger returns a logger object with a prefix of "conroller.name" and aditional controller context fields

Check failure on line 98 in controllers/openstackdataplanenodeset_controller.go

View workflow job for this annotation

GitHub Actions / github (govet, golint and gotest)

comment on exported method OpenStackDataPlaneNodeSetReconciler.GetLogger should be of the form "GetLogger ..."
func (r *OpenStackDataPlaneNodeSetReconciler) GetLogger(ctx context.Context) logr.Logger {
return log.FromContext(ctx).WithName("Controllers").WithName("OpenStackDataPlaneNodeSet")
}

//+kubebuilder:rbac:groups=dataplane.openstack.org,resources=openstackdataplanenodesets,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -131,8 +135,8 @@ type OpenStackDataPlaneNodeSetReconciler struct {
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *OpenStackDataPlaneNodeSetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {

logger := log.FromContext(ctx)
logger.Info("Reconciling NodeSet")
Log := r.GetLogger(ctx)
Log.Info("Reconciling NodeSet")

// Fetch the OpenStackDataPlaneNodeSet instance
instance := &dataplanev1.OpenStackDataPlaneNodeSet{}
Expand All @@ -153,7 +157,7 @@ func (r *OpenStackDataPlaneNodeSetReconciler) Reconcile(ctx context.Context, req
r.Client,
r.Kclient,
r.Scheme,
logger,
Log,
)
if err != nil {
return ctrl.Result{}, err
Expand All @@ -174,7 +178,7 @@ func (r *OpenStackDataPlaneNodeSetReconciler) Reconcile(ctx context.Context, req
}
err := helper.PatchInstance(ctx, instance)
if err != nil {
logger.Error(err, "Error updating instance status conditions")
Log.Error(err, "Error updating instance status conditions")
_err = err
return
}
Expand Down Expand Up @@ -274,7 +278,7 @@ func (r *OpenStackDataPlaneNodeSetReconciler) Reconcile(ctx context.Context, req
if instance.Status.Deployed && instance.DeletionTimestamp.IsZero() {
// The role is already deployed and not being deleted, so reconciliation
// is already complete.
logger.Info("NodeSet already deployed", "instance", instance)
Log.Info("NodeSet already deployed", "instance", instance)
return ctrl.Result{}, nil
}

Expand All @@ -292,23 +296,23 @@ func (r *OpenStackDataPlaneNodeSetReconciler) Reconcile(ctx context.Context, req
// Set DeploymentReadyCondition to False if it was unknown.
// Handles the case where the NodeSet is created, but not yet deployed.
if instance.Status.Conditions.IsUnknown(condition.DeploymentReadyCondition) {
logger.Info("Set DeploymentReadyCondition false")
Log.Info("Set DeploymentReadyCondition false")
instance.Status.Conditions.MarkFalse(condition.DeploymentReadyCondition,
condition.NotRequestedReason, condition.SeverityInfo,
condition.DeploymentReadyInitMessage)
}

deploymentExists, isDeploymentReady, err := checkDeployment(helper, req)
if err != nil {
logger.Error(err, "Unable to get deployed OpenStackDataPlaneDeployments.")
Log.Error(err, "Unable to get deployed OpenStackDataPlaneDeployments.")
return ctrl.Result{}, err
}
if isDeploymentReady {
logger.Info("Set NodeSet DeploymentReadyCondition true")
Log.Info("Set NodeSet DeploymentReadyCondition true")
instance.Status.Conditions.MarkTrue(condition.DeploymentReadyCondition,
condition.DeploymentReadyMessage)
} else if deploymentExists {
logger.Info("Set NodeSet DeploymentReadyCondition false")
Log.Info("Set NodeSet DeploymentReadyCondition false")
instance.Status.Conditions.MarkFalse(condition.DeploymentReadyCondition,
condition.RequestedReason, condition.SeverityInfo,
condition.DeploymentReadyRunningMessage)
Expand Down Expand Up @@ -341,8 +345,9 @@ func checkDeployment(helper *helper.Helper,
}

// SetupWithManager sets up the controller with the Manager.
func (r *OpenStackDataPlaneNodeSetReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *OpenStackDataPlaneNodeSetReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
reconcileFunction := handler.EnqueueRequestsFromMapFunc(func(o client.Object) []reconcile.Request {
Log := r.GetLogger(ctx)
result := []reconcile.Request{}

// For each DNSMasq change event get the list of all
Expand All @@ -354,7 +359,7 @@ func (r *OpenStackDataPlaneNodeSetReconciler) SetupWithManager(mgr ctrl.Manager)
client.InNamespace(o.GetNamespace()),
}
if err := r.Client.List(context.Background(), nodeSets, listOpts...); err != nil {
r.Log.Error(err, "Unable to retrieve OpenStackDataPlaneNodeSetList %w")
Log.Error(err, "Unable to retrieve OpenStackDataPlaneNodeSetList %w")
return nil
}

Expand Down
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"context"
"flag"
"os"
"strconv"
Expand Down Expand Up @@ -74,7 +75,7 @@ func main() {
"Enabling this will ensure there is only one active controller manager.")
devMode, err := strconv.ParseBool(os.Getenv("DEV_MODE"))
if err != nil {
devMode = false
devMode = true
}
opts := zap.Options{
Development: devMode,
Expand Down Expand Up @@ -124,8 +125,7 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Kclient: kclient,
Log: ctrl.Log.WithName("controllers").WithName("OpenStackDataPlaneNodeSet"),
}).SetupWithManager(mgr); err != nil {
}).SetupWithManager(context.Background(), mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OpenStackDataPlaneNodeSet")
os.Exit(1)
}
Expand All @@ -144,7 +144,6 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Kclient: kclient,
Log: ctrl.Log.WithName("controllers").WithName("OpenStackDataPlaneDeployment"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OpenStackDataPlaneDeployment")
os.Exit(1)
Expand Down
4 changes: 1 addition & 3 deletions tests/functional/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,13 @@ var _ = BeforeSuite(func() {
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
Kclient: kclient,
Log: ctrl.Log.WithName("controllers").WithName("DataplaneNodeSet"),
}).SetupWithManager(k8sManager)
}).SetupWithManager(context.Background(), k8sManager)
Expect(err).ToNot(HaveOccurred())

err = (&controllers.OpenStackDataPlaneDeploymentReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
Kclient: kclient,
Log: ctrl.Log.WithName("controllers").WithName("DataplaneDeployment"),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

Expand Down

0 comments on commit 523d07d

Please sign in to comment.