Skip to content

Commit

Permalink
Improve Watch/Reconcile triggers of lbs controller (#283)
Browse files Browse the repository at this point in the history
Co-authored-by: Maximilian Geberl <48486938+dergeberl@users.noreply.github.com>
  • Loading branch information
maboehm and dergeberl committed Feb 14, 2024
1 parent 69cbb54 commit 19dcae4
Showing 1 changed file with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package loadbalancerset
import (
"context"
"crypto/rand"
"errors"
"fmt"
"time"

Expand All @@ -21,8 +22,10 @@ import (
"k8s.io/client-go/tools/record"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/ratelimiter"
)

Expand Down Expand Up @@ -94,21 +97,16 @@ func (r *LoadBalancerSetReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}
}

// TODO: patchStatus *after* we've reconciled the replicas
if err := r.patchStatus(ctx, &set, readyMachineCount, hasKeepalivedMaster); err != nil {
return ctrl.Result{}, err
}
res, err := r.reconcileReplicas(ctx, &set, childMachines.Items, deletedMachineCount)

if res, err := r.reconcileReplicas(
ctx,
&set,
childMachines.Items,
deletedMachineCount,
); err != nil || res.Requeue || res.RequeueAfter != 0 {
return res, err
if patchErr := r.patchStatus(ctx, &set, readyMachineCount, hasKeepalivedMaster); patchErr != nil {
return ctrl.Result{}, errors.Join(err, patchErr)
}

return ctrl.Result{RequeueAfter: 10 * time.Second}, nil
if res.RequeueAfter == 0 {
res.RequeueAfter = 10 * time.Second
}
return res, nil
}

func (r *LoadBalancerSetReconciler) deletionRoutine(
Expand Down Expand Up @@ -345,7 +343,18 @@ func (r *LoadBalancerSetReconciler) deleteAllMachines(ctx context.Context, set *

func (r *LoadBalancerSetReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&yawolv1beta1.LoadBalancerSet{}).
For(
&yawolv1beta1.LoadBalancerSet{},
builder.WithPredicates(predicate.Or(
// Don't react to status updates. We usually reconcile every 10
// seconds (2 seconds after updating machines) anyways, and
// triggering reconciles after writing the status ourselves is not
// necessary.
predicate.GenerationChangedPredicate{},
predicate.AnnotationChangedPredicate{},
predicate.LabelChangedPredicate{},
)),
).
WithOptions(controller.Options{
MaxConcurrentReconciles: r.WorkerCount,
RateLimiter: r.RateLimiter,
Expand Down

0 comments on commit 19dcae4

Please sign in to comment.