@@ -187,7 +187,7 @@ func (r *SolverReconciler) handleFindCandidate(ctx context.Context, req ctrl.Req
187187 // If some PeeringCandidates are available, select one and book it
188188 if len (pc ) > 0 {
189189 // If some PeeringCandidates are available, select one and book it
190- selectedPc , err := r .selectAndBookPeeringCandidate (ctx , solver , pc )
190+ selectedPc , err := r .selectAndBookPeeringCandidate (pc )
191191 if err != nil {
192192 klog .Errorf ("Error when selecting and booking a candidate for Solver %s: %s" , req .NamespacedName .Name , err )
193193 return ctrl.Result {}, err
@@ -201,7 +201,6 @@ func (r *SolverReconciler) handleFindCandidate(ctx context.Context, req ctrl.Req
201201 }
202202 return ctrl.Result {}, nil
203203 }
204-
205204 // If no PeeringCandidate is available, Create a Discovery
206205 klog .Infof ("Solver %s has not found any candidate. Trying a Discovery" , req .NamespacedName .Name )
207206 solver .SetFindCandidateStatus (nodecorev1alpha1 .PhaseRunning )
@@ -213,6 +212,7 @@ func (r *SolverReconciler) handleFindCandidate(ctx context.Context, req ctrl.Req
213212 return ctrl.Result {}, err
214213 }
215214 return ctrl.Result {}, nil
215+
216216 case nodecorev1alpha1 .PhaseRunning :
217217 // Check solver expiration
218218 if tools .CheckExpiration (solver .Status .SolverPhase .LastChangeTime , flags .ExpirationPhaseRunning ) {
@@ -279,8 +279,16 @@ func (r *SolverReconciler) handleReserveAndBuy(ctx context.Context, req ctrl.Req
279279
280280 // Filter PeeringCandidate by SolverID
281281 for i := range pcList {
282- if pcList [i ].Spec .SolverID == solver . Name {
282+ if pcList [i ].Spec .Available {
283283 pc = pcList [i ]
284+ // Update the SolverID in the PeeringCandidate selected if it is different from the current Solver
285+ if pc .Spec .SolverID != solver .Name {
286+ pc .Spec .SolverID = solver .Name
287+ if err := r .Client .Update (ctx , & pc ); err != nil {
288+ klog .Errorf ("Error when updating PeeringCandidate %s for Solver %s: %s" , pc .Name , solver .Name , err )
289+ return ctrl.Result {}, err
290+ }
291+ }
284292 break
285293 }
286294 }
@@ -505,8 +513,8 @@ func (r *SolverReconciler) searchPeeringCandidates(ctx context.Context,
505513}
506514
507515// TODO: unify this logic with the one of the discovery controller.
508- func (r * SolverReconciler ) selectAndBookPeeringCandidate (ctx context. Context ,
509- solver * nodecorev1alpha1. Solver , pcList []advertisementv1alpha1.PeeringCandidate ) (* advertisementv1alpha1.PeeringCandidate , error ) {
516+ func (r * SolverReconciler ) selectAndBookPeeringCandidate (
517+ pcList []advertisementv1alpha1.PeeringCandidate ) (* advertisementv1alpha1.PeeringCandidate , error ) {
510518 // Select the first PeeringCandidate
511519
512520 var pc * advertisementv1alpha1.PeeringCandidate
@@ -515,39 +523,13 @@ func (r *SolverReconciler) selectAndBookPeeringCandidate(ctx context.Context,
515523 pc = & pcList [i ]
516524 // Select the first PeeringCandidate that is not reserved
517525 if pc .Spec .Available {
518- // Book the PeeringCandidate
519- pc .Spec .Available = false
520- pc .Spec .SolverID = solver .Name
521-
522- // Update the PeeringCandidate
523- if err := r .Update (ctx , pc ); err != nil {
524- klog .Errorf ("Error when updating PeeringCandidate %s: %s" , pc .Name , err )
525- continue
526- }
527-
528- // Getting the just updated PeeringCandidate
529- if err := r .Get (ctx , types.NamespacedName {Name : pc .Name , Namespace : pc .Namespace }, pc ); err != nil {
530- klog .Errorf ("Error when getting the reserved PeeringCandidate %s: %s" , pc .Name , err )
531- continue
532- }
526+ return pc , nil
533527
534- // Check if the PeeringCandidate has been reserved correctly
535- if pc .Spec .Available || pc .Spec .SolverID != solver .Name {
536- klog .Errorf ("Error when reserving PeeringCandidate %s. Trying with another one" , pc .Name )
537- continue
538- }
539-
540- break
541528 }
542529 }
543530
544- // check if a PeeringCandidate has been selected
545- if pc .Name == "" {
546- klog .Infof ("No PeeringCandidate selected" )
547- return nil , errors .NewNotFound (schema.GroupResource {Group : "advertisement" , Resource : "PeeringCandidate" }, "PeeringCandidate" )
548- }
549-
550- return pc , nil
531+ klog .Infof ("No PeeringCandidate selected" )
532+ return nil , errors .NewNotFound (schema.GroupResource {Group : "advertisement" , Resource : "PeeringCandidate" }, "PeeringCandidate" )
551533}
552534
553535func (r * SolverReconciler ) createOrGetDiscovery (ctx context.Context , solver * nodecorev1alpha1.Solver ) (* advertisementv1alpha1.Discovery , error ) {
0 commit comments