Procházet zdrojové kódy

Add support in CloudCosts for AmortizedCost field.

Signed-off-by: Niko Kovacevic <nikovacevic@gmail.com>
Niko Kovacevic před 3 roky
rodič
revize
15e65ec6ff

+ 15 - 3
pkg/kubecost/cloudcost.go

@@ -18,10 +18,11 @@ type CloudCost struct {
 	NetCost          CostMetric           `json:"netCost"`
 	AmortizedNetCost CostMetric           `json:"amortizedNetCost"`
 	InvoicedCost     CostMetric           `json:"invoicedCost"`
+	AmortizedCost    CostMetric           `json:"amortizedCost"`
 }
 
 // NewCloudCost instantiates a new CloudCost
-func NewCloudCost(start, end time.Time, ccProperties *CloudCostProperties, kubernetesPercent, listCost, netCost, amortizedNetCost, invoicedCost float64) *CloudCost {
+func NewCloudCost(start, end time.Time, ccProperties *CloudCostProperties, kubernetesPercent, listCost, netCost, amortizedNetCost, invoicedCost, amortizedCost float64) *CloudCost {
 	return &CloudCost{
 		Properties: ccProperties,
 		Window:     NewWindow(&start, &end),
@@ -38,7 +39,11 @@ func NewCloudCost(start, end time.Time, ccProperties *CloudCostProperties, kuber
 			KubernetesPercent: kubernetesPercent,
 		},
 		InvoicedCost: CostMetric{
-			Cost:              listCost,
+			Cost:              invoicedCost,
+			KubernetesPercent: kubernetesPercent,
+		},
+		AmortizedCost: CostMetric{
+			Cost:              amortizedCost,
 			KubernetesPercent: kubernetesPercent,
 		},
 	}
@@ -52,6 +57,7 @@ func (cc *CloudCost) Clone() *CloudCost {
 		NetCost:          cc.NetCost.Clone(),
 		AmortizedNetCost: cc.AmortizedNetCost.Clone(),
 		InvoicedCost:     cc.InvoicedCost.Clone(),
+		AmortizedCost:    cc.AmortizedCost.Clone(),
 	}
 }
 
@@ -65,7 +71,8 @@ func (cc *CloudCost) Equal(that *CloudCost) bool {
 		cc.ListCost.Equal(that.ListCost) &&
 		cc.NetCost.Equal(that.NetCost) &&
 		cc.AmortizedNetCost.Equal(that.AmortizedNetCost) &&
-		cc.InvoicedCost.Equal(that.InvoicedCost)
+		cc.InvoicedCost.Equal(that.InvoicedCost) &&
+		cc.AmortizedCost.Equal(that.AmortizedCost)
 }
 
 func (cc *CloudCost) add(that *CloudCost) {
@@ -81,6 +88,7 @@ func (cc *CloudCost) add(that *CloudCost) {
 	cc.NetCost = cc.NetCost.add(that.NetCost)
 	cc.AmortizedNetCost = cc.AmortizedNetCost.add(that.AmortizedNetCost)
 	cc.InvoicedCost = cc.InvoicedCost.add(that.InvoicedCost)
+	cc.AmortizedCost = cc.AmortizedCost.add(that.AmortizedCost)
 
 	cc.Window = cc.Window.Expand(that.Window)
 }
@@ -131,6 +139,8 @@ func (cc *CloudCost) GetCostMetric(costMetricName string) (CostMetric, error) {
 		return cc.AmortizedNetCost, nil
 	case InvoicedCostMetric:
 		return cc.InvoicedCost, nil
+	case AmortizedCostMetric:
+		return cc.AmortizedCost, nil
 	}
 	return CostMetric{}, fmt.Errorf("invalid Cost Metric: %s", costMetricName)
 }
@@ -486,6 +496,7 @@ func (ccsr *CloudCostSetRange) LoadCloudCost(cloudCost *CloudCost) {
 				NetCost:          cloudCost.NetCost.percent(pct),
 				AmortizedNetCost: cloudCost.AmortizedNetCost.percent(pct),
 				InvoicedCost:     cloudCost.InvoicedCost.percent(pct),
+				AmortizedCost:    cloudCost.AmortizedCost.percent(pct),
 			}
 		}
 
@@ -507,6 +518,7 @@ const (
 	NetCostMetric          string = "NetCost"
 	AmortizedNetCostMetric string = "AmortizedNetCost"
 	InvoicedCostMetric     string = "InvoicedCost"
+	AmortizedCostMetric    string = "AmortizedCost"
 )
 
 type CostMetric struct {

+ 12 - 0
pkg/kubecost/cloudcost_test.go

@@ -43,6 +43,7 @@ func TestCloudCost_LoadCloudCost(t *testing.T) {
 					NetCost:          CostMetric{Cost: 80, KubernetesPercent: 1},
 					AmortizedNetCost: CostMetric{Cost: 90, KubernetesPercent: 1},
 					InvoicedCost:     CostMetric{Cost: 95, KubernetesPercent: 1},
+					AmortizedCost:    CostMetric{Cost: 85, KubernetesPercent: 1},
 				},
 			},
 			ccsr: emtpyCCSR.Clone(),
@@ -58,6 +59,7 @@ func TestCloudCost_LoadCloudCost(t *testing.T) {
 							NetCost:          CostMetric{Cost: 80, KubernetesPercent: 1},
 							AmortizedNetCost: CostMetric{Cost: 90, KubernetesPercent: 1},
 							InvoicedCost:     CostMetric{Cost: 95, KubernetesPercent: 1},
+							AmortizedCost:    CostMetric{Cost: 85, KubernetesPercent: 1},
 						},
 					},
 				},
@@ -82,6 +84,7 @@ func TestCloudCost_LoadCloudCost(t *testing.T) {
 					NetCost:          CostMetric{Cost: 80, KubernetesPercent: 1},
 					AmortizedNetCost: CostMetric{Cost: 90, KubernetesPercent: 1},
 					InvoicedCost:     CostMetric{Cost: 95, KubernetesPercent: 1},
+					AmortizedCost:    CostMetric{Cost: 85, KubernetesPercent: 1},
 				},
 			},
 			ccsr: emtpyCCSR.Clone(),
@@ -97,6 +100,7 @@ func TestCloudCost_LoadCloudCost(t *testing.T) {
 							NetCost:          CostMetric{Cost: 40, KubernetesPercent: 1},
 							AmortizedNetCost: CostMetric{Cost: 45, KubernetesPercent: 1},
 							InvoicedCost:     CostMetric{Cost: 47.5, KubernetesPercent: 1},
+							AmortizedCost:    CostMetric{Cost: 42.5, KubernetesPercent: 1},
 						},
 					},
 				},
@@ -111,6 +115,7 @@ func TestCloudCost_LoadCloudCost(t *testing.T) {
 							NetCost:          CostMetric{Cost: 40, KubernetesPercent: 1},
 							AmortizedNetCost: CostMetric{Cost: 45, KubernetesPercent: 1},
 							InvoicedCost:     CostMetric{Cost: 47.5, KubernetesPercent: 1},
+							AmortizedCost:    CostMetric{Cost: 42.5, KubernetesPercent: 1},
 						},
 					},
 				},
@@ -130,6 +135,7 @@ func TestCloudCost_LoadCloudCost(t *testing.T) {
 					NetCost:          CostMetric{Cost: 80, KubernetesPercent: 1},
 					AmortizedNetCost: CostMetric{Cost: 90, KubernetesPercent: 1},
 					InvoicedCost:     CostMetric{Cost: 95, KubernetesPercent: 1},
+					AmortizedCost:    CostMetric{Cost: 85, KubernetesPercent: 1},
 				},
 			},
 			ccsr: emtpyCCSR.Clone(),
@@ -145,6 +151,7 @@ func TestCloudCost_LoadCloudCost(t *testing.T) {
 							NetCost:          CostMetric{Cost: 40, KubernetesPercent: 1},
 							AmortizedNetCost: CostMetric{Cost: 45, KubernetesPercent: 1},
 							InvoicedCost:     CostMetric{Cost: 47.5, KubernetesPercent: 1},
+							AmortizedCost:    CostMetric{Cost: 42.5, KubernetesPercent: 1},
 						},
 					},
 				},
@@ -169,6 +176,7 @@ func TestCloudCost_LoadCloudCost(t *testing.T) {
 					NetCost:          CostMetric{Cost: 80, KubernetesPercent: 1},
 					AmortizedNetCost: CostMetric{Cost: 90, KubernetesPercent: 1},
 					InvoicedCost:     CostMetric{Cost: 95, KubernetesPercent: 1},
+					AmortizedCost:    CostMetric{Cost: 85, KubernetesPercent: 1},
 				},
 			},
 			ccsr: emtpyCCSR.Clone(),
@@ -194,6 +202,7 @@ func TestCloudCost_LoadCloudCost(t *testing.T) {
 							NetCost:          CostMetric{Cost: 40, KubernetesPercent: 1},
 							AmortizedNetCost: CostMetric{Cost: 45, KubernetesPercent: 1},
 							InvoicedCost:     CostMetric{Cost: 47.5, KubernetesPercent: 1},
+							AmortizedCost:    CostMetric{Cost: 42.5, KubernetesPercent: 1},
 						},
 					},
 				},
@@ -208,6 +217,7 @@ func TestCloudCost_LoadCloudCost(t *testing.T) {
 					NetCost:          CostMetric{Cost: 40, KubernetesPercent: 1},
 					AmortizedNetCost: CostMetric{Cost: 60, KubernetesPercent: 1},
 					InvoicedCost:     CostMetric{Cost: 50, KubernetesPercent: 1},
+					AmortizedCost:    CostMetric{Cost: 80, KubernetesPercent: 1},
 				},
 				{
 					Properties:       ccProperties1,
@@ -216,6 +226,7 @@ func TestCloudCost_LoadCloudCost(t *testing.T) {
 					NetCost:          CostMetric{Cost: 60, KubernetesPercent: 0},
 					AmortizedNetCost: CostMetric{Cost: 40, KubernetesPercent: 0},
 					InvoicedCost:     CostMetric{Cost: 50, KubernetesPercent: 0},
+					AmortizedCost:    CostMetric{Cost: 20, KubernetesPercent: 0},
 				},
 			},
 			ccsr: emtpyCCSR.Clone(),
@@ -236,6 +247,7 @@ func TestCloudCost_LoadCloudCost(t *testing.T) {
 							NetCost:          CostMetric{Cost: 100, KubernetesPercent: 0.4},
 							AmortizedNetCost: CostMetric{Cost: 100, KubernetesPercent: 0.6},
 							InvoicedCost:     CostMetric{Cost: 100, KubernetesPercent: 0.5},
+							AmortizedCost:    CostMetric{Cost: 100, KubernetesPercent: 0.8},
 						},
 					},
 				},

+ 24 - 6
pkg/kubecost/kubecost_codecs.go

@@ -33,6 +33,12 @@ const (
 )
 
 const (
+	// AuditCodecVersion is used for any resources listed in the Audit version set
+	AuditCodecVersion uint8 = 1
+
+	// CloudCostCodecVersion is used for any resources listed in the CloudCost version set
+	CloudCostCodecVersion uint8 = 1
+
 	// DefaultCodecVersion is used for any resources listed in the Default version set
 	DefaultCodecVersion uint8 = 17
 
@@ -41,12 +47,6 @@ const (
 
 	// AllocationCodecVersion is used for any resources listed in the Allocation version set
 	AllocationCodecVersion uint8 = 16
-
-	// AuditCodecVersion is used for any resources listed in the Audit version set
-	AuditCodecVersion uint8 = 1
-
-	// CloudCostCodecVersion is used for any resources listed in the CloudCost version set
-	CloudCostCodecVersion uint8 = 1
 )
 
 //--------------------------------------------------------------------------
@@ -4753,6 +4753,14 @@ func (target *CloudCost) MarshalBinaryWithContext(ctx *EncodingContext) (err err
 	}
 	// --- [end][write][struct](CostMetric) ---
 
+	// --- [begin][write][struct](CostMetric) ---
+	buff.WriteInt(0) // [compatibility, unused]
+	errG := target.AmortizedCost.MarshalBinaryWithContext(ctx)
+	if errG != nil {
+		return errG
+	}
+	// --- [end][write][struct](CostMetric) ---
+
 	return nil
 }
 
@@ -4874,6 +4882,16 @@ func (target *CloudCost) UnmarshalBinaryWithContext(ctx *DecodingContext) (err e
 	target.InvoicedCost = *f
 	// --- [end][read][struct](CostMetric) ---
 
+	// --- [begin][read][struct](CostMetric) ---
+	g := &CostMetric{}
+	buff.ReadInt() // [compatibility, unused]
+	errG := g.UnmarshalBinaryWithContext(ctx)
+	if errG != nil {
+		return errG
+	}
+	target.AmortizedCost = *g
+	// --- [end][read][struct](CostMetric) ---
+
 	return nil
 }