Skip to content

Commit f5e8e2b

Browse files
authored
Bug fix in Solver controller: expected behaviour when dealing with multiple Solver CRs (#63)
Correct PC Spec when reserving it Solve else statement that prevents linter to correct execution Solve line length that prevents linter to correct execution
1 parent e4d8e52 commit f5e8e2b

2 files changed

Lines changed: 19 additions & 34 deletions

File tree

pkg/rear-controller/contract-manager/reservation_controller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func (r *ReservationReconciler) handleReserve(ctx context.Context,
163163

164164
// Set the peering candidate as not available
165165
peeringCandidate.Spec.Available = false
166+
peeringCandidate.Spec.SolverID = reservation.Spec.SolverID
166167
if err := r.Update(ctx, peeringCandidate); err != nil {
167168
klog.Errorf("Error when updating PeeringCandidate %s status before reconcile: %s", req.NamespacedName, err)
168169
return ctrl.Result{}, err
@@ -185,6 +186,7 @@ func (r *ReservationReconciler) handleReserve(ctx context.Context,
185186

186187
// Set the peering candidate as available again
187188
peeringCandidate.Spec.Available = true
189+
peeringCandidate.Spec.SolverID = ""
188190
if err := r.Update(ctx, peeringCandidate); err != nil {
189191
klog.Errorf("Error when updating PeeringCandidate %s status before reconcile: %s", req.NamespacedName, err)
190192
return ctrl.Result{}, err
@@ -299,6 +301,7 @@ func (r *ReservationReconciler) handlePurchase(ctx context.Context,
299301
}
300302

301303
peeringCandidate.Spec.Available = true
304+
peeringCandidate.Spec.SolverID = ""
302305
if err := r.Update(ctx, &peeringCandidate); err != nil {
303306
klog.Errorf("Error when updating PeeringCandidate %s status before reconcile: %s", req.NamespacedName, err)
304307
return ctrl.Result{}, err

pkg/rear-manager/solver_controller.go

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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

553535
func (r *SolverReconciler) createOrGetDiscovery(ctx context.Context, solver *nodecorev1alpha1.Solver) (*advertisementv1alpha1.Discovery, error) {

0 commit comments

Comments
 (0)