Skip to content

Commit

Permalink
update logging
Browse files Browse the repository at this point in the history
  • Loading branch information
pinikomarov committed Oct 8, 2023
1 parent 54a6f75 commit 0258fb6
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 48 deletions.
59 changes: 33 additions & 26 deletions controllers/ironicconductor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

k8s_types "k8s.io/apimachinery/pkg/types"

"github.com/go-logr/logr"
routev1 "github.com/openshift/api/route/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -35,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 @@ -63,6 +65,11 @@ type IronicConductorReconciler struct {
Scheme *runtime.Scheme
}

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

// +kubebuilder:rbac:groups=ironic.openstack.org,resources=ironicconductors,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=ironic.openstack.org,resources=ironicconductors/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=ironic.openstack.org,resources=ironicconductors/finalizers,verbs=update
Expand All @@ -87,7 +94,7 @@ type IronicConductorReconciler struct {

// Reconcile -
func (r *IronicConductorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
log := GetLog(ctx, "IronicConductor")
Log := r.GetLogger(ctx)

// Fetch the IronicConductor instance
instance := &ironicv1.IronicConductor{}
Expand All @@ -108,7 +115,7 @@ func (r *IronicConductorReconciler) Reconcile(ctx context.Context, req ctrl.Requ
r.Client,
r.Kclient,
r.Scheme,
l,
Log,
)
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -200,15 +207,15 @@ func (r *IronicConductorReconciler) SetupWithManager(mgr ctrl.Manager, ctx conte
// watch for configmap where the CM owner label AND the CR.Spec.ManagingCrName label matches
configMapFn := func(o client.Object) []reconcile.Request {
result := []reconcile.Request{}
log := GetLog(ctx, "IronicConductor")
Log := r.GetLogger(ctx)

// get all API CRs
apis := &ironicv1.IronicConductorList{}
listOpts := []client.ListOption{
client.InNamespace(o.GetNamespace()),
}
if err := r.Client.List(context.Background(), apis, listOpts...); err != nil {
l.Error(err, "Unable to retrieve API CRs %v")
Log.Error(err, "Unable to retrieve API CRs %v")
return nil
}

Expand All @@ -223,7 +230,7 @@ func (r *IronicConductorReconciler) SetupWithManager(mgr ctrl.Manager, ctx conte
Namespace: o.GetNamespace(),
Name: cr.Name,
}
log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l))
Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l))
result = append(result, reconcile.Request{NamespacedName: name})
}
}
Expand Down Expand Up @@ -251,13 +258,13 @@ func (r *IronicConductorReconciler) SetupWithManager(mgr ctrl.Manager, ctx conte
}

func (r *IronicConductorReconciler) reconcileDelete(ctx context.Context, instance *ironicv1.IronicConductor, helper *helper.Helper) (ctrl.Result, error) {
log := GetLog(ctx, "IronicConductor")
Log := r.GetLogger(ctx)

log.Info("Reconciling Conductor delete")
Log.Info("Reconciling Conductor delete")

// Service is deleted so remove the finalizer.
controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())
log.Info("Reconciled Conductor delete successfully")
Log.Info("Reconciled Conductor delete successfully")

return ctrl.Result{}, nil
}
Expand All @@ -268,9 +275,9 @@ func (r *IronicConductorReconciler) reconcileServices(
helper *helper.Helper,
serviceLabels map[string]string,
) (ctrl.Result, error) {
log := GetLog(ctx, "IronicConductor")
Log := r.GetLogger(ctx)

log.Info("Reconciling Conductor Services")
Log.Info("Reconciling Conductor Services")

podList, err := ironicconductor.ConductorPods(ctx, instance, helper, serviceLabels)
if err != nil {
Expand Down Expand Up @@ -304,13 +311,13 @@ func (r *IronicConductorReconciler) reconcileServices(
conductorService,
)
if err != nil && k8s_errors.IsNotFound(err) {
log.Info(fmt.Sprintf("Service port %s does not exist, creating it", conductorService.Name))
Log.Info(fmt.Sprintf("Service port %s does not exist, creating it", conductorService.Name))
err = r.Create(ctx, conductorService)
if err != nil {
return ctrl.Result{}, err
}
} else {
log.Info(fmt.Sprintf("Service port %s exists, updating it", conductorService.Name))
Log.Info(fmt.Sprintf("Service port %s exists, updating it", conductorService.Name))
err = r.Update(ctx, conductorService)
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -347,13 +354,13 @@ func (r *IronicConductorReconciler) reconcileServices(
conductorRoute,
)
if err != nil && k8s_errors.IsNotFound(err) {
log.Info(fmt.Sprintf("Route %s does not exist, creating it", conductorRoute.Name))
Log.Info(fmt.Sprintf("Route %s does not exist, creating it", conductorRoute.Name))
err = r.Create(ctx, conductorRoute)
if err != nil {
return ctrl.Result{}, err
}
} else {
log.Info(fmt.Sprintf("Route %s exists, updating it", conductorRoute.Name))
Log.Info(fmt.Sprintf("Route %s exists, updating it", conductorRoute.Name))
err = r.Update(ctx, conductorRoute)
if err != nil {
return ctrl.Result{}, err
Expand All @@ -367,14 +374,14 @@ func (r *IronicConductorReconciler) reconcileServices(
// TODO: rework this
//

log.Info("Reconciled Conductor Services successfully")
Log.Info("Reconciled Conductor Services successfully")
return ctrl.Result{}, nil
}

func (r *IronicConductorReconciler) reconcileNormal(ctx context.Context, instance *ironicv1.IronicConductor, helper *helper.Helper) (ctrl.Result, error) {
log := GetLog(ctx, "IronicConductor")
Log := r.GetLogger(ctx)

log.Info("Reconciling Conductor")
Log.Info("Reconciling Conductor")

if ironicv1.GetOwningIronicName(instance) == "" {
// Service account, role, binding
Expand Down Expand Up @@ -619,21 +626,21 @@ func (r *IronicConductorReconciler) reconcileNormal(ctx context.Context, instanc
instance.Status.Conditions.MarkTrue(condition.DeploymentReadyCondition, condition.DeploymentReadyMessage)
}

log.Info("Reconciled Conductor successfully")
Log.Info("Reconciled Conductor successfully")
return ctrl.Result{}, nil
}

func (r *IronicConductorReconciler) reconcileUpdate(ctx context.Context, instance *ironicv1.IronicConductor, helper *helper.Helper) (ctrl.Result, error) {
// log.Info("Reconciling Service update")
// Log.Info("Reconciling Service update")

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

func (r *IronicConductorReconciler) reconcileUpgrade(ctx context.Context, instance *ironicv1.IronicConductor, helper *helper.Helper) (ctrl.Result, error) {
// log.Info("Reconciling Service upgrade")
// Log.Info("Reconciling Service upgrade")

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

Expand All @@ -649,7 +656,7 @@ func (r *IronicConductorReconciler) generateServiceConfigMaps(
// create custom Configmap for ironic-conductor-specific config input
// - %-config-data configmap holding custom config for the service's ironic.conf
//

Log := r.GetLogger(ctx)
cmLabels := labels.GetLabels(instance, labels.GetGroupLabel(ironic.ServiceName), map[string]string{})

// customData hold any customization for the service.
Expand Down Expand Up @@ -682,7 +689,7 @@ func (r *IronicConductorReconciler) generateServiceConfigMaps(
}
dhcpRanges, err := ironic.PrefixOrNetmaskFromCIDR(instance.Spec.DHCPRanges)
if err != nil {
l.Error(err, "Failed to get Prefix or Netmask from IP network Prefix (CIDR)")
Log.Error(err, "Failed to get Prefix or Netmask from IP network Prefix (CIDR)")
}
templateParameters["DHCPRanges"] = dhcpRanges
templateParameters["Standalone"] = instance.Spec.Standalone
Expand Down Expand Up @@ -730,7 +737,7 @@ func (r *IronicConductorReconciler) createHashOfInputHashes(
instance *ironicv1.IronicConductor,
envVars map[string]env.Setter,
) (string, bool, error) {
log := GetLog(ctx, "IronicConductor")
Log := r.GetLogger(ctx)

var hashMap map[string]string
changed := false
Expand All @@ -741,7 +748,7 @@ func (r *IronicConductorReconciler) createHashOfInputHashes(
}
if hashMap, changed = util.SetHash(instance.Status.Hash, common.InputHashName, hash); changed {
instance.Status.Hash = hashMap
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
}
10 changes: 5 additions & 5 deletions controllers/ironicinspector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"strings"
"time"

"github.com/cloudflare/cfssl/log"
"github.com/go-logr/logr"
ironic "github.com/openstack-k8s-operators/ironic-operator/pkg/ironic"
ironicinspector "github.com/openstack-k8s-operators/ironic-operator/pkg/ironicinspector"
Expand Down Expand Up @@ -59,6 +58,7 @@ import (
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -270,11 +270,11 @@ func (r *IronicInspectorReconciler) SetupWithManager(
client.InNamespace(o.GetNamespace()),
}
if err := r.Client.List(
context.Background(),
ctx,
apis,
listOpts...); err != nil {

log.Error(err, "Unable to retrieve API CRs %v")
Log.Error(err, "Unable to retrieve API CRs %v")
return nil
}

Expand Down Expand Up @@ -1131,7 +1131,7 @@ func (r *IronicInspectorReconciler) generateServiceConfigMaps(
instance,
labels.GetGroupLabel(ironic.ServiceName),
map[string]string{})

Log := r.GetLogger(ctx)
// customData hold any customization for the service.
// custom.conf is going to /etc/ironic-inspector/inspector.conf.d
// all other files get placed into /etc/ironic-inspector to allow
Expand Down Expand Up @@ -1176,7 +1176,7 @@ func (r *IronicInspectorReconciler) generateServiceConfigMaps(
}
dhcpRanges, err := ironic.PrefixOrNetmaskFromCIDR(instance.Spec.DHCPRanges)
if err != nil {
log.Error(err, "unable to get Prefix or Netmask from IP network Prefix (CIDR)")
Log.Error(err, "unable to get Prefix or Netmask from IP network Prefix (CIDR)")
}
templateParameters["DHCPRanges"] = dhcpRanges
templateParameters["Standalone"] = instance.Spec.Standalone
Expand Down
Loading

0 comments on commit 0258fb6

Please sign in to comment.