volcano.sh/volcano@v1.9.0/pkg/scheduler/api/shared_device_pool.go (about)

     1  /*
     2   Copyright 2023 The Volcano 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 api
    18  
    19  import (
    20  	"sync"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	"k8s.io/client-go/kubernetes"
    24  
    25  	"volcano.sh/volcano/pkg/scheduler/api/devices/nvidia/gpushare"
    26  	"volcano.sh/volcano/pkg/scheduler/api/devices/nvidia/vgpu"
    27  )
    28  
    29  const (
    30  	GPUSharingDevice = "GpuShare"
    31  )
    32  
    33  type Devices interface {
    34  	//following two functions used in node_info
    35  	//AddResource is to add the corresponding device resource of this 'pod' into current scheduler cache
    36  	AddResource(pod *v1.Pod)
    37  	//SubResource is to subtract the corresponding device resource of this 'pod' from current scheduler cache
    38  	SubResource(pod *v1.Pod)
    39  
    40  	//following four functions used in predicate
    41  	//HasDeviceRequest checks if the 'pod' request this device
    42  	HasDeviceRequest(pod *v1.Pod) bool
    43  	// FilterNode checks if the 'pod' fit in current node
    44  	// The first return value represents the filtering result, and the value range is "0, 1, 2, 3"
    45  	// 0: Success
    46  	// Success means that plugin ran correctly and found pod schedulable.
    47  
    48  	// 1: Error
    49  	// Error is used for internal plugin errors, unexpected input, etc.
    50  
    51  	// 2: Unschedulable
    52  	// Unschedulable is used when a plugin finds a pod unschedulable. The scheduler might attempt to
    53  	// preempt other pods to get this pod scheduled. Use UnschedulableAndUnresolvable to make the
    54  	// scheduler skip preemption.
    55  	// The accompanying status message should explain why the pod is unschedulable.
    56  
    57  	// 3: UnschedulableAndUnresolvable
    58  	// UnschedulableAndUnresolvable is used when a plugin finds a pod unschedulable and
    59  	// preemption would not change anything. Plugins should return Unschedulable if it is possible
    60  	// that the pod can get scheduled with preemption.
    61  	// The accompanying status message should explain why the pod is unschedulable.
    62  	FilterNode(pod *v1.Pod, policy string) (int, string, error)
    63  	// ScoreNode will be invoked when using devicescore plugin, devices api can use it to implement multiple
    64  	// scheduling policies.
    65  	ScoreNode(pod *v1.Pod, policy string) float64
    66  
    67  	// Allocate action in predicate
    68  	Allocate(kubeClient kubernetes.Interface, pod *v1.Pod) error
    69  	// Release action in predicate
    70  	Release(kubeClient kubernetes.Interface, pod *v1.Pod) error
    71  
    72  	// GetIgnoredDevices notify vc-scheduler to ignore devices in return list
    73  	GetIgnoredDevices() []string
    74  
    75  	// GetStatus used for debug and monitor
    76  	GetStatus() string
    77  }
    78  
    79  // make sure GPUDevices implements Devices interface
    80  var _ Devices = new(gpushare.GPUDevices)
    81  
    82  var RegisteredDevices = []string{
    83  	GPUSharingDevice, vgpu.DeviceName,
    84  }
    85  
    86  var IgnoredDevicesList = ignoredDevicesList{}
    87  
    88  type ignoredDevicesList struct {
    89  	sync.RWMutex
    90  	ignoredDevices []string
    91  }
    92  
    93  func (l *ignoredDevicesList) Set(deviceLists ...[]string) {
    94  	l.Lock()
    95  	defer l.Unlock()
    96  	l.ignoredDevices = l.ignoredDevices[:0]
    97  	for _, devices := range deviceLists {
    98  		l.ignoredDevices = append(l.ignoredDevices, devices...)
    99  	}
   100  }
   101  
   102  func (l *ignoredDevicesList) Range(f func(i int, device string) bool) {
   103  	l.RLock()
   104  	defer l.RUnlock()
   105  	for i, device := range l.ignoredDevices {
   106  		if !f(i, device) {
   107  			break
   108  		}
   109  	}
   110  }