Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Rory Z <[email protected]>
  • Loading branch information
Rory-Z committed Sep 3, 2024
1 parent df0410b commit a5810fe
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 37 deletions.
8 changes: 4 additions & 4 deletions apis/apps/v2beta1/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ type EMQXStatus struct {
// Represents the latest available observations of a EMQX Custom Resource current state.
Conditions []metav1.Condition `json:"conditions,omitempty"`

CoreNodes []EMQXNode `json:"coreNodes,omitempty"`
CoreNodesStatus EMQXNodesStatus `json:"coreNodesStatus,omitempty"`
CoreNodes []EMQXNode `json:"coreNodes,omitempty"`
CoreNodesStatus *EMQXNodesStatus `json:"coreNodesStatus,omitempty"`

ReplicantNodes []EMQXNode `json:"replicantNodes,omitempty"`
ReplicantNodesStatus EMQXNodesStatus `json:"replicantNodesStatus,omitempty"`
ReplicantNodes []EMQXNode `json:"replicantNodes,omitempty"`
ReplicantNodesStatus *EMQXNodesStatus `json:"replicantNodesStatus,omitempty"`

NodeEvacuationsStatus []NodeEvacuationStatus `json:"nodEvacuationsStatus,omitempty"`
}
Expand Down
12 changes: 10 additions & 2 deletions apis/apps/v2beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions controllers/apps/v2beta1/add_emqx_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ type addCore struct {
}

func (a *addCore) reconcile(ctx context.Context, logger logr.Logger, instance *appsv2beta1.EMQX, _ innerReq.RequesterInterface) subResult {
if instance.Status.CoreNodesStatus == nil {
instance.Status.CoreNodesStatus = &appsv2beta1.EMQXNodesStatus{}
}

preSts := getNewStatefulSet(instance)
preStsHash := preSts.Labels[appsv2beta1.LabelsPodTemplateHashKey]
updateSts, _, _ := getStateFulSetList(ctx, a.Client, instance)
Expand Down
1 change: 1 addition & 0 deletions controllers/apps/v2beta1/add_emqx_core_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var _ = Describe("Check add core controller", Ordered, Label("core"), func() {
LastTransitionTime: metav1.Time{Time: time.Now().AddDate(0, 0, -1)},
},
}
instance.Status.CoreNodesStatus = &appsv2beta1.EMQXNodesStatus{}
})

It("create namespace", func() {
Expand Down
2 changes: 1 addition & 1 deletion controllers/apps/v2beta1/add_emqx_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestGetNewStatefulSet(t *testing.T) {
},
}
instance.Spec.CoreTemplate.Spec.Replicas = ptr.To(int32(3))
instance.Status.CoreNodesStatus = appsv2beta1.EMQXNodesStatus{
instance.Status.CoreNodesStatus = &appsv2beta1.EMQXNodesStatus{
CollisionCount: ptr.To(int32(0)),
}

Expand Down
9 changes: 8 additions & 1 deletion controllers/apps/v2beta1/add_emqx_repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (a *addRepl) reconcile(ctx context.Context, logger logr.Logger, instance *a
return subResult{}
}

if instance.Status.ReplicantNodesStatus == nil {
instance.Status.ReplicantNodesStatus = &appsv2beta1.EMQXNodesStatus{}
}

preRs := getNewReplicaSet(instance)
preRsHash := preRs.Labels[appsv2beta1.LabelsPodTemplateHashKey]
updateRs, _, _ := getReplicaSetList(ctx, a.Client, instance)
Expand Down Expand Up @@ -124,7 +128,10 @@ func getNewReplicaSet(instance *appsv2beta1.EMQX) *appsv1.ReplicaSet {
svcPorts, _ := appsv2beta1.GetDashboardServicePort(instance.Spec.Config.Data)

preRs := generateReplicaSet(instance)
podTemplateSpecHash := computeHash(preRs.Spec.Template.DeepCopy(), instance.Status.ReplicantNodesStatus.CollisionCount)
podTemplateSpecHash := computeHash(preRs.Spec.Template.DeepCopy(), nil)
if instance.Status.ReplicantNodesStatus != nil {
podTemplateSpecHash = computeHash(preRs.Spec.Template.DeepCopy(), instance.Status.ReplicantNodesStatus.CollisionCount)
}
preRs.Name = preRs.Name + "-" + podTemplateSpecHash
preRs.Labels = appsv2beta1.CloneAndAddLabel(preRs.Labels, appsv2beta1.LabelsPodTemplateHashKey, podTemplateSpecHash)
preRs.Spec.Selector = appsv2beta1.CloneSelectorAndAddLabel(preRs.Spec.Selector, appsv2beta1.LabelsPodTemplateHashKey, podTemplateSpecHash)
Expand Down
2 changes: 1 addition & 1 deletion controllers/apps/v2beta1/add_emqx_repl_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var _ = Describe("Check add repl controller", Ordered, Label("repl"), func() {
},
}
instance.Status = appsv2beta1.EMQXStatus{
ReplicantNodesStatus: appsv2beta1.EMQXNodesStatus{
ReplicantNodesStatus: &appsv2beta1.EMQXNodesStatus{
Replicas: 3,
},
Conditions: []metav1.Condition{
Expand Down
2 changes: 1 addition & 1 deletion controllers/apps/v2beta1/add_emqx_repl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestGetNewReplicaSet(t *testing.T) {
Replicas: ptr.To(int32(3)),
},
}
instance.Status.ReplicantNodesStatus = appsv2beta1.EMQXNodesStatus{
instance.Status.ReplicantNodesStatus = &appsv2beta1.EMQXNodesStatus{
CollisionCount: ptr.To(int32(0)),
}

Expand Down
6 changes: 3 additions & 3 deletions controllers/apps/v2beta1/sync_pods_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ var _ = Describe("Check sync pods controller", Ordered, Label("node"), func() {
LastTransitionTime: metav1.Time{Time: time.Now().AddDate(0, 0, -1)},
},
},
CoreNodesStatus: appsv2beta1.EMQXNodesStatus{
CoreNodesStatus: &appsv2beta1.EMQXNodesStatus{
UpdateRevision: "update",
UpdateReplicas: 1,
CurrentRevision: "current",
CurrentReplicas: 1,
ReadyReplicas: 2,
Replicas: 1,
},
ReplicantNodesStatus: appsv2beta1.EMQXNodesStatus{
ReplicantNodesStatus: &appsv2beta1.EMQXNodesStatus{
UpdateRevision: "update",
UpdateReplicas: 1,
CurrentRevision: "current",
Expand Down Expand Up @@ -334,7 +334,7 @@ var _ = Describe("check can be scale down", func() {
Replicas: ptr.To(int32(3)),
},
}
instance.Status.ReplicantNodesStatus = appsv2beta1.EMQXNodesStatus{
instance.Status.ReplicantNodesStatus = &appsv2beta1.EMQXNodesStatus{
UpdateRevision: "update",
CurrentRevision: "current",
}
Expand Down
7 changes: 7 additions & 0 deletions controllers/apps/v2beta1/update_emqx_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ type updateStatus struct {
}

func (u *updateStatus) reconcile(ctx context.Context, logger logr.Logger, instance *appsv2beta1.EMQX, r innerReq.RequesterInterface) subResult {
if instance.Status.CoreNodesStatus == nil {
instance.Status.CoreNodesStatus = &appsv2beta1.EMQXNodesStatus{}
}
instance.Status.CoreNodesStatus.Replicas = *instance.Spec.CoreTemplate.Spec.Replicas

if instance.Spec.ReplicantTemplate != nil {
if instance.Status.ReplicantNodesStatus == nil {
instance.Status.ReplicantNodesStatus = &appsv2beta1.EMQXNodesStatus{}
}
instance.Status.ReplicantNodesStatus.Replicas = *instance.Spec.ReplicantTemplate.Spec.Replicas
}

Expand Down
15 changes: 9 additions & 6 deletions controllers/apps/v2beta1/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,23 @@ func getRsPodMap(ctx context.Context, k8sClient client.Client, instance *appsv2b
}

func getStateFulSetList(ctx context.Context, k8sClient client.Client, instance *appsv2beta1.EMQX) (updateSts, currentSts *appsv1.StatefulSet, oldStsList []*appsv1.StatefulSet) {
if instance.Status.CoreNodesStatus == nil {
return
}
list := &appsv1.StatefulSetList{}
_ = k8sClient.List(ctx, list,
client.InNamespace(instance.Namespace),
client.MatchingLabels(appsv2beta1.DefaultCoreLabels(instance)),
)
for _, sts := range list.Items {
if hash, ok := sts.Labels[appsv2beta1.LabelsPodTemplateHashKey]; ok {
if hash == instance.Status.CoreNodesStatus.UpdateRevision {
if instance.Status.CoreNodesStatus == nil && hash == instance.Status.CoreNodesStatus.UpdateRevision {
updateSts = sts.DeepCopy()
}
if hash == instance.Status.CoreNodesStatus.CurrentRevision {
if instance.Status.CoreNodesStatus == nil && hash == instance.Status.CoreNodesStatus.CurrentRevision {
currentSts = sts.DeepCopy()
}
if hash != instance.Status.CoreNodesStatus.UpdateRevision && hash != instance.Status.CoreNodesStatus.CurrentRevision {
if instance.Status.CoreNodesStatus != nil || (hash != instance.Status.CoreNodesStatus.UpdateRevision && hash != instance.Status.CoreNodesStatus.CurrentRevision) {
oldStsList = append(oldStsList, sts.DeepCopy())
}
}
Expand All @@ -96,13 +99,13 @@ func getReplicaSetList(ctx context.Context, k8sClient client.Client, instance *a

for _, rs := range list.Items {
if hash, ok := rs.Labels[appsv2beta1.LabelsPodTemplateHashKey]; ok {
if hash == instance.Status.ReplicantNodesStatus.UpdateRevision {
if instance.Status.ReplicantNodesStatus != nil && hash == instance.Status.ReplicantNodesStatus.UpdateRevision {
updateRs = rs.DeepCopy()
}
if hash == instance.Status.ReplicantNodesStatus.CurrentRevision {
if instance.Status.ReplicantNodesStatus != nil && hash == instance.Status.ReplicantNodesStatus.CurrentRevision {
currentRs = rs.DeepCopy()
}
if hash != instance.Status.ReplicantNodesStatus.UpdateRevision && hash != instance.Status.ReplicantNodesStatus.CurrentRevision {
if instance.Status.ReplicantNodesStatus == nil || (hash != instance.Status.ReplicantNodesStatus.UpdateRevision && hash != instance.Status.ReplicantNodesStatus.CurrentRevision) {
oldRsList = append(oldRsList, rs.DeepCopy())
}
}
Expand Down
36 changes: 18 additions & 18 deletions e2e/v2beta1/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
return instance.Status.CoreNodes
}, HaveLen(int(*instance.Spec.CoreTemplate.Spec.Replicas))),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.CoreNodesStatus
return *instance.Status.CoreNodesStatus
}, And(
HaveField("Replicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
HaveField("ReadyReplicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
Expand All @@ -81,7 +81,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
return instance.Status.ReplicantNodes
}, BeNil()),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.ReplicantNodesStatus
return *instance.Status.ReplicantNodesStatus
}, And(
HaveField("Replicas", Equal(int32(0))),
HaveField("ReadyReplicas", Equal(int32(0))),
Expand Down Expand Up @@ -126,7 +126,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
return instance.Status.CoreNodes
}, HaveLen(int(*instance.Spec.CoreTemplate.Spec.Replicas))),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.CoreNodesStatus
return *instance.Status.CoreNodesStatus
}, And(
HaveField("Replicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
HaveField("ReadyReplicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
Expand All @@ -139,7 +139,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
return instance.Status.ReplicantNodes
}, BeNil()),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.ReplicantNodesStatus
return *instance.Status.ReplicantNodesStatus
}, And(
HaveField("Replicas", Equal(int32(0))),
HaveField("ReadyReplicas", Equal(int32(0))),
Expand Down Expand Up @@ -184,7 +184,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
return instance.Status.CoreNodes
}, HaveLen(int(*instance.Spec.CoreTemplate.Spec.Replicas))),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.CoreNodesStatus
return *instance.Status.CoreNodesStatus
}, And(
HaveField("Replicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
HaveField("ReadyReplicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
Expand All @@ -197,7 +197,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
return instance.Status.ReplicantNodes
}, BeNil()),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.ReplicantNodesStatus
return *instance.Status.ReplicantNodesStatus
}, And(
HaveField("Replicas", Equal(int32(0))),
HaveField("ReadyReplicas", Equal(int32(0))),
Expand Down Expand Up @@ -239,7 +239,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
return instance.Status.IsConditionTrue(appsv2beta1.Ready)
}, BeTrue()),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.CoreNodesStatus
return *instance.Status.CoreNodesStatus
}, And(
HaveField("Replicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
HaveField("ReadyReplicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
Expand All @@ -249,7 +249,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
HaveField("UpdateReplicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
)),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.ReplicantNodesStatus
return *instance.Status.ReplicantNodesStatus
}, And(
HaveField("Replicas", Equal(int32(0))),
HaveField("ReadyReplicas", Equal(int32(0))),
Expand Down Expand Up @@ -358,7 +358,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
return instance.Status.IsConditionTrue(appsv2beta1.Ready)
}, BeTrue()),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.CoreNodesStatus
return *instance.Status.CoreNodesStatus
}, And(
HaveField("Replicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
HaveField("ReadyReplicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
Expand All @@ -368,7 +368,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
HaveField("UpdateReplicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
)),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.ReplicantNodesStatus
return *instance.Status.ReplicantNodesStatus
}, And(
HaveField("Replicas", Equal(int32(*instance.Spec.ReplicantTemplate.Spec.Replicas))),
HaveField("ReadyReplicas", Equal(int32(*instance.Spec.ReplicantTemplate.Spec.Replicas))),
Expand Down Expand Up @@ -413,7 +413,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
return instance.Status.CoreNodes
}, HaveLen(int(*instance.Spec.CoreTemplate.Spec.Replicas))),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.CoreNodesStatus
return *instance.Status.CoreNodesStatus
}, And(
HaveField("Replicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
HaveField("ReadyReplicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
Expand All @@ -426,7 +426,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
return instance.Status.ReplicantNodes
}, HaveLen(int(*instance.Spec.ReplicantTemplate.Spec.Replicas))),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.ReplicantNodesStatus
return *instance.Status.ReplicantNodesStatus
}, And(
HaveField("Replicas", Equal(int32(*instance.Spec.ReplicantTemplate.Spec.Replicas))),
HaveField("ReadyReplicas", Equal(int32(*instance.Spec.ReplicantTemplate.Spec.Replicas))),
Expand Down Expand Up @@ -471,7 +471,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
return instance.Status.CoreNodes
}, HaveLen(int(*instance.Spec.CoreTemplate.Spec.Replicas))),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.CoreNodesStatus
return *instance.Status.CoreNodesStatus
}, And(
HaveField("Replicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
HaveField("ReadyReplicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
Expand All @@ -484,7 +484,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
return instance.Status.ReplicantNodes
}, HaveLen(int(*instance.Spec.ReplicantTemplate.Spec.Replicas))),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.ReplicantNodesStatus
return *instance.Status.ReplicantNodesStatus
}, And(
HaveField("Replicas", Equal(int32(0))),
HaveField("ReadyReplicas", Equal(int32(0))),
Expand Down Expand Up @@ -526,7 +526,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
return instance.Status.IsConditionTrue(appsv2beta1.Ready)
}, BeTrue()),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.CoreNodesStatus
return *instance.Status.CoreNodesStatus
}, And(
HaveField("Replicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
HaveField("ReadyReplicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
Expand All @@ -536,7 +536,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
HaveField("UpdateReplicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
)),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.ReplicantNodesStatus
return *instance.Status.ReplicantNodesStatus
}, And(
HaveField("Replicas", Equal(int32(*instance.Spec.ReplicantTemplate.Spec.Replicas))),
HaveField("ReadyReplicas", Equal(int32(*instance.Spec.ReplicantTemplate.Spec.Replicas))),
Expand Down Expand Up @@ -579,7 +579,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
return instance.Status.IsConditionTrue(appsv2beta1.Ready)
}, BeTrue()),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.CoreNodesStatus
return *instance.Status.CoreNodesStatus
}, And(
HaveField("Replicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
HaveField("ReadyReplicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
Expand All @@ -589,7 +589,7 @@ var _ = Describe("E2E Test", Label("base"), Ordered, func() {
HaveField("UpdateReplicas", Equal(int32(*instance.Spec.CoreTemplate.Spec.Replicas))),
)),
WithTransform(func(instance *appsv2beta1.EMQX) appsv2beta1.EMQXNodesStatus {
return instance.Status.ReplicantNodesStatus
return *instance.Status.ReplicantNodesStatus
}, And(
HaveField("Replicas", Equal(int32(0))),
HaveField("ReadyReplicas", Equal(int32(0))),
Expand Down

0 comments on commit a5810fe

Please sign in to comment.