@@ -10,6 +10,7 @@ import (
1010 "k8s.io/apimachinery/pkg/api/resource"
1111
1212 "github.com/kai-scheduler/KAI-scheduler/pkg/common/constants"
13+ common_resources "github.com/kai-scheduler/KAI-scheduler/pkg/common/resources"
1314)
1415
1516const (
@@ -19,56 +20,36 @@ const (
1920func ExtractGPUSharingRequestedResources (pod * v1.Pod ) (v1.ResourceList , error ) {
2021 resources := v1.ResourceList {}
2122
22- fractionsCount := int64 (1 )
23- gpuFractionsCountStr , hasAnnotation := pod .Annotations [constants .GpuFractionsNumDevices ]
24- if hasAnnotation {
25- quantity , err := resource .ParseQuantity (gpuFractionsCountStr )
26- if err != nil {
27- return v1.ResourceList {},
28- fmt .Errorf ("failed to parse gpu fraction count annotation value <%s>, error: %s" ,
29- gpuFractionsCountStr , err .Error ())
30- }
31- var successfulIntExtraction bool
32- fractionsCount , successfulIntExtraction = quantity .AsInt64 ()
33- if ! successfulIntExtraction {
34- return v1.ResourceList {},
35- fmt .Errorf ("failed to extract int value from gpu fraction count annotation. value <%s>" ,
36- gpuFractionsCountStr )
37- }
23+ req , err := common_resources .ParsePodGPUFractionRequest (pod )
24+ if err != nil {
25+ return v1.ResourceList {},
26+ fmt .Errorf ("failed to parse GPU fraction for pod %s/%s: %s" , pod .Namespace , pod .Name , err )
27+ }
28+ if req == nil {
29+ return resources , nil
3830 }
3931
40- gpuFractionStr , hasAnnotation := pod . Annotations [ constants . GpuFraction ]
41- if hasAnnotation {
42- quantity , err := resource .ParseQuantity (gpuFractionStr )
32+ if req . Portion > 0 {
33+ fractionStr := fmt . Sprintf ( "%g" , req . Portion )
34+ quantity , err := resource .ParseQuantity (fractionStr )
4335 if err != nil {
4436 return v1.ResourceList {},
45- fmt .Errorf ("failed to parse gpu fraction annotation value <%s>, error: %s" ,
46- gpuFractionStr , err .Error ())
37+ fmt .Errorf ("failed to parse gpu fraction value <%s>: %s" , fractionStr , err )
4738 }
48- successfulMulti := quantity .Mul (fractionsCount )
49- if ! successfulMulti {
39+ if ok := quantity .Mul (req .NumDevices ); ! ok {
5040 return v1.ResourceList {},
51- fmt .Errorf ("failed to multiple gpu fraction by the fraction count. " +
52- "Please check resource.Quantity restrictions. fraction <%s>, count: %d" ,
53- gpuFractionStr , fractionsCount )
41+ fmt .Errorf ("failed to multiply gpu fraction by device count. fraction <%s>, count: %d" ,
42+ fractionStr , req .NumDevices )
5443 }
5544 resources [v1 .ResourceName (constants .NvidiaGpuResource )] = quantity
5645 }
5746
58- gpuMemoryStr , hasAnnotation := pod .Annotations [constants .GpuMemory ]
59- if hasAnnotation {
60- quantity , err := resource .ParseQuantity (gpuMemoryStr )
61- if err != nil {
62- return v1.ResourceList {},
63- fmt .Errorf ("failed to parse gpu memory annotation value <%s>, error: %s" ,
64- gpuMemoryStr , err .Error ())
65- }
66- successfulMulti := quantity .Mul (fractionsCount )
67- if ! successfulMulti {
47+ if req .Memory != nil {
48+ quantity := req .Memory .DeepCopy ()
49+ if ok := quantity .Mul (req .NumDevices ); ! ok {
6850 return v1.ResourceList {},
69- fmt .Errorf ("failed to multiple gpu memory by the fraction count. " +
70- "Please check resource.Quantity restrictions. fraction <%s>, count: %d" ,
71- gpuMemoryStr , fractionsCount )
51+ fmt .Errorf ("failed to multiply gpu memory by device count. memory <%s>, count: %d" ,
52+ req .Memory .String (), req .NumDevices )
7253 }
7354 resources [v1 .ResourceName (gpuMemoryResourceName )] = quantity
7455 }
0 commit comments