Sfoglia il codice sorgente

ENG-3056: Stop double counting GPURequestAvg (#2987)

Signed-off-by: Pranav Bhat <pbhat@kubecost.com>
kubecost-pb 1 anno fa
parent
commit
15318510f9

+ 0 - 15
core/pkg/opencost/allocation.go

@@ -1414,21 +1414,6 @@ func (a *Allocation) add(that *Allocation) {
 	// Sum LoadBalancer Allocations
 	a.LoadBalancers = a.LoadBalancers.Add(that.LoadBalancers)
 
-	// Sum GPU Allocations
-	if that.GPUAllocation != nil {
-		if a.GPUAllocation == nil {
-			a.GPUAllocation = that.GPUAllocation.Clone()
-		} else {
-			if a.GPUAllocation.GPUUsageAverage != nil && that.GPUAllocation.GPUUsageAverage != nil {
-				*a.GPUAllocation.GPUUsageAverage += *that.GPUAllocation.GPUUsageAverage
-			}
-
-			if a.GPUAllocation.GPURequestAverage != nil && that.GPUAllocation.GPURequestAverage != nil {
-				*a.GPUAllocation.GPURequestAverage += *that.GPUAllocation.GPURequestAverage
-			}
-		}
-	}
-
 	// Any data that is in a "raw allocation only" is not valid in any
 	// sort of cumulative Allocation (like one that is added).
 	a.RawAllocationOnly = nil

+ 5 - 2
pkg/costmodel/allocation.go

@@ -280,9 +280,12 @@ func (cm *CostModel) ComputeAllocation(start, end time.Time, resolution time.Dur
 				resultAlloc.RawAllocationOnly.RAMBytesUsageMax = alloc.RawAllocationOnly.RAMBytesUsageMax
 			}
 
-			if alloc.RawAllocationOnly.CPUCoreUsageMax > resultAlloc.RawAllocationOnly.CPUCoreUsageMax {
-				resultAlloc.RawAllocationOnly.GPUUsageMax = alloc.RawAllocationOnly.GPUUsageMax
+			if alloc.RawAllocationOnly.GPUUsageMax != nil {
+				if *alloc.RawAllocationOnly.GPUUsageMax > *resultAlloc.RawAllocationOnly.GPUUsageMax {
+					resultAlloc.RawAllocationOnly.GPUUsageMax = alloc.RawAllocationOnly.GPUUsageMax
+				}
 			}
+
 		}
 	}
 

+ 7 - 8
pkg/costmodel/allocation_helpers.go

@@ -667,7 +667,13 @@ func applyGPUUsage(podMap map[podKey]*pod, resGPUUsageAvgOrMax []*prom.QueryResu
 					thisPod.Allocations[container].GPUAllocation.GPUUsageAverage = &res.Values[0].Value
 				}
 			case GpuUsageMaxMode:
-				thisPod.Allocations[container].RawAllocationOnly.GPUUsageMax = &res.Values[0].Value
+				if thisPod.Allocations[container].RawAllocationOnly == nil {
+					thisPod.Allocations[container].RawAllocationOnly = &opencost.RawAllocationOnlyData{
+						GPUUsageMax: &res.Values[0].Value,
+					}
+				} else {
+					thisPod.Allocations[container].RawAllocationOnly.GPUUsageMax = &res.Values[0].Value
+				}
 			case GpuIsSharedMode:
 				// if a container is using a GPU and it is shared, isGPUShared will be true
 				// if a container is using GPU and it is NOT shared, isGPUShared will be false
@@ -768,13 +774,6 @@ func applyGPUsAllocated(podMap map[podKey]*pod, resGPUsRequested []*prom.QueryRe
 				}
 			} else {
 				thisPod.Allocations[container].GPUAllocation.GPURequestAverage = &res.Values[0].Value
-				if thisPod.Allocations[container].GPUAllocation == nil {
-					thisPod.Allocations[container].GPUAllocation = &opencost.GPUAllocation{
-						GPURequestAverage: &res.Values[0].Value,
-					}
-				} else {
-					thisPod.Allocations[container].GPUAllocation.GPURequestAverage = &res.Values[0].Value
-				}
 			}
 		}
 	}