@@ -53,6 +53,36 @@ const (
5353 defaultPollTime = 10 * time .Second
5454)
5555
56+ // popInstance removes and returns the last instance UUID from the slice.
57+ // Returns the modified slice and the UUID (empty string if slice was empty).
58+ func popInstance (instances []string ) (remaining []string , uuid string ) {
59+ if len (instances ) == 0 {
60+ return instances , ""
61+ }
62+ return instances [:len (instances )- 1 ], instances [len (instances )- 1 ]
63+ }
64+
65+ // peekInstance returns the last instance UUID without removing it.
66+ // Returns empty string if the slice is empty.
67+ func peekInstance (instances []string ) string {
68+ if len (instances ) == 0 {
69+ return ""
70+ }
71+ return instances [len (instances )- 1 ]
72+ }
73+
74+ // moveToBack moves the last instance to the front of the slice,
75+ // effectively deprioritizing it. Returns the modified slice.
76+ func moveToBack (instances []string ) []string {
77+ if len (instances ) < 2 {
78+ return instances
79+ }
80+ uuid := instances [len (instances )- 1 ]
81+ copy (instances [1 :], instances [:len (instances )- 1 ])
82+ instances [0 ] = uuid
83+ return instances
84+ }
85+
5686// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=evictions,verbs=get;list;watch;create;update;patch;delete
5787// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=evictions/status,verbs=get;update;patch
5888// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=evictions/finalizers,verbs=update
@@ -224,9 +254,11 @@ func (r *EvictionReconciler) handleNotFound(ctx context.Context, eviction, base
224254 if base == nil {
225255 base = eviction .DeepCopy ()
226256 }
227- instances := & eviction .Status .OutstandingInstances
228- uuid := (* instances )[len (* instances )- 1 ]
229- * instances = (* instances )[:len (* instances )- 1 ]
257+ var uuid string
258+ eviction .Status .OutstandingInstances , uuid = popInstance (eviction .Status .OutstandingInstances )
259+ if uuid == "" {
260+ return nil
261+ }
230262 meta .SetStatusCondition (& eviction .Status .Conditions , metav1.Condition {
231263 Type : kvmv1 .ConditionTypeMigration ,
232264 Status : metav1 .ConditionFalse ,
@@ -238,8 +270,10 @@ func (r *EvictionReconciler) handleNotFound(ctx context.Context, eviction, base
238270
239271func (r * EvictionReconciler ) evictNext (ctx context.Context , eviction * kvmv1.Eviction ) (ctrl.Result , error ) {
240272 base := eviction .DeepCopy ()
241- instances := & eviction .Status .OutstandingInstances
242- uuid := (* instances )[len (* instances )- 1 ]
273+ uuid := peekInstance (eviction .Status .OutstandingInstances )
274+ if uuid == "" {
275+ return ctrl.Result {}, nil
276+ }
243277 log := logger .FromContext (ctx ).WithName ("Evict" ).WithValues ("server" , uuid )
244278 logger .IntoContext (ctx , log )
245279
@@ -265,8 +299,7 @@ func (r *EvictionReconciler) evictNext(ctx context.Context, eviction *kvmv1.Evic
265299 case "ERROR" :
266300 // Needs manual intervention (or another operator fixes it)
267301 // put it at the end of the list (beginning of array)
268- copy ((* instances )[1 :], (* instances )[:len (* instances )- 1 ])
269- (* instances )[0 ] = uuid
302+ eviction .Status .OutstandingInstances = moveToBack (eviction .Status .OutstandingInstances )
270303 log .Info ("error" , "faultMessage" , vm .Fault .Message )
271304 meta .SetStatusCondition (& eviction .Status .Conditions , metav1.Condition {
272305 Type : kvmv1 .ConditionTypeMigration ,
@@ -303,14 +336,13 @@ func (r *EvictionReconciler) evictNext(ctx context.Context, eviction *kvmv1.Evic
303336 }
304337
305338 // All done
306- * instances = ( * instances )[: len ( * instances ) - 1 ]
339+ eviction . Status . OutstandingInstances , _ = popInstance ( eviction . Status . OutstandingInstances )
307340 return ctrl.Result {}, r .updateStatus (ctx , eviction , base )
308341 }
309342
310343 if vm .TaskState == "deleting" { //nolint:gocritic
311344 // We just have to wait for it to be gone. Try the next one.
312- copy ((* instances )[1 :], (* instances )[:len (* instances )- 1 ])
313- (* instances )[0 ] = uuid
345+ eviction .Status .OutstandingInstances = moveToBack (eviction .Status .OutstandingInstances )
314346
315347 meta .SetStatusCondition (& eviction .Status .Conditions , metav1.Condition {
316348 Type : kvmv1 .ConditionTypeMigration ,
0 commit comments