Sean Holcomb %!s(int64=5) %!d(string=hai) anos
pai
achega
8eff64bd49
Modificáronse 2 ficheiros con 66 adicións e 69 borrados
  1. 3 4
      pkg/costmodel/allocation.go
  2. 63 65
      pkg/kubecost/allocation_test.go

+ 3 - 4
pkg/costmodel/allocation.go

@@ -304,7 +304,6 @@ func (cm *CostModel) ComputeAllocation(start, end time.Time, resolution time.Dur
 
 
 	// TODO breakdown network costs?
 	// TODO breakdown network costs?
 
 
-
 	// Build out a map of Nodes with resource costs, discounts, and node types
 	// Build out a map of Nodes with resource costs, discounts, and node types
 	// for converting resource allocation data to cumulative costs.
 	// for converting resource allocation data to cumulative costs.
 	nodeMap := map[nodeKey]*NodePricing{}
 	nodeMap := map[nodeKey]*NodePricing{}
@@ -1285,7 +1284,7 @@ func applyControllersToPods(podMap map[podKey]*Pod, podControllerMap map[podKey]
 }
 }
 
 
 func applyNodeCostPerCPUHr(nodeMap map[nodeKey]*NodePricing, resNodeCostPerCPUHr []*prom.QueryResult,
 func applyNodeCostPerCPUHr(nodeMap map[nodeKey]*NodePricing, resNodeCostPerCPUHr []*prom.QueryResult,
-	providerIDParser func(string) string,) {
+	providerIDParser func(string) string) {
 	for _, res := range resNodeCostPerCPUHr {
 	for _, res := range resNodeCostPerCPUHr {
 		cluster, err := res.GetString("cluster_id")
 		cluster, err := res.GetString("cluster_id")
 		if err != nil {
 		if err != nil {
@@ -1324,7 +1323,7 @@ func applyNodeCostPerCPUHr(nodeMap map[nodeKey]*NodePricing, resNodeCostPerCPUHr
 }
 }
 
 
 func applyNodeCostPerRAMGiBHr(nodeMap map[nodeKey]*NodePricing, resNodeCostPerRAMGiBHr []*prom.QueryResult,
 func applyNodeCostPerRAMGiBHr(nodeMap map[nodeKey]*NodePricing, resNodeCostPerRAMGiBHr []*prom.QueryResult,
-	providerIDParser func(string) string,) {
+	providerIDParser func(string) string) {
 	for _, res := range resNodeCostPerRAMGiBHr {
 	for _, res := range resNodeCostPerRAMGiBHr {
 		cluster, err := res.GetString("cluster_id")
 		cluster, err := res.GetString("cluster_id")
 		if err != nil {
 		if err != nil {
@@ -1363,7 +1362,7 @@ func applyNodeCostPerRAMGiBHr(nodeMap map[nodeKey]*NodePricing, resNodeCostPerRA
 }
 }
 
 
 func applyNodeCostPerGPUHr(nodeMap map[nodeKey]*NodePricing, resNodeCostPerGPUHr []*prom.QueryResult,
 func applyNodeCostPerGPUHr(nodeMap map[nodeKey]*NodePricing, resNodeCostPerGPUHr []*prom.QueryResult,
-	providerIDParser func(string) string,) {
+	providerIDParser func(string) string) {
 	for _, res := range resNodeCostPerGPUHr {
 	for _, res := range resNodeCostPerGPUHr {
 		cluster, err := res.GetString("cluster_id")
 		cluster, err := res.GetString("cluster_id")
 		if err != nil {
 		if err != nil {

+ 63 - 65
pkg/kubecost/allocation_test.go

@@ -537,8 +537,8 @@ func TestNewAllocationSet(t *testing.T) {
 func generateAllocationSet(start time.Time) *AllocationSet {
 func generateAllocationSet(start time.Time) *AllocationSet {
 	// Idle allocations
 	// Idle allocations
 	a1i := NewUnitAllocation(fmt.Sprintf("cluster1/%s", IdleSuffix), start, day, &AllocationProperties{
 	a1i := NewUnitAllocation(fmt.Sprintf("cluster1/%s", IdleSuffix), start, day, &AllocationProperties{
-		Cluster: "cluster1",
-		Node:    "node1",
+		Cluster:    "cluster1",
+		Node:       "node1",
 		ProviderID: "c1nodes",
 		ProviderID: "c1nodes",
 	})
 	})
 	a1i.CPUCost = 5.0
 	a1i.CPUCost = 5.0
@@ -554,99 +554,99 @@ func generateAllocationSet(start time.Time) *AllocationSet {
 
 
 	// Active allocations
 	// Active allocations
 	a1111 := NewUnitAllocation("cluster1/namespace1/pod1/container1", start, day, &AllocationProperties{
 	a1111 := NewUnitAllocation("cluster1/namespace1/pod1/container1", start, day, &AllocationProperties{
-		Cluster:   "cluster1",
-		Namespace: "namespace1",
-		Pod:       "pod1",
-		Container: "container1",
+		Cluster:    "cluster1",
+		Namespace:  "namespace1",
+		Pod:        "pod1",
+		Container:  "container1",
 		ProviderID: "c1nodes",
 		ProviderID: "c1nodes",
 	})
 	})
 	a1111.RAMCost = 11.00
 	a1111.RAMCost = 11.00
 
 
 	a11abc2 := NewUnitAllocation("cluster1/namespace1/pod-abc/container2", start, day, &AllocationProperties{
 	a11abc2 := NewUnitAllocation("cluster1/namespace1/pod-abc/container2", start, day, &AllocationProperties{
-		Cluster:   "cluster1",
-		Namespace: "namespace1",
-		Pod:       "pod-abc",
-		Container: "container2",
+		Cluster:    "cluster1",
+		Namespace:  "namespace1",
+		Pod:        "pod-abc",
+		Container:  "container2",
 		ProviderID: "c1nodes",
 		ProviderID: "c1nodes",
 	})
 	})
 
 
 	a11def3 := NewUnitAllocation("cluster1/namespace1/pod-def/container3", start, day, &AllocationProperties{
 	a11def3 := NewUnitAllocation("cluster1/namespace1/pod-def/container3", start, day, &AllocationProperties{
-		Cluster:   "cluster1",
-		Namespace: "namespace1",
-		Pod:       "pod-def",
-		Container: "container3",
+		Cluster:    "cluster1",
+		Namespace:  "namespace1",
+		Pod:        "pod-def",
+		Container:  "container3",
 		ProviderID: "c1nodes",
 		ProviderID: "c1nodes",
 	})
 	})
 
 
 	a12ghi4 := NewUnitAllocation("cluster1/namespace2/pod-ghi/container4", start, day, &AllocationProperties{
 	a12ghi4 := NewUnitAllocation("cluster1/namespace2/pod-ghi/container4", start, day, &AllocationProperties{
-		Cluster:   "cluster1",
-		Namespace: "namespace2",
-		Pod:       "pod-ghi",
-		Container: "container4",
+		Cluster:    "cluster1",
+		Namespace:  "namespace2",
+		Pod:        "pod-ghi",
+		Container:  "container4",
 		ProviderID: "c1nodes",
 		ProviderID: "c1nodes",
 	})
 	})
 
 
 	a12ghi5 := NewUnitAllocation("cluster1/namespace2/pod-ghi/container5", start, day, &AllocationProperties{
 	a12ghi5 := NewUnitAllocation("cluster1/namespace2/pod-ghi/container5", start, day, &AllocationProperties{
-		Cluster:   "cluster1",
-		Namespace: "namespace2",
-		Pod:       "pod-ghi",
-		Container: "container5",
+		Cluster:    "cluster1",
+		Namespace:  "namespace2",
+		Pod:        "pod-ghi",
+		Container:  "container5",
 		ProviderID: "c1nodes",
 		ProviderID: "c1nodes",
 	})
 	})
 
 
 	a12jkl6 := NewUnitAllocation("cluster1/namespace2/pod-jkl/container6", start, day, &AllocationProperties{
 	a12jkl6 := NewUnitAllocation("cluster1/namespace2/pod-jkl/container6", start, day, &AllocationProperties{
-		Cluster:   "cluster1",
-		Namespace: "namespace2",
-		Pod:       "pod-jkl",
-		Container: "container6",
+		Cluster:    "cluster1",
+		Namespace:  "namespace2",
+		Pod:        "pod-jkl",
+		Container:  "container6",
 		ProviderID: "c1nodes",
 		ProviderID: "c1nodes",
 	})
 	})
 
 
 	a22mno4 := NewUnitAllocation("cluster2/namespace2/pod-mno/container4", start, day, &AllocationProperties{
 	a22mno4 := NewUnitAllocation("cluster2/namespace2/pod-mno/container4", start, day, &AllocationProperties{
-		Cluster:   "cluster2",
-		Namespace: "namespace2",
-		Pod:       "pod-mno",
-		Container: "container4",
+		Cluster:    "cluster2",
+		Namespace:  "namespace2",
+		Pod:        "pod-mno",
+		Container:  "container4",
 		ProviderID: "node1",
 		ProviderID: "node1",
 	})
 	})
 
 
 	a22mno5 := NewUnitAllocation("cluster2/namespace2/pod-mno/container5", start, day, &AllocationProperties{
 	a22mno5 := NewUnitAllocation("cluster2/namespace2/pod-mno/container5", start, day, &AllocationProperties{
-		Cluster:   "cluster2",
-		Namespace: "namespace2",
-		Pod:       "pod-mno",
-		Container: "container5",
+		Cluster:    "cluster2",
+		Namespace:  "namespace2",
+		Pod:        "pod-mno",
+		Container:  "container5",
 		ProviderID: "node1",
 		ProviderID: "node1",
 	})
 	})
 
 
 	a22pqr6 := NewUnitAllocation("cluster2/namespace2/pod-pqr/container6", start, day, &AllocationProperties{
 	a22pqr6 := NewUnitAllocation("cluster2/namespace2/pod-pqr/container6", start, day, &AllocationProperties{
-		Cluster:   "cluster2",
-		Namespace: "namespace2",
-		Pod:       "pod-pqr",
-		Container: "container6",
+		Cluster:    "cluster2",
+		Namespace:  "namespace2",
+		Pod:        "pod-pqr",
+		Container:  "container6",
 		ProviderID: "node2",
 		ProviderID: "node2",
 	})
 	})
 
 
 	a23stu7 := NewUnitAllocation("cluster2/namespace3/pod-stu/container7", start, day, &AllocationProperties{
 	a23stu7 := NewUnitAllocation("cluster2/namespace3/pod-stu/container7", start, day, &AllocationProperties{
-		Cluster:   "cluster2",
-		Namespace: "namespace3",
-		Pod:       "pod-stu",
-		Container: "container7",
+		Cluster:    "cluster2",
+		Namespace:  "namespace3",
+		Pod:        "pod-stu",
+		Container:  "container7",
 		ProviderID: "node2",
 		ProviderID: "node2",
 	})
 	})
 
 
 	a23vwx8 := NewUnitAllocation("cluster2/namespace3/pod-vwx/container8", start, day, &AllocationProperties{
 	a23vwx8 := NewUnitAllocation("cluster2/namespace3/pod-vwx/container8", start, day, &AllocationProperties{
-		Cluster:   "cluster2",
-		Namespace: "namespace3",
-		Pod:       "pod-vwx",
-		Container: "container8",
+		Cluster:    "cluster2",
+		Namespace:  "namespace3",
+		Pod:        "pod-vwx",
+		Container:  "container8",
 		ProviderID: "node3",
 		ProviderID: "node3",
 	})
 	})
 
 
 	a23vwx9 := NewUnitAllocation("cluster2/namespace3/pod-vwx/container9", start, day, &AllocationProperties{
 	a23vwx9 := NewUnitAllocation("cluster2/namespace3/pod-vwx/container9", start, day, &AllocationProperties{
-		Cluster:   "cluster2",
-		Namespace: "namespace3",
-		Pod:       "pod-vwx",
-		Container: "container9",
+		Cluster:    "cluster2",
+		Namespace:  "namespace3",
+		Pod:        "pod-vwx",
+		Container:  "container9",
 		ProviderID: "node3",
 		ProviderID: "node3",
 	})
 	})
 
 
@@ -711,7 +711,7 @@ func generateAllocationSet(start time.Time) *AllocationSet {
 	)
 	)
 }
 }
 
 
-func generateAssetSets(start, end time.Time) []*AssetSet{
+func generateAssetSets(start, end time.Time) []*AssetSet {
 	var assetSets []*AssetSet
 	var assetSets []*AssetSet
 
 
 	// Create an AssetSet representing cluster costs for two clusters (cluster1
 	// Create an AssetSet representing cluster costs for two clusters (cluster1
@@ -1669,13 +1669,12 @@ func TestAllocationSet_ComputeIdleAllocations(t *testing.T) {
 
 
 	cases := map[string]struct {
 	cases := map[string]struct {
 		allocationSet *AllocationSet
 		allocationSet *AllocationSet
-		assetSet *AssetSet
-		clusters map[string]Allocation
-
+		assetSet      *AssetSet
+		clusters      map[string]Allocation
 	}{
 	}{
-		"1a" : {
+		"1a": {
 			allocationSet: as,
 			allocationSet: as,
-			assetSet: assetSets[0],
+			assetSet:      assetSets[0],
 			clusters: map[string]Allocation{
 			clusters: map[string]Allocation{
 				"cluster1": {
 				"cluster1": {
 					CPUCost: 44.0,
 					CPUCost: 44.0,
@@ -1689,9 +1688,9 @@ func TestAllocationSet_ComputeIdleAllocations(t *testing.T) {
 				},
 				},
 			},
 			},
 		},
 		},
-		"1b" : {
+		"1b": {
 			allocationSet: as,
 			allocationSet: as,
-			assetSet: assetSets[1],
+			assetSet:      assetSets[1],
 			clusters: map[string]Allocation{
 			clusters: map[string]Allocation{
 				"cluster1": {
 				"cluster1": {
 					CPUCost: 44.0,
 					CPUCost: 44.0,
@@ -1757,13 +1756,12 @@ func TestAllocationSet_ReconcileAllocations(t *testing.T) {
 
 
 	cases := map[string]struct {
 	cases := map[string]struct {
 		allocationSet *AllocationSet
 		allocationSet *AllocationSet
-		assetSet *AssetSet
-		allocations map[string]Allocation
-
+		assetSet      *AssetSet
+		allocations   map[string]Allocation
 	}{
 	}{
-		"1a" : {
+		"1a": {
 			allocationSet: as,
 			allocationSet: as,
-			assetSet: assetSets[0],
+			assetSet:      assetSets[0],
 			allocations: map[string]Allocation{
 			allocations: map[string]Allocation{
 				// Allocation adjustments are found with the formula:
 				// Allocation adjustments are found with the formula:
 				// ADJUSTMENT_RATE * NODE_COST * (ALLOC_HOURS / NODE_HOURS) - ALLOC_COST
 				// ADJUSTMENT_RATE * NODE_COST * (ALLOC_HOURS / NODE_HOURS) - ALLOC_COST
@@ -1854,9 +1852,9 @@ func TestAllocationSet_ReconcileAllocations(t *testing.T) {
 				},
 				},
 			},
 			},
 		},
 		},
-		"1b" : {
+		"1b": {
 			allocationSet: as,
 			allocationSet: as,
-			assetSet: assetSets[1],
+			assetSet:      assetSets[1],
 			allocations: map[string]Allocation{
 			allocations: map[string]Allocation{
 				// ADJUSTMENT_RATE: 10
 				// ADJUSTMENT_RATE: 10
 				// Type | NODE_COST | NODE_HOURs | ALLOC_COST | ALLOC_HOURS
 				// Type | NODE_COST | NODE_HOURs | ALLOC_COST | ALLOC_HOURS