From 6ee6417b3bb9d71ec57ef6ecd507389107c16b1c Mon Sep 17 00:00:00 2001 From: yangw Date: Sun, 22 Dec 2024 14:22:09 +0800 Subject: [PATCH] feat(cluster): add event recording functionality for RedisCluster controller. (#1182) * refactor: remove unused Scheme references in Redis controller files - Removed the Scheme field from the Reconciler struct in rediscluster_controller.go, main.go, and rediscluster_controller_suite_test.go as it was not being utilized. - This cleanup enhances code clarity and maintainability by eliminating unnecessary components. Signed-off-by: drivebyer * feat(cluster): add event recording functionality for RedisCluster controller - Introduced a new events package to manage event recording, including a Recorder struct and methods for adding and retrieving events. - Updated the RedisCluster controller to utilize the event recorder, allowing it to log downscale events. - Enhanced the main function to initialize the event recorder for the RedisCluster controller. This update improves observability capabilities for RedisCluster operations. Signed-off-by: drivebyer * update Signed-off-by: drivebyer --------- Signed-off-by: drivebyer --- main.go | 5 +--- pkg/controllers/common/events/events.go | 30 +++++++++++++++++++ pkg/controllers/redis/redis_controller.go | 2 -- .../redis/redis_controller_suite_test.go | 1 - .../rediscluster/rediscluster_controller.go | 7 +++-- .../rediscluster_controller_suite_test.go | 2 +- .../redisreplication_controller.go | 2 -- .../redisreplication_controller_suite_test.go | 1 - .../redissentinel/redissentinel_controller.go | 2 -- .../redissentinel_controller_suite_test.go | 1 - 10 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 pkg/controllers/common/events/events.go diff --git a/main.go b/main.go index 576d16fbf..354ba1ccb 100644 --- a/main.go +++ b/main.go @@ -122,7 +122,6 @@ func main() { Client: mgr.GetClient(), K8sClient: k8sclient, Dk8sClient: dk8sClient, - Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Redis") os.Exit(1) @@ -131,7 +130,7 @@ func main() { Client: mgr.GetClient(), K8sClient: k8sclient, Dk8sClient: dk8sClient, - Scheme: mgr.GetScheme(), + Recorder: mgr.GetEventRecorderFor("rediscluster-controller"), StatefulSet: k8sutils.NewStatefulSetService(k8sclient), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "RedisCluster") @@ -141,7 +140,6 @@ func main() { Client: mgr.GetClient(), K8sClient: k8sclient, Dk8sClient: dk8sClient, - Scheme: mgr.GetScheme(), Pod: k8sutils.NewPodService(k8sclient), StatefulSet: k8sutils.NewStatefulSetService(k8sclient), }).SetupWithManager(mgr); err != nil { @@ -152,7 +150,6 @@ func main() { Client: mgr.GetClient(), K8sClient: k8sclient, Dk8sClient: dk8sClient, - Scheme: mgr.GetScheme(), ReplicationWatcher: intctrlutil.NewResourceWatcher(), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "RedisSentinel") diff --git a/pkg/controllers/common/events/events.go b/pkg/controllers/common/events/events.go new file mode 100644 index 000000000..0d09c91b7 --- /dev/null +++ b/pkg/controllers/common/events/events.go @@ -0,0 +1,30 @@ +package events + +const ( + EventReasonRedisClusterDownscale = "RedisClusterDownscale" +) + +type Event struct { + EventType string + Reason string + Message string +} + +type Recorder struct { + events []Event +} + +func NewRecorder() *Recorder { + return &Recorder{events: []Event{}} +} + +func (r *Recorder) AddEvent(typ, reason, message string) { + if r.events == nil { + r.events = []Event{} + } + r.events = append(r.events, Event{EventType: typ, Reason: reason, Message: message}) +} + +func (r *Recorder) Events() []Event { + return r.events +} diff --git a/pkg/controllers/redis/redis_controller.go b/pkg/controllers/redis/redis_controller.go index 061f33e4c..9fb9ae5d5 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" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" ctrl "sigs.k8s.io/controller-runtime" @@ -35,7 +34,6 @@ type Reconciler struct { client.Client K8sClient kubernetes.Interface Dk8sClient dynamic.Interface - Scheme *runtime.Scheme } func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { diff --git a/pkg/controllers/redis/redis_controller_suite_test.go b/pkg/controllers/redis/redis_controller_suite_test.go index 594bd2007..68a75c2a8 100644 --- a/pkg/controllers/redis/redis_controller_suite_test.go +++ b/pkg/controllers/redis/redis_controller_suite_test.go @@ -103,7 +103,6 @@ var _ = BeforeSuite(func() { Client: k8sManager.GetClient(), K8sClient: k8sClient, Dk8sClient: dk8sClient, - Scheme: k8sManager.GetScheme(), }).SetupWithManager(k8sManager) Expect(err).ToNot(HaveOccurred()) diff --git a/pkg/controllers/rediscluster/rediscluster_controller.go b/pkg/controllers/rediscluster/rediscluster_controller.go index 18ad9ca2d..e06ccd48d 100644 --- a/pkg/controllers/rediscluster/rediscluster_controller.go +++ b/pkg/controllers/rediscluster/rediscluster_controller.go @@ -23,13 +23,15 @@ import ( "github.com/OT-CONTAINER-KIT/redis-operator/api/status" redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2" + "github.com/OT-CONTAINER-KIT/redis-operator/pkg/controllers/common/events" 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" appsv1 "k8s.io/api/apps/v1" - "k8s.io/apimachinery/pkg/runtime" + corev1 "k8s.io/api/core/v1" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" + "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/log" @@ -41,7 +43,7 @@ type Reconciler struct { k8sutils.StatefulSet K8sClient kubernetes.Interface Dk8sClient dynamic.Interface - Scheme *runtime.Scheme + Recorder record.EventRecorder } func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { @@ -78,6 +80,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu return intctrlutil.Reconciled() } if masterCount := k8sutils.CheckRedisNodeCount(ctx, r.K8sClient, instance, "leader"); masterCount == leaderCount { + r.Recorder.Event(instance, corev1.EventTypeNormal, events.EventReasonRedisClusterDownscale, "Redis cluster is downscaling...") logger.Info("Redis cluster is downscaling...", "Current.LeaderReplicas", leaderCount, "Desired.LeaderReplicas", leaderReplicas) for shardIdx := leaderCount - 1; shardIdx >= leaderReplicas; shardIdx-- { logger.Info("Remove the shard", "Shard.Index", shardIdx) diff --git a/pkg/controllers/rediscluster/rediscluster_controller_suite_test.go b/pkg/controllers/rediscluster/rediscluster_controller_suite_test.go index 8cfe65206..33aa9cb91 100644 --- a/pkg/controllers/rediscluster/rediscluster_controller_suite_test.go +++ b/pkg/controllers/rediscluster/rediscluster_controller_suite_test.go @@ -104,7 +104,7 @@ var _ = BeforeSuite(func() { Client: k8sManager.GetClient(), K8sClient: k8sClient, Dk8sClient: dk8sClient, - Scheme: k8sManager.GetScheme(), + Recorder: k8sManager.GetEventRecorderFor("rediscluster-controller"), 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 33b2fc9db..b205468b3 100644 --- a/pkg/controllers/redisreplication/redisreplication_controller.go +++ b/pkg/controllers/redisreplication/redisreplication_controller.go @@ -9,7 +9,6 @@ import ( "github.com/OT-CONTAINER-KIT/redis-operator/pkg/k8sutils" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" ctrl "sigs.k8s.io/controller-runtime" @@ -24,7 +23,6 @@ type Reconciler struct { k8sutils.StatefulSet K8sClient kubernetes.Interface Dk8sClient dynamic.Interface - Scheme *runtime.Scheme } func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { diff --git a/pkg/controllers/redisreplication/redisreplication_controller_suite_test.go b/pkg/controllers/redisreplication/redisreplication_controller_suite_test.go index 445f7ce7d..aa673e735 100644 --- a/pkg/controllers/redisreplication/redisreplication_controller_suite_test.go +++ b/pkg/controllers/redisreplication/redisreplication_controller_suite_test.go @@ -104,7 +104,6 @@ var _ = BeforeSuite(func() { Client: k8sManager.GetClient(), K8sClient: k8sClient, Dk8sClient: dk8sClient, - Scheme: k8sManager.GetScheme(), Pod: k8sutils.NewPodService(k8sClient), StatefulSet: k8sutils.NewStatefulSetService(k8sClient), }).SetupWithManager(k8sManager) diff --git a/pkg/controllers/redissentinel/redissentinel_controller.go b/pkg/controllers/redissentinel/redissentinel_controller.go index 7606ac917..6828cfe88 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" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" @@ -21,7 +20,6 @@ type RedisSentinelReconciler struct { client.Client K8sClient kubernetes.Interface Dk8sClient dynamic.Interface - Scheme *runtime.Scheme ReplicationWatcher *intctrlutil.ResourceWatcher } diff --git a/pkg/controllers/redissentinel/redissentinel_controller_suite_test.go b/pkg/controllers/redissentinel/redissentinel_controller_suite_test.go index ece28dc28..90fde159f 100644 --- a/pkg/controllers/redissentinel/redissentinel_controller_suite_test.go +++ b/pkg/controllers/redissentinel/redissentinel_controller_suite_test.go @@ -104,7 +104,6 @@ var _ = BeforeSuite(func() { Client: k8sManager.GetClient(), K8sClient: k8sClient, Dk8sClient: dk8sClient, - Scheme: k8sManager.GetScheme(), ReplicationWatcher: intctrlutil.NewResourceWatcher(), }).SetupWithManager(k8sManager) Expect(err).ToNot(HaveOccurred())