Quellcode durchsuchen

WIP namespace labels - still need to handle query param

Signed-off-by: Alex Meijer <ameijer@kubecost.com>
(cherry picked from commit 28796a27567bc77404401ab745d52971f3ec740c)
Signed-off-by: Alex Meijer <ameijer@kubecost.com>
Alex Meijer vor 3 Jahren
Ursprung
Commit
a7b4656b33
3 geänderte Dateien mit 20 neuen und 1 gelöschten Zeilen
  1. 2 1
      pkg/costmodel/costmodel.go
  2. 1 0
      pkg/kubecost/allocation.go
  3. 17 0
      pkg/kubecost/allocationprops.go

+ 2 - 1
pkg/costmodel/costmodel.go

@@ -2295,7 +2295,7 @@ func measureTimeAsync(start time.Time, threshold time.Duration, name string, ch
 	}
 }
 
-func (cm *CostModel) QueryAllocation(window kubecost.Window, resolution, step time.Duration, aggregate []string, includeIdle, idleByNode, includeProportionalAssetResourceCosts bool) (*kubecost.AllocationSetRange, error) {
+func (cm *CostModel) QueryAllocation(window kubecost.Window, resolution, step time.Duration, aggregate []string, includeIdle, idleByNode, includeProportionalAssetResourceCosts, includeNamespaceLabels bool) (*kubecost.AllocationSetRange, error) {
 	// Validate window is legal
 	if window.IsOpen() || window.IsNegative() {
 		return nil, fmt.Errorf("illegal window: %s", window)
@@ -2347,6 +2347,7 @@ func (cm *CostModel) QueryAllocation(window kubecost.Window, resolution, step ti
 	opts := &kubecost.AllocationAggregationOptions{
 		IncludeProportionalAssetResourceCosts: includeProportionalAssetResourceCosts,
 		IdleByNode:                            idleByNode,
+		IncludeNamespaceLabels:                includeNamespaceLabels,
 	}
 
 	// Aggregate

+ 1 - 0
pkg/kubecost/allocation.go

@@ -921,6 +921,7 @@ type AllocationAggregationOptions struct {
 	ShareSplit                            string
 	SharedHourlyCosts                     map[string]float64
 	SplitIdle                             bool
+	IncludeNamespaceLabels                bool
 }
 
 // AggregateBy aggregates the Allocations in the given AllocationSet by the given

+ 17 - 0
pkg/kubecost/allocationprops.go

@@ -440,6 +440,9 @@ func (p *AllocationProperties) Intersection(that *AllocationProperties) *Allocat
 	}
 	if p.Namespace == that.Namespace {
 		intersectionProps.Namespace = p.Namespace
+
+		intersectionProps.Annotations = mapIntersection(p.Annotations, that.Annotations)
+		intersectionProps.Labels = mapIntersection(p.Labels, that.Labels)
 	}
 	if p.Pod == that.Pod {
 		intersectionProps.Pod = p.Pod
@@ -450,6 +453,20 @@ func (p *AllocationProperties) Intersection(that *AllocationProperties) *Allocat
 	return intersectionProps
 }
 
+func mapIntersection(map1, map2 map[string]string) map[string]string {
+	result := make(map[string]string)
+	for key, value := range map1 {
+		if value2, ok := map2[key]; ok {
+			if value2 == value {
+				result[key] = value
+			}
+		}
+
+	}
+
+	return result
+}
+
 func (p *AllocationProperties) String() string {
 	if p == nil {
 		return "<nil>"