Skip to content

Commit

Permalink
chore(v2alpha2): more better status machine
Browse files Browse the repository at this point in the history
Signed-off-by: Rory Z <[email protected]>
  • Loading branch information
Rory-Z committed Jun 20, 2023
1 parent 49fdee3 commit c28e165
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 103 deletions.
2 changes: 2 additions & 0 deletions apis/apps/v2alpha2/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ func (s *EMQXStatus) SetNodes(nodes []EMQXNode) {
}
}
s.CoreNodeStatus.Nodes = coreNodes
s.CoreNodeStatus.ReadyReplicas = int32(len(coreNodes))
s.ReplicantNodeStatus.Nodes = replNodes
s.ReplicantNodeStatus.ReadyReplicas = int32(len(replNodes))
}

func (s *EMQXStatus) SetCondition(c metav1.Condition) {
Expand Down
3 changes: 3 additions & 0 deletions apis/apps/v2alpha2/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestSetNodes(t *testing.T) {
}
status.SetNodes(nodes)

assert.Equal(t, int32(2), status.CoreNodeStatus.ReadyReplicas)
assert.Equal(t, []EMQXNode{
{
Node: "emqx-1",
Expand All @@ -63,6 +64,8 @@ func TestSetNodes(t *testing.T) {
Uptime: 10000,
},
}, status.CoreNodeStatus.Nodes)

assert.Equal(t, int32(2), status.ReplicantNodeStatus.ReadyReplicas)
assert.Equal(t, []EMQXNode{
{
Node: "emqx-3",
Expand Down
1 change: 0 additions & 1 deletion controllers/apps/v2alpha2/add_emqx_repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func (a *addRepl) generateReplicaSet(instance *appsv2alpha2.EMQX) *appsv1.Replic
}
*instance.Status.ReplicantNodeStatus.CollisionCount++
}
instance.Status.ReplicantNodeStatus.CurrentVersion = podTemplateSpecHash

podTemplate.Labels = appsv2alpha2.CloneAndAddLabel(podTemplate.Labels, appsv1.DefaultDeploymentUniqueLabelKey, podTemplateSpecHash)
rs := &appsv1.ReplicaSet{
Expand Down
60 changes: 8 additions & 52 deletions controllers/apps/v2alpha2/status_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ type status interface {
type emqxStatusMachine struct {
emqx *appsv2alpha2.EMQX

init status

// EMQX cluster status
initialized status
coreNodesProgressing status
Expand All @@ -45,13 +43,11 @@ func newEMQXStatusMachine(emqx *appsv2alpha2.EMQX) *emqxStatusMachine {
emqx: emqx,
}

initStatus := &initStatus{emqxStatusMachine: emqxStatusMachine}
initializedStatus := &initializedStatus{emqxStatusMachine: emqxStatusMachine}
coreNodesProgressingStatus := &coreNodesProgressingStatus{emqxStatusMachine: emqxStatusMachine}
codeNodesReadyStatus := &codeNodesReadyStatus{emqxStatusMachine: emqxStatusMachine}
readyStatus := &readyStatus{emqxStatusMachine: emqxStatusMachine}

emqxStatusMachine.init = initStatus
emqxStatusMachine.initialized = initializedStatus
emqxStatusMachine.coreNodesProgressing = coreNodesProgressingStatus
emqxStatusMachine.codeNodesReady = codeNodesReadyStatus
Expand All @@ -62,52 +58,26 @@ func newEMQXStatusMachine(emqx *appsv2alpha2.EMQX) *emqxStatusMachine {
}

func (s *emqxStatusMachine) setCurrentStatus(emqx *appsv2alpha2.EMQX) {
if emqx.Status.Conditions == nil {
s.currentStatus = s.init
}

condition := emqx.Status.GetLastTrueCondition()
if condition == nil {
return
condition = &metav1.Condition{
Type: appsv2alpha2.Initialized,
Status: metav1.ConditionTrue,
Reason: "Initialized",
Message: "initialized EMQX cluster",
}
s.emqx.Status.SetCondition(*condition)
}

switch condition.Type {
case appsv2alpha2.Initialized:
s.currentStatus = s.initialized
case appsv2alpha2.CoreNodesProgressing:
s.currentStatus = s.coreNodesProgressing
case appsv2alpha2.CodeNodesReady:
s.currentStatus = s.codeNodesReady
case appsv2alpha2.Ready:
s.currentStatus = s.ready
default:
panic("unknown condition type")
}
}

func (s *emqxStatusMachine) UpdateNodeCount(emqxNodes []appsv2alpha2.EMQXNode) {
s.emqx.Status.CoreNodeStatus.Replicas = *s.emqx.Spec.CoreTemplate.Spec.Replicas

if isExistReplicant(s.emqx) {
s.emqx.Status.ReplicantNodeStatus.Replicas = *s.emqx.Spec.ReplicantTemplate.Spec.Replicas
}

s.emqx.Status.CoreNodeStatus.ReadyReplicas = int32(0)
s.emqx.Status.ReplicantNodeStatus.ReadyReplicas = int32(0)

if emqxNodes != nil {
s.emqx.Status.SetNodes(emqxNodes)

for _, node := range emqxNodes {
if node.NodeStatus == "running" {
if node.Role == "core" {
s.emqx.Status.CoreNodeStatus.ReadyReplicas++
}
if node.Role == "replicant" {
s.emqx.Status.ReplicantNodeStatus.ReadyReplicas++
}
}
}
s.currentStatus = s.initialized
}
}

Expand All @@ -119,20 +89,6 @@ func (s *emqxStatusMachine) GetEMQX() *appsv2alpha2.EMQX {
return s.emqx
}

type initStatus struct {
emqxStatusMachine *emqxStatusMachine
}

func (s *initStatus) nextStatus(_ *appsv1.StatefulSet, _ *appsv1.ReplicaSet) {
s.emqxStatusMachine.emqx.Status.SetCondition(metav1.Condition{
Type: appsv2alpha2.Initialized,
Status: metav1.ConditionTrue,
Reason: "Initialized",
Message: "initialized EMQX cluster",
})
s.emqxStatusMachine.setCurrentStatus(s.emqxStatusMachine.emqx)
}

type initializedStatus struct {
emqxStatusMachine *emqxStatusMachine
}
Expand Down
49 changes: 3 additions & 46 deletions controllers/apps/v2alpha2/status_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,56 +25,13 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
)

func TestCheckNodeCount(t *testing.T) {
t.Run("have replicant nodes", func(t *testing.T) {
emqx := &appsv2alpha2.EMQX{}
emqx.Spec.CoreTemplate.Spec.Replicas = pointer.Int32(1)
emqx.Spec.ReplicantTemplate = &appsv2alpha2.EMQXReplicantTemplate{
Spec: appsv2alpha2.EMQXReplicantTemplateSpec{
Replicas: pointer.Int32(1),
},
}
emqx.Status.Conditions = []metav1.Condition{
{Type: appsv2alpha2.Initialized, Status: metav1.ConditionTrue},
}

emqxNodes := []appsv2alpha2.EMQXNode{
{
Role: "core",
NodeStatus: "running",
},
{
Role: "replicant",
NodeStatus: "running",
},
{
Role: "fake role",
NodeStatus: "stop",
},
}

emqxStatusMachine := newEMQXStatusMachine(emqx)
emqxStatusMachine.UpdateNodeCount(emqxNodes)
assert.Equal(t, emqxStatusMachine.GetEMQX().Status.CoreNodeStatus.Replicas, int32(1))
assert.Equal(t, emqxStatusMachine.GetEMQX().Status.CoreNodeStatus.ReadyReplicas, int32(1))
assert.Equal(t, emqxStatusMachine.GetEMQX().Status.ReplicantNodeStatus.Replicas, int32(1))
assert.Equal(t, emqxStatusMachine.GetEMQX().Status.ReplicantNodeStatus.ReadyReplicas, int32(1))
})
}

func TestNextStatusForInit(t *testing.T) {
existedSts := &appsv1.StatefulSet{}
existedRs := &appsv1.ReplicaSet{}
emqx := &appsv2alpha2.EMQX{}
emqxStatusMachine := newEMQXStatusMachine(emqx)
assert.Equal(t, emqxStatusMachine.init, emqxStatusMachine.currentStatus)

emqxStatusMachine.NextStatus(existedSts, existedRs)
assert.Equal(t, emqxStatusMachine.initialized, emqxStatusMachine.currentStatus)
assert.Equal(t, appsv2alpha2.Initialized, emqxStatusMachine.GetEMQX().Status.Conditions[0].Type)
assert.Equal(t, appsv2alpha2.Initialized, emqxStatusMachine.emqx.Status.Conditions[0].Type)
}

func TestNextStatusForCreate(t *testing.T) {
Expand Down Expand Up @@ -199,7 +156,7 @@ func TestNextStatusForCoreUpdate(t *testing.T) {
})
}

func TestNextStatusForcodeNodesReady(t *testing.T) {
func TestNextStatusForCodeNodesReady(t *testing.T) {
t.Run("change image", func(t *testing.T) {
existedSts := &appsv1.StatefulSet{}
existedRs := &appsv1.ReplicaSet{}
Expand Down Expand Up @@ -317,7 +274,7 @@ func TestNextStatusForcodeNodesReady(t *testing.T) {

}

func TestNextStatusForCoreready(t *testing.T) {
func TestNextStatusForCoreReady(t *testing.T) {
t.Run("change image", func(t *testing.T) {
existedSts := &appsv1.StatefulSet{}
existedRs := &appsv1.ReplicaSet{}
Expand Down
8 changes: 4 additions & 4 deletions controllers/apps/v2alpha2/update_emqx_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ func (u *updateStatus) reconcile(ctx context.Context, instance *appsv2alpha2.EMQ

if existedSts.UID != "" {
instance.Status.CoreNodeStatus.CurrentVersion = existedSts.Status.CurrentRevision
instance.Status.CoreNodeStatus.Replicas = *instance.Spec.CoreTemplate.Spec.Replicas
}
if existedRs.UID != "" {
instance.Status.ReplicantNodeStatus.CurrentVersion = existedRs.Labels[appsv1.DefaultDeploymentUniqueLabelKey]
instance.Status.ReplicantNodeStatus.Replicas = *instance.Spec.ReplicantTemplate.Spec.Replicas
}
instance.Status.SetNodes(emqxNodes)

emqxStatusMachine := newEMQXStatusMachine(instance)
emqxStatusMachine.UpdateNodeCount(emqxNodes)
emqxStatusMachine.NextStatus(existedSts, existedRs)
// emqxStatusMachine.GetEMQX()
newEMQXStatusMachine(instance).NextStatus(existedSts, existedRs)

if err := u.Client.Status().Update(ctx, instance); err != nil {
return subResult{err: emperror.Wrap(err, "failed to update status")}
Expand Down

0 comments on commit c28e165

Please sign in to comment.