Skip to content

Commit

Permalink
controllers: switch to k8s contextual logger
Browse files Browse the repository at this point in the history
Jira: https://issues.redhat.com/browse/RHOAIENG-14714

This is a squashed commit of the following patches:

d012b67 ("controllers: switch to k8s contextual logger")
9880530 ("logger: blindly convert ctrl.Log users to contextual")

- controllers: switch to k8s contextual logger

Remove own logger from controllers' reconcilers and switch to k8s
contextual logger instead [1].

Use contextual logger for SecretGeneratorReconciler and
CertConfigmapGeneratorReconciler setups as well.

Add name to the logger coming from the framework. It will contains
"controller" field already, and like in webhook with the name it's
easy to distinguish framework and operator messages.

- logger: blindly convert ctrl.Log users to contextual

All the users should have proper context now. The log level changes
will affect it as well.

[1] https://www.kubernetes.dev/blog/2022/05/25/contextual-logging/

Signed-off-by: Yauheni Kaliuta <[email protected]>
  • Loading branch information
ykaliuta committed Oct 18, 2024
1 parent 12fd9e5 commit 5e678ae
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 81 deletions.
5 changes: 3 additions & 2 deletions components/kserve/servicemesh_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

operatorv1 "github.com/openshift/api/operator/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"

dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
Expand Down Expand Up @@ -37,6 +37,7 @@ func (k *Kserve) removeServiceMeshConfigurations(ctx context.Context, cli client
}

func (k *Kserve) defineServiceMeshFeatures(ctx context.Context, cli client.Client, dscispec *dsciv1.DSCInitializationSpec) feature.FeaturesProvider {
log := logf.FromContext(ctx)
return func(registry feature.FeaturesRegistry) error {
authorinoInstalled, err := cluster.SubscriptionExists(ctx, cli, "authorino-operator")
if err != nil {
Expand Down Expand Up @@ -68,7 +69,7 @@ func (k *Kserve) defineServiceMeshFeatures(ctx context.Context, cli client.Clien
return kserveExtAuthzErr
}
} else {
ctrl.Log.Info("WARN: Authorino operator is not installed on the cluster, skipping authorization capability")
log.Info("WARN: Authorino operator is not installed on the cluster, skipping authorization capability")
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"reflect"

"github.com/go-logr/logr"
operatorv1 "github.com/openshift/api/operator/v1"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand All @@ -16,6 +15,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

Expand All @@ -28,13 +28,11 @@ import (
type CertConfigmapGeneratorReconciler struct {
Client client.Client
Scheme *runtime.Scheme
Log logr.Logger
}

// SetupWithManager sets up the controller with the Manager.
func (r *CertConfigmapGeneratorReconciler) SetupWithManager(mgr ctrl.Manager) error {
log := r.Log
log.Info("Adding controller for Configmap Generation.")
func (r *CertConfigmapGeneratorReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
logf.FromContext(ctx).Info("Adding controller for Configmap Generation.")
return ctrl.NewControllerManagedBy(mgr).
Named("cert-configmap-generator-controller").
Watches(&corev1.ConfigMap{}, handler.EnqueueRequestsFromMapFunc(r.watchTrustedCABundleConfigMapResource), builder.WithPredicates(ConfigMapChangedPredicate)).
Expand All @@ -45,7 +43,7 @@ func (r *CertConfigmapGeneratorReconciler) SetupWithManager(mgr ctrl.Manager) er
// Reconcile will generate new configmap, odh-trusted-ca-bundle, that includes cluster-wide trusted-ca bundle and custom
// ca bundle in every new namespace created.
func (r *CertConfigmapGeneratorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log
log := logf.FromContext(ctx).WithName("CertConfigmapGenerator")
// Request includes namespace that is newly created or where odh-trusted-ca-bundle configmap is updated.
log.Info("Reconciling CertConfigMapGenerator.", " Request.Namespace", req.NamespacedName)
// Get namespace instance
Expand Down Expand Up @@ -109,8 +107,8 @@ func (r *CertConfigmapGeneratorReconciler) watchNamespaceResource(_ context.Cont
return nil
}

func (r *CertConfigmapGeneratorReconciler) watchTrustedCABundleConfigMapResource(_ context.Context, a client.Object) []reconcile.Request {
log := r.Log
func (r *CertConfigmapGeneratorReconciler) watchTrustedCABundleConfigMapResource(ctx context.Context, a client.Object) []reconcile.Request {
log := logf.FromContext(ctx)
if a.GetName() == trustedcabundle.CAConfigMapName {
log.Info("Cert configmap has been updated, start reconcile")
return []reconcile.Request{{NamespacedName: types.NamespacedName{Name: a.GetName(), Namespace: a.GetNamespace()}}}
Expand Down
20 changes: 9 additions & 11 deletions controllers/datasciencecluster/datasciencecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ import (
type DataScienceClusterReconciler struct {
client.Client
Scheme *runtime.Scheme
Log logr.Logger
// Recorder to generate events
Recorder record.EventRecorder
DataScienceCluster *DataScienceClusterConfig
Expand All @@ -85,7 +84,7 @@ const (
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
func (r *DataScienceClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { //nolint:maintidx,gocyclo
log := r.Log
log := logf.FromContext(ctx).WithName("DataScienceCluster")
log.Info("Reconciling DataScienceCluster resources", "Request.Name", req.Name)

// Get information on version and platform
Expand Down Expand Up @@ -169,7 +168,7 @@ func (r *DataScienceClusterReconciler) Reconcile(ctx context.Context, req ctrl.R
saved.Status.Phase = status.PhaseError
})
if err != nil {
r.reportError(err, instance, "failed to update DataScienceCluster condition")
r.reportError(ctx, err, instance, "failed to update DataScienceCluster condition")

return ctrl.Result{}, err
}
Expand Down Expand Up @@ -233,7 +232,7 @@ func (r *DataScienceClusterReconciler) Reconcile(ctx context.Context, req ctrl.R
saved.Status.Release = currentOperatorRelease
})
if err != nil {
_ = r.reportError(err, instance, fmt.Sprintf("failed to add conditions to status of DataScienceCluster resource name %s", req.Name))
_ = r.reportError(ctx, err, instance, fmt.Sprintf("failed to add conditions to status of DataScienceCluster resource name %s", req.Name))

return ctrl.Result{}, err
}
Expand Down Expand Up @@ -291,7 +290,7 @@ func (r *DataScienceClusterReconciler) Reconcile(ctx context.Context, req ctrl.R
func (r *DataScienceClusterReconciler) reconcileSubComponent(ctx context.Context, instance *dscv1.DataScienceCluster,
platform cluster.Platform, component components.ComponentInterface,
) (*dscv1.DataScienceCluster, error) {
log := r.Log
log := logf.FromContext(ctx)
componentName := component.GetComponentName()

enabled := component.GetManagementState() == operatorv1.Managed
Expand All @@ -308,7 +307,7 @@ func (r *DataScienceClusterReconciler) reconcileSubComponent(ctx context.Context
status.SetComponentCondition(&saved.Status.Conditions, componentName, status.ReconcileInit, message, corev1.ConditionUnknown)
})
if err != nil {
_ = r.reportError(err, instance, "failed to update DataScienceCluster conditions before first time reconciling "+componentName)
_ = r.reportError(ctx, err, instance, "failed to update DataScienceCluster conditions before first time reconciling "+componentName)
// try to continue with reconciliation, as further updates can fix the status
}
}
Expand All @@ -321,7 +320,7 @@ func (r *DataScienceClusterReconciler) reconcileSubComponent(ctx context.Context

if err != nil {
// reconciliation failed: log errors, raise event and update status accordingly
instance = r.reportError(err, instance, "failed to reconcile "+componentName+" on DataScienceCluster")
instance = r.reportError(ctx, err, instance, "failed to reconcile "+componentName+" on DataScienceCluster")
instance, _ = status.UpdateWithRetry(ctx, r.Client, instance, func(saved *dscv1.DataScienceCluster) {
if enabled {
if strings.Contains(err.Error(), datasciencepipelines.ArgoWorkflowCRD+" CRD already exists") {
Expand Down Expand Up @@ -357,7 +356,7 @@ func (r *DataScienceClusterReconciler) reconcileSubComponent(ctx context.Context
}
})
if err != nil {
instance = r.reportError(err, instance, "failed to update DataScienceCluster status after reconciling "+componentName)
instance = r.reportError(ctx, err, instance, "failed to update DataScienceCluster status after reconciling "+componentName)

return instance, err
}
Expand All @@ -374,9 +373,8 @@ func newComponentLogger(logger logr.Logger, componentName string, dscispec *dsci
return ctrlogger.NewNamedLogger(logger, "DSC.Components."+componentName, mode)
}

func (r *DataScienceClusterReconciler) reportError(err error, instance *dscv1.DataScienceCluster, message string) *dscv1.DataScienceCluster {
log := r.Log
log.Error(err, message, "instance.Name", instance.Name)
func (r *DataScienceClusterReconciler) reportError(ctx context.Context, err error, instance *dscv1.DataScienceCluster, message string) *dscv1.DataScienceCluster {
logf.FromContext(ctx).Error(err, message, "instance.Name", instance.Name)
r.Recorder.Eventf(instance, corev1.EventTypeWarning, "DataScienceClusterReconcileError",
"%s for instance %s", message, instance.Name)
return instance
Expand Down
15 changes: 7 additions & 8 deletions controllers/dscinitialization/dscinitialization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"path/filepath"
"reflect"

"github.com/go-logr/logr"
operatorv1 "github.com/openshift/api/operator/v1"
routev1 "github.com/openshift/api/route/v1"
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -40,6 +39,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

Expand All @@ -64,7 +64,6 @@ var managementStateChangeTrustedCA = false
type DSCInitializationReconciler struct {
client.Client
Scheme *runtime.Scheme
Log logr.Logger
Recorder record.EventRecorder
ApplicationsNamespace string
}
Expand All @@ -78,7 +77,7 @@ type DSCInitializationReconciler struct {

// Reconcile contains controller logic specific to DSCInitialization instance updates.
func (r *DSCInitializationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { //nolint:funlen,gocyclo,maintidx
log := r.Log
log := logf.FromContext(ctx).WithName("DSCInitialization")
log.Info("Reconciling DSCInitialization.", "DSCInitialization Request.Name", req.Name)

currentOperatorRelease := cluster.GetRelease()
Expand Down Expand Up @@ -392,8 +391,8 @@ var dsciPredicateStateChangeTrustedCA = predicate.Funcs{
},
}

func (r *DSCInitializationReconciler) watchMonitoringConfigMapResource(_ context.Context, a client.Object) []reconcile.Request {
log := r.Log
func (r *DSCInitializationReconciler) watchMonitoringConfigMapResource(ctx context.Context, a client.Object) []reconcile.Request {
log := logf.FromContext(ctx)
if a.GetName() == "prometheus" && a.GetNamespace() == "redhat-ods-monitoring" {
log.Info("Found monitoring configmap has updated, start reconcile")

Expand All @@ -402,8 +401,8 @@ func (r *DSCInitializationReconciler) watchMonitoringConfigMapResource(_ context
return nil
}

func (r *DSCInitializationReconciler) watchMonitoringSecretResource(_ context.Context, a client.Object) []reconcile.Request {
log := r.Log
func (r *DSCInitializationReconciler) watchMonitoringSecretResource(ctx context.Context, a client.Object) []reconcile.Request {
log := logf.FromContext(ctx)
operatorNs, err := cluster.GetOperatorNamespace()
if err != nil {
return nil
Expand All @@ -418,7 +417,7 @@ func (r *DSCInitializationReconciler) watchMonitoringSecretResource(_ context.Co
}

func (r *DSCInitializationReconciler) watchDSCResource(ctx context.Context) []reconcile.Request {
log := r.Log
log := logf.FromContext(ctx)
instanceList := &dscv1.DataScienceClusterList{}
if err := r.Client.List(ctx, instanceList); err != nil {
// do not handle if cannot get list
Expand Down
13 changes: 7 additions & 6 deletions controllers/dscinitialization/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"

dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
Expand All @@ -39,7 +40,7 @@ var (
// only when reconcile on DSCI CR, initial set to true
// if reconcile from monitoring, initial set to false, skip blackbox and rolebinding.
func (r *DSCInitializationReconciler) configureManagedMonitoring(ctx context.Context, dscInit *dsciv1.DSCInitialization, initial string) error {
log := r.Log
log := logf.FromContext(ctx)
if initial == "init" {
// configure Blackbox exporter
if err := configureBlackboxExporter(ctx, dscInit, r); err != nil {
Expand Down Expand Up @@ -89,7 +90,7 @@ func (r *DSCInitializationReconciler) configureManagedMonitoring(ctx context.Con
}

func configureAlertManager(ctx context.Context, dsciInit *dsciv1.DSCInitialization, r *DSCInitializationReconciler) error {
log := r.Log
log := logf.FromContext(ctx)
// Get Deadmansnitch secret
deadmansnitchSecret, err := r.waitForManagedSecret(ctx, "redhat-rhods-deadmanssnitch", dsciInit.Spec.Monitoring.Namespace)
if err != nil {
Expand Down Expand Up @@ -200,7 +201,7 @@ func configureAlertManager(ctx context.Context, dsciInit *dsciv1.DSCInitializati
}

func configurePrometheus(ctx context.Context, dsciInit *dsciv1.DSCInitialization, r *DSCInitializationReconciler) error {
log := r.Log
log := logf.FromContext(ctx)
// Update rolebinding-viewer
err := common.ReplaceStringsInFile(filepath.Join(prometheusManifestsPath, "prometheus-rolebinding-viewer.yaml"),
map[string]string{
Expand Down Expand Up @@ -348,7 +349,7 @@ func configurePrometheus(ctx context.Context, dsciInit *dsciv1.DSCInitialization
}

func configureBlackboxExporter(ctx context.Context, dsciInit *dsciv1.DSCInitialization, r *DSCInitializationReconciler) error {
log := r.Log
log := logf.FromContext(ctx)
consoleRoute := &routev1.Route{}
err := r.Client.Get(ctx, client.ObjectKey{Name: "console", Namespace: "openshift-console"}, consoleRoute)
if err != nil {
Expand Down Expand Up @@ -438,7 +439,7 @@ func createMonitoringProxySecret(ctx context.Context, cli client.Client, name st
}

func (r *DSCInitializationReconciler) configureSegmentIO(ctx context.Context, dsciInit *dsciv1.DSCInitialization) error {
log := r.Log
log := logf.FromContext(ctx)
// create segment.io only when configmap does not exist in the cluster
segmentioConfigMap := &corev1.ConfigMap{}
if err := r.Client.Get(ctx, client.ObjectKey{
Expand Down Expand Up @@ -467,7 +468,7 @@ func (r *DSCInitializationReconciler) configureSegmentIO(ctx context.Context, ds
}

func (r *DSCInitializationReconciler) configureCommonMonitoring(ctx context.Context, dsciInit *dsciv1.DSCInitialization) error {
log := r.Log
log := logf.FromContext(ctx)
if err := r.configureSegmentIO(ctx, dsciInit); err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions controllers/dscinitialization/servicemesh_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
conditionsv1 "github.com/openshift/custom-resource-status/conditions/v1"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"

dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/controllers/status"
Expand All @@ -19,7 +20,7 @@ import (
)

func (r *DSCInitializationReconciler) configureServiceMesh(ctx context.Context, instance *dsciv1.DSCInitialization) error {
log := r.Log
log := logf.FromContext(ctx)
serviceMeshManagementState := operatorv1.Removed
if instance.Spec.ServiceMesh != nil {
serviceMeshManagementState = instance.Spec.ServiceMesh.ManagementState
Expand Down Expand Up @@ -62,7 +63,7 @@ func (r *DSCInitializationReconciler) configureServiceMesh(ctx context.Context,
}

func (r *DSCInitializationReconciler) removeServiceMesh(ctx context.Context, instance *dsciv1.DSCInitialization) error {
log := r.Log
log := logf.FromContext(ctx)
// on condition of Managed, do not handle Removed when set to Removed it trigger DSCI reconcile to clean up
if instance.Spec.ServiceMesh == nil {
return nil
Expand Down
1 change: 0 additions & 1 deletion controllers/dscinitialization/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ var _ = BeforeSuite(func() {
err = (&dscictrl.DSCInitializationReconciler{
Client: k8sClient,
Scheme: testScheme,
Log: ctrl.Log.WithName("controllers").WithName("DSCInitialization"),
Recorder: mgr.GetEventRecorderFor("dscinitialization-controller"),
}).SetupWithManager(gCtx, mgr)

Expand Down
9 changes: 5 additions & 4 deletions controllers/dscinitialization/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"k8s.io/client-go/util/retry"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"

dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
Expand All @@ -37,7 +38,7 @@ var (
// - Network Policies 'opendatahub' that allow traffic between the ODH namespaces
// - RoleBinding 'opendatahub'.
func (r *DSCInitializationReconciler) createOdhNamespace(ctx context.Context, dscInit *dsciv1.DSCInitialization, name string, platform cluster.Platform) error {
log := r.Log
log := logf.FromContext(ctx)
// Expected application namespace for the given name
desiredNamespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -142,7 +143,7 @@ func (r *DSCInitializationReconciler) createOdhNamespace(ctx context.Context, ds
}

func (r *DSCInitializationReconciler) createDefaultRoleBinding(ctx context.Context, name string, dscInit *dsciv1.DSCInitialization) error {
log := r.Log
log := logf.FromContext(ctx)
// Expected namespace for the given name
desiredRoleBinding := &rbacv1.RoleBinding{
TypeMeta: metav1.TypeMeta{
Expand Down Expand Up @@ -190,7 +191,7 @@ func (r *DSCInitializationReconciler) createDefaultRoleBinding(ctx context.Conte
}

func (r *DSCInitializationReconciler) reconcileDefaultNetworkPolicy(ctx context.Context, name string, dscInit *dsciv1.DSCInitialization, platform cluster.Platform) error {
log := r.Log
log := logf.FromContext(ctx)
if platform == cluster.ManagedRhods || platform == cluster.SelfManagedRhods {
// Get operator namepsace
operatorNs, err := cluster.GetOperatorNamespace()
Expand Down Expand Up @@ -375,7 +376,7 @@ func GenerateRandomHex(length int) ([]byte, error) {
}

func (r *DSCInitializationReconciler) createOdhCommonConfigMap(ctx context.Context, name string, dscInit *dsciv1.DSCInitialization) error {
log := r.Log
log := logf.FromContext(ctx)
// Expected configmap for the given namespace
desiredConfigMap := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Expand Down
Loading

0 comments on commit 5e678ae

Please sign in to comment.