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(cluster): add event recording functionality for RedisCluster controller. #1182

Merged
merged 3 commits into from
Dec 22, 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
5 changes: 1 addition & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@
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 @@
Client: mgr.GetClient(),
K8sClient: k8sclient,
Dk8sClient: dk8sClient,
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("rediscluster-controller"),

Check warning on line 133 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L133

Added line #L133 was not covered by tests
StatefulSet: k8sutils.NewStatefulSetService(k8sclient),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "RedisCluster")
Expand All @@ -141,7 +140,6 @@
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 @@
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{}}

Check warning on line 18 in pkg/controllers/common/events/events.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/common/events/events.go#L17-L18

Added lines #L17 - L18 were not covered by tests
}

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})

Check warning on line 25 in pkg/controllers/common/events/events.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/common/events/events.go#L21-L25

Added lines #L21 - L25 were not covered by tests
}

func (r *Recorder) Events() []Event {
return r.events

Check warning on line 29 in pkg/controllers/common/events/events.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/common/events/events.go#L28-L29

Added lines #L28 - L29 were not covered by tests
}
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 @@

"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 @@
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 @@
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...")

Check warning on line 83 in pkg/controllers/rediscluster/rediscluster_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/rediscluster/rediscluster_controller.go#L83

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