Skip to content

Commit

Permalink
refactor: reconcile on statefulset update
Browse files Browse the repository at this point in the history
Signed-off-by: drivebyer <yang.wu@daocloud.io>
  • Loading branch information
drivebyer committed Jun 14, 2024
1 parent 46ded9f commit 93d4ea2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
14 changes: 4 additions & 10 deletions controllers/rediscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllers

import (
"context"
"errors"
"strconv"
"time"

Expand All @@ -26,7 +27,7 @@ import (
"github.com/OT-CONTAINER-KIT/redis-operator/k8sutils"
intctrlutil "github.com/OT-CONTAINER-KIT/redis-operator/pkg/controllerutil"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -123,12 +124,8 @@ func (r *RedisClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request
return intctrlutil.RequeueWithError(err, reqLogger, "")
}

// todo: remove me after watch statefulset in controller
redisLeaderInfo, err := k8sutils.GetStatefulSet(r.K8sClient, r.Log, instance.GetNamespace(), instance.GetName()+"-leader")
if err != nil {
if errors.IsNotFound(err) {
return intctrlutil.RequeueAfter(reqLogger, time.Second*60, "")
}
return intctrlutil.RequeueWithError(err, reqLogger, "")
}

Expand Down Expand Up @@ -157,12 +154,8 @@ func (r *RedisClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request
return intctrlutil.RequeueWithError(err, reqLogger, "")
}
}
// todo: remove me after watch statefulset in controller
redisFollowerInfo, err := k8sutils.GetStatefulSet(r.K8sClient, r.Log, instance.GetNamespace(), instance.GetName()+"-follower")
if err != nil {
if errors.IsNotFound(err) {
return intctrlutil.RequeueAfter(reqLogger, time.Second*60, "")
}
return intctrlutil.RequeueWithError(err, reqLogger, "")
}

Expand All @@ -171,7 +164,7 @@ func (r *RedisClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request
}

if !(r.IsStatefulSetReady(ctx, instance.Namespace, instance.Name+"-leader") && r.IsStatefulSetReady(ctx, instance.Namespace, instance.Name+"-follower")) {
return intctrlutil.RequeueAfter(reqLogger, time.Second*60, "Redis leader and follower nodes are not ready yet")
return intctrlutil.RequeueWithError(errors.New("redis leader and follower nodes are not ready yet"), reqLogger, "")

Check warning on line 167 in controllers/rediscluster_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/rediscluster_controller.go#L167

Added line #L167 was not covered by tests
}

// Mark the cluster status as bootstrapping if all the leader and follower nodes are ready
Expand Down Expand Up @@ -239,5 +232,6 @@ func (r *RedisClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request
func (r *RedisClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&redisv1beta2.RedisCluster{}).
Owns(&appsv1.StatefulSet{}).
Complete(r)
}
6 changes: 4 additions & 2 deletions controllers/redisreplication_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/OT-CONTAINER-KIT/redis-operator/k8sutils"
intctrlutil "github.com/OT-CONTAINER-KIT/redis-operator/pkg/controllerutil"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
Expand Down Expand Up @@ -67,12 +68,12 @@ func (r *RedisReplicationReconciler) Reconcile(ctx context.Context, req ctrl.Req

redisReplicationInfo, err := k8sutils.GetStatefulSet(r.K8sClient, r.Log, instance.GetNamespace(), instance.GetName())
if err != nil {
return intctrlutil.RequeueAfter(reqLogger, time.Second*60, "")
return intctrlutil.RequeueWithError(err, reqLogger, "")

Check warning on line 71 in controllers/redisreplication_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/redisreplication_controller.go#L71

Added line #L71 was not covered by tests
}

// Check that the Leader and Follower are ready in redis replication
if redisReplicationInfo.Status.ReadyReplicas != totalReplicas {
return intctrlutil.RequeueAfter(reqLogger, time.Second*60, "Redis replication nodes are not ready yet", "Ready.Replicas", redisReplicationInfo.Status.ReadyReplicas, "Expected.Replicas", totalReplicas)
return intctrlutil.RequeueWithError(err, reqLogger, "Redis replication nodes are not ready yet", "Ready.Replicas", redisReplicationInfo.Status.ReadyReplicas, "Expected.Replicas", totalReplicas)
}

var realMaster string
Expand Down Expand Up @@ -138,5 +139,6 @@ func (r *RedisReplicationReconciler) UpdateRedisPodRoleLabel(ctx context.Context
func (r *RedisReplicationReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&redisv1beta2.RedisReplication{}).
Owns(&appsv1.StatefulSet{}).
Complete(r)
}

0 comments on commit 93d4ea2

Please sign in to comment.