Browse Source

ENG-2956: Minor fix to only log if GPUUsageMax is not nil and isNaN (#2995)

* ENG-3056: Stop double counting GPURequestAvg

Signed-off-by: Pranav Bhat <pbhat@kubecost.com>

* ENG-2956: Log only if GPUUsageMax is not nil and isNaN

Signed-off-by: Pranav Bhat <pbhat@kubecost.com>

* ENG-2956: Update UT

Signed-off-by: Pranav Bhat <pbhat@kubecost.com>

* ENG-2956: Update UT

Signed-off-by: Pranav Bhat <pbhat@kubecost.com>

---------

Signed-off-by: Pranav Bhat <pbhat@kubecost.com>
kubecost-pb 1 year ago
parent
commit
a0b013c319
2 changed files with 27 additions and 1 deletions
  1. 1 1
      core/pkg/opencost/allocation.go
  2. 26 0
      core/pkg/opencost/allocation_test.go

+ 1 - 1
core/pkg/opencost/allocation.go

@@ -279,7 +279,7 @@ func (r *RawAllocationOnlyData) SanitizeNaN() {
 		log.DedupedWarningf(5, "RawAllocationOnlyData: Unexpected NaN found for RAMBytesUsageMax")
 		r.RAMBytesUsageMax = 0
 	}
-	if r.GPUUsageMax == nil || math.IsNaN(*r.GPUUsageMax) {
+	if r.GPUUsageMax != nil && math.IsNaN(*r.GPUUsageMax) {
 		log.DedupedWarningf(5, "RawAllocationOnlyData: Unexpected NaN found for GPUUsageMax")
 		r.GPUUsageMax = nil
 	}

+ 26 - 0
core/pkg/opencost/allocation_test.go

@@ -3784,12 +3784,38 @@ func TestRawAllocationOnlyData_SanitizeNaN(t *testing.T) {
 	raw.SanitizeNaN()
 	v := reflect.ValueOf(*raw)
 	checkAllFloat64sForNaN(t, v, "TestRawAllocationOnlyData_SanitizeNaN")
+
+	nan := math.NaN()
+	nilRawAllocation := &RawAllocationOnlyData{
+		CPUCoreUsageMax:  nan,
+		RAMBytesUsageMax: nan,
+		GPUUsageMax:      &nan,
+	}
+
+	nilRawAllocation.SanitizeNaN()
+
+	// SanitizeNaN allocates nil if NaN is passed
+	if nilRawAllocation.GPUUsageMax != nil {
+		t.Fatalf("want: nil, got: %v", nilRawAllocation.GPUUsageMax)
+	}
+
+	// SanitizeNaN allocates 0.0 if NaN is passed
+	if nilRawAllocation.CPUCoreUsageMax != 0.0 {
+		t.Fatalf("want: 0.0, got: %v", nilRawAllocation.CPUCoreUsageMax)
+	}
+
+	// SanitizeNaN allocates 0.0 if NaN is passed
+	if nilRawAllocation.RAMBytesUsageMax != 0.0 {
+		t.Fatalf("want: 0.0, got: %v", nilRawAllocation.RAMBytesUsageMax)
+	}
+
 }
 
 func getMockRawAllocationOnlyData(f float64) *RawAllocationOnlyData {
 	return &RawAllocationOnlyData{
 		CPUCoreUsageMax:  f,
 		RAMBytesUsageMax: f,
+		GPUUsageMax:      &f,
 	}
 }