Ver Fonte

Added an hours property to allocation load balancers to improve reconciliation calculation complexity

Signed-off-by: Matt Bolt <mbolt35@gmail.com>
Matt Bolt há 2 anos atrás
pai
commit
7b4a23e08b

+ 9 - 1
core/pkg/opencost/allocation.go

@@ -115,6 +115,7 @@ func (orig LbAllocations) Clone() LbAllocations {
 			Cost:    lbAlloc.Cost,
 			Private: lbAlloc.Private,
 			Ip:      lbAlloc.Ip,
+			Hours:   lbAlloc.Hours,
 		}
 	}
 	return newAllocs
@@ -124,7 +125,8 @@ type LbAllocation struct {
 	Service string  `json:"service"`
 	Cost    float64 `json:"cost"`
 	Private bool    `json:"private"`
-	Ip      string  `json:"ip"` //@bingen:field[version=19]
+	Ip      string  `json:"ip"`    //@bingen:field[version=19]
+	Hours   float64 `json:"hours"` //@bingen:field[version=21]
 }
 
 func (lba *LbAllocation) SanitizeNaN() {
@@ -135,6 +137,10 @@ func (lba *LbAllocation) SanitizeNaN() {
 		log.DedupedWarningf(5, "LBAllocation: Unexpected NaN found for Cost service:%s", lba.Service)
 		lba.Cost = 0
 	}
+	if math.IsNaN(lba.Hours) {
+		log.DedupedWarningf(5, "LBAllocation: Unexpected NaN found for Hours service:%s", lba.Service)
+		lba.Hours = 0
+	}
 }
 
 // RawAllocationOnlyData is information that only belong in "raw" Allocations,
@@ -1249,10 +1255,12 @@ func (thisLbAllocs LbAllocations) Add(thatLbAllocs LbAllocations) LbAllocations
 				thisLbAlloc = &LbAllocation{
 					Service: thatlbAlloc.Service,
 					Cost:    thatlbAlloc.Cost,
+					Hours:   thatlbAlloc.Hours,
 				}
 				mergedLbAllocs[lbKey] = thisLbAlloc
 			} else {
 				thisLbAlloc.Cost += thatlbAlloc.Cost
+				thisLbAlloc.Hours += thatlbAlloc.Hours
 			}
 
 		}

+ 1 - 1
core/pkg/opencost/bingen.go

@@ -46,7 +46,7 @@ package opencost
 // @bingen:end
 
 // Allocation Version Set: Includes Allocation pipeline specific resources
-// @bingen:set[name=Allocation,version=20]
+// @bingen:set[name=Allocation,version=21]
 // @bingen:generate:Allocation
 // @bingen:generate[stringtable]:AllocationSet
 // @bingen:generate:AllocationSetRange

+ 11 - 1
core/pkg/opencost/opencost_codecs.go

@@ -41,7 +41,7 @@ const (
 	AssetsCodecVersion uint8 = 21
 
 	// AllocationCodecVersion is used for any resources listed in the Allocation version set
-	AllocationCodecVersion uint8 = 20
+	AllocationCodecVersion uint8 = 21
 
 	// CloudCostCodecVersion is used for any resources listed in the CloudCost version set
 	CloudCostCodecVersion uint8 = 2
@@ -5433,6 +5433,7 @@ func (target *LbAllocation) MarshalBinaryWithContext(ctx *EncodingContext) (err
 	} else {
 		buff.WriteString(target.Ip) // write string
 	}
+	buff.WriteFloat64(target.Hours) // write float64
 	return nil
 }
 
@@ -5522,6 +5523,15 @@ func (target *LbAllocation) UnmarshalBinaryWithContext(ctx *DecodingContext) (er
 		target.Ip = "" // default
 	}
 
+	// field version check
+	if uint8(21) <= version {
+		k := buff.ReadFloat64() // read float64
+		target.Hours = k
+
+	} else {
+		target.Hours = float64(0) // default
+	}
+
 	return nil
 }
 

+ 11 - 0
pkg/costmodel/allocation_helpers.go

@@ -1440,18 +1440,29 @@ func applyLoadBalancersToPods(window opencost.Window, podMap map[podKey]*pod, lb
 		}
 
 		for _, alloc := range allocs {
+			// reocord the hours overlapped with the allocation for the load balancer
+			// if there was overlap. Otherwise, record a 0.0.
+			// TODO: Do we really want to include load balancers that have 0 overlap
+			// TODO: hours with the allocation?
+			var hours float64 = 0.0
+			if _, ok := allocHours[alloc]; ok {
+				hours = allocHours[alloc]
+			}
+
 			if alloc.LoadBalancers == nil {
 				alloc.LoadBalancers = opencost.LbAllocations{}
 			}
 
 			if _, found := alloc.LoadBalancers[sKey.String()]; found {
 				alloc.LoadBalancers[sKey.String()].Cost += alloc.LoadBalancerCost
+				alloc.LoadBalancers[sKey.String()].Hours += hours
 			} else {
 				alloc.LoadBalancers[sKey.String()] = &opencost.LbAllocation{
 					Service: sKey.Namespace + "/" + sKey.Service,
 					Cost:    alloc.LoadBalancerCost,
 					Private: lb.Private,
 					Ip:      lb.Ip,
+					Hours:   hours,
 				}
 			}
 		}