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

Allocation ETL: on-demand external cost: fix Properties

Niko Kovacevic 5 лет назад
Родитель
Сommit
f5436a6955
2 измененных файлов с 46 добавлено и 0 удалено
  1. 34 0
      pkg/kubecost/asset.go
  2. 12 0
      pkg/kubecost/asset_test.go

+ 34 - 0
pkg/kubecost/asset.go

@@ -99,10 +99,14 @@ func AssetToExternalAllocation(asset Asset, aggregateBy []string, allocationProp
 	// names will collect the slash-separated names accrued by iterating over
 	// aggregateBy and checking the relevant labels.
 	names := []string{}
+
 	// match records whether or not a match was found in the Asset labels,
 	// such that is can genuinely be turned into an external Allocation.
 	match := false
 
+	// props records the relevant Properties to set on the resultant Allocation
+	props := Properties{}
+
 	for _, aggBy := range aggregateBy {
 		// labelName should be derived from the mapping of properties to
 		// label names, unless the aggBy is explicitly a label, in which
@@ -127,9 +131,38 @@ func AssetToExternalAllocation(asset Asset, aggregateBy []string, allocationProp
 				// e.g. aggBy="label:env", value="prod" => "env=prod"
 				names = append(names, fmt.Sprintf("%s=%s", strings.TrimPrefix(aggBy, "label:"), value))
 				match = true
+
+				// Set the corresponding label in props
+				labels, err := props.GetLabels()
+				if err != nil {
+					labels = map[string]string{}
+				}
+				labels[labelName] = value
+				props.SetLabels(labels)
 			} else {
 				names = append(names, value)
 				match = true
+
+				// Set the corresponding property on props
+				switch aggBy {
+				case ClusterProp.String():
+					props.SetCluster(value)
+				case NodeProp.String():
+					props.SetNode(value)
+				case NamespaceProp.String():
+					props.SetNamespace(value)
+				case ControllerKindProp.String():
+					props.SetControllerKind(value)
+				case ControllerProp.String():
+					props.SetController(value)
+				case PodProp.String():
+					props.SetPod(value)
+				case ContainerProp.String():
+					props.SetContainer(value)
+				case ServiceProp.String():
+					// TODO niko/allocation-etl how to do this? multi-service?
+					props.SetServices([]string{value})
+				}
 			}
 		} else {
 			// No value label value was found on the Asset; consider it
@@ -148,6 +181,7 @@ func AssetToExternalAllocation(asset Asset, aggregateBy []string, allocationProp
 	// TODO niko/allocation-etl resource totals?
 	return &Allocation{
 		Name:         strings.Join(names, "/"),
+		Properties:   props,
 		ExternalCost: asset.TotalCost(),
 		TotalCost:    asset.TotalCost(),
 	}, nil

+ 12 - 0
pkg/kubecost/asset_test.go

@@ -1080,6 +1080,9 @@ func TestAssetToExternalAllocation(t *testing.T) {
 	if alloc.Name != "monitoring" {
 		t.Fatalf("expected external allocation with name '%s'; got '%s'", "monitoring", alloc.Name)
 	}
+	if ns, err := alloc.Properties.GetNamespace(); err != nil || ns != "monitoring" {
+		t.Fatalf("expected external allocation with Properties.Namespace '%s'; got '%s' (%s)", "monitoring", ns, err)
+	}
 	if alloc.ExternalCost != 10.00 {
 		t.Fatalf("expected external allocation with ExternalCost %f; got %f", 10.00, alloc.ExternalCost)
 	}
@@ -1095,6 +1098,12 @@ func TestAssetToExternalAllocation(t *testing.T) {
 	if alloc.Name != "monitoring/env=prod" {
 		t.Fatalf("expected external allocation with name '%s'; got '%s'", "monitoring/env=prod", alloc.Name)
 	}
+	if ns, err := alloc.Properties.GetNamespace(); err != nil || ns != "monitoring" {
+		t.Fatalf("expected external allocation with Properties.Namespace '%s'; got '%s' (%s)", "monitoring", ns, err)
+	}
+	if ls, err := alloc.Properties.GetLabels(); err != nil || ls["env"] != "prod" {
+		t.Fatalf("expected external allocation with Properties.Labels[\"env\"] '%s'; got '%s' (%s)", "prod", ls["env"], err)
+	}
 	if alloc.ExternalCost != 10.00 {
 		t.Fatalf("expected external allocation with ExternalCost %f; got %f", 10.00, alloc.ExternalCost)
 	}
@@ -1110,6 +1119,9 @@ func TestAssetToExternalAllocation(t *testing.T) {
 	if alloc.Name != "monitoring/__unallocated__" {
 		t.Fatalf("expected external allocation with name '%s'; got '%s'", "monitoring/__unallocated__", alloc.Name)
 	}
+	if ns, err := alloc.Properties.GetNamespace(); err != nil || ns != "monitoring" {
+		t.Fatalf("expected external allocation with Properties.Namespace '%s'; got '%s' (%s)", "monitoring", ns, err)
+	}
 	if alloc.ExternalCost != 10.00 {
 		t.Fatalf("expected external allocation with ExternalCost %f; got %f", 10.00, alloc.ExternalCost)
 	}