Ver Fonte

Add additional adjustment fields

Sean Holcomb há 5 anos atrás
pai
commit
b8aa6222e5
2 ficheiros alterados com 145 adições e 98 exclusões
  1. 87 52
      pkg/kubecost/allocation.go
  2. 58 46
      pkg/kubecost/kubecost_codecs.go

+ 87 - 52
pkg/kubecost/allocation.go

@@ -47,33 +47,36 @@ const ShareNone = "__none__"
 // Allocation is a unit of resource allocation and cost for a given window
 // of time and for a given kubernetes construct with its associated set of
 // properties.
-// TODO:CLEANUP consider dropping name in favor of just AllocationProperties and an
+// TODO:CLEANUP consider dropping name in favor of just Allocation and an
 // Assets-style key() function for AllocationSet.
 type Allocation struct {
-	Name                   string                `json:"name"`
-	Properties             *AllocationProperties `json:"properties,omitempty"`
-	Window                 Window                `json:"window"`
-	Start                  time.Time             `json:"start"`
-	End                    time.Time             `json:"end"`
-	CPUCoreHours           float64               `json:"cpuCoreHours"`
-	CPUCoreRequestAverage  float64               `json:"cpuCoreRequestAverage"`
-	CPUCoreUsageAverage    float64               `json:"cpuCoreUsageAverage"`
-	CPUCost                float64               `json:"cpuCost"`
-	CPUCostAdjustment      float64               `json:"cpuCostAdjustment"`
-	GPUHours               float64               `json:"gpuHours"`
-	GPUCost                float64               `json:"gpuCost"`
-	GPUCostAdjustment      float64               `json:"gpuCostAdjustment"`
-	NetworkCost            float64               `json:"networkCost"`
-	LoadBalancerCost       float64               `json:"loadBalancerCost"`
-	PVs                    PVAllocations         `json:"-"`
-	PVCostAdjustment       float64               `json:"pvCostAdjustment"`
-	RAMByteHours           float64               `json:"ramByteHours"`
-	RAMBytesRequestAverage float64               `json:"ramByteRequestAverage"`
-	RAMBytesUsageAverage   float64               `json:"ramByteUsageAverage"`
-	RAMCost                float64               `json:"ramCost"`
-	RAMCostAdjustment      float64               `json:"ramCostAdjustment"`
-	SharedCost             float64               `json:"sharedCost"`
-	ExternalCost           float64               `json:"externalCost"`
+	Name                       string                `json:"name"`
+	Properties                 *AllocationProperties `json:"properties,omitempty"`
+	Window                     Window                `json:"window"`
+	Start                      time.Time             `json:"start"`
+	End                        time.Time             `json:"end"`
+	CPUCoreHours               float64               `json:"cpuCoreHours"`
+	CPUCoreRequestAverage      float64               `json:"cpuCoreRequestAverage"`
+	CPUCoreUsageAverage        float64               `json:"cpuCoreUsageAverage"`
+	CPUCost                    float64               `json:"cpuCost"`
+	CPUCostAdjustment          float64               `json:"cpuCostAdjustment"`
+	GPUHours                   float64               `json:"gpuHours"`
+	GPUCost                    float64               `json:"gpuCost"`
+	GPUCostAdjustment          float64               `json:"gpuCostAdjustment"`
+	NetworkCost                float64               `json:"networkCost"`
+	NetworkCostAdjustment      float64               `json:"networkCostAdjustment"`
+	LoadBalancerCost           float64               `json:"loadBalancerCost"`
+	LoadBalancerCostAdjustment float64               `json:"loadBalancerCostAdjustment"`
+	PVs                        PVAllocations         `json:"-"`
+	PVCostAdjustment           float64               `json:"pvCostAdjustment"`
+	RAMByteHours               float64               `json:"ramByteHours"`
+	RAMBytesRequestAverage     float64               `json:"ramByteRequestAverage"`
+	RAMBytesUsageAverage       float64               `json:"ramByteUsageAverage"`
+	RAMCost                    float64               `json:"ramCost"`
+	RAMCostAdjustment          float64               `json:"ramCostAdjustment"`
+	SharedCost                 float64               `json:"sharedCost"`
+	SharedCostAdjustment       float64               `json:"sharedCostAdjustment"`
+	ExternalCost               float64               `json:"externalCost"`
 	// RawAllocationOnly is a pointer so if it is not present it will be
 	// marshalled as null rather than as an object with Go default values.
 	RawAllocationOnly *RawAllocationOnlyData `json:"rawAllocationOnly"`
@@ -190,31 +193,34 @@ func (a *Allocation) Clone() *Allocation {
 	}
 
 	return &Allocation{
-		Name:                   a.Name,
-		Properties:             a.Properties.Clone(),
-		Window:                 a.Window.Clone(),
-		Start:                  a.Start,
-		End:                    a.End,
-		CPUCoreHours:           a.CPUCoreHours,
-		CPUCoreRequestAverage:  a.CPUCoreRequestAverage,
-		CPUCoreUsageAverage:    a.CPUCoreUsageAverage,
-		CPUCost:                a.CPUCost,
-		CPUCostAdjustment:      a.CPUCostAdjustment,
-		GPUHours:               a.GPUHours,
-		GPUCost:                a.GPUCost,
-		GPUCostAdjustment:      a.GPUCostAdjustment,
-		NetworkCost:            a.NetworkCost,
-		LoadBalancerCost:       a.LoadBalancerCost,
-		PVs:                    a.PVs.Clone(),
-		PVCostAdjustment:       a.PVCostAdjustment,
-		RAMByteHours:           a.RAMByteHours,
-		RAMBytesRequestAverage: a.RAMBytesRequestAverage,
-		RAMBytesUsageAverage:   a.RAMBytesUsageAverage,
-		RAMCost:                a.RAMCost,
-		RAMCostAdjustment:      a.RAMCostAdjustment,
-		SharedCost:             a.SharedCost,
-		ExternalCost:           a.ExternalCost,
-		RawAllocationOnly:      a.RawAllocationOnly.Clone(),
+		Name:                       a.Name,
+		Properties:                 a.Properties.Clone(),
+		Window:                     a.Window.Clone(),
+		Start:                      a.Start,
+		End:                        a.End,
+		CPUCoreHours:               a.CPUCoreHours,
+		CPUCoreRequestAverage:      a.CPUCoreRequestAverage,
+		CPUCoreUsageAverage:        a.CPUCoreUsageAverage,
+		CPUCost:                    a.CPUCost,
+		CPUCostAdjustment:          a.CPUCostAdjustment,
+		GPUHours:                   a.GPUHours,
+		GPUCost:                    a.GPUCost,
+		GPUCostAdjustment:          a.GPUCostAdjustment,
+		NetworkCost:                a.NetworkCost,
+		NetworkCostAdjustment:      a.NetworkCostAdjustment,
+		LoadBalancerCost:           a.LoadBalancerCost,
+		LoadBalancerCostAdjustment: a.LoadBalancerCostAdjustment,
+		PVs:                        a.PVs.Clone(),
+		PVCostAdjustment:           a.PVCostAdjustment,
+		RAMByteHours:               a.RAMByteHours,
+		RAMBytesRequestAverage:     a.RAMBytesRequestAverage,
+		RAMBytesUsageAverage:       a.RAMBytesUsageAverage,
+		RAMCost:                    a.RAMCost,
+		RAMCostAdjustment:          a.RAMCostAdjustment,
+		SharedCost:                 a.SharedCost,
+		SharedCostAdjustment:       a.SharedCostAdjustment,
+		ExternalCost:               a.ExternalCost,
+		RawAllocationOnly:          a.RawAllocationOnly.Clone(),
 	}
 }
 
@@ -275,10 +281,15 @@ func (a *Allocation) Equal(that *Allocation) bool {
 	if !util.IsApproximately(a.NetworkCost, that.NetworkCost) {
 		return false
 	}
+	if !util.IsApproximately(a.NetworkCostAdjustment, that.NetworkCostAdjustment) {
+		return false
+	}
 	if !util.IsApproximately(a.LoadBalancerCost, that.LoadBalancerCost) {
 		return false
 	}
-
+	if !util.IsApproximately(a.LoadBalancerCostAdjustment, that.LoadBalancerCostAdjustment) {
+		return false
+	}
 	if !util.IsApproximately(a.PVCostAdjustment, that.PVCostAdjustment) {
 		return false
 	}
@@ -294,6 +305,9 @@ func (a *Allocation) Equal(that *Allocation) bool {
 	if !util.IsApproximately(a.SharedCost, that.SharedCost) {
 		return false
 	}
+	if !util.IsApproximately(a.SharedCostAdjustment, that.SharedCostAdjustment) {
+		return false
+	}
 	if !util.IsApproximately(a.ExternalCost, that.ExternalCost) {
 		return false
 	}
@@ -331,7 +345,7 @@ func (a *Allocation) Equal(that *Allocation) bool {
 
 // TotalCost is the total cost of the Allocation including adjustments
 func (a *Allocation) TotalCost() float64 {
-	return a.CPUTotalCost() + a.GPUTotalCost() + a.RAMTotalCost() + a.PVTotalCost() + a.NetworkCost + a.SharedCost + a.ExternalCost + a.LoadBalancerCost
+	return a.CPUTotalCost() + a.GPUTotalCost() + a.RAMTotalCost() + a.PVTotalCost() + a.NetworkTotalCost() + a.LoadBalancerCost + a.SharedTotalCost() + a.ExternalCost
 }
 
 // CPUTotalCost calculates total CPU cost of Allocation including adjustment
@@ -354,6 +368,21 @@ func (a *Allocation) PVTotalCost() float64 {
 	return a.PVCost() + a.PVCostAdjustment
 }
 
+// NetworkTotalCost calculates total Network cost of Allocation including adjustment
+func (a *Allocation) NetworkTotalCost() float64 {
+	return a.NetworkCost + a.NetworkCostAdjustment
+}
+
+// LBTotalCost calculates total LB cost of Allocation including adjustment
+func (a *Allocation) LBTotalCost() float64 {
+	return a.LoadBalancerCost + a.LoadBalancerCostAdjustment
+}
+
+// SharedTotalCost calculates total shared cost of Allocation including adjustment
+func (a *Allocation) SharedTotalCost() float64 {
+	return a.SharedCost + a.SharedCostAdjustment
+}
+
 // PVCost calculate cumulative cost of all PVs that Allocation is attached to
 func (a *Allocation) PVCost() float64 {
 	cost := 0.0
@@ -467,7 +496,9 @@ func (a *Allocation) MarshalJSON() ([]byte, error) {
 	jsonEncodeFloat64(buffer, "gpuCost", a.GPUCost, ",")
 	jsonEncodeFloat64(buffer, "gpuCostAdjustment", a.GPUCostAdjustment, ",")
 	jsonEncodeFloat64(buffer, "networkCost", a.NetworkCost, ",")
+	jsonEncodeFloat64(buffer, "networkCostAdjustment", a.NetworkCostAdjustment, ",")
 	jsonEncodeFloat64(buffer, "loadBalancerCost", a.LoadBalancerCost, ",")
+	jsonEncodeFloat64(buffer, "loadBalancerCostAdjustment", a.LoadBalancerCostAdjustment, ",")
 	jsonEncodeFloat64(buffer, "pvBytes", a.PVBytes(), ",")
 	jsonEncodeFloat64(buffer, "pvByteHours", a.PVByteHours(), ",")
 	jsonEncodeFloat64(buffer, "pvCost", a.PVCost(), ",")
@@ -481,6 +512,7 @@ func (a *Allocation) MarshalJSON() ([]byte, error) {
 	jsonEncodeFloat64(buffer, "ramCostAdjustment", a.RAMCostAdjustment, ",")
 	jsonEncodeFloat64(buffer, "ramEfficiency", a.RAMEfficiency(), ",")
 	jsonEncodeFloat64(buffer, "sharedCost", a.SharedCost, ",")
+	jsonEncodeFloat64(buffer, "sharedCostAdjustment", a.SharedCostAdjustment, ",")
 	jsonEncodeFloat64(buffer, "externalCost", a.ExternalCost, ",")
 	jsonEncodeFloat64(buffer, "totalCost", a.TotalCost(), ",")
 	jsonEncodeFloat64(buffer, "totalEfficiency", a.TotalEfficiency(), ",")
@@ -614,6 +646,9 @@ func (a *Allocation) add(that *Allocation) {
 	a.RAMCostAdjustment += that.RAMCostAdjustment
 	a.GPUCostAdjustment += that.GPUCostAdjustment
 	a.PVCostAdjustment += that.PVCostAdjustment
+	a.NetworkCostAdjustment += that.NetworkCostAdjustment
+	a.LoadBalancerCostAdjustment += a.LoadBalancerCostAdjustment
+	a.SharedCostAdjustment += a.SharedCostAdjustment
 
 	// Any data that is in a "raw allocation only" is not valid in any
 	// sort of cumulative Allocation (like one that is added).

+ 58 - 46
pkg/kubecost/kubecost_codecs.go

@@ -162,16 +162,18 @@ func (target *Allocation) MarshalBinary() (data []byte, err error) {
 	buff.WriteBytes(d)
 	// --- [end][write][reference](time.Time) ---
 
-	buff.WriteFloat64(target.CPUCoreHours)          // write float64
-	buff.WriteFloat64(target.CPUCoreRequestAverage) // write float64
-	buff.WriteFloat64(target.CPUCoreUsageAverage)   // write float64
-	buff.WriteFloat64(target.CPUCost)               // write float64
-	buff.WriteFloat64(target.CPUCostAdjustment)     // write float64
-	buff.WriteFloat64(target.GPUHours)              // write float64
-	buff.WriteFloat64(target.GPUCost)               // write float64
-	buff.WriteFloat64(target.GPUCostAdjustment)     // write float64
-	buff.WriteFloat64(target.NetworkCost)           // write float64
-	buff.WriteFloat64(target.LoadBalancerCost)      // write float64
+	buff.WriteFloat64(target.CPUCoreHours)               // write float64
+	buff.WriteFloat64(target.CPUCoreRequestAverage)      // write float64
+	buff.WriteFloat64(target.CPUCoreUsageAverage)        // write float64
+	buff.WriteFloat64(target.CPUCost)                    // write float64
+	buff.WriteFloat64(target.CPUCostAdjustment)          // write float64
+	buff.WriteFloat64(target.GPUHours)                   // write float64
+	buff.WriteFloat64(target.GPUCost)                    // write float64
+	buff.WriteFloat64(target.GPUCostAdjustment)          // write float64
+	buff.WriteFloat64(target.NetworkCost)                // write float64
+	buff.WriteFloat64(target.NetworkCostAdjustment)      // write float64
+	buff.WriteFloat64(target.LoadBalancerCost)           // write float64
+	buff.WriteFloat64(target.LoadBalancerCostAdjustment) // write float64
 	// --- [begin][write][alias](PVAllocations) ---
 	if map[PVKey]*PVAllocation(target.PVs) == nil {
 		buff.WriteUInt8(uint8(0)) // write nil byte
@@ -218,6 +220,7 @@ func (target *Allocation) MarshalBinary() (data []byte, err error) {
 	buff.WriteFloat64(target.RAMCost)                // write float64
 	buff.WriteFloat64(target.RAMCostAdjustment)      // write float64
 	buff.WriteFloat64(target.SharedCost)             // write float64
+	buff.WriteFloat64(target.SharedCostAdjustment)   // write float64
 	buff.WriteFloat64(target.ExternalCost)           // write float64
 	if target.RawAllocationOnly == nil {
 		buff.WriteUInt8(uint8(0)) // write nil byte
@@ -340,26 +343,32 @@ func (target *Allocation) UnmarshalBinary(data []byte) (err error) {
 	target.NetworkCost = y
 
 	aa := buff.ReadFloat64() // read float64
-	target.LoadBalancerCost = aa
+	target.NetworkCostAdjustment = aa
+
+	bb := buff.ReadFloat64() // read float64
+	target.LoadBalancerCost = bb
+
+	cc := buff.ReadFloat64() // read float64
+	target.LoadBalancerCostAdjustment = cc
 
 	// --- [begin][read][alias](PVAllocations) ---
-	var bb map[PVKey]*PVAllocation
+	var dd map[PVKey]*PVAllocation
 	if buff.ReadUInt8() == uint8(0) {
-		bb = nil
+		dd = nil
 	} else {
 		// --- [begin][read][map](map[PVKey]*PVAllocation) ---
-		dd := buff.ReadInt() // map len
-		cc := make(map[PVKey]*PVAllocation, dd)
-		for i := 0; i < dd; i++ {
+		ff := buff.ReadInt() // map len
+		ee := make(map[PVKey]*PVAllocation, ff)
+		for i := 0; i < ff; i++ {
 			// --- [begin][read][struct](PVKey) ---
-			ee := &PVKey{}
-			ff := buff.ReadInt()     // byte array length
-			gg := buff.ReadBytes(ff) // byte array
-			errE := ee.UnmarshalBinary(gg)
+			gg := &PVKey{}
+			hh := buff.ReadInt()     // byte array length
+			kk := buff.ReadBytes(hh) // byte array
+			errE := gg.UnmarshalBinary(kk)
 			if errE != nil {
 				return errE
 			}
-			v := *ee
+			v = *gg
 			// --- [end][read][struct](PVKey) ---
 
 			var z *PVAllocation
@@ -367,62 +376,65 @@ func (target *Allocation) UnmarshalBinary(data []byte) (err error) {
 				z = nil
 			} else {
 				// --- [begin][read][struct](PVAllocation) ---
-				hh := &PVAllocation{}
-				kk := buff.ReadInt()     // byte array length
-				ll := buff.ReadBytes(kk) // byte array
-				errF := hh.UnmarshalBinary(ll)
+				ll := &PVAllocation{}
+				mm := buff.ReadInt()     // byte array length
+				nn := buff.ReadBytes(mm) // byte array
+				errF := ll.UnmarshalBinary(nn)
 				if errF != nil {
 					return errF
 				}
-				z = hh
+				z = ll
 				// --- [end][read][struct](PVAllocation) ---
 
 			}
-			cc[v] = z
+			ee[v] = z
 		}
-		bb = cc
+		dd = ee
 		// --- [end][read][map](map[PVKey]*PVAllocation) ---
 
 	}
-	target.PVs = PVAllocations(bb)
+	target.PVs = PVAllocations(dd)
 	// --- [end][read][alias](PVAllocations) ---
 
-	mm := buff.ReadFloat64() // read float64
-	target.PVCostAdjustment = mm
-
-	nn := buff.ReadFloat64() // read float64
-	target.RAMByteHours = nn
-
 	oo := buff.ReadFloat64() // read float64
-	target.RAMBytesRequestAverage = oo
+	target.PVCostAdjustment = oo
 
 	pp := buff.ReadFloat64() // read float64
-	target.RAMBytesUsageAverage = pp
+	target.RAMByteHours = pp
 
 	qq := buff.ReadFloat64() // read float64
-	target.RAMCost = qq
+	target.RAMBytesRequestAverage = qq
 
 	rr := buff.ReadFloat64() // read float64
-	target.RAMCostAdjustment = rr
+	target.RAMBytesUsageAverage = rr
 
 	ss := buff.ReadFloat64() // read float64
-	target.SharedCost = ss
+	target.RAMCost = ss
 
 	tt := buff.ReadFloat64() // read float64
-	target.ExternalCost = tt
+	target.RAMCostAdjustment = tt
+
+	uu := buff.ReadFloat64() // read float64
+	target.SharedCost = uu
+
+	ww := buff.ReadFloat64() // read float64
+	target.SharedCostAdjustment = ww
+
+	xx := buff.ReadFloat64() // read float64
+	target.ExternalCost = xx
 
 	if buff.ReadUInt8() == uint8(0) {
 		target.RawAllocationOnly = nil
 	} else {
 		// --- [begin][read][struct](RawAllocationOnlyData) ---
-		uu := &RawAllocationOnlyData{}
-		ww := buff.ReadInt()     // byte array length
-		xx := buff.ReadBytes(ww) // byte array
-		errG := uu.UnmarshalBinary(xx)
+		yy := &RawAllocationOnlyData{}
+		aaa := buff.ReadInt()      // byte array length
+		bbb := buff.ReadBytes(aaa) // byte array
+		errG := yy.UnmarshalBinary(bbb)
 		if errG != nil {
 			return errG
 		}
-		target.RawAllocationOnly = uu
+		target.RawAllocationOnly = yy
 		// --- [end][read][struct](RawAllocationOnlyData) ---
 
 	}