Procházet zdrojové kódy

do not use nil as sentinel value, pass aggregated metadata prefs down via object

Signed-off-by: Alex Meijer <ameijer@kubecost.com>
(cherry picked from commit c1aefca5dc709c6fe9ca3851ce5b38184c895518)
Signed-off-by: Alex Meijer <ameijer@kubecost.com>
Alex Meijer před 3 roky
rodič
revize
24460d0324
2 změnil soubory, kde provedl 28 přidání a 27 odebrání
  1. 3 5
      pkg/kubecost/allocation.go
  2. 25 22
      pkg/kubecost/allocationprops.go

+ 3 - 5
pkg/kubecost/allocation.go

@@ -1043,11 +1043,9 @@ func (as *AllocationSet) AggregateBy(aggregateBy []string, options *AllocationAg
 	// to aggregate.
 	for _, alloc := range as.Allocations {
 		// if the user does not want any aggregated labels/annotations returned
-		// set the
-		if !options.IncludeAggregatedMetadata {
-			alloc.Properties.Annotations = nil
-			alloc.Properties.Labels = nil
-		}
+		// set the properties accordingly
+		alloc.Properties.AggregatedMetadata = options.IncludeAggregatedMetadata
+
 		// External allocations get aggregated post-hoc (see step 6) and do
 		// not necessarily contain complete sets of properties, so they are
 		// moved to a separate AllocationSet.

+ 25 - 22
pkg/kubecost/allocationprops.go

@@ -92,17 +92,18 @@ func ParseProperty(text string) (string, error) {
 
 // AllocationProperties describes a set of Kubernetes objects.
 type AllocationProperties struct {
-	Cluster        string                `json:"cluster,omitempty"`
-	Node           string                `json:"node,omitempty"`
-	Container      string                `json:"container,omitempty"`
-	Controller     string                `json:"controller,omitempty"`
-	ControllerKind string                `json:"controllerKind,omitempty"`
-	Namespace      string                `json:"namespace,omitempty"`
-	Pod            string                `json:"pod,omitempty"`
-	Services       []string              `json:"services,omitempty"`
-	ProviderID     string                `json:"providerID,omitempty"`
-	Labels         AllocationLabels      `json:"labels,omitempty"`
-	Annotations    AllocationAnnotations `json:"annotations,omitempty"`
+	Cluster            string                `json:"cluster,omitempty"`
+	Node               string                `json:"node,omitempty"`
+	Container          string                `json:"container,omitempty"`
+	Controller         string                `json:"controller,omitempty"`
+	ControllerKind     string                `json:"controllerKind,omitempty"`
+	Namespace          string                `json:"namespace,omitempty"`
+	Pod                string                `json:"pod,omitempty"`
+	Services           []string              `json:"services,omitempty"`
+	ProviderID         string                `json:"providerID,omitempty"`
+	Labels             AllocationLabels      `json:"labels,omitempty"`
+	Annotations        AllocationAnnotations `json:"annotations,omitempty"`
+	AggregatedMetadata bool                  `json:"-"`
 }
 
 // AllocationLabels is a schema-free mapping of key/value pairs that can be
@@ -440,19 +441,21 @@ func (p *AllocationProperties) Intersection(that *AllocationProperties) *Allocat
 	}
 	if p.Namespace == that.Namespace {
 
-		// ignore the incoming labels from unallocated or unmounted special case pods
 		intersectionProps.Namespace = p.Namespace
-		if p.Container == UnmountedSuffix || p.Container == UnallocatedSuffix {
-			intersectionProps.Annotations = that.Annotations
-			intersectionProps.Labels = that.Labels
-		} else if that.Container == UnmountedSuffix || that.Container == UnallocatedSuffix {
-			intersectionProps.Annotations = p.Annotations
-			intersectionProps.Labels = p.Labels
-		} else {
-			intersectionProps.Annotations = mapIntersection(p.Annotations, that.Annotations)
-			intersectionProps.Labels = mapIntersection(p.Labels, that.Labels)
+		// ignore the incoming labels from unallocated or unmounted special case pods
+		if p.AggregatedMetadata || that.AggregatedMetadata {
+			intersectionProps.AggregatedMetadata = true
+			if p.Container == UnmountedSuffix || p.Container == UnallocatedSuffix {
+				intersectionProps.Annotations = that.Annotations
+				intersectionProps.Labels = that.Labels
+			} else if that.Container == UnmountedSuffix || that.Container == UnallocatedSuffix {
+				intersectionProps.Annotations = p.Annotations
+				intersectionProps.Labels = p.Labels
+			} else {
+				intersectionProps.Annotations = mapIntersection(p.Annotations, that.Annotations)
+				intersectionProps.Labels = mapIntersection(p.Labels, that.Labels)
+			}
 		}
-
 	}
 	if p.Pod == that.Pod {
 		intersectionProps.Pod = p.Pod