Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add context param and get logger from it #1132

Merged
merged 8 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,37 +118,32 @@
os.Exit(1)
}

if err = (&redis.RedisReconciler{
if err = (&redis.Reconciler{

Check warning on line 121 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L121

Added line #L121 was not covered by tests
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{

Check warning on line 130 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L130

Added line #L130 was not covered by tests
Client: mgr.GetClient(),
K8sClient: k8sclient,
Dk8sClient: dk8sClient,
Log: rcLog,
Scheme: mgr.GetScheme(),
StatefulSet: k8sutils.NewStatefulSetService(k8sclient, rcLog),
StatefulSet: k8sutils.NewStatefulSetService(k8sclient),

Check warning on line 135 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L135

Added line #L135 was not covered by tests
}).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{

Check warning on line 140 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L140

Added line #L140 was not covered by tests
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),

Check warning on line 146 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L145-L146

Added lines #L145 - L146 were not covered by tests
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "RedisReplication")
os.Exit(1)
Expand All @@ -157,7 +152,6 @@
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 @@
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 {

Check warning on line 80 in mocks/utils/utils.go

View check run for this annotation

Codecov / codecov/patch

mocks/utils/utils.go#L80

Added line #L80 was not covered by tests
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 @@
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")

Check warning on line 46 in pkg/controllers/redis/redis_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/redis/redis_controller.go#L46

Added line #L46 was not covered by tests
}
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")

Check warning on line 50 in pkg/controllers/redis/redis_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/redis/redis_controller.go#L49-L50

Added lines #L49 - L50 were not covered by tests
}
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")

Check warning on line 55 in pkg/controllers/redis/redis_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/redis/redis_controller.go#L55

Added line #L55 was not covered by tests
}
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")

Check warning on line 58 in pkg/controllers/redis/redis_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/redis/redis_controller.go#L58

Added line #L58 was not covered by tests
}
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")

Check warning on line 62 in pkg/controllers/redis/redis_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/redis/redis_controller.go#L62

Added line #L62 was not covered by tests
}
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")

Check warning on line 66 in pkg/controllers/redis/redis_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/redis/redis_controller.go#L66

Added line #L66 was not covered by tests
}
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
Loading