Skip to content

Commit

Permalink
feat: add context param and get logger from it (#1132)
Browse files Browse the repository at this point in the history
* rename

Signed-off-by: drivebyer <yang.wu@daocloud.io>

* add context param

Signed-off-by: drivebyer <yang.wu@daocloud.io>

* remove logger

Signed-off-by: drivebyer <yang.wu@daocloud.io>

* remove logger

Signed-off-by: drivebyer <yang.wu@daocloud.io>

* remove logger

Signed-off-by: drivebyer <yang.wu@daocloud.io>

* lint

Signed-off-by: drivebyer <yang.wu@daocloud.io>

* update

* update

---------

Signed-off-by: drivebyer <yang.wu@daocloud.io>
  • Loading branch information
drivebyer authored Nov 15, 2024
1 parent f72628d commit 3135e0f
Show file tree
Hide file tree
Showing 31 changed files with 632 additions and 750 deletions.
18 changes: 6 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,37 +118,32 @@ func main() {
os.Exit(1)
}

if err = (&redis.RedisReconciler{
if err = (&redis.Reconciler{
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.RedisClusterReconciler{
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.RedisReplicationReconciler{
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
3 changes: 2 additions & 1 deletion mocks/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"context"
"fmt"
"strconv"

Expand Down Expand Up @@ -76,7 +77,7 @@ func CreateFakeObjectWithSecret(name, namespace, key string) []runtime.Object {
return []runtime.Object{secret}
}

func CreateFakeClientWithSecrets(cr *redisv1beta2.RedisCluster, secretName, secretKey, secretValue string) *fake.Clientset {
func CreateFakeClientWithSecrets(ctx context.Context, cr *redisv1beta2.RedisCluster, secretName, secretKey, secretValue string) *fake.Clientset {
leaderReplicas := cr.Spec.GetReplicaCounts("leader")
followerReplicas := cr.Spec.GetReplicaCounts("follower")
pods := make([]runtime.Object, 0)
Expand Down
34 changes: 15 additions & 19 deletions pkg/controllers/redis/redis_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,53 @@ 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"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// RedisReconciler reconciles a Redis object
type RedisReconciler struct {
// Reconciler reconciles a Redis object
type Reconciler struct {
client.Client
K8sClient kubernetes.Interface
Dk8sClient dynamic.Interface
Log logr.Logger
Scheme *runtime.Scheme
}

func (r *RedisReconciler) 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")
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
instance := &redisv1beta2.Redis{}

err := r.Client.Get(context.TODO(), req.NamespacedName, instance)
if err != nil {
return intctrlutil.RequeueWithErrorChecking(err, reqLogger, "failed to get redis instance")
return intctrlutil.RequeueWithErrorChecking(ctx, err, "failed to get redis instance")
}
if instance.ObjectMeta.GetDeletionTimestamp() != nil {
if err = k8sutils.HandleRedisFinalizer(r.Client, r.K8sClient, r.Log, instance); err != nil {
return intctrlutil.RequeueWithError(err, reqLogger, "failed to handle redis finalizer")
if err = k8sutils.HandleRedisFinalizer(ctx, r.Client, r.K8sClient, instance); err != nil {
return intctrlutil.RequeueWithError(ctx, err, "failed to handle redis finalizer")
}
return intctrlutil.Reconciled()
}
if _, found := instance.ObjectMeta.GetAnnotations()["redis.opstreelabs.in/skip-reconcile"]; found {
return intctrlutil.RequeueAfter(reqLogger, time.Second*10, "found skip reconcile annotation")
return intctrlutil.RequeueAfter(ctx, time.Second*10, "found skip reconcile annotation")
}
if err = k8sutils.AddFinalizer(instance, k8sutils.RedisFinalizer, r.Client); err != nil {
return intctrlutil.RequeueWithError(err, reqLogger, "failed to add finalizer")
if err = k8sutils.AddFinalizer(ctx, instance, k8sutils.RedisFinalizer, r.Client); err != nil {
return intctrlutil.RequeueWithError(ctx, err, "failed to add finalizer")
}
err = k8sutils.CreateStandaloneRedis(instance, r.K8sClient)
err = k8sutils.CreateStandaloneRedis(ctx, instance, r.K8sClient)
if err != nil {
return intctrlutil.RequeueWithError(err, reqLogger, "failed to create redis")
return intctrlutil.RequeueWithError(ctx, err, "failed to create redis")
}
err = k8sutils.CreateStandaloneService(instance, r.K8sClient)
err = k8sutils.CreateStandaloneService(ctx, instance, r.K8sClient)
if err != nil {
return intctrlutil.RequeueWithError(err, reqLogger, "failed to create service")
return intctrlutil.RequeueWithError(ctx, err, "failed to create service")
}
return intctrlutil.RequeueAfter(reqLogger, time.Second*10, "requeue after 10 seconds")
return intctrlutil.RequeueAfter(ctx, time.Second*10, "requeue after 10 seconds")
}

// SetupWithManager sets up the controller with the Manager.
func (r *RedisReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&redisv1beta2.Redis{}).
Complete(r)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/redis/redis_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var _ = BeforeSuite(func() {
dk8sClient, err := dynamic.NewForConfig(cfg)
Expect(err).ToNot(HaveOccurred())

err = (&RedisReconciler{
err = (&Reconciler{
Client: k8sManager.GetClient(),
K8sClient: k8sClient,
Dk8sClient: dk8sClient,
Expand Down
Loading

0 comments on commit 3135e0f

Please sign in to comment.