Skip to content

Commit 3c0f791

Browse files
simplify keeper version conditions
1 parent a40b68d commit 3c0f791

2 files changed

Lines changed: 24 additions & 64 deletions

File tree

internal/controller/keeper/sync.go

Lines changed: 23 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"math"
88
"slices"
99
"strconv"
10-
"strings"
1110
"time"
1211

1312
"gopkg.in/yaml.v2"
@@ -596,9 +595,7 @@ func (r *keeperReconciler) evaluateReplicaConditions() {
596595
r.SetCondition(chctrl.ReplicaStartupCondition(errorIDs))
597596
r.SetCondition(chctrl.HealthyCondition(notReadyIDs))
598597
r.SetCondition(chctrl.ConfigSyncCondition(nil, notUpdatedIDs, nil))
599-
versionCond, versionEvents := keeperVersionSyncCondition(replicaVersions, len(notUpdatedIDs) > 0)
600-
r.SetCondition(versionCond, versionEvents...)
601-
r.evaluateUpgradeCondition(replicaVersions)
598+
r.evaluateVersionConditions(len(notUpdatedIDs) > 0)
602599

603600
// Ready condition — keeper-specific logic.
604601
exists := len(r.ReplicaState)
@@ -677,75 +674,38 @@ func (r *keeperReconciler) evaluateReplicaConditions() {
677674
)
678675
}
679676

680-
func (r *keeperReconciler) evaluateUpgradeCondition(replicaVersions map[string]string) {
681-
version, ok := keeperObservedVersion(replicaVersions)
682-
if ok {
683-
r.Cluster.Status.Version = version
684-
}
685-
686-
if r.Checker == nil || !ok {
687-
meta.RemoveStatusCondition(r.Cluster.GetStatus().GetConditions(), v1.ConditionTypeVersionUpgraded)
688-
return
689-
}
677+
func (r *keeperReconciler) evaluateVersionConditions(isUpdating bool) {
678+
versionByReplica := map[string]string{}
679+
countByVersion := map[string]int{}
680+
commonVersion := ""
690681

691-
cond, event := chctrl.GetUpgradeCondition(*r.Checker, chctrl.VersionProbeResult{Version: version}, r.Cluster.Spec.UpgradeChannel)
692-
r.SetCondition(cond, event...)
693-
}
694-
695-
func keeperVersionSyncCondition(replicaVersions map[string]string, isUpdating bool) (metav1.Condition, []chctrl.EventSpec) {
696-
newCond := func(status metav1.ConditionStatus, reason v1.ConditionReason, message string) metav1.Condition {
697-
return metav1.Condition{
698-
Type: v1.ConditionTypeVersionInSync,
699-
Status: status,
700-
Reason: reason,
701-
Message: message,
682+
for i, s := range r.ReplicaState {
683+
if s.Status.Version != "" {
684+
versionByReplica[strconv.FormatInt(int64(i), 10)] = s.Status.Version
685+
countByVersion[s.Status.Version]++
686+
commonVersion = s.Status.Version
702687
}
703688
}
704689

705-
if len(replicaVersions) == 0 {
706-
return newCond(metav1.ConditionUnknown, v1.ConditionReasonVersionPending, "No Keeper replica version has been observed yet"), nil
707-
}
708-
709-
var observed []string
710-
for id, version := range replicaVersions {
711-
observed = append(observed, fmt.Sprintf("%s: %s", id, version))
690+
// Record the version only when all observed replicas agree, otherwise keep the last known one.
691+
if len(countByVersion) == 1 {
692+
r.Cluster.Status.Version = commonVersion
712693
}
713694

714-
if version, ok := keeperObservedVersion(replicaVersions); ok {
715-
return newCond(metav1.ConditionTrue, v1.ConditionReasonVersionMatch,
716-
"All observed Keeper replicas report version "+version), nil
695+
probe := chctrl.VersionProbeResult{
696+
Version: r.Cluster.Status.Version,
697+
Pending: len(versionByReplica) == 0,
717698
}
718699

719-
slices.Sort(observed)
720-
cond := newCond(metav1.ConditionFalse, v1.ConditionReasonVersionMismatch,
721-
"Keeper replica versions differ: "+strings.Join(observed, ", "))
722-
723-
if isUpdating {
724-
return cond, nil
725-
}
726-
727-
return cond, []chctrl.EventSpec{{
728-
Type: corev1.EventTypeWarning,
729-
Reason: v1.EventReasonVersionDiverge,
730-
Action: v1.EventActionVersionCheck,
731-
Message: cond.Message,
732-
}}
733-
}
734-
735-
func keeperObservedVersion(replicaVersions map[string]string) (string, bool) {
736-
var result string
737-
for _, version := range replicaVersions {
738-
if result == "" {
739-
result = version
740-
continue
741-
}
700+
cond, event := chctrl.GetVersionSyncCondition(probe, versionByReplica, isUpdating)
701+
r.SetCondition(cond, event...)
742702

743-
if version != result {
744-
return "", false
745-
}
703+
if r.Checker != nil {
704+
cond, event = chctrl.GetUpgradeCondition(*r.Checker, probe, r.Cluster.Spec.UpgradeChannel)
705+
r.SetCondition(cond, event...)
706+
} else {
707+
meta.RemoveStatusCondition(r.Cluster.GetStatus().GetConditions(), v1.ConditionTypeVersionUpgraded)
746708
}
747-
748-
return result, result != ""
749709
}
750710

751711
func (r *keeperReconciler) updateReplica(ctx context.Context, log ctrlutil.Logger, replicaID v1.KeeperReplicaID) (*ctrl.Result, error) {

test/e2e/keeper_e2e_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var _ = Describe("Keeper controller", Label("keeper"), func() {
6060

6161
WaitKeeperUpdatedAndReady(ctx, &cr, 3*time.Minute, true)
6262
ExpectWithOffset(1, k8sClient.Get(ctx, cr.NamespacedName(), &cr)).To(Succeed())
63-
63+
Expect(cr.Status.Version).To(HavePrefix(cr.Spec.ContainerTemplate.Image.Tag))
6464
KeeperRWChecks(ctx, &cr, &checks)
6565
},
6666
Entry("update log level", v1.KeeperClusterSpec{Settings: v1.KeeperSettings{

0 commit comments

Comments
 (0)