Skip to content

Commit

Permalink
This automatically adds additional Log fields like reconcile_id etc..…
Browse files Browse the repository at this point in the history
… from the controller context.

Follow up from: openstack-k8s-operators/keystone-operator#220
Epic :  [OSP-22582](https://issues.redhat.com/browse/OSP-22582) Switch Operators to structured logging

before :
```bash
{"level":"info","ts":"2023-07-04T10:11:14.655+0300","logger":"controllers.OpenStackControlPlane","msg":"Reconciling Glance","Glance.Namespace":"openstack","Glance.Name":"glance"}
```

after:
```bash
2023-07-04T16:37:17.388+0300    INFO    Controllers.OpenstackControlPlane       Reconciling Cinder      {"controller": "openstackcontrolplane", "controllerGroup": "core.openstack.org", "controllerKind": "OpenStackControlPlane", "OpenStackControlPlane": {"name":"openstack-network-isolation","namespace":"openstack"}, "namespace": "openstack", "name": "openstack-network-isolation",
"reconcileID": "4801c762-5a50-4773-849c-c8b19f59841d", "Cinder.Namespace": "openstack", "Cinder.Name": "cinder"}

```

Full logs with before and after log messages for the multiple resources called by this operator:

Full Before :
http://pastebin.test.redhat.com/1104088

Full After:
http://pastebin.test.redhat.com/1104132

set log dev true for console output
  • Loading branch information
pinikomarov committed Jul 4, 2023
1 parent 015b6a5 commit 54e05d1
Show file tree
Hide file tree
Showing 22 changed files with 92 additions and 57 deletions.
13 changes: 6 additions & 7 deletions controllers/core/openstackcontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ import (
telemetryv1 "github.com/openstack-k8s-operators/telemetry-operator/api/v1beta1"
rabbitmqv1 "github.com/rabbitmq/cluster-operator/api/v1beta1"

"github.com/go-logr/logr"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"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"

"github.com/go-logr/logr"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
)

// OpenStackControlPlaneReconciler reconciles a OpenStackControlPlane object
Expand Down Expand Up @@ -90,9 +88,10 @@ type OpenStackControlPlaneReconciler struct {
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile

func (r *OpenStackControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
_ = log.FromContext(ctx)

l := openstack.GetLog(ctx)
// Fetch the OpenStackControlPlane instance
instance := &corev1beta1.OpenStackControlPlane{}
err := r.Client.Get(ctx, req.NamespacedName, instance)
Expand All @@ -101,7 +100,7 @@ func (r *OpenStackControlPlaneReconciler) Reconcile(ctx context.Context, req ctr
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected.
// For additional cleanup logic use finalizers. Return and don't requeue.
r.Log.Info("OpenStackControlPlane instance is not found, probaby deleted. Nothing to do.")
l.Info("OpenStackControlPlane instance is not found, probaby deleted. Nothing to do.")
return ctrl.Result{}, nil
}
// Error reading the object - requeue the request.
Expand All @@ -117,7 +116,7 @@ func (r *OpenStackControlPlaneReconciler) Reconcile(ctx context.Context, req ctr
)
if err != nil {
// helper might be nil, so can't use util.LogErrorForObject since it requires helper as first arg
r.Log.Error(err, fmt.Sprintf("unable to acquire helper for OpenStackControlPlane %s", instance.Name))
l.Error(err, fmt.Sprintf("unable to acquire helper for OpenStackControlPlane %s", instance.Name))
return ctrl.Result{}, err
}

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,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
6 changes: 4 additions & 2 deletions pkg/openstack/ceilometer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func ReconcileCeilometer(ctx context.Context, instance *corev1beta1.OpenStackCon
},
}

l := GetLog(ctx)

if !instance.Spec.Ceilometer.Enabled {
if res, err := EnsureDeleted(ctx, helper, ceilometer); err != nil {
return res, err
Expand All @@ -38,7 +40,7 @@ func ReconcileCeilometer(ctx context.Context, instance *corev1beta1.OpenStackCon
return ctrl.Result{}, nil
}

helper.GetLogger().Info("Reconciling Ceilometer", ceilometerNamespaceLabel, instance.Namespace, ceilometerNameLabel, ceilometerName)
l.Info("Reconciling Ceilometer", ceilometerNamespaceLabel, instance.Namespace, ceilometerNameLabel, ceilometerName)
op, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), ceilometer, func() error {
instance.Spec.Ceilometer.Template.DeepCopyInto(&ceilometer.Spec)

Expand All @@ -63,7 +65,7 @@ func ReconcileCeilometer(ctx context.Context, instance *corev1beta1.OpenStackCon
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
helper.GetLogger().Info(fmt.Sprintf("%s %s - %s", ceilometerName, ceilometer.Name, op))
l.Info(fmt.Sprintf("%s %s - %s", ceilometerName, ceilometer.Name, op))
}

if ceilometer.IsReady() {
Expand Down
5 changes: 3 additions & 2 deletions pkg/openstack/cinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ func ReconcileCinder(ctx context.Context, instance *corev1beta1.OpenStackControl
instance.Status.Conditions.Remove(corev1beta1.OpenStackControlPlaneCinderReadyCondition)
return ctrl.Result{}, nil
}
l := GetLog(ctx)

helper.GetLogger().Info("Reconciling Cinder", "Cinder.Namespace", instance.Namespace, "Cinder.Name", "cinder")
l.Info("Reconciling Cinder", "Cinder.Namespace", instance.Namespace, "Cinder.Name", "cinder")
op, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), cinder, func() error {
instance.Spec.Cinder.Template.DeepCopyInto(&cinder.Spec)
if cinder.Spec.Secret == "" {
Expand Down Expand Up @@ -77,7 +78,7 @@ func ReconcileCinder(ctx context.Context, instance *corev1beta1.OpenStackControl
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
helper.GetLogger().Info(fmt.Sprintf("Cinder %s - %s", cinder.Name, op))
l.Info(fmt.Sprintf("Cinder %s - %s", cinder.Name, op))
}

if cinder.IsReady() {
Expand Down
7 changes: 7 additions & 0 deletions pkg/openstack/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ package openstack
import (
"context"

"github.com/go-logr/logr"
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)

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

// EnsureDeleted - Delete the object which in turn will clean the sub resources
func EnsureDeleted(ctx context.Context, helper *helper.Helper, obj client.Object) (ctrl.Result, error) {
key := client.ObjectKeyFromObject(obj)
Expand Down
6 changes: 4 additions & 2 deletions pkg/openstack/dnsmasq.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func ReconcileDNSMasqs(ctx context.Context, instance *corev1beta1.OpenStackContr
return ctrl.Result{}, nil
}

helper.GetLogger().Info("Reconciling DNSMasq", "DNSMasq.Namespace", instance.Namespace, "DNSMasq.Name", "dnsmasq")
l := GetLog(ctx)

l.Info("Reconciling DNSMasq", "DNSMasq.Namespace", instance.Namespace, "DNSMasq.Name", "dnsmasq")
op, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), dnsmasq, func() error {
instance.Spec.DNS.Template.DeepCopyInto(&dnsmasq.Spec)
if dnsmasq.Spec.NodeSelector == nil && instance.Spec.NodeSelector != nil {
Expand All @@ -56,7 +58,7 @@ func ReconcileDNSMasqs(ctx context.Context, instance *corev1beta1.OpenStackContr
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
helper.GetLogger().Info(fmt.Sprintf("dnsmasq %s - %s", dnsmasq.Name, op))
l.Info(fmt.Sprintf("dnsmasq %s - %s", dnsmasq.Name, op))
}

if dnsmasq.IsReady() {
Expand Down
5 changes: 3 additions & 2 deletions pkg/openstack/galera.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func reconcileGalera(
Namespace: instance.Namespace,
},
}
l := GetLog(ctx)

if !instance.Spec.Galera.Enabled {
if _, err := EnsureDeleted(ctx, helper, galera); err != nil {
Expand All @@ -102,7 +103,7 @@ func reconcileGalera(
return galeraReady, nil
}

helper.GetLogger().Info("Reconciling Galera", "Galera.Namespace", instance.Namespace, "Galera.Name", name)
l.Info("Reconciling Galera", "Galera.Namespace", instance.Namespace, "Galera.Name", name)
op, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), galera, func() error {
spec.DeepCopyInto(&galera.Spec)
if galera.Spec.Secret == "" {
Expand All @@ -123,7 +124,7 @@ func reconcileGalera(
return galeraFailed, err
}
if op != controllerutil.OperationResultNone {
helper.GetLogger().Info(fmt.Sprintf("Galera %s - %s", galera.Name, op))
l.Info(fmt.Sprintf("Galera %s - %s", galera.Name, op))
}

if galera.IsReady() {
Expand Down
6 changes: 4 additions & 2 deletions pkg/openstack/glance.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func ReconcileGlance(ctx context.Context, instance *corev1beta1.OpenStackControl
},
}

l := GetLog(ctx)

if !instance.Spec.Glance.Enabled {
if res, err := EnsureDeleted(ctx, helper, glance); err != nil {
return res, err
Expand All @@ -32,7 +34,7 @@ func ReconcileGlance(ctx context.Context, instance *corev1beta1.OpenStackControl
return ctrl.Result{}, nil
}

helper.GetLogger().Info("Reconciling Glance", "Glance.Namespace", instance.Namespace, "Glance.Name", "glance")
l.Info("Reconciling Glance", "Glance.Namespace", instance.Namespace, "Glance.Name", "glance")
op, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), glance, func() error {
instance.Spec.Glance.Template.DeepCopyInto(&glance.Spec)
if glance.Spec.Secret == "" {
Expand Down Expand Up @@ -76,7 +78,7 @@ func ReconcileGlance(ctx context.Context, instance *corev1beta1.OpenStackControl
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
helper.GetLogger().Info(fmt.Sprintf("glance %s - %s", glance.Name, op))
l.Info(fmt.Sprintf("glance %s - %s", glance.Name, op))
}

if glance.IsReady() {
Expand Down
7 changes: 5 additions & 2 deletions pkg/openstack/heat.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ func ReconcileHeat(ctx context.Context, instance *corev1beta1.OpenStackControlPl
instance.Status.Conditions.Remove(corev1beta1.OpenStackControlPlaneHeatReadyCondition)
return ctrl.Result{}, nil
}
helper.GetLogger().Info("Reconcile heat", "heat.Namespace", instance.Namespace, "heat.Name", "heat")

l := GetLog(ctx)

l.Info("Reconcile heat", "heat.Namespace", instance.Namespace, "heat.Name", "heat")
op, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), heat, func() error {
instance.Spec.Heat.Template.DeepCopyInto(&heat.Spec)
err := controllerutil.SetControllerReference(helper.GetBeforeObject(), heat, helper.GetScheme())
Expand All @@ -51,7 +54,7 @@ func ReconcileHeat(ctx context.Context, instance *corev1beta1.OpenStackControlPl
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
helper.GetLogger().Info(fmt.Sprintf("heat %s - %s", heat.Name, op))
l.Info(fmt.Sprintf("heat %s - %s", heat.Name, op))
}

if heat.IsReady() {
Expand Down
5 changes: 3 additions & 2 deletions pkg/openstack/horizon.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func ReconcileHorizon(ctx context.Context, instance *corev1beta1.OpenStackContro
Namespace: instance.Namespace,
},
}
l := GetLog(ctx)

if !instance.Spec.Horizon.Enabled {
if res, err := EnsureDeleted(ctx, helper, horizon); err != nil {
Expand All @@ -31,7 +32,7 @@ func ReconcileHorizon(ctx context.Context, instance *corev1beta1.OpenStackContro
instance.Status.Conditions.Remove(corev1beta1.OpenStackControlPlaneHorizonReadyCondition)
return ctrl.Result{}, nil
}
helper.GetLogger().Info("Reconcile Horizon", "horizon.Namespace", instance.Namespace, "horizon.Name", "horizon")
l.Info("Reconcile Horizon", "horizon.Namespace", instance.Namespace, "horizon.Name", "horizon")
op, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), horizon, func() error {
instance.Spec.Horizon.Template.DeepCopyInto(&horizon.Spec)
err := controllerutil.SetControllerReference(helper.GetBeforeObject(), horizon, helper.GetScheme())
Expand All @@ -51,7 +52,7 @@ func ReconcileHorizon(ctx context.Context, instance *corev1beta1.OpenStackContro
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
helper.GetLogger().Info(fmt.Sprintf("Horizon %s - %s", horizon.Name, op))
l.Info(fmt.Sprintf("Horizon %s - %s", horizon.Name, op))
}

if horizon.IsReady() {
Expand Down
5 changes: 3 additions & 2 deletions pkg/openstack/ironic.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func ReconcileIronic(ctx context.Context, instance *corev1beta1.OpenStackControl
Namespace: instance.Namespace,
},
}
l := GetLog(ctx)

if !instance.Spec.Ironic.Enabled {
if res, err := EnsureDeleted(ctx, helper, ironic); err != nil {
Expand All @@ -32,7 +33,7 @@ func ReconcileIronic(ctx context.Context, instance *corev1beta1.OpenStackControl
return ctrl.Result{}, nil
}

helper.GetLogger().Info("Reconciling Ironic", "Ironic.Namespace", instance.Namespace, "Ironic.Name", "ironic")
l.Info("Reconciling Ironic", "Ironic.Namespace", instance.Namespace, "Ironic.Name", "ironic")
op, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), ironic, func() error {
instance.Spec.Ironic.Template.DeepCopyInto(&ironic.Spec)
err := controllerutil.SetControllerReference(helper.GetBeforeObject(), ironic, helper.GetScheme())
Expand All @@ -52,7 +53,7 @@ func ReconcileIronic(ctx context.Context, instance *corev1beta1.OpenStackControl
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
helper.GetLogger().Info(fmt.Sprintf("ironic %s - %s", ironic.Name, op))
l.Info(fmt.Sprintf("ironic %s - %s", ironic.Name, op))
}

if ironic.IsReady() {
Expand Down
6 changes: 4 additions & 2 deletions pkg/openstack/keystone.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func ReconcileKeystoneAPI(ctx context.Context, instance *corev1beta1.OpenStackCo
},
}

l := GetLog(ctx)

if !instance.Spec.Keystone.Enabled {
if res, err := EnsureDeleted(ctx, helper, keystoneAPI); err != nil {
return res, err
Expand All @@ -32,7 +34,7 @@ func ReconcileKeystoneAPI(ctx context.Context, instance *corev1beta1.OpenStackCo
return ctrl.Result{}, nil
}

helper.GetLogger().Info("Reconciling KeystoneAPI", "KeystoneAPI.Namespace", instance.Namespace, "KeystoneAPI.Name", "keystone")
l.Info("Reconciling KeystoneAPI", "KeystoneAPI.Namespace", instance.Namespace, "KeystoneAPI.Name", "keystone")
op, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), keystoneAPI, func() error {
instance.Spec.Keystone.Template.DeepCopyInto(&keystoneAPI.Spec)
if keystoneAPI.Spec.Secret == "" {
Expand Down Expand Up @@ -62,7 +64,7 @@ func ReconcileKeystoneAPI(ctx context.Context, instance *corev1beta1.OpenStackCo
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
helper.GetLogger().Info(fmt.Sprintf("KeystoneAPI %s - %s", keystoneAPI.Name, op))
l.Info(fmt.Sprintf("KeystoneAPI %s - %s", keystoneAPI.Name, op))
}

if keystoneAPI.IsReady() {
Expand Down
5 changes: 3 additions & 2 deletions pkg/openstack/manila.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func ReconcileManila(ctx context.Context, instance *corev1beta1.OpenStackControl
Namespace: instance.Namespace,
},
}
l := GetLog(ctx)

if !instance.Spec.Manila.Enabled {
if res, err := EnsureDeleted(ctx, helper, manila); err != nil {
Expand All @@ -32,7 +33,7 @@ func ReconcileManila(ctx context.Context, instance *corev1beta1.OpenStackControl
return ctrl.Result{}, nil
}

helper.GetLogger().Info("Reconciling Manila", "Manila.Namespace", instance.Namespace, "Manila.Name", "manila")
l.Info("Reconciling Manila", "Manila.Namespace", instance.Namespace, "Manila.Name", "manila")
op, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), manila, func() error {
instance.Spec.Manila.Template.DeepCopyInto(&manila.Spec)
if manila.Spec.Secret == "" {
Expand Down Expand Up @@ -77,7 +78,7 @@ func ReconcileManila(ctx context.Context, instance *corev1beta1.OpenStackControl
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
helper.GetLogger().Info(fmt.Sprintf("Manila %s - %s", manila.Name, op))
l.Info(fmt.Sprintf("Manila %s - %s", manila.Name, op))
}

if manila.IsReady() {
Expand Down
6 changes: 4 additions & 2 deletions pkg/openstack/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func reconcileMariaDB(
},
}

l := GetLog(ctx)

if !instance.Spec.Mariadb.Enabled {
if _, err := EnsureDeleted(ctx, helper, mariadb); err != nil {
return mariadbFailed, err
Expand All @@ -102,7 +104,7 @@ func reconcileMariaDB(
return mariadbReady, nil
}

helper.GetLogger().Info("Reconciling MariaDB", "MariaDB.Namespace", instance.Namespace, "Mariadb.Name", name)
l.Info("Reconciling MariaDB", "MariaDB.Namespace", instance.Namespace, "Mariadb.Name", name)
op, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), mariadb, func() error {
spec.DeepCopyInto(&mariadb.Spec)
if mariadb.Spec.Secret == "" {
Expand All @@ -123,7 +125,7 @@ func reconcileMariaDB(
return mariadbFailed, err
}
if op != controllerutil.OperationResultNone {
helper.GetLogger().Info(fmt.Sprintf("MariaDB %s - %s", mariadb.Name, op))
l.Info(fmt.Sprintf("MariaDB %s - %s", mariadb.Name, op))
}

if mariadb.IsReady() {
Expand Down
6 changes: 4 additions & 2 deletions pkg/openstack/memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ func reconcileMemcached(
},
}

l := GetLog(ctx)

if !instance.Spec.Memcached.Enabled {
if _, err := EnsureDeleted(ctx, helper, memcached); err != nil {
return memcachedFailed, err
Expand All @@ -146,7 +148,7 @@ func reconcileMemcached(
return memcachedReady, nil
}

helper.GetLogger().Info("Reconciling Memcached", "Memcached.Namespace", instance.Namespace, "Memcached.Name", name)
l.Info("Reconciling Memcached", "Memcached.Namespace", instance.Namespace, "Memcached.Name", name)
op, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), memcached, func() error {
spec.DeepCopyInto(&memcached.Spec)
err := controllerutil.SetControllerReference(helper.GetBeforeObject(), memcached, helper.GetScheme())
Expand All @@ -161,7 +163,7 @@ func reconcileMemcached(
return memcachedFailed, err
}
if op != controllerutil.OperationResultNone {
helper.GetLogger().Info(fmt.Sprintf("Memcached %s - %s", memcached.Name, op))
l.Info(fmt.Sprintf("Memcached %s - %s", memcached.Name, op))
}

if memcached.IsReady() {
Expand Down
Loading

0 comments on commit 54e05d1

Please sign in to comment.