Просмотр исходного кода

Fix bug where Summary Allocation would not account for disabled reconciliation in sharing coefficients

Niko Kovacevic 4 лет назад
Родитель
Сommit
570b1a2e37
2 измененных файлов с 21 добавлено и 5 удалено
  1. 2 0
      pkg/kubecost/allocation.go
  2. 19 5
      pkg/kubecost/summaryallocation.go

+ 2 - 0
pkg/kubecost/allocation.go

@@ -834,6 +834,8 @@ type AllocationAggregationOptions struct {
 	IdleByNode            bool
 	IdleByNode            bool
 	LabelConfig           *LabelConfig
 	LabelConfig           *LabelConfig
 	MergeUnallocated      bool
 	MergeUnallocated      bool
+	Reconcile             bool
+	ReconcileNetwork      bool
 	ShareFuncs            []AllocationMatchFunc
 	ShareFuncs            []AllocationMatchFunc
 	ShareIdle             string
 	ShareIdle             string
 	ShareSplit            string
 	ShareSplit            string

+ 19 - 5
pkg/kubecost/summaryallocation.go

@@ -326,14 +326,14 @@ func NewSummaryAllocationSet(as *AllocationSet, ffs, kfs []AllocationMatchFunc,
 	for _, alloc := range as.allocations {
 	for _, alloc := range as.allocations {
 		// First, detect if the allocation should be kept. If so, mark it as
 		// First, detect if the allocation should be kept. If so, mark it as
 		// such, insert it, and continue.
 		// such, insert it, and continue.
-		sholdKeep := false
+		shouldKeep := false
 		for _, kf := range kfs {
 		for _, kf := range kfs {
 			if kf(alloc) {
 			if kf(alloc) {
-				sholdKeep = true
+				shouldKeep = true
 				break
 				break
 			}
 			}
 		}
 		}
-		if sholdKeep {
+		if shouldKeep {
 			sa := NewSummaryAllocation(alloc, reconcile, reconcileNetwork)
 			sa := NewSummaryAllocation(alloc, reconcile, reconcileNetwork)
 			sa.Share = true
 			sa.Share = true
 			sas.Insert(sa)
 			sas.Insert(sa)
@@ -454,8 +454,8 @@ func (sas *SummaryAllocationSet) AggregateBy(aggregateBy []string, options *Allo
 	// an empty slice implies that we should aggregate everything. (See
 	// an empty slice implies that we should aggregate everything. (See
 	// generateKey for why that makes sense.)
 	// generateKey for why that makes sense.)
 	shouldAggregate := aggregateBy != nil
 	shouldAggregate := aggregateBy != nil
-	sholdKeep := len(options.SharedHourlyCosts) > 0 || len(options.ShareFuncs) > 0
-	if !shouldAggregate && !sholdKeep {
+	shouldKeep := len(options.SharedHourlyCosts) > 0 || len(options.ShareFuncs) > 0
+	if !shouldAggregate && !shouldKeep {
 		return nil
 		return nil
 	}
 	}
 
 
@@ -627,6 +627,20 @@ func (sas *SummaryAllocationSet) AggregateBy(aggregateBy []string, options *Allo
 		}
 		}
 	}
 	}
 
 
+	// If reconciliation has been fully or partially disabled, clear the
+	// relevant adjustments from the alloc totals
+	if allocTotals != nil && (!options.Reconcile || !options.ReconcileNetwork) {
+		if !options.Reconcile {
+			for _, tot := range allocTotals {
+				tot.ClearAdjustments()
+			}
+		} else if !options.ReconcileNetwork {
+			for _, tot := range allocTotals {
+				tot.NetworkCostAdjustment = 0.0
+			}
+		}
+	}
+
 	// If filters have been applied, then we need to record allocation resource
 	// If filters have been applied, then we need to record allocation resource
 	// totals after filtration (i.e. the allocations that are present) so that
 	// totals after filtration (i.e. the allocations that are present) so that
 	// we can identify the proportion of idle cost to keep. That is, we should
 	// we can identify the proportion of idle cost to keep. That is, we should