|
| 1 | +// Copyright 2025 NVIDIA CORPORATION |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package gpusharingnodevalidation |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + v1 "k8s.io/api/core/v1" |
| 11 | + |
| 12 | + kaiv1common "github.com/kai-scheduler/KAI-scheduler/pkg/apis/kai/v1/common" |
| 13 | + "github.com/kai-scheduler/KAI-scheduler/pkg/common/constants" |
| 14 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api" |
| 15 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api/common_info" |
| 16 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api/node_info" |
| 17 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api/pod_info" |
| 18 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api/resource_info" |
| 19 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/framework" |
| 20 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/gpu_sharing" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + fractionalGPUReadyConditionType = v1.NodeConditionType(constants.NvFractionNodeReadyConditionType) |
| 25 | + fractionalGPUReadyReasonMissing = "ConditionNotFound" |
| 26 | +) |
| 27 | + |
| 28 | +func Validate(task *pod_info.PodInfo, node *node_info.NodeInfo, ssn *framework.Session) error { |
| 29 | + if err := checkMaxPodsWithGpuGroupReservation(task, node, ssn); err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + |
| 33 | + if err := checkNvFractionalGPUReadyCondition(task, node, isNvFractionsMode(ssn)); err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + |
| 37 | + return nil |
| 38 | +} |
| 39 | + |
| 40 | +// Check if the gpu-sharing operator set this node as ready. This is relevant only for NvFractions mode. |
| 41 | +func checkNvFractionalGPUReadyCondition(task *pod_info.PodInfo, node *node_info.NodeInfo, nvFractionsMode bool) error { |
| 42 | + if !nvFractionsMode || !task.IsSharedGPURequest() { |
| 43 | + return nil |
| 44 | + } |
| 45 | + |
| 46 | + conditionStatus := v1.ConditionUnknown |
| 47 | + conditionReason := fractionalGPUReadyReasonMissing |
| 48 | + for _, condition := range node.Node.Status.Conditions { |
| 49 | + if condition.Type != fractionalGPUReadyConditionType { |
| 50 | + continue |
| 51 | + } |
| 52 | + if condition.Status == v1.ConditionTrue { |
| 53 | + return nil |
| 54 | + } |
| 55 | + conditionStatus = condition.Status |
| 56 | + if condition.Reason != "" { |
| 57 | + conditionReason = condition.Reason |
| 58 | + } |
| 59 | + break |
| 60 | + } |
| 61 | + |
| 62 | + return common_info.NewFitError(task.Name, task.Namespace, node.Name, fmt.Sprintf( |
| 63 | + "node is not ready for fractional GPU scheduling. Condition %s is %s. Reason: %s", |
| 64 | + fractionalGPUReadyConditionType, conditionStatus, conditionReason, |
| 65 | + )) |
| 66 | +} |
| 67 | + |
| 68 | +func isNvFractionsMode(ssn *framework.Session) bool { |
| 69 | + return ssn != nil && ssn.SchedulerParams.GpuSharingMode != nil && |
| 70 | + *ssn.SchedulerParams.GpuSharingMode == kaiv1common.GpuSharingModeNvFractions |
| 71 | +} |
| 72 | + |
| 73 | +func checkMaxPodsWithGpuGroupReservation( |
| 74 | + task *pod_info.PodInfo, node *node_info.NodeInfo, ssn *framework.Session) error { |
| 75 | + availablePods := node.IdleVector.Get(resource_info.PodsIndex) + node.ReleasingVector.Get(resource_info.PodsIndex) |
| 76 | + |
| 77 | + if !task.IsSharedGPURequest() { |
| 78 | + if availablePods > 0 { |
| 79 | + return nil |
| 80 | + } |
| 81 | + return common_info.NewFitError(task.Name, task.Namespace, node.Name, api.NodePodNumberExceeded) |
| 82 | + } |
| 83 | + |
| 84 | + needsNewGpuGroup := willCreateNewGpuGroup(task, node, ssn) |
| 85 | + if !needsNewGpuGroup { |
| 86 | + return nil |
| 87 | + } |
| 88 | + |
| 89 | + if availablePods < 2 { |
| 90 | + return common_info.NewFitError(task.Name, task.Namespace, node.Name, api.NodePodNumberExceeded) |
| 91 | + } |
| 92 | + |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +// willCreateNewGpuGroup determines if allocating this task will create a new GPU group |
| 97 | +// and thus require a new reservation pod. |
| 98 | +func willCreateNewGpuGroup(task *pod_info.PodInfo, node *node_info.NodeInfo, ssn *framework.Session) bool { |
| 99 | + if ssn == nil { |
| 100 | + return true |
| 101 | + } |
| 102 | + |
| 103 | + fittingGPUs := ssn.FittingGPUs(node, task) |
| 104 | + gpuForSharingImmediate := gpu_sharing.GetNodePreferableGpuForSharing(fittingGPUs, node, task, false) |
| 105 | + |
| 106 | + if gpuForSharingImmediate != nil && !gpuForSharingImmediate.IsReleasing { |
| 107 | + return containsNewGpuGroup(gpuForSharingImmediate.GroupIDs()) |
| 108 | + } |
| 109 | + |
| 110 | + gpuForSharingPipelined := gpu_sharing.GetNodePreferableGpuForSharing(fittingGPUs, node, task, true) |
| 111 | + |
| 112 | + if gpuForSharingPipelined != nil { |
| 113 | + return containsNewGpuGroup(gpuForSharingPipelined.GroupIDs()) |
| 114 | + } |
| 115 | + |
| 116 | + // No GPU assignment possible - conservatively assume new group would be needed |
| 117 | + return true |
| 118 | +} |
| 119 | + |
| 120 | +// containsNewGpuGroup checks if any of the GPU groups is a newly created one (UUID format). |
| 121 | +func containsNewGpuGroup(groups []string) bool { |
| 122 | + for _, gpuGroup := range groups { |
| 123 | + if isNewGpuGroup(gpuGroup) { |
| 124 | + return true |
| 125 | + } |
| 126 | + } |
| 127 | + return false |
| 128 | +} |
| 129 | + |
| 130 | +// isNewGpuGroup determines if a GPU group ID represents a new group (UUID) vs an existing one (numeric). |
| 131 | +func isNewGpuGroup(gpuGroup string) bool { |
| 132 | + // New GPU groups are UUIDs (e.g., "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") |
| 133 | + // Existing GPU groups are numeric strings ("0", "1", "2", etc.) |
| 134 | + _, err := strconv.Atoi(gpuGroup) |
| 135 | + return err != nil // If not a number, it's a UUID = new group |
| 136 | +} |
0 commit comments