Skip to content

Commit

Permalink
Merge pull request #2948 from davidfestal/global-informers-in-scheduling
Browse files Browse the repository at this point in the history
🌱 Use cached informers in scheduling controllers
  • Loading branch information
openshift-merge-robot authored Apr 6, 2023
2 parents 419199c + 3bb3d9d commit 85e6339
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
17 changes: 12 additions & 5 deletions pkg/reconciler/scheduling/placement/placement_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const (
func NewController(
kcpClusterClient kcpclientset.ClusterInterface,
namespaceInformer kcpcorev1informers.NamespaceClusterInformer,
locationInformer schedulingv1alpha1informers.LocationClusterInformer,
locationInformer, globalLocationInformer schedulingv1alpha1informers.LocationClusterInformer,
placementInformer schedulingv1alpha1informers.PlacementClusterInformer,
) (*controller, error) {
queue := workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), ControllerName)
Expand All @@ -78,8 +78,13 @@ func NewController(

namespaceLister: namespaceInformer.Lister(),

locationLister: locationInformer.Lister(),
locationIndexer: locationInformer.Informer().GetIndexer(),
listLocationsByPath: func(path logicalcluster.Path) ([]*schedulingv1alpha1.Location, error) {
objs, err := indexers.ByIndexWithFallback[*schedulingv1alpha1.Location](locationInformer.Informer().GetIndexer(), globalLocationInformer.Informer().GetIndexer(), indexers.ByLogicalClusterPath, path.String())
if err != nil {
return nil, err
}
return objs, nil
},

placementLister: placementInformer.Lister(),
placementIndexer: placementInformer.Informer().GetIndexer(),
Expand All @@ -96,6 +101,9 @@ func NewController(
indexers.AddIfNotPresentOrDie(locationInformer.Informer().GetIndexer(), cache.Indexers{
indexers.ByLogicalClusterPath: indexers.IndexByLogicalClusterPath,
})
indexers.AddIfNotPresentOrDie(globalLocationInformer.Informer().GetIndexer(), cache.Indexers{
indexers.ByLogicalClusterPath: indexers.IndexByLogicalClusterPath,
})

// namespaceBlocklist holds a set of namespaces that should never be synced from kcp to physical clusters.
var namespaceBlocklist = sets.NewString("kube-system", "kube-public", "kube-node-lease")
Expand Down Expand Up @@ -163,8 +171,7 @@ type controller struct {

namespaceLister corev1listers.NamespaceClusterLister

locationLister schedulingv1alpha1listers.LocationClusterLister
locationIndexer cache.Indexer
listLocationsByPath func(path logicalcluster.Path) ([]*schedulingv1alpha1.Location, error)

placementLister schedulingv1alpha1listers.PlacementClusterLister
placementIndexer cache.Indexer
Expand Down
13 changes: 0 additions & 13 deletions pkg/reconciler/scheduling/placement/placement_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"k8s.io/apimachinery/pkg/labels"
utilserrors "k8s.io/apimachinery/pkg/util/errors"

"github.com/kcp-dev/kcp/pkg/indexers"
schedulingv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/scheduling/v1alpha1"
)

Expand Down Expand Up @@ -67,18 +66,6 @@ func (c *controller) reconcile(ctx context.Context, placement *schedulingv1alpha
return utilserrors.NewAggregate(errs)
}

func (c *controller) listLocationsByPath(path logicalcluster.Path) ([]*schedulingv1alpha1.Location, error) {
objs, err := c.locationIndexer.ByIndex(indexers.ByLogicalClusterPath, path.String())
if err != nil {
return nil, err
}
ret := make([]*schedulingv1alpha1.Location, 0, len(objs))
for _, obj := range objs {
ret = append(ret, obj.(*schedulingv1alpha1.Location))
}
return ret, nil
}

func (c *controller) listNamespacesWithAnnotation(clusterName logicalcluster.Name) ([]*corev1.Namespace, error) {
items, err := c.namespaceLister.Cluster(clusterName).List(labels.Everything())
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/reconciler/workload/placement/placement_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func NewController(
indexers.ByLogicalClusterPathAndName: indexers.IndexByLogicalClusterPathAndName,
})

indexers.AddIfNotPresentOrDie(globalLocationInformer.Informer().GetIndexer(), cache.Indexers{
indexers.ByLogicalClusterPathAndName: indexers.IndexByLogicalClusterPathAndName,
})

logger := logging.WithReconciler(klog.Background(), ControllerName)

locationInformer.Informer().AddEventHandler(
Expand Down
1 change: 1 addition & 0 deletions tmc/pkg/server/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ func (s *Server) installSchedulingPlacementController(ctx context.Context, confi
kcpClusterClient,
s.Core.KubeSharedInformerFactory.Core().V1().Namespaces(),
s.Core.KcpSharedInformerFactory.Scheduling().V1alpha1().Locations(),
s.Core.CacheKcpSharedInformerFactory.Scheduling().V1alpha1().Locations(),
s.Core.KcpSharedInformerFactory.Scheduling().V1alpha1().Placements(),
)
if err != nil {
Expand Down

0 comments on commit 85e6339

Please sign in to comment.