-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This automatically adds additional Log fields like reconcile_id etc.. 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 fix non needed fmt import update logging usage update logging naming update main.go logging update logging fixup
- Loading branch information
1 parent
8c479ca
commit 46df8c0
Showing
24 changed files
with
108 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,6 @@ package core | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
certmgrv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1" | ||
routev1 "github.com/openshift/api/route/v1" | ||
|
@@ -50,22 +49,25 @@ import ( | |
telemetryv1 "github.com/openstack-k8s-operators/telemetry-operator/api/v1beta1" | ||
rabbitmqv2 "github.com/rabbitmq/cluster-operator/v2/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 | ||
type OpenStackControlPlaneReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
Kclient kubernetes.Interface | ||
Log logr.Logger | ||
} | ||
|
||
// GetLog returns a logger object with a prefix of "conroller.name" and aditional controller context fields | ||
func (r *OpenStackControlPlaneReconciler) GetLogger(ctx context.Context) logr.Logger { | ||
return log.FromContext(ctx).WithName("Controllers").WithName("OpenStackControlPlane") | ||
} | ||
|
||
//+kubebuilder:rbac:groups=core.openstack.org,resources=openstackcontrolplanes,verbs=get;list;watch;create;update;patch;delete | ||
|
@@ -107,8 +109,8 @@ 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) | ||
|
||
Log := r.GetLogger(ctx) | ||
// Fetch the OpenStackControlPlane instance | ||
instance := &corev1beta1.OpenStackControlPlane{} | ||
err := r.Client.Get(ctx, req.NamespacedName, instance) | ||
|
@@ -117,7 +119,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.") | ||
Log.Info("OpenStackControlPlane instance is not found, probaby deleted. Nothing to do.") | ||
return ctrl.Result{}, nil | ||
} | ||
// Error reading the object - requeue the request. | ||
|
@@ -129,11 +131,11 @@ func (r *OpenStackControlPlaneReconciler) Reconcile(ctx context.Context, req ctr | |
r.Client, | ||
r.Kclient, | ||
r.Scheme, | ||
r.Log, | ||
Log, | ||
) | ||
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)) | ||
Log.Error(err, "unable to acquire helper for OpenStackControlPlane") | ||
return ctrl.Result{}, err | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.