Skip to content

Commit

Permalink
Add HA support for the visibility API
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Jan 9, 2024
1 parent ed81667 commit b12efb5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 5 deletions.
5 changes: 4 additions & 1 deletion pkg/controller/core/admissioncheck_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -220,7 +222,8 @@ func (r *AdmissionCheckReconciler) SetupWithManager(mgr ctrl.Manager) error {
}
return ctrl.NewControllerManagedBy(mgr).
For(&kueue.AdmissionCheck{}).
WithOptions(controller.Options{NeedLeaderElection: ptr.To(false)}).
WatchesRawSource(&source.Channel{Source: r.cqUpdateCh}, &handler).
WithEventFilter(r).
Complete(r)
Complete(WithLeadingManager(mgr, r))
}
5 changes: 4 additions & 1 deletion pkg/controller/core/clusterqueue_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand Down Expand Up @@ -613,13 +615,14 @@ func (r *ClusterQueueReconciler) SetupWithManager(mgr ctrl.Manager) error {
}
return ctrl.NewControllerManagedBy(mgr).
For(&kueue.ClusterQueue{}).
WithOptions(controller.Options{NeedLeaderElection: ptr.To(false)}).
Watches(&corev1.Namespace{}, &nsHandler).
WatchesRawSource(&source.Channel{Source: r.wlUpdateCh}, &wHandler).
WatchesRawSource(&source.Channel{Source: r.rfUpdateCh}, &rfHandler).
WatchesRawSource(&source.Channel{Source: r.acUpdateCh}, &acHandler).
WatchesRawSource(&source.Channel{Source: r.snapUpdateCh}, &snapHandler).
WithEventFilter(r).
Complete(r)
Complete(WithLeadingManager(mgr, r))
}

func (r *ClusterQueueReconciler) updateCqStatusIfChanged(
Expand Down
52 changes: 52 additions & 0 deletions pkg/controller/core/leader_aware_reconciler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package core

import (
"context"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

// WithLeadingManager returns a decorating reconcile.Reconciler that skips reconciliation requests
// until the manager has been elected leader. Once the manager is elected leader, the returned
// reconcile.Reconciler simply delegates to the provided reconcile.Reconciler.
func WithLeadingManager(mgr ctrl.Manager, reconciler reconcile.Reconciler) reconcile.Reconciler {
return &leaderAwareReconciler{
elected: mgr.Elected(),
delegate: reconciler,
}
}

type leaderAwareReconciler struct {
elected <-chan struct{}
delegate reconcile.Reconciler
}

var _ reconcile.Reconciler = (*leaderAwareReconciler)(nil)

func (r *leaderAwareReconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
select {
case <-r.elected:
// The manager has been elected leader, delegate reconciliation to the provided reconciler.
return r.delegate.Reconcile(ctx, request)
default:
// The manager hasn't been elected leader yet, skip reconciliation.
return ctrl.Result{}, nil
}
}
5 changes: 4 additions & 1 deletion pkg/controller/core/localqueue_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
Expand Down Expand Up @@ -254,10 +256,11 @@ func (r *LocalQueueReconciler) SetupWithManager(mgr ctrl.Manager) error {
}
return ctrl.NewControllerManagedBy(mgr).
For(&kueue.LocalQueue{}).
WithOptions(controller.Options{NeedLeaderElection: ptr.To(false)}).
WatchesRawSource(&source.Channel{Source: r.wlUpdateCh}, &qWorkloadHandler{}).
Watches(&kueue.ClusterQueue{}, &queueCQHandler).
WithEventFilter(r).
Complete(r)
Complete(WithLeadingManager(mgr, r))
}

func (r *LocalQueueReconciler) UpdateStatusIfChanged(
Expand Down
5 changes: 4 additions & 1 deletion pkg/controller/core/resourceflavor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand Down Expand Up @@ -255,9 +257,10 @@ func (r *ResourceFlavorReconciler) SetupWithManager(mgr ctrl.Manager) error {
}
return ctrl.NewControllerManagedBy(mgr).
For(&kueue.ResourceFlavor{}).
WithOptions(controller.Options{NeedLeaderElection: ptr.To(false)}).
WatchesRawSource(&source.Channel{Source: r.cqUpdateCh}, &handler).
WithEventFilter(r).
Complete(r)
Complete(WithLeadingManager(mgr, r))
}

func resourceFlavors(cq *kueue.ClusterQueue) sets.Set[kueue.ResourceFlavorReference] {
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/core/workload_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand Down Expand Up @@ -495,11 +496,12 @@ func (r *WorkloadReconciler) SetupWithManager(mgr ctrl.Manager) error {
}
return ctrl.NewControllerManagedBy(mgr).
For(&kueue.Workload{}).
WithOptions(controller.Options{NeedLeaderElection: ptr.To(false)}).
Watches(&corev1.LimitRange{}, ruh).
Watches(&nodev1.RuntimeClass{}, ruh).
Watches(&kueue.ClusterQueue{}, &workloadCqHandler{client: r.client}).
WithEventFilter(r).
Complete(r)
Complete(WithLeadingManager(mgr, r))
}

// admittedNotReadyWorkload returns as a pair of values. The first boolean determines
Expand Down

0 comments on commit b12efb5

Please sign in to comment.