Преглед изворни кода

fix sharing coeffient and total as a result of unmounted pv cost in each allocation

Signed-off-by: Alan Rodrigues <alanr5691@yahoo.com>
Alan Rodrigues пре 2 година
родитељ
комит
8e21576777
2 измењених фајлова са 30 додато и 4 уклоњено
  1. 20 3
      pkg/kubecost/summaryallocation.go
  2. 10 1
      pkg/kubecost/totals.go

+ 20 - 3
pkg/kubecost/summaryallocation.go

@@ -298,7 +298,6 @@ func (sa *SummaryAllocation) IsUnmounted() bool {
 	if sa == nil {
 		return false
 	}
-
 	return strings.Contains(sa.Name, UnmountedSuffix)
 }
 
@@ -700,7 +699,12 @@ func (sas *SummaryAllocationSet) AggregateBy(aggregateBy []string, options *Allo
 		// Track total unmounted cost because it must be taken out of total
 		// allocated costs for sharing coefficients.
 		if sa.IsUnmounted() {
-			totalUnmountedCost += sa.TotalCost()
+			props := sa.Properties
+			if props != nil {
+				if props.Container == UnmountedSuffix && props.Namespace == UnmountedSuffix && props.Pod == UnmountedSuffix {
+					totalUnmountedCost += sa.TotalCost()
+				}
+			}
 		}
 	}
 
@@ -1013,6 +1017,10 @@ func (sas *SummaryAllocationSet) AggregateBy(aggregateBy []string, options *Allo
 			// Compute sharing coeffs by dividing the thus-far accumulated
 			// numerators by the now-finalized denominator.
 			for key := range sharingCoeffs {
+				// shared coeff shouldnt be part of the unmounted suffix key
+				if key == UnmountedSuffix {
+					continue
+				}
 				if sharingCoeffs[key] > 0.0 {
 					before := sharingCoeffs[key]
 					sharingCoeffs[key] /= sharingCoeffDenominator
@@ -1037,9 +1045,18 @@ func (sas *SummaryAllocationSet) AggregateBy(aggregateBy []string, options *Allo
 			for key, sa := range resultSet.SummaryAllocations {
 				// Idle and unmounted allocations, by definition, do not
 				// receive shared cost
-				if sa.IsIdle() || sa.IsUnmounted() {
+				if sa.IsIdle() {
 					continue
 				}
+				// If unmounted and is part of the computation it is subjected to sharing coeffient correction!
+				if sa.IsUnmounted() {
+					props := sa.Properties
+					if props != nil {
+						if props.Container == UnmountedSuffix && props.Namespace == UnmountedSuffix && props.Pod == UnmountedSuffix {
+							continue
+						}
+					}
+				}
 
 				sharingCoeff := sharingCoeffs[key]
 

+ 10 - 1
pkg/kubecost/totals.go

@@ -114,9 +114,18 @@ func ComputeAllocationTotals(as *AllocationSet, prop string) map[string]*Allocat
 
 	for _, alloc := range as.Allocations {
 		// Do not count idle or unmounted allocations
-		if alloc.IsIdle() || alloc.IsUnmounted() {
+		if alloc.IsIdle() {
 			continue
 		}
+		if alloc.IsUnmounted() {
+			props := alloc.Properties
+			if props == nil {
+				continue
+			}
+			if props.Container == UnmountedSuffix && props.Namespace == UnmountedSuffix && props.Pod == UnmountedSuffix {
+				continue
+			}
+		}
 
 		// Default to computing totals by Cluster, but allow override to use Node.
 		key := alloc.Properties.Cluster