Skip to content

Commit

Permalink
feat(cluster): add event recording functionality for RedisCluster con…
Browse files Browse the repository at this point in the history
…troller. (#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 <[email protected]>

* 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 <[email protected]>

* update

Signed-off-by: drivebyer <[email protected]>

---------

Signed-off-by: drivebyer <[email protected]>
  • Loading branch information
drivebyer authored Dec 22, 2024
1 parent c6df0b3 commit 6ee6417
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 16 deletions.
5 changes: 1 addition & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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 {
Expand All @@ -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")
Expand Down
30 changes: 30 additions & 0 deletions pkg/controllers/common/events/events.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 0 additions & 2 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"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion pkg/controllers/redis/redis_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ var _ = BeforeSuite(func() {
Client: k8sManager.GetClient(),
K8sClient: k8sClient,
Dk8sClient: dk8sClient,
Scheme: k8sManager.GetScheme(),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

Expand Down
7 changes: 5 additions & 2 deletions pkg/controllers/rediscluster/rediscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 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"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
Expand All @@ -21,7 +20,6 @@ type RedisSentinelReconciler struct {
client.Client
K8sClient kubernetes.Interface
Dk8sClient dynamic.Interface
Scheme *runtime.Scheme
ReplicationWatcher *intctrlutil.ResourceWatcher
}

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

0 comments on commit 6ee6417

Please sign in to comment.