Skip to content

Commit 5640085

Browse files
committed
improve the test and add namespace selector
Signed-off-by: Ryan Zhang <[email protected]>
1 parent 599fd2f commit 5640085

File tree

2 files changed

+225
-287
lines changed

2 files changed

+225
-287
lines changed

pkg/scheduler/scheduler.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -285,17 +285,19 @@ func (s *Scheduler) cleanUpAllBindingsFor(ctx context.Context, placement fleetv1
285285
//
286286
// Note that the listing is performed using the uncached client; this is to ensure that all related
287287
// bindings can be found, even if they have not been synced to the cache yet.
288+
var listOptions []client.ListOption
289+
listOptions = append(listOptions, client.MatchingLabels{
290+
fleetv1beta1.CRPTrackingLabel: string(placementKey),
291+
})
288292
var bindingList fleetv1beta1.BindingObjList
289293
if placement.GetNamespace() == "" {
290294
bindingList = &fleetv1beta1.ClusterResourceBindingList{}
291295
} else {
292296
bindingList = &fleetv1beta1.ResourceBindingList{}
293-
}
294-
listOptions := client.MatchingLabels{
295-
fleetv1beta1.CRPTrackingLabel: string(placementKey),
297+
listOptions = append(listOptions, client.InNamespace(placement.GetNamespace()))
296298
}
297299
// TO-DO (chenyu1): this is a very expensive op; explore options for optimization.
298-
if err := s.uncachedReader.List(ctx, bindingList, listOptions); err != nil {
300+
if err := s.uncachedReader.List(ctx, bindingList, listOptions...); err != nil {
299301
klog.ErrorS(err, "Failed to list all bindings", "placement", placementRef)
300302
return controller.NewAPIServerError(false, err)
301303
}
@@ -337,27 +339,30 @@ func (s *Scheduler) cleanUpAllBindingsFor(ctx context.Context, placement fleetv1
337339
}
338340

339341
// lookupLatestPolicySnapshot returns the latest (i.e., active) policy snapshot associated with a placement.
342+
// TODO: move this to a common lib
340343
func (s *Scheduler) lookupLatestPolicySnapshot(ctx context.Context, placement fleetv1beta1.PlacementObj) (fleetv1beta1.PolicySnapshotObj, error) {
341344
placementRef := klog.KObj(placement)
342345

343346
// Get the placement key which handles both cluster-scoped and namespaced placements
344347
placementKey := controller.GetPlacementKeyFromObj(placement)
345-
348+
var listOptions []client.ListOption
349+
labelSelector := labels.SelectorFromSet(labels.Set{
350+
fleetv1beta1.CRPTrackingLabel: string(placementKey),
351+
fleetv1beta1.IsLatestSnapshotLabel: strconv.FormatBool(true),
352+
})
353+
listOptions = append(listOptions, &client.ListOptions{LabelSelector: labelSelector})
346354
// Find out the latest policy snapshot associated with the placement.
347355
var policySnapshotList fleetv1beta1.PolicySnapshotList
348356
if placement.GetNamespace() == "" {
349357
policySnapshotList = &fleetv1beta1.ClusterSchedulingPolicySnapshotList{}
350358
} else {
351359
policySnapshotList = &fleetv1beta1.SchedulingPolicySnapshotList{}
360+
listOptions = append(listOptions, client.InNamespace(placement.GetNamespace()))
352361
}
353-
labelSelector := labels.SelectorFromSet(labels.Set{
354-
fleetv1beta1.CRPTrackingLabel: string(placementKey),
355-
fleetv1beta1.IsLatestSnapshotLabel: strconv.FormatBool(true),
356-
})
357-
listOptions := &client.ListOptions{LabelSelector: labelSelector}
362+
358363
// The scheduler lists with a cached client; this does not have any consistency concern as sources
359364
// will always trigger the scheduler when a new policy snapshot is created.
360-
if err := s.client.List(ctx, policySnapshotList, listOptions); err != nil {
365+
if err := s.client.List(ctx, policySnapshotList, listOptions...); err != nil {
361366
klog.ErrorS(err, "Failed to list policy snapshots of a placement", "placement", placementRef)
362367
return nil, controller.NewAPIServerError(true, err)
363368
}

0 commit comments

Comments
 (0)