diff --git a/main.go b/main.go index 7d761d59d..576d16fbf 100644 --- a/main.go +++ b/main.go @@ -122,33 +122,28 @@ func main() { Client: mgr.GetClient(), K8sClient: k8sclient, Dk8sClient: dk8sClient, - Log: ctrl.Log.WithName("controllers").WithName("Redis"), Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Redis") os.Exit(1) } - rcLog := ctrl.Log.WithName("controllers").WithName("RedisCluster") if err = (&rediscluster.Reconciler{ Client: mgr.GetClient(), K8sClient: k8sclient, Dk8sClient: dk8sClient, - Log: rcLog, Scheme: mgr.GetScheme(), - StatefulSet: k8sutils.NewStatefulSetService(k8sclient, rcLog), + StatefulSet: k8sutils.NewStatefulSetService(k8sclient), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "RedisCluster") os.Exit(1) } - rrLog := ctrl.Log.WithName("controllers").WithName("RedisReplication") if err = (&redisreplication.Reconciler{ Client: mgr.GetClient(), K8sClient: k8sclient, Dk8sClient: dk8sClient, - Log: rrLog, Scheme: mgr.GetScheme(), - Pod: k8sutils.NewPodService(k8sclient, rrLog), - StatefulSet: k8sutils.NewStatefulSetService(k8sclient, rrLog), + Pod: k8sutils.NewPodService(k8sclient), + StatefulSet: k8sutils.NewStatefulSetService(k8sclient), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "RedisReplication") os.Exit(1) @@ -157,7 +152,6 @@ func main() { Client: mgr.GetClient(), K8sClient: k8sclient, Dk8sClient: dk8sClient, - Log: ctrl.Log.WithName("controllers").WithName("RedisSentinel"), Scheme: mgr.GetScheme(), ReplicationWatcher: intctrlutil.NewResourceWatcher(), }).SetupWithManager(mgr); err != nil { diff --git a/pkg/controllers/redis/redis_controller.go b/pkg/controllers/redis/redis_controller.go index 531261164..061f33e4c 100644 --- a/pkg/controllers/redis/redis_controller.go +++ b/pkg/controllers/redis/redis_controller.go @@ -23,7 +23,6 @@ import ( redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2" intctrlutil "github.com/OT-CONTAINER-KIT/redis-operator/pkg/controllerutil" "github.com/OT-CONTAINER-KIT/redis-operator/pkg/k8sutils" - "github.com/go-logr/logr" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" @@ -36,13 +35,10 @@ type Reconciler struct { client.Client K8sClient kubernetes.Interface Dk8sClient dynamic.Interface - Log logr.Logger Scheme *runtime.Scheme } func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - reqLogger := r.Log.WithValues("Request.Namespace", req.Namespace, "Request.Name", req.Name) - reqLogger.Info("Reconciling opstree redis controller") instance := &redisv1beta2.Redis{} err := r.Client.Get(context.TODO(), req.NamespacedName, instance) diff --git a/pkg/controllers/rediscluster/rediscluster_controller.go b/pkg/controllers/rediscluster/rediscluster_controller.go index ce81f0a9b..d1d53d936 100644 --- a/pkg/controllers/rediscluster/rediscluster_controller.go +++ b/pkg/controllers/rediscluster/rediscluster_controller.go @@ -27,7 +27,6 @@ import ( intctrlutil "github.com/OT-CONTAINER-KIT/redis-operator/pkg/controllerutil" "github.com/OT-CONTAINER-KIT/redis-operator/pkg/k8sutils" retry "github.com/avast/retry-go" - "github.com/go-logr/logr" appsv1 "k8s.io/api/apps/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/dynamic" @@ -42,7 +41,6 @@ type Reconciler struct { k8sutils.StatefulSet K8sClient kubernetes.Interface Dk8sClient dynamic.Interface - Log logr.Logger Scheme *runtime.Scheme } diff --git a/pkg/controllers/rediscluster/rediscluster_controller_suite_test.go b/pkg/controllers/rediscluster/rediscluster_controller_suite_test.go index 4b09ff422..8cfe65206 100644 --- a/pkg/controllers/rediscluster/rediscluster_controller_suite_test.go +++ b/pkg/controllers/rediscluster/rediscluster_controller_suite_test.go @@ -100,13 +100,12 @@ var _ = BeforeSuite(func() { dk8sClient, err := dynamic.NewForConfig(cfg) Expect(err).ToNot(HaveOccurred()) - rcLog := ctrl.Log.WithName("controllers").WithName("RedisCluster") err = (&Reconciler{ Client: k8sManager.GetClient(), K8sClient: k8sClient, Dk8sClient: dk8sClient, Scheme: k8sManager.GetScheme(), - StatefulSet: k8sutils.NewStatefulSetService(k8sClient, rcLog), + StatefulSet: k8sutils.NewStatefulSetService(k8sClient), }).SetupWithManager(k8sManager) Expect(err).ToNot(HaveOccurred()) diff --git a/pkg/controllers/redisreplication/redisreplication_controller.go b/pkg/controllers/redisreplication/redisreplication_controller.go index 4410f021a..544d69595 100644 --- a/pkg/controllers/redisreplication/redisreplication_controller.go +++ b/pkg/controllers/redisreplication/redisreplication_controller.go @@ -2,12 +2,12 @@ package redisreplication import ( "context" + "sigs.k8s.io/controller-runtime/pkg/log" "time" redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2" intctrlutil "github.com/OT-CONTAINER-KIT/redis-operator/pkg/controllerutil" "github.com/OT-CONTAINER-KIT/redis-operator/pkg/k8sutils" - "github.com/go-logr/logr" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" @@ -24,13 +24,11 @@ type Reconciler struct { k8sutils.StatefulSet K8sClient kubernetes.Interface Dk8sClient dynamic.Interface - Log logr.Logger Scheme *runtime.Scheme } func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - reqLogger := r.Log.WithValues("Request.Namespace", req.Namespace, "Request.Name", req.Name) - reqLogger.Info("Reconciling opstree redis replication controller") + logger := log.FromContext(ctx, "Request.Namespace", req.Namespace, "Request.Name", req.Name) instance := &redisv1beta2.RedisReplication{} err := r.Client.Get(context.TODO(), req.NamespacedName, instance) @@ -64,7 +62,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu var realMaster string masterNodes := k8sutils.GetRedisNodesByRole(ctx, r.K8sClient, instance, "master") if len(masterNodes) > 1 { - reqLogger.Info("Creating redis replication by executing replication creation commands") + logger.Info("Creating redis replication by executing replication creation commands") slaveNodes := k8sutils.GetRedisNodesByRole(ctx, r.K8sClient, instance, "slave") realMaster = k8sutils.GetRedisReplicationRealMaster(ctx, r.K8sClient, instance, masterNodes) if len(slaveNodes) == 0 { diff --git a/pkg/controllers/redisreplication/redisreplication_controller_suite_test.go b/pkg/controllers/redisreplication/redisreplication_controller_suite_test.go index 8ef3a7f5f..445f7ce7d 100644 --- a/pkg/controllers/redisreplication/redisreplication_controller_suite_test.go +++ b/pkg/controllers/redisreplication/redisreplication_controller_suite_test.go @@ -100,14 +100,13 @@ var _ = BeforeSuite(func() { dk8sClient, err := dynamic.NewForConfig(cfg) Expect(err).ToNot(HaveOccurred()) - rrLog := ctrl.Log.WithName("controllers").WithName("RedisReplication") err = (&Reconciler{ Client: k8sManager.GetClient(), K8sClient: k8sClient, Dk8sClient: dk8sClient, Scheme: k8sManager.GetScheme(), - Pod: k8sutils.NewPodService(k8sClient, rrLog), - StatefulSet: k8sutils.NewStatefulSetService(k8sClient, rrLog), + Pod: k8sutils.NewPodService(k8sClient), + StatefulSet: k8sutils.NewStatefulSetService(k8sClient), }).SetupWithManager(k8sManager) Expect(err).ToNot(HaveOccurred()) diff --git a/pkg/controllers/redissentinel/redissentinel_controller.go b/pkg/controllers/redissentinel/redissentinel_controller.go index e33b5647d..420730a4d 100644 --- a/pkg/controllers/redissentinel/redissentinel_controller.go +++ b/pkg/controllers/redissentinel/redissentinel_controller.go @@ -7,7 +7,6 @@ import ( redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2" intctrlutil "github.com/OT-CONTAINER-KIT/redis-operator/pkg/controllerutil" "github.com/OT-CONTAINER-KIT/redis-operator/pkg/k8sutils" - "github.com/go-logr/logr" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/dynamic" @@ -21,15 +20,12 @@ type RedisSentinelReconciler struct { client.Client K8sClient kubernetes.Interface Dk8sClient dynamic.Interface - Log logr.Logger Scheme *runtime.Scheme ReplicationWatcher *intctrlutil.ResourceWatcher } func (r *RedisSentinelReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - reqLogger := r.Log.WithValues("Request.Namespace", req.Namespace, "Request.Name", req.Name) - reqLogger.Info("Reconciling opstree redis controller") instance := &redisv1beta2.RedisSentinel{} err := r.Client.Get(context.TODO(), req.NamespacedName, instance) diff --git a/pkg/k8sutils/pod.go b/pkg/k8sutils/pod.go index e52b131fc..21ff33a2a 100644 --- a/pkg/k8sutils/pod.go +++ b/pkg/k8sutils/pod.go @@ -4,9 +4,9 @@ import ( "context" "encoding/json" "fmt" + "sigs.k8s.io/controller-runtime/pkg/log" "strings" - "github.com/go-logr/logr" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -20,14 +20,11 @@ type Pod interface { type PodService struct { kubeClient kubernetes.Interface - log logr.Logger } -func NewPodService(kubeClient kubernetes.Interface, log logr.Logger) *PodService { - log = log.WithValues("service", "k8s.pod") +func NewPodService(kubeClient kubernetes.Interface) *PodService { return &PodService{ kubeClient: kubeClient, - log: log, } } @@ -48,7 +45,7 @@ type patchStringValue struct { } func (s *PodService) PatchPodLabels(ctx context.Context, namespace, podName string, labels map[string]string) error { - s.log.Info("Patch pod labels", "namespace", namespace, "podName", podName, "labels", labels) + log.FromContext(ctx).Info("Patch pod labels", "namespace", namespace, "podName", podName, "labels", labels) var payloads []interface{} for labelKey, labelValue := range labels { @@ -63,7 +60,7 @@ func (s *PodService) PatchPodLabels(ctx context.Context, namespace, podName stri _, err := s.kubeClient.CoreV1().Pods(namespace).Patch(ctx, podName, types.JSONPatchType, payloadBytes, metav1.PatchOptions{}) if err != nil { - s.log.Error(err, "Patch pod labels failed", "namespace", namespace, "podName", podName) + log.FromContext(ctx).Error(err, "Patch pod labels failed", "namespace", namespace, "podName", podName) } return err } diff --git a/pkg/k8sutils/redis-sentinel_test.go b/pkg/k8sutils/redis-sentinel_test.go index 56e997762..5f2160748 100644 --- a/pkg/k8sutils/redis-sentinel_test.go +++ b/pkg/k8sutils/redis-sentinel_test.go @@ -9,7 +9,6 @@ import ( common "github.com/OT-CONTAINER-KIT/redis-operator/api" redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2" - "github.com/go-logr/logr" "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1" @@ -292,7 +291,6 @@ func Test_generateRedisSentinelInitContainerParams(t *testing.T) { func Test_getSentinelEnvVariable(t *testing.T) { type args struct { client kubernetes.Interface - logger logr.Logger cr *redisv1beta2.RedisSentinel } tests := []struct { @@ -304,7 +302,6 @@ func Test_getSentinelEnvVariable(t *testing.T) { name: "When RedisSentinelConfig is nil", args: args{ client: nil, - logger: logr.Logger{}, cr: &redisv1beta2.RedisSentinel{}, }, want: &[]corev1.EnvVar{}, @@ -313,7 +310,6 @@ func Test_getSentinelEnvVariable(t *testing.T) { name: "When RedisSentinelConfig is not nil", args: args{ client: nil, - logger: logr.Logger{}, cr: &redisv1beta2.RedisSentinel{ Spec: redisv1beta2.RedisSentinelSpec{ RedisSentinelConfig: &redisv1beta2.RedisSentinelConfig{ diff --git a/pkg/k8sutils/statefulset.go b/pkg/k8sutils/statefulset.go index 1eb29765b..5a3939dfb 100644 --- a/pkg/k8sutils/statefulset.go +++ b/pkg/k8sutils/statefulset.go @@ -12,7 +12,6 @@ import ( redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2" "github.com/OT-CONTAINER-KIT/redis-operator/pkg/util" "github.com/banzaicloud/k8s-objectmatcher/patch" - "github.com/go-logr/logr" "github.com/pkg/errors" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -31,14 +30,11 @@ type StatefulSet interface { type StatefulSetService struct { kubeClient kubernetes.Interface - log logr.Logger } -func NewStatefulSetService(kubeClient kubernetes.Interface, log logr.Logger) *StatefulSetService { - log = log.WithValues("service", "k8s.statefulset") +func NewStatefulSetService(kubeClient kubernetes.Interface) *StatefulSetService { return &StatefulSetService{ kubeClient: kubeClient, - log: log, } }