Skip to content

Commit cc49907

Browse files
committed
Refactor GPU allocatable detection into reusable function
Extract the GPU allocatable detection loop into a new NodeHasGpuAllocatable helper function in utils/gpu/gpu.go. This eliminates code duplication across gpu_processor.go and makes the logic more maintainable. The new function returns both the GPU allocatable value and whether it exists, allowing callers to get both pieces of information in a single call. Changes: - Add NodeHasGpuAllocatable() helper in utils/gpu/gpu.go - Update NodeHasGpu() to use the new helper - Simplify FilterOutNodesWithUnreadyResources() in gpu_processor.go - Simplify GetNodeGpuTarget() in gpu_processor.go
1 parent 5873c7f commit cc49907

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

cluster-autoscaler/processors/customresources/gpu_processor.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,7 @@ func (p *GpuCustomResourcesProcessor) FilterOutNodesWithUnreadyResources(autosca
4848
}
4949

5050
_, hasGpuLabel := node.Labels[autoscalingCtx.CloudProvider.GPULabel()]
51-
hasAnyGpuAllocatable := false
52-
for _, gpuVendorResourceName := range gpu.GPUVendorResourceNames {
53-
gpuAllocatable, hasGpuAllocatable := node.Status.Allocatable[gpuVendorResourceName]
54-
if hasGpuAllocatable && !gpuAllocatable.IsZero() {
55-
hasAnyGpuAllocatable = true
56-
break
57-
}
58-
}
51+
_, hasAnyGpuAllocatable := gpu.NodeHasGpuAllocatable(node)
5952
if hasGpuLabel && !hasAnyGpuAllocatable {
6053
klog.V(3).Infof("Overriding status of node %v, which seems to have unready GPU",
6154
node.Name)
@@ -94,10 +87,8 @@ func (p *GpuCustomResourcesProcessor) GetNodeGpuTarget(autoscalingCtx *ca_contex
9487
return CustomResourceTarget{}, nil
9588
}
9689

97-
for _, gpuVendorResourceName := range gpu.GPUVendorResourceNames {
98-
if gpuAllocatable, found := node.Status.Allocatable[gpuVendorResourceName]; found && gpuAllocatable.Value() > 0 {
99-
return CustomResourceTarget{gpuLabel, gpuAllocatable.Value()}, nil
100-
}
90+
if gpuAllocatableValue, hasGpuAllocatable := gpu.NodeHasGpuAllocatable(node); hasGpuAllocatable {
91+
return CustomResourceTarget{gpuLabel, gpuAllocatableValue}, nil
10192
}
10293

10394
// A node is supposed to have GPUs (based on label), but they're not available yet

cluster-autoscaler/utils/gpu/gpu.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,20 @@ func NodeHasGpu(GPULabel string, node *apiv1.Node) bool {
126126
return true
127127
}
128128
// Check for extended resources as well
129+
_, hasGpuAllocatable := NodeHasGpuAllocatable(node)
130+
return hasGpuAllocatable
131+
}
132+
133+
// NodeHasGpuAllocatable returns the GPU allocatable value and whether the node has GPU allocatable resources.
134+
// It checks all known GPU vendor resource names and returns the first non-zero allocatable GPU value found.
135+
func NodeHasGpuAllocatable(node *apiv1.Node) (gpuAllocatableValue int64, hasGpuAllocatable bool) {
129136
for _, gpuVendorResourceName := range GPUVendorResourceNames {
130-
gpuAllocatable, hasGpuAllocatable := node.Status.Allocatable[gpuVendorResourceName]
131-
if hasGpuAllocatable && !gpuAllocatable.IsZero() {
132-
return true
137+
gpuAllocatable, found := node.Status.Allocatable[gpuVendorResourceName]
138+
if found && !gpuAllocatable.IsZero() {
139+
return gpuAllocatable.Value(), true
133140
}
134141
}
135-
return false
142+
return 0, false
136143
}
137144

138145
// PodRequestsGpu returns true if a given pod has GPU request.

0 commit comments

Comments
 (0)