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

- 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: [Your Name]
Signed-off-by: drivebyer <[email protected]>
  • Loading branch information
drivebyer committed Dec 22, 2024
1 parent 1109494 commit 18d95e6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func main() {
Client: mgr.GetClient(),
K8sClient: k8sclient,
Dk8sClient: dk8sClient,
Recorder: mgr.GetEventRecorderFor("rediscluster-controller"),
StatefulSet: k8sutils.NewStatefulSetService(k8sclient),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "RedisCluster")
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
}
5 changes: 5 additions & 0 deletions pkg/controllers/rediscluster/rediscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +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"
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 @@ -40,6 +43,7 @@ type Reconciler struct {
k8sutils.StatefulSet
K8sClient kubernetes.Interface
Dk8sClient dynamic.Interface
Recorder record.EventRecorder
}

func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down Expand Up @@ -76,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,6 +104,7 @@ var _ = BeforeSuite(func() {
Client: k8sManager.GetClient(),
K8sClient: k8sClient,
Dk8sClient: dk8sClient,
Recorder: k8sManager.GetEventRecorderFor("rediscluster-controller"),
StatefulSet: k8sutils.NewStatefulSetService(k8sClient),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())
Expand Down

0 comments on commit 18d95e6

Please sign in to comment.