Skip to content

Commit

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

update loggingWq

update logging

update logging

update logging
  • Loading branch information
pinikomarov committed Oct 24, 2023
1 parent 5dc5942 commit df8512a
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 127 deletions.
57 changes: 36 additions & 21 deletions controllers/ironic_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,21 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
)

// IronicReconciler reconciles a Ironic object
type IronicReconciler 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 *IronicReconciler) GetLogger(ctx context.Context) logr.Logger {
return log.FromContext(ctx).WithName("Controllers").WithName("Ironic")
}

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

// Fetch the Ironic instance
instance := &ironicv1.Ironic{}
Expand All @@ -123,7 +128,7 @@ func (r *IronicReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
r.Client,
r.Kclient,
r.Scheme,
r.Log,
Log,
)
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -217,7 +222,9 @@ func (r *IronicReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

func (r *IronicReconciler) reconcileDelete(ctx context.Context, instance *ironicv1.Ironic, helper *helper.Helper) (ctrl.Result, error) {
r.Log.Info("Reconciling Ironic delete")
Log := r.GetLogger(ctx)

Log.Info("Reconciling Ironic delete")

// remove db finalizer first
db, err := mariadbv1.GetDatabaseByName(ctx, helper, instance.Name)
Expand All @@ -233,13 +240,15 @@ func (r *IronicReconciler) reconcileDelete(ctx context.Context, instance *ironic

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

return ctrl.Result{}, nil
}

func (r *IronicReconciler) reconcileNormal(ctx context.Context, instance *ironicv1.Ironic, helper *helper.Helper) (ctrl.Result, error) {
r.Log.Info("Reconciling Service")
Log := r.GetLogger(ctx)

Log.Info("Reconciling Service")

// Service account, role, binding
rbacResult, err := common_rbac.ReconcileRbac(ctx, helper, instance, getCommonRbacRules())
Expand Down Expand Up @@ -279,13 +288,13 @@ func (r *IronicReconciler) reconcileNormal(ctx context.Context, instance *ironic
}

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 @@ -443,7 +452,7 @@ func (r *IronicReconciler) reconcileNormal(ctx context.Context, instance *ironic
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
r.Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", ironicConductor.Name, string(op)))
Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", ironicConductor.Name, string(op)))
}
// Mirror IronicConductor status' ReadyCount to this parent CR
condGrp := conductorSpec.ConductorGroup
Expand All @@ -470,7 +479,7 @@ func (r *IronicReconciler) reconcileNormal(ctx context.Context, instance *ironic
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
r.Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", ironicAPI.Name, string(op)))
Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", ironicAPI.Name, string(op)))
}

// Mirror IronicAPI status' APIEndpoints and ReadyCount to this parent CR
Expand Down Expand Up @@ -499,7 +508,7 @@ func (r *IronicReconciler) reconcileNormal(ctx context.Context, instance *ironic
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
r.Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", ironicInspector.Name, string(op)))
Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", ironicInspector.Name, string(op)))
}

// Mirror IronicInspector status APIEndpoints and ReadyCount to this parent CR
Expand Down Expand Up @@ -543,7 +552,7 @@ func (r *IronicReconciler) reconcileNormal(ctx context.Context, instance *ironic
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
r.Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", ironicNeutronAgent.Name, string(op)))
Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", ironicNeutronAgent.Name, string(op)))
}
// Mirror IronicNeutronAgent status ReadyCount to this parent CR
instance.Status.IronicNeutronAgentReadyCount = ironicNeutronAgent.Status.ReadyCount
Expand All @@ -568,7 +577,7 @@ func (r *IronicReconciler) reconcileNormal(ctx context.Context, instance *ironic
instance.Status.Conditions.MarkTrue(ironicv1.IronicNeutronAgentReadyCondition, "")
}

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

Expand All @@ -578,7 +587,9 @@ func (r *IronicReconciler) reconcileInit(
helper *helper.Helper,
serviceLabels map[string]string,
) (ctrl.Result, error) {
r.Log.Info("Reconciling Ironic init")
Log := r.GetLogger(ctx)

Log.Info("Reconciling Ironic init")

//
// create service DB instance
Expand Down Expand Up @@ -639,14 +650,14 @@ func (r *IronicReconciler) reconcileInit(

// create service DB - end

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

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

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

Expand All @@ -656,7 +667,9 @@ func (r *IronicReconciler) reconcileUpgrade(
helper *helper.Helper,
serviceLabels map[string]string,
) (ctrl.Result, error) {
r.Log.Info("Reconciling Ironic upgrade")
Log := r.GetLogger(ctx)

Log.Info("Reconciling Ironic upgrade")

//
// run ironic db sync
Expand Down Expand Up @@ -693,13 +706,13 @@ func (r *IronicReconciler) reconcileUpgrade(
}
if dbSyncjob.HasChanged() {
instance.Status.Hash[ironicv1.DbSyncHash] = dbSyncjob.GetHash()
r.Log.Info(fmt.Sprintf("Job %s hash added - %s", jobDef.Name, instance.Status.Hash[ironicv1.DbSyncHash]))
Log.Info(fmt.Sprintf("Job %s hash added - %s", jobDef.Name, instance.Status.Hash[ironicv1.DbSyncHash]))
}
instance.Status.Conditions.MarkTrue(condition.DBSyncReadyCondition, condition.DBSyncReadyMessage)

// run ironic db sync - end

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

Expand Down Expand Up @@ -874,6 +887,8 @@ func (r *IronicReconciler) createHashOfInputHashes(
instance *ironicv1.Ironic,
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 @@ -883,7 +898,7 @@ func (r *IronicReconciler) 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
52 changes: 33 additions & 19 deletions controllers/ironicapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"time"

"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand All @@ -35,7 +36,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/go-logr/logr"
ironicv1 "github.com/openstack-k8s-operators/ironic-operator/api/v1beta1"
ironic "github.com/openstack-k8s-operators/ironic-operator/pkg/ironic"
ironicapi "github.com/openstack-k8s-operators/ironic-operator/pkg/ironicapi"
Expand All @@ -59,7 +59,6 @@ import (
type IronicAPIReconciler struct {
client.Client
Kclient kubernetes.Interface
Log logr.Logger
Scheme *runtime.Scheme
}

Expand All @@ -73,6 +72,11 @@ var (
}
)

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

// +kubebuilder:rbac:groups=ironic.openstack.org,resources=ironicapis,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=ironic.openstack.org,resources=ironicapis/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=ironic.openstack.org,resources=ironicapis/finalizers,verbs=update
Expand All @@ -95,7 +99,7 @@ var (

// Reconcile -
func (r *IronicAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
_ = log.FromContext(ctx)
Log := r.GetLogger(ctx)

// Fetch the IronicAPI instance
instance := &ironicv1.IronicAPI{}
Expand All @@ -116,7 +120,7 @@ func (r *IronicAPIReconciler) 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 @@ -198,9 +202,11 @@ func (r *IronicAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}

// SetupWithManager sets up the controller with the Manager.
func (r *IronicAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *IronicAPIReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
// watch for configmap where the CM owner label AND the CR.Spec.ManagingCrName label matches
configMapFn := func(o client.Object) []reconcile.Request {
Log := r.GetLogger(ctx)

result := []reconcile.Request{}

// get all API CRs
Expand All @@ -209,7 +215,7 @@ func (r *IronicAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
client.InNamespace(o.GetNamespace()),
}
if err := r.Client.List(context.Background(), apis, listOpts...); err != nil {
r.Log.Error(err, "Unable to retrieve API CRs %v")
Log.Error(err, "Unable to retrieve API CRs %v")
return nil
}

Expand All @@ -224,7 +230,7 @@ func (r *IronicAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
Namespace: o.GetNamespace(),
Name: cr.Name,
}
r.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 @@ -252,7 +258,9 @@ func (r *IronicAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

func (r *IronicAPIReconciler) reconcileDelete(ctx context.Context, instance *ironicv1.IronicAPI, helper *helper.Helper) (ctrl.Result, error) {
r.Log.Info("Reconciling API delete")
Log := r.GetLogger(ctx)

Log.Info("Reconciling API delete")

for _, ksSvc := range keystoneServices {
// Remove the finalizer from our KeystoneEndpoint CR
Expand Down Expand Up @@ -290,7 +298,7 @@ func (r *IronicAPIReconciler) reconcileDelete(ctx context.Context, instance *iro

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

return ctrl.Result{}, nil
}
Expand All @@ -301,7 +309,9 @@ func (r *IronicAPIReconciler) reconcileInit(
helper *helper.Helper,
serviceLabels map[string]string,
) (ctrl.Result, error) {
r.Log.Info("Reconciling API init")
Log := r.GetLogger(ctx)

Log.Info("Reconciling API init")

//
// expose the service (create service and return the created endpoint URLs)
Expand Down Expand Up @@ -423,7 +433,7 @@ func (r *IronicAPIReconciler) reconcileInit(
if !instance.Spec.Standalone {

for _, ksSvc := range keystoneServices {
r.Log.Info("Reconciled API init successfully")
Log.Info("Reconciled API init successfully")
ksSvcSpec := keystonev1.KeystoneServiceSpec{
ServiceType: ksSvc["type"],
ServiceName: ksSvc["name"],
Expand Down Expand Up @@ -481,12 +491,14 @@ func (r *IronicAPIReconciler) reconcileInit(
}
}

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

func (r *IronicAPIReconciler) reconcileNormal(ctx context.Context, instance *ironicv1.IronicAPI, helper *helper.Helper) (ctrl.Result, error) {
r.Log.Info("Reconciling Service")
Log := r.GetLogger(ctx)

Log.Info("Reconciling Service")

if ironicv1.GetOwningIronicName(instance) == "" {
// Service account, role, binding
Expand Down Expand Up @@ -721,21 +733,21 @@ func (r *IronicAPIReconciler) reconcileNormal(ctx context.Context, instance *iro
}
// create Deployment - end

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

func (r *IronicAPIReconciler) reconcileUpdate(ctx context.Context, instance *ironicv1.IronicAPI, helper *helper.Helper) (ctrl.Result, error) {
// r.Log.Info("Reconciling API update")
// Log.Info("Reconciling API update")

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

func (r *IronicAPIReconciler) reconcileUpgrade(ctx context.Context, instance *ironicv1.IronicAPI, helper *helper.Helper) (ctrl.Result, error) {
// r.Log.Info("Reconciling API upgrade")
// Log.Info("Reconciling API upgrade")

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

Expand Down Expand Up @@ -819,6 +831,8 @@ func (r *IronicAPIReconciler) createHashOfInputHashes(
instance *ironicv1.IronicAPI,
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 @@ -828,7 +842,7 @@ func (r *IronicAPIReconciler) 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
}
Loading

0 comments on commit df8512a

Please sign in to comment.