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

Merge pull request #749 from kubecost/AjayTripathy-fix-multilabels

Ajay tripathy fix multilabels
Ajay Tripathy 5 лет назад
Родитель
Сommit
58222125f5
2 измененных файлов с 25 добавлено и 9 удалено
  1. 8 2
      pkg/kubecost/asset.go
  2. 17 7
      pkg/kubecost/asset_test.go

+ 8 - 2
pkg/kubecost/asset.go

@@ -123,7 +123,7 @@ type Asset interface {
 //   => nil, err
 //
 // (See asset_test.go for assertions of these examples and more.)
-func AssetToExternalAllocation(asset Asset, aggregateBy []string) (*Allocation, error) {
+func AssetToExternalAllocation(asset Asset, aggregateBy []string, externalLabelsCfg map[string]string) (*Allocation, error) {
 	if asset == nil {
 		return nil, fmt.Errorf("asset is nil")
 	}
@@ -143,9 +143,15 @@ func AssetToExternalAllocation(asset Asset, aggregateBy []string) (*Allocation,
 		// labelName should be derived from the mapping of properties to
 		// label names, unless the aggBy is explicitly a label, in which
 		// case we should pull the label name from the aggBy string.
+		// Unless this matches a special aggregation, as we have that mapping already transformed...
 		labelName := aggBy
+		agglName := aggBy
 		if strings.HasPrefix(aggBy, "label:") {
 			labelName = strings.TrimPrefix(aggBy, "label:")
+			agglName = labelName
+			if v, ok := externalLabelsCfg[labelName]; ok {
+				agglName = v
+			}
 		}
 
 		if labelName == "" {
@@ -155,7 +161,7 @@ func AssetToExternalAllocation(asset Asset, aggregateBy []string) (*Allocation,
 			continue
 		}
 
-		if value := asset.Labels()[labelName]; value != "" {
+		if value := asset.Labels()[agglName]; value != "" {
 			// Valid label value was found for one of the aggregation properties,
 			// so add it to the name.
 			if strings.HasPrefix(aggBy, "label:") {

+ 17 - 7
pkg/kubecost/asset_test.go

@@ -1028,7 +1028,7 @@ func TestAssetToExternalAllocation(t *testing.T) {
 	var alloc *Allocation
 	var err error
 
-	alloc, err = AssetToExternalAllocation(asset, []string{"namespace"})
+	alloc, err = AssetToExternalAllocation(asset, []string{"namespace"}, map[string]string{})
 	if err == nil {
 		t.Fatalf("expected error due to nil asset")
 	}
@@ -1045,15 +1045,16 @@ func TestAssetToExternalAllocation(t *testing.T) {
 	cloud.SetLabels(map[string]string{
 		"namespace": "monitoring",
 		"env":       "prod",
+		"product":   "cost-analyzer",
 	})
 	cloud.Cost = 10.00
 	asset = cloud
 
-	alloc, err = AssetToExternalAllocation(asset, []string{"namespace"})
+	alloc, err = AssetToExternalAllocation(asset, []string{"namespace"}, map[string]string{})
 	if err != nil {
 		t.Fatalf("expected to not error")
 	}
-	alloc, err = AssetToExternalAllocation(asset, nil)
+	alloc, err = AssetToExternalAllocation(asset, nil, map[string]string{})
 	if err == nil {
 		t.Fatalf("expected error due to nil aggregateBy")
 	}
@@ -1081,7 +1082,7 @@ func TestAssetToExternalAllocation(t *testing.T) {
 	//   => nil, err
 
 	// 1) single-prop full match
-	alloc, err = AssetToExternalAllocation(asset, []string{"namespace"})
+	alloc, err = AssetToExternalAllocation(asset, []string{"namespace"}, map[string]string{})
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -1099,7 +1100,7 @@ func TestAssetToExternalAllocation(t *testing.T) {
 	}
 
 	// 2) multi-prop full match
-	alloc, err = AssetToExternalAllocation(asset, []string{"namespace", "label:env"})
+	alloc, err = AssetToExternalAllocation(asset, []string{"namespace", "label:env"}, map[string]string{})
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -1120,7 +1121,7 @@ func TestAssetToExternalAllocation(t *testing.T) {
 	}
 
 	// 3) multi-prop partial match
-	alloc, err = AssetToExternalAllocation(asset, []string{"namespace", "label:foo"})
+	alloc, err = AssetToExternalAllocation(asset, []string{"namespace", "label:foo"}, map[string]string{})
 	if err != nil {
 		t.Fatalf("unexpected error: %s", err)
 	}
@@ -1138,10 +1139,19 @@ func TestAssetToExternalAllocation(t *testing.T) {
 	}
 
 	// 3) no match
-	alloc, err = AssetToExternalAllocation(asset, []string{"cluster"})
+	alloc, err = AssetToExternalAllocation(asset, []string{"cluster"}, map[string]string{})
 	if err == nil {
 		t.Fatalf("expected 'no match' error")
 	}
+
+	alloc, err = AssetToExternalAllocation(asset, []string{"namespace", "label:app"}, map[string]string{"app": "product"})
+	if alloc.ExternalCost != 10.00 {
+		t.Fatalf("expected external allocation with ExternalCost %f; got %f", 10.00, alloc.ExternalCost)
+	}
+	if alloc.TotalCost() != 10.00 {
+		t.Fatalf("expected external allocation with TotalCost %f; got %f", 10.00, alloc.TotalCost())
+	}
+
 }
 
 // TODO merge conflict had this: