Parcourir la source

fix broken test and add extra sanitizeNaN nil checks

Signed-off-by: saweber <saweber@gmail.com>
saweber il y a 2 ans
Parent
commit
87fb5251df
2 fichiers modifiés avec 14 ajouts et 2 suppressions
  1. 12 0
      pkg/kubecost/asset.go
  2. 2 2
      pkg/kubecost/summaryallocation_json_test.go

+ 12 - 0
pkg/kubecost/asset.go

@@ -1584,6 +1584,9 @@ type Breakdown struct {
 }
 
 func (b *Breakdown) SanitizeNaN() {
+	if b == nil {
+		return
+	}
 	if math.IsNaN(b.Idle) {
 		log.DedupedWarningf(5, "Breakdown: Unexpected NaN found for Idle")
 		b.Idle = 0
@@ -1912,6 +1915,9 @@ type NodeOverhead struct {
 }
 
 func (n *NodeOverhead) SanitizeNaN() {
+	if n == nil {
+		return
+	}
 	if math.IsNaN(n.CpuOverheadFraction) {
 		log.DedupedWarningf(5, "NodeOverhead: Unexpected NaN found for CpuOverheadFraction")
 		n.CpuOverheadFraction = 0
@@ -2349,6 +2355,9 @@ func (n *Node) GPUs() float64 {
 }
 
 func (n *Node) SanitizeNaN() {
+	if n == nil {
+		return
+	}
 	if math.IsNaN(n.Adjustment) {
 		log.DedupedWarningf(5, "Node: Unexpected NaN found for Adjustment: labels:%v, window:%s, properties:%s", n.Labels, n.Window.String(), n.Properties.String())
 		n.Adjustment = 0
@@ -3464,6 +3473,9 @@ func (as *AssetSet) accumulate(that *AssetSet) (*AssetSet, error) {
 }
 
 func (as *AssetSet) SanitizeNaN() {
+	if as == nil {
+		return
+	}
 	for _, a := range as.Assets {
 		a.SanitizeNaN()
 	}

+ 2 - 2
pkg/kubecost/summaryallocation_json_test.go

@@ -112,9 +112,9 @@ func TestSummaryAllocationSetRangeResponse_MarshalJSON(t *testing.T) {
 	sas := NewSummaryAllocationSet(as, nil, nil, true, true)
 	sasr := NewSummaryAllocationSetRange(sas)
 
-	// Confirm that SummaryAllocationSetRange does error because on NaN
+	// Confirm that SummaryAllocationSetRange does not error on NaN
 	_, err := json.Marshal(sasr)
-	if err == nil {
+	if err != nil {
 		t.Fatalf("expected NaN values to cause error")
 	}