@@ -47,6 +47,13 @@ const (
4747 placementNoMigrationPolling = time .Second
4848)
4949
50+ type migrationTargetExpectation int
51+
52+ const (
53+ migrationTargetMustMatch migrationTargetExpectation = iota
54+ migrationTargetMustDiffer
55+ )
56+
5057var _ = Describe ("VirtualMachineAffinityAndToleration" , Ordered , func () {
5158 var (
5259 f * framework.Framework
@@ -161,9 +168,7 @@ var _ = Describe("VirtualMachineAffinityAndToleration", Ordered, func() {
161168 err := f .GenericClient ().Update (ctx , vmC )
162169 Expect (err ).NotTo (HaveOccurred ())
163170
164- waitForFreshVMMigration (ctx , f , crclient .ObjectKeyFromObject (vmC ), startedAt , sourceNode , nodeA , false , framework .MaxTimeout )
165- waitForVMMigrationFinished (ctx , f , crclient .ObjectKeyFromObject (vmC ), framework .LongTimeout )
166- waitForVMActivePodReady (ctx , f , crclient .ObjectKeyFromObject (vmC ), framework .LongTimeout )
171+ waitForStabilizedVMMigration (ctx , f , crclient .ObjectKeyFromObject (vmC ), startedAt , sourceNode , nodeA , migrationTargetMustDiffer , framework .MaxTimeout )
167172 })
168173
169174 var migratedNodeC string
@@ -183,9 +188,7 @@ var _ = Describe("VirtualMachineAffinityAndToleration", Ordered, func() {
183188 err := f .GenericClient ().Update (ctx , vmC )
184189 Expect (err ).NotTo (HaveOccurred ())
185190
186- waitForFreshVMMigration (ctx , f , crclient .ObjectKeyFromObject (vmC ), startedAt , migratedNodeC , nodeA , true , framework .MaxTimeout )
187- waitForVMMigrationFinished (ctx , f , crclient .ObjectKeyFromObject (vmC ), framework .LongTimeout )
188- waitForVMActivePodReady (ctx , f , crclient .ObjectKeyFromObject (vmC ), framework .LongTimeout )
191+ waitForStabilizedVMMigration (ctx , f , crclient .ObjectKeyFromObject (vmC ), startedAt , migratedNodeC , nodeA , migrationTargetMustMatch , framework .MaxTimeout )
189192 })
190193
191194 By ("Verifying vm-c returned to vm-a node via status.nodeName" , func () {
@@ -244,9 +247,7 @@ var _ = Describe("VirtualMachineAffinityAndToleration", Ordered, func() {
244247 err = f .GenericClient ().Update (ctx , vmNodeSelector )
245248 Expect (err ).NotTo (HaveOccurred ())
246249
247- waitForFreshVMMigration (ctx , f , crclient .ObjectKeyFromObject (vmNodeSelector ), startedAt , sourceNode , targetNode , true , framework .MaxTimeout )
248- waitForVMMigrationFinished (ctx , f , crclient .ObjectKeyFromObject (vmNodeSelector ), framework .LongTimeout )
249- waitForVMActivePodReady (ctx , f , crclient .ObjectKeyFromObject (vmNodeSelector ), framework .LongTimeout )
250+ waitForStabilizedVMMigration (ctx , f , crclient .ObjectKeyFromObject (vmNodeSelector ), startedAt , sourceNode , targetNode , migrationTargetMustMatch , framework .MaxTimeout )
250251 })
251252
252253 By ("Verifying the nodeSelector migration result via status.nodeName" , func () {
@@ -308,9 +309,7 @@ var _ = Describe("VirtualMachineAffinityAndToleration", Ordered, func() {
308309 err = f .GenericClient ().Update (ctx , vmNodeAffinity )
309310 Expect (err ).NotTo (HaveOccurred ())
310311
311- waitForFreshVMMigration (ctx , f , crclient .ObjectKeyFromObject (vmNodeAffinity ), startedAt , sourceNode , targetNode , true , framework .MaxTimeout )
312- waitForVMMigrationFinished (ctx , f , crclient .ObjectKeyFromObject (vmNodeAffinity ), framework .LongTimeout )
313- waitForVMActivePodReady (ctx , f , crclient .ObjectKeyFromObject (vmNodeAffinity ), framework .LongTimeout )
312+ waitForStabilizedVMMigration (ctx , f , crclient .ObjectKeyFromObject (vmNodeAffinity ), startedAt , sourceNode , targetNode , migrationTargetMustMatch , framework .MaxTimeout )
314313 })
315314
316315 By ("Verifying the nodeAffinity migration result via status.nodeName" , func () {
@@ -408,14 +407,14 @@ func vmAffinityTerm(vmName string) v1alpha2.VirtualMachineAndPodAffinityTerm {
408407 }
409408}
410409
411- func waitForFreshVMMigration (
410+ func waitForStabilizedVMMigration (
412411 ctx context.Context ,
413412 f * framework.Framework ,
414413 key crclient.ObjectKey ,
415414 notBefore time.Time ,
416415 sourceNode string ,
417416 targetNode string ,
418- expectSameTarget bool ,
417+ targetExpectation migrationTargetExpectation ,
419418 timeout time.Duration ,
420419) {
421420 GinkgoHelper ()
@@ -429,17 +428,29 @@ func waitForFreshVMMigration(
429428 g .Expect (state .StartTimestamp ).NotTo (BeNil ())
430429 g .Expect (state .StartTimestamp .UTC ().Before (notBefore )).To (BeFalse (), "expected a fresh migration" )
431430 g .Expect (state .EndTimestamp .IsZero ()).To (BeFalse (), "migration is not completed" )
431+
432432 if state .Result == v1alpha2 .MigrationResultFailed {
433433 Fail (fmt .Sprintf ("migration failed for vm %s/%s: %s" , vm .Namespace , vm .Name , migrationFailureDetails (vm )))
434434 }
435+
435436 g .Expect (state .Result ).To (Equal (v1alpha2 .MigrationResultSucceeded ))
437+
436438 g .Expect (state .Source .Node ).To (Equal (sourceNode ))
437439 g .Expect (vm .Status .Node ).To (Equal (state .Target .Node ))
438- if expectSameTarget {
440+
441+ switch targetExpectation {
442+ case migrationTargetMustMatch :
439443 g .Expect (state .Target .Node ).To (Equal (targetNode ))
440- } else {
444+ case migrationTargetMustDiffer :
441445 g .Expect (state .Target .Node ).NotTo (Equal (targetNode ))
446+ default :
447+ Fail (fmt .Sprintf ("unknown migration target expectation: %d" , targetExpectation ))
442448 }
449+
450+ activePod := getActiveVirtualMachinePod (ctx , f , vm )
451+ g .Expect (activePod .Spec .NodeName ).To (Equal (vm .Status .Node ))
452+ g .Expect (activePod .Status .Phase ).To (Equal (corev1 .PodRunning ))
453+ g .Expect (isPodReady (activePod )).To (BeTrue (), "active pod %s/%s is not ready" , activePod .Namespace , activePod .Name )
443454 }).WithTimeout (timeout ).WithPolling (time .Second ).Should (Succeed ())
444455}
445456
@@ -455,65 +466,32 @@ func migrationFailureDetails(vm *v1alpha2.VirtualMachine) string {
455466 return fmt .Sprintf ("result=%s source=%s target=%s current=%s" , vm .Status .MigrationState .Result , vm .Status .MigrationState .Source .Node , vm .Status .MigrationState .Target .Node , vm .Status .Node )
456467}
457468
458- func waitForVMMigrationFinished (
459- ctx context.Context ,
460- f * framework.Framework ,
461- key crclient.ObjectKey ,
462- timeout time.Duration ,
463- ) {
469+ func getActiveVirtualMachinePod (ctx context.Context , f * framework.Framework , vm * v1alpha2.VirtualMachine ) * corev1.Pod {
464470 GinkgoHelper ()
465471
466- Eventually (func (g Gomega ) {
467- vm := getVirtualMachine (ctx , f , key .Name )
468- state := vm .Status .MigrationState
469- g .Expect (state ).NotTo (BeNil ())
470- g .Expect (state .EndTimestamp .IsZero ()).To (BeFalse (), "migration is not completed" )
471-
472- for _ , condition := range vm .Status .Conditions {
473- if condition .Type == vmcondition .TypeMigrating .String () {
474- g .Expect (condition .Status ).To (Equal (metav1 .ConditionFalse ))
475- return
476- }
472+ activePodName := ""
473+ for _ , pod := range vm .Status .VirtualMachinePods {
474+ if pod .Active {
475+ activePodName = pod .Name
476+ break
477477 }
478- }).WithTimeout (timeout ).WithPolling (time .Second ).Should (Succeed ())
479- }
480-
481- func waitForVMActivePodReady (
482- ctx context.Context ,
483- f * framework.Framework ,
484- key crclient.ObjectKey ,
485- timeout time.Duration ,
486- ) {
487- GinkgoHelper ()
478+ }
479+ Expect (activePodName ).NotTo (BeEmpty (), "no active pod found for vm %s/%s" , vm .Namespace , vm .Name )
488480
489- Eventually (func (g Gomega ) {
490- vm := getVirtualMachine (ctx , f , key .Name )
491- g .Expect (vm .Status .Node ).NotTo (BeEmpty ())
481+ pod := & corev1.Pod {}
482+ err := f .GenericClient ().Get (ctx , crclient.ObjectKey {Namespace : vm .Namespace , Name : activePodName }, pod )
483+ Expect (err ).NotTo (HaveOccurred ())
484+ return pod
485+ }
492486
493- activePodName := ""
494- for _ , pod := range vm .Status .VirtualMachinePods {
495- if pod .Active {
496- activePodName = pod .Name
497- break
498- }
487+ func isPodReady (pod * corev1.Pod ) bool {
488+ for _ , condition := range pod .Status .Conditions {
489+ if condition .Type == corev1 .PodReady && condition .Status == corev1 .ConditionTrue {
490+ return true
499491 }
500- g .Expect (activePodName ).NotTo (BeEmpty (), "no active pod found for vm %s/%s" , vm .Namespace , vm .Name )
501-
502- pod := & corev1.Pod {}
503- err := f .GenericClient ().Get (ctx , crclient.ObjectKey {Namespace : vm .Namespace , Name : activePodName }, pod )
504- g .Expect (err ).NotTo (HaveOccurred ())
505- g .Expect (pod .Spec .NodeName ).To (Equal (vm .Status .Node ))
506- g .Expect (pod .Status .Phase ).To (Equal (corev1 .PodRunning ))
507-
508- ready := false
509- for _ , condition := range pod .Status .Conditions {
510- if condition .Type == corev1 .PodReady && condition .Status == corev1 .ConditionTrue {
511- ready = true
512- break
513- }
514- }
515- g .Expect (ready ).To (BeTrue (), "active pod %s/%s is not ready" , pod .Namespace , pod .Name )
516- }).WithTimeout (timeout ).WithPolling (time .Second ).Should (Succeed ())
492+ }
493+
494+ return false
517495}
518496
519497func assertNoVMMigration (
0 commit comments