Skip to content

Commit

Permalink
remove logger
Browse files Browse the repository at this point in the history
Signed-off-by: drivebyer <[email protected]>
  • Loading branch information
drivebyer committed Nov 15, 2024
1 parent 95b5c57 commit 8df44c3
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 45 deletions.
12 changes: 3 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions pkg/controllers/redis/redis_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions pkg/controllers/rediscluster/rediscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -42,7 +41,6 @@ type Reconciler struct {
k8sutils.StatefulSet
K8sClient kubernetes.Interface
Dk8sClient dynamic.Interface
Log logr.Logger
Scheme *runtime.Scheme
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
4 changes: 0 additions & 4 deletions pkg/controllers/redissentinel/redissentinel_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
11 changes: 4 additions & 7 deletions pkg/k8sutils/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
}
}

Expand All @@ -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 {
Expand All @@ -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
}
4 changes: 0 additions & 4 deletions pkg/k8sutils/redis-sentinel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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{},
Expand All @@ -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{
Expand Down
6 changes: 1 addition & 5 deletions pkg/k8sutils/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
}
}

Expand Down

0 comments on commit 8df44c3

Please sign in to comment.