Răsfoiți Sursa

Merge pull request #895 from kubecost/niko/labelfix

Support multi-agg for comma-separated label aliases
Niko Kovacevic 4 ani în urmă
părinte
comite
2657418d83
2 a modificat fișierele cu 48 adăugiri și 28 ștergeri
  1. 40 25
      pkg/kubecost/allocation.go
  2. 8 3
      pkg/kubecost/allocation_test.go

+ 40 - 25
pkg/kubecost/allocation.go

@@ -1517,11 +1517,14 @@ func (a *Allocation) generateKey(aggregateBy []string, labelConfig *LabelConfig)
 			if labels == nil {
 				names = append(names, UnallocatedSuffix)
 			} else {
-				labelName := labelConfig.Sanitize(labelConfig.DepartmentLabel)
-				if labelValue, ok := labels[labelName]; ok {
-					names = append(names, labelValue)
-				} else {
-					names = append(names, UnallocatedSuffix)
+				labelNames := strings.Split(labelConfig.DepartmentLabel, ",")
+				for _, labelName := range labelNames {
+					labelName = labelConfig.Sanitize(labelName)
+					if labelValue, ok := labels[labelName]; ok {
+						names = append(names, labelValue)
+					} else {
+						names = append(names, UnallocatedSuffix)
+					}
 				}
 			}
 		case agg == AllocationEnvironmentProp:
@@ -1529,11 +1532,14 @@ func (a *Allocation) generateKey(aggregateBy []string, labelConfig *LabelConfig)
 			if labels == nil {
 				names = append(names, UnallocatedSuffix)
 			} else {
-				labelName := labelConfig.Sanitize(labelConfig.EnvironmentLabel)
-				if labelValue, ok := labels[labelName]; ok {
-					names = append(names, labelValue)
-				} else {
-					names = append(names, UnallocatedSuffix)
+				labelNames := strings.Split(labelConfig.EnvironmentLabel, ",")
+				for _, labelName := range labelNames {
+					labelName = labelConfig.Sanitize(labelName)
+					if labelValue, ok := labels[labelName]; ok {
+						names = append(names, labelValue)
+					} else {
+						names = append(names, UnallocatedSuffix)
+					}
 				}
 			}
 		case agg == AllocationOwnerProp:
@@ -1541,11 +1547,14 @@ func (a *Allocation) generateKey(aggregateBy []string, labelConfig *LabelConfig)
 			if labels == nil {
 				names = append(names, UnallocatedSuffix)
 			} else {
-				labelName := labelConfig.Sanitize(labelConfig.OwnerLabel)
-				if labelValue, ok := labels[labelName]; ok {
-					names = append(names, labelValue)
-				} else {
-					names = append(names, UnallocatedSuffix)
+				labelNames := strings.Split(labelConfig.OwnerLabel, ",")
+				for _, labelName := range labelNames {
+					labelName = labelConfig.Sanitize(labelName)
+					if labelValue, ok := labels[labelName]; ok {
+						names = append(names, labelValue)
+					} else {
+						names = append(names, UnallocatedSuffix)
+					}
 				}
 			}
 		case agg == AllocationProductProp:
@@ -1553,11 +1562,14 @@ func (a *Allocation) generateKey(aggregateBy []string, labelConfig *LabelConfig)
 			if labels == nil {
 				names = append(names, UnallocatedSuffix)
 			} else {
-				labelName := labelConfig.Sanitize(labelConfig.ProductLabel)
-				if labelValue, ok := labels[labelName]; ok {
-					names = append(names, labelValue)
-				} else {
-					names = append(names, UnallocatedSuffix)
+				labelNames := strings.Split(labelConfig.ProductLabel, ",")
+				for _, labelName := range labelNames {
+					labelName = labelConfig.Sanitize(labelName)
+					if labelValue, ok := labels[labelName]; ok {
+						names = append(names, labelValue)
+					} else {
+						names = append(names, UnallocatedSuffix)
+					}
 				}
 			}
 		case agg == AllocationTeamProp:
@@ -1565,11 +1577,14 @@ func (a *Allocation) generateKey(aggregateBy []string, labelConfig *LabelConfig)
 			if labels == nil {
 				names = append(names, UnallocatedSuffix)
 			} else {
-				labelName := labelConfig.Sanitize(labelConfig.TeamLabel)
-				if labelValue, ok := labels[labelName]; ok {
-					names = append(names, labelValue)
-				} else {
-					names = append(names, UnallocatedSuffix)
+				labelNames := strings.Split(labelConfig.TeamLabel, ",")
+				for _, labelName := range labelNames {
+					labelName = labelConfig.Sanitize(labelName)
+					if labelValue, ok := labels[labelName]; ok {
+						names = append(names, labelValue)
+					} else {
+						names = append(names, UnallocatedSuffix)
+					}
 				}
 			}
 		default:

+ 8 - 3
pkg/kubecost/allocation_test.go

@@ -529,12 +529,14 @@ func TestAllocationSet_generateKey(t *testing.T) {
 	}
 
 	// Ensure that labels with illegal Prometheus characters in LabelConfig
-	// still match their sanitized values.
+	// still match their sanitized values. Ensure also that multiple comma-
+	// separated values work.
 
 	labelConfig.DepartmentLabel = "prom/illegal-department"
 	labelConfig.EnvironmentLabel = " env "
 	labelConfig.OwnerLabel = "$owner%"
 	labelConfig.ProductLabel = "app.kubernetes.io/app"
+	labelConfig.TeamLabel = "team,app.kubernetes.io/team,k8s-team"
 
 	alloc.Properties = &AllocationProperties{
 		Cluster:   "cluster1",
@@ -543,7 +545,9 @@ func TestAllocationSet_generateKey(t *testing.T) {
 			"prom_illegal_department": "dept1",
 			"env":                     "envt1",
 			"_owner_":                 "ownr1",
+			"team":                    "team1",
 			"app_kubernetes_io_app":   "prod1",
+			"app_kubernetes_io_team":  "team2",
 		},
 	}
 
@@ -552,11 +556,12 @@ func TestAllocationSet_generateKey(t *testing.T) {
 		AllocationEnvironmentProp,
 		AllocationOwnerProp,
 		AllocationProductProp,
+		AllocationTeamProp,
 	}
 
 	key = alloc.generateKey(props, labelConfig)
-	if key != "dept1/envt1/ownr1/prod1" {
-		t.Fatalf("generateKey: expected \"dept1/envt1/ownr1/prod\"; actual \"%s\"", key)
+	if key != "dept1/envt1/ownr1/prod1/team1/team2/__unallocated__" {
+		t.Fatalf("generateKey: expected \"dept1/envt1/ownr1/prod1/team1/team2/__unallocated__\"; actual \"%s\"", key)
 	}
 }