-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathgpu_sharing.go
More file actions
95 lines (76 loc) · 2.72 KB
/
Copy pathgpu_sharing.go
File metadata and controls
95 lines (76 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2025 NVIDIA CORPORATION
// SPDX-License-Identifier: Apache-2.0
package gpusharing
import (
"fmt"
"strconv"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/kai-scheduler/KAI-scheduler/pkg/binder/common/gpusharingconfigmap"
"github.com/kai-scheduler/KAI-scheduler/pkg/common/constants"
"github.com/kai-scheduler/KAI-scheduler/pkg/common/resources"
"github.com/kai-scheduler/KAI-scheduler/pkg/binder/common"
)
const (
CdiDeviceNameBase = "k8s.device-plugin.nvidia.com/gpu=%s"
)
type GPUSharing struct {
kubeClient client.Client
gpuSharingEnabled bool
}
func New(kubeClient client.Client, gpuSharingEnabled bool) *GPUSharing {
return &GPUSharing{
kubeClient: kubeClient,
gpuSharingEnabled: gpuSharingEnabled,
}
}
func (p *GPUSharing) Name() string {
return "gpusharing"
}
func (p *GPUSharing) Validate(pod *v1.Pod) error {
if !p.gpuSharingEnabled && resources.RequestsGPUFraction(pod) {
return fmt.Errorf(
"attempting to create a pod %s/%s with gpu sharing request, while GPU sharing is disabled",
pod.Namespace, pod.Name,
)
}
return resources.ValidateGPUFractionRequest(pod)
}
func (p *GPUSharing) Mutate(pod *v1.Pod) error {
if len(pod.Spec.Containers) == 0 {
return nil
}
if !resources.RequestsGPUFraction(pod) {
return nil
}
containerRef, err := common.GetFractionContainerRef(pod)
if err != nil {
return fmt.Errorf("failed to get fraction container ref: %w", err)
}
err = adjustFractionalMemoryAnnotations(pod, containerRef)
if err != nil {
return err
}
capabilitiesConfigMapName := gpusharingconfigmap.SetGpuCapabilitiesConfigMapName(pod, containerRef)
directEnvVarsMapName, err := gpusharingconfigmap.ExtractDirectEnvVarsConfigMapName(pod, containerRef)
if err != nil {
return err
}
common.AddGPUSharingEnvVars(containerRef.Container, capabilitiesConfigMapName)
common.SetConfigMapVolume(pod, capabilitiesConfigMapName)
common.AddDirectEnvVarsConfigMapSource(containerRef.Container, directEnvVarsMapName)
return nil
}
// adjustFractionalMemoryAnnotations adjusts the old fractional memory annotations to NvFractions format
func adjustFractionalMemoryAnnotations(pod *v1.Pod, containerRef *gpusharingconfigmap.PodContainerRef) error {
gpuMemoryRequestMiB, foundGPUMemory := pod.Annotations[constants.GpuMemory]
if foundGPUMemory {
gpuMemoryRequestMiB, err := strconv.ParseUint(gpuMemoryRequestMiB, 10, 64)
if err != nil {
return fmt.Errorf("failed to parse gpu memory annotation value: %w", err)
}
memoryQuantity := resources.GpuMemoryAnnotationToNvFractionsMemoryRequest(gpuMemoryRequestMiB)
pod.Annotations[resources.CalcGpuFractionAnnotationForContainer(containerRef.Container.Name)] = memoryQuantity.String()
}
return nil
}