فهرست منبع

Nil out overhead when adding nil to non-nil (#2251)

Signed-off-by: Michael Dresser <michaelmdresser@gmail.com>
Signed-off-by: Thomas Evans <tevans3@icloud.com>
Michael Dresser 2 سال پیش
والد
کامیت
2326892cc2
2فایلهای تغییر یافته به همراه11 افزوده شده و 0 حذف شده
  1. 2 0
      pkg/kubecost/asset.go
  2. 9 0
      pkg/kubecost/asset_test.go

+ 2 - 0
pkg/kubecost/asset.go

@@ -2165,6 +2165,8 @@ func (n *Node) add(that *Node) {
 	if n.Overhead != nil && that.Overhead != nil {
 		n.Overhead.RamOverheadFraction = (n.Overhead.RamOverheadFraction*n.RAMCost + that.Overhead.RamOverheadFraction*that.RAMCost) / totalRAMCost
 		n.Overhead.CpuOverheadFraction = (n.Overhead.CpuOverheadFraction*n.CPUCost + that.Overhead.CpuOverheadFraction*that.CPUCost) / totalCPUCost
+	} else {
+		n.Overhead = nil
 	}
 
 	n.CPUCoreHours += that.CPUCoreHours

+ 9 - 0
pkg/kubecost/asset_test.go

@@ -482,6 +482,11 @@ func TestNode_Add(t *testing.T) {
 	node3.RAMCost = 0.0
 	node3.Discount = 0.3
 	node3.SetAdjustment(0.0)
+	node3.Overhead = &NodeOverhead{
+		CpuOverheadFraction:  0.6,
+		RamOverheadFraction:  0.75,
+		OverheadCostFraction: 0.7,
+	}
 
 	node4 := NewNode("node4", "cluster1", "node4", *windows[0].start, *windows[0].end, windows[0])
 	node4.CPUCoreHours = 0 * hours
@@ -492,6 +497,7 @@ func TestNode_Add(t *testing.T) {
 	node4.RAMCost = 0.0
 	node4.Discount = 0.1
 	node4.SetAdjustment(0.0)
+	node4.Overhead = nil
 
 	nodeT = node3.Add(node4).(*Node)
 
@@ -502,6 +508,9 @@ func TestNode_Add(t *testing.T) {
 	if nodeT.Discount != 0.2 {
 		t.Fatalf("Node.Add: expected %f; got %f", 0.2, nodeT.Discount)
 	}
+	if nodeT.Overhead != nil {
+		t.Errorf("Node.Add: adding a node with nil overhead should nil the resulting overhead")
+	}
 
 	// Accumulate: one nodes, two window
 	nodeA1 := NewNode("nodeA1", "cluster1", "nodeA1", *windows[0].start, *windows[0].end, windows[0])