k8s.io/kubernetes@v1.29.3/pkg/scheduler/internal/queue/testing.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package queue
    18  
    19  import (
    20  	"context"
    21  
    22  	"k8s.io/apimachinery/pkg/runtime"
    23  	"k8s.io/client-go/informers"
    24  	"k8s.io/client-go/kubernetes/fake"
    25  	"k8s.io/kubernetes/pkg/scheduler/framework"
    26  )
    27  
    28  // NewTestQueue creates a priority queue with an empty informer factory.
    29  func NewTestQueue(ctx context.Context, lessFn framework.LessFunc, opts ...Option) *PriorityQueue {
    30  	return NewTestQueueWithObjects(ctx, lessFn, nil, opts...)
    31  }
    32  
    33  // NewTestQueueWithObjects creates a priority queue with an informer factory
    34  // populated with the provided objects.
    35  func NewTestQueueWithObjects(
    36  	ctx context.Context,
    37  	lessFn framework.LessFunc,
    38  	objs []runtime.Object,
    39  	opts ...Option,
    40  ) *PriorityQueue {
    41  	informerFactory := informers.NewSharedInformerFactory(fake.NewSimpleClientset(objs...), 0)
    42  	return NewTestQueueWithInformerFactory(ctx, lessFn, informerFactory, opts...)
    43  }
    44  
    45  func NewTestQueueWithInformerFactory(
    46  	ctx context.Context,
    47  	lessFn framework.LessFunc,
    48  	informerFactory informers.SharedInformerFactory,
    49  	opts ...Option,
    50  ) *PriorityQueue {
    51  	pq := NewPriorityQueue(lessFn, informerFactory, opts...)
    52  	informerFactory.Start(ctx.Done())
    53  	informerFactory.WaitForCacheSync(ctx.Done())
    54  	return pq
    55  }