Skip to content

Commit

Permalink
chore: change node status to pointer
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 4, 2024
1 parent df0410b commit 0b460eb
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 90 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.

2 changes: 1 addition & 1 deletion config/samples/emqx/v2beta1/emqx-full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
apps.emqx.io/instance: emqx
apps.emqx.io/managed-by: emqx-operator
spec:
image: "emqx:5"
image: "emqx/emqx-enterprise:latest"
imagePullPolicy: Always
revisionHistoryLimit: 3
config:
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
5 changes: 4 additions & 1 deletion controllers/apps/v2beta1/add_emqx_repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,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
50 changes: 32 additions & 18 deletions controllers/apps/v2beta1/update_emqx_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,28 @@ type updateStatus struct {
}

func (u *updateStatus) reconcile(ctx context.Context, logger logr.Logger, instance *appsv2beta1.EMQX, r innerReq.RequesterInterface) subResult {
instance.Status.CoreNodesStatus.Replicas = *instance.Spec.CoreTemplate.Spec.Replicas
if instance.Spec.ReplicantTemplate != nil {
instance.Status.ReplicantNodesStatus.Replicas = *instance.Spec.ReplicantTemplate.Spec.Replicas
if instance.Status.CoreNodesStatus == nil {
instance.Status.CoreNodesStatus = &appsv2beta1.EMQXNodesStatus{}
if err := u.Client.Status().Update(ctx, instance); err != nil {
return subResult{err: emperror.Wrap(err, "failed to update status")}
}

Check warning on line 28 in controllers/apps/v2beta1/update_emqx_status.go

View check run for this annotation

Codecov / codecov/patch

controllers/apps/v2beta1/update_emqx_status.go#L27-L28

Added lines #L27 - L28 were not covered by tests
}

instance.Status.CoreNodesStatus.Replicas = *instance.Spec.CoreTemplate.Spec.Replicas
if instance.Status.CoreNodesStatus.UpdateRevision != "" && instance.Status.CoreNodesStatus.CurrentRevision == "" {
instance.Status.CoreNodesStatus.CurrentRevision = instance.Status.CoreNodesStatus.UpdateRevision
}
if instance.Status.ReplicantNodesStatus.UpdateRevision != "" && instance.Status.ReplicantNodesStatus.CurrentRevision == "" {
instance.Status.ReplicantNodesStatus.CurrentRevision = instance.Status.ReplicantNodesStatus.UpdateRevision

if instance.Spec.ReplicantTemplate != nil {
if instance.Status.ReplicantNodesStatus == nil {
instance.Status.ReplicantNodesStatus = &appsv2beta1.EMQXNodesStatus{}
if err := u.Client.Status().Update(ctx, instance); err != nil {
return subResult{err: emperror.Wrap(err, "failed to update status")}
}

Check warning on line 40 in controllers/apps/v2beta1/update_emqx_status.go

View check run for this annotation

Codecov / codecov/patch

controllers/apps/v2beta1/update_emqx_status.go#L39-L40

Added lines #L39 - L40 were not covered by tests
}
instance.Status.ReplicantNodesStatus.Replicas = *instance.Spec.ReplicantTemplate.Spec.Replicas
if instance.Status.ReplicantNodesStatus.UpdateRevision != "" && instance.Status.ReplicantNodesStatus.CurrentRevision == "" {
instance.Status.ReplicantNodesStatus.CurrentRevision = instance.Status.ReplicantNodesStatus.UpdateRevision
}
}

updateSts, currentSts, oldStsList := getStateFulSetList(ctx, u.Client, instance)
Expand Down Expand Up @@ -102,18 +114,20 @@ func (u *updateStatus) reconcile(ctx context.Context, logger logr.Logger, instan
}

instance.Status.ReplicantNodes = replNodes
instance.Status.ReplicantNodesStatus.ReadyReplicas = 0
instance.Status.ReplicantNodesStatus.CurrentReplicas = 0
instance.Status.ReplicantNodesStatus.UpdateReplicas = 0
for _, node := range replNodes {
if node.NodeStatus == "running" {
instance.Status.ReplicantNodesStatus.ReadyReplicas++
}
if currentRs != nil && node.ControllerUID == currentRs.UID {
instance.Status.ReplicantNodesStatus.CurrentReplicas++
}
if updateRs != nil && node.ControllerUID == updateRs.UID {
instance.Status.ReplicantNodesStatus.UpdateReplicas++
if instance.Status.ReplicantNodesStatus != nil {
instance.Status.ReplicantNodesStatus.ReadyReplicas = 0
instance.Status.ReplicantNodesStatus.CurrentReplicas = 0
instance.Status.ReplicantNodesStatus.UpdateReplicas = 0
for _, node := range replNodes {
if node.NodeStatus == "running" {
instance.Status.ReplicantNodesStatus.ReadyReplicas++
}
if currentRs != nil && node.ControllerUID == currentRs.UID {
instance.Status.ReplicantNodesStatus.CurrentReplicas++
}
if updateRs != nil && node.ControllerUID == updateRs.UID {
instance.Status.ReplicantNodesStatus.UpdateReplicas++
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions controllers/apps/v2beta1/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ func getStateFulSetList(ctx context.Context, k8sClient client.Client, 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 +96,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
2 changes: 1 addition & 1 deletion e2e/v2beta1/e2e_rebalance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var _ = Describe("EMQX 5 Rebalance Test", Label("rebalance"), func() {
var r *appsv2beta1.Rebalance
BeforeEach(func() {
instance = genEMQX().DeepCopy()
instance.Spec.Image = "emqx/emqx-enterprise:5.1"
instance.Spec.Image = "emqx/emqx-enterprise:latest"
})

Context("EMQX is not found", func() {
Expand Down
Loading

0 comments on commit 0b460eb

Please sign in to comment.