Skip to content

Commit 356fef2

Browse files
authored
chore: fix the random failure in the FlatInstanceOrdinal test (#9662)
1 parent 19589e6 commit 356fef2

3 files changed

Lines changed: 50 additions & 16 deletions

File tree

controllers/workloads/instanceset_controller_test.go

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -641,50 +641,78 @@ var _ = Describe("InstanceSet Controller", func() {
641641
})
642642

643643
Context("pod naming rule", func() {
644-
checkPodOrdinal := func(ordinals []int, exist bool) {
644+
checkPodOrdinal := func(ordinals []int, checkFunc func(podKey types.NamespacedName)) {
645645
for _, ordinal := range ordinals {
646646
podKey := types.NamespacedName{
647647
Namespace: itsObj.Namespace,
648648
Name: fmt.Sprintf("%v-%v", itsObj.Name, ordinal),
649649
}
650-
if exist {
651-
Eventually(testapps.CheckObjExists(&testCtx, podKey, &corev1.Pod{}, true)).Should(Succeed())
652-
} else {
653-
Eventually(testapps.CheckObjExists(&testCtx, podKey, &corev1.Pod{}, false)).Should(Succeed())
654-
}
650+
checkFunc(podKey)
655651
}
656652
}
657653

654+
eventuallyExist := func(podKey types.NamespacedName) {
655+
Eventually(testapps.CheckObjExists(&testCtx, podKey, &corev1.Pod{}, true)).Should(Succeed())
656+
}
657+
658+
eventuallyNotExist := func(podKey types.NamespacedName) {
659+
Eventually(testapps.CheckObjExists(&testCtx, podKey, &corev1.Pod{}, false)).Should(Succeed())
660+
}
661+
662+
consistentlyExist := func(podKey types.NamespacedName) {
663+
Consistently(testapps.CheckObjExists(&testCtx, podKey, &corev1.Pod{}, true)).Should(Succeed())
664+
}
665+
666+
consistentlyNotExist := func(podKey types.NamespacedName) {
667+
Consistently(testapps.CheckObjExists(&testCtx, podKey, &corev1.Pod{}, false)).Should(Succeed())
668+
}
669+
658670
It("works with FlatInstanceOrdinal", func() {
659671
createITSObj(itsName, func(f *testapps.MockInstanceSetFactory) {
660672
f.SetFlatInstanceOrdinal(true)
661673
f.SetPodManagementPolicy(appsv1.ParallelPodManagement)
662674
f.SetReplicas(3)
663675
})
664676

665-
checkPodOrdinal([]int{0, 1, 2}, true)
677+
checkPodOrdinal([]int{0, 1, 2}, eventuallyExist)
666678
mockPodReady(itsObj.Name+"-0", itsObj.Name+"-1", itsObj.Name+"-2")
679+
By("check its status")
680+
Eventually(testapps.CheckObj(&testCtx, itsKey, func(g Gomega, its *workloads.InstanceSet) {
681+
g.Expect(its.Status.Ordinals).Should(HaveExactElements(int32(0), int32(1), int32(2)))
682+
})).Should(Succeed())
667683

668684
// offline one instance
669-
Eventually(testapps.GetAndChangeObj(&testCtx, itsKey, func(its *workloads.InstanceSet) {
685+
Expect(testapps.GetAndChangeObj(&testCtx, itsKey, func(its *workloads.InstanceSet) {
670686
its.Spec.Replicas = ptr.To[int32](2)
671687
its.Spec.OfflineInstances = []string{itsObj.Name + "-1"}
688+
})()).Should(Succeed())
689+
checkPodOrdinal([]int{1}, eventuallyNotExist)
690+
By("check its status")
691+
Eventually(testapps.CheckObj(&testCtx, itsKey, func(g Gomega, its *workloads.InstanceSet) {
692+
g.Expect(its.Status.Ordinals).Should(HaveExactElements(int32(0), int32(2)))
672693
})).Should(Succeed())
673-
checkPodOrdinal([]int{1}, false)
674694

675695
// scale up
676-
Eventually(testapps.GetAndChangeObj(&testCtx, itsKey, func(its *workloads.InstanceSet) {
696+
Expect(testapps.GetAndChangeObj(&testCtx, itsKey, func(its *workloads.InstanceSet) {
677697
its.Spec.Replicas = ptr.To[int32](4)
678-
})).Should(Succeed())
679-
checkPodOrdinal([]int{0, 2, 3, 4}, true)
698+
})()).Should(Succeed())
699+
checkPodOrdinal([]int{0, 2, 3, 4}, eventuallyExist)
680700
mockPodReady(itsObj.Name+"-3", itsObj.Name+"-4")
701+
By("check its status")
702+
Eventually(testapps.CheckObj(&testCtx, itsKey, func(g Gomega, its *workloads.InstanceSet) {
703+
g.Expect(its.Status.Ordinals).Should(HaveExactElements(int32(0), int32(2), int32(3), int32(4)))
704+
})).Should(Succeed())
681705

682-
// delete OfflineInstances will not affect running instance
683-
Eventually(testapps.GetAndChangeObj(&testCtx, itsKey, func(its *workloads.InstanceSet) {
706+
// delete OfflineInstances will not affect running instances
707+
Expect(testapps.GetAndChangeObj(&testCtx, itsKey, func(its *workloads.InstanceSet) {
684708
its.Spec.OfflineInstances = []string{}
709+
})()).Should(Succeed())
710+
checkPodOrdinal([]int{0, 2, 3, 4}, consistentlyExist)
711+
checkPodOrdinal([]int{1}, consistentlyNotExist)
712+
By("check its status")
713+
Consistently(testapps.CheckObj(&testCtx, itsKey, func(g Gomega, its *workloads.InstanceSet) {
714+
g.Expect(its.Status.Ordinals).Should(HaveExactElements(int32(0), int32(2), int32(3), int32(4)))
685715
})).Should(Succeed())
686-
checkPodOrdinal([]int{0, 2, 3, 4}, true)
687-
checkPodOrdinal([]int{1}, false)
688716
})
689717
})
690718
})

pkg/controller/instanceset/reconciler_status.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package instanceset
2121

2222
import (
2323
"encoding/json"
24+
"slices"
2425
"sort"
2526
"time"
2627

@@ -145,6 +146,7 @@ func (r *statusReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (kubebuilder
145146
}
146147
its.Status.Replicas = replicas
147148
its.Status.Ordinals = ordinals
149+
slices.Sort(its.Status.Ordinals)
148150
its.Status.ReadyReplicas = readyReplicas
149151
its.Status.AvailableReplicas = availableReplicas
150152
its.Status.CurrentReplicas = currentReplicas
@@ -215,6 +217,7 @@ func buildTemplatesStatus(template2TemplatesStatus map[string]*workloads.Instanc
215217
if len(templateName) == 0 {
216218
continue
217219
}
220+
slices.Sort(templateStatus.Ordinals)
218221
templatesStatus = append(templatesStatus, *templateStatus)
219222
}
220223
sort.Slice(templatesStatus, func(i, j int) bool {

pkg/controller/instanceset2/reconciler_status.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package instanceset2
2121

2222
import (
2323
"encoding/json"
24+
"slices"
2425
"sort"
2526
"time"
2627

@@ -134,6 +135,7 @@ func (r *statusReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (kubebuilder
134135
}
135136
its.Status.Replicas = replicas
136137
its.Status.Ordinals = ordinals
138+
slices.Sort(its.Status.Ordinals)
137139
its.Status.ReadyReplicas = readyReplicas
138140
its.Status.AvailableReplicas = availableReplicas
139141
its.Status.CurrentReplicas = currentReplicas
@@ -204,6 +206,7 @@ func buildTemplatesStatus(template2TemplatesStatus map[string]*workloads.Instanc
204206
if len(templateName) == 0 {
205207
continue
206208
}
209+
slices.Sort(templateStatus.Ordinals)
207210
templatesStatus = append(templatesStatus, *templateStatus)
208211
}
209212
sort.Slice(templatesStatus, func(i, j int) bool {

0 commit comments

Comments
 (0)