Quellcode durchsuchen

Sanitize labels in AggregateBy on label aliases

Niko Kovacevic vor 4 Jahren
Ursprung
Commit
92520d1126
3 geänderte Dateien mit 33 neuen und 18 gelöschten Zeilen
  1. 5 17
      pkg/kubecost/allocation.go
  2. 7 0
      pkg/kubecost/config.go
  3. 21 1
      pkg/kubecost/config_test.go

+ 5 - 17
pkg/kubecost/allocation.go

@@ -1487,7 +1487,7 @@ func (a *Allocation) generateKey(aggregateBy []string, labelConfig *LabelConfig)
 			if labels == nil {
 				names = append(names, UnallocatedSuffix)
 			} else {
-				labelName := labelConfig.DepartmentLabel
+				labelName := labelConfig.Sanitize(labelConfig.DepartmentLabel)
 				if labelValue, ok := labels[labelName]; ok {
 					names = append(names, labelValue)
 				} else {
@@ -1499,7 +1499,7 @@ func (a *Allocation) generateKey(aggregateBy []string, labelConfig *LabelConfig)
 			if labels == nil {
 				names = append(names, UnallocatedSuffix)
 			} else {
-				labelName := labelConfig.EnvironmentLabel
+				labelName := labelConfig.Sanitize(labelConfig.EnvironmentLabel)
 				if labelValue, ok := labels[labelName]; ok {
 					names = append(names, labelValue)
 				} else {
@@ -1511,7 +1511,7 @@ func (a *Allocation) generateKey(aggregateBy []string, labelConfig *LabelConfig)
 			if labels == nil {
 				names = append(names, UnallocatedSuffix)
 			} else {
-				labelName := labelConfig.OwnerLabel
+				labelName := labelConfig.Sanitize(labelConfig.OwnerLabel)
 				if labelValue, ok := labels[labelName]; ok {
 					names = append(names, labelValue)
 				} else {
@@ -1523,7 +1523,7 @@ func (a *Allocation) generateKey(aggregateBy []string, labelConfig *LabelConfig)
 			if labels == nil {
 				names = append(names, UnallocatedSuffix)
 			} else {
-				labelName := labelConfig.ProductLabel
+				labelName := labelConfig.Sanitize(labelConfig.ProductLabel)
 				if labelValue, ok := labels[labelName]; ok {
 					names = append(names, labelValue)
 				} else {
@@ -1535,7 +1535,7 @@ func (a *Allocation) generateKey(aggregateBy []string, labelConfig *LabelConfig)
 			if labels == nil {
 				names = append(names, UnallocatedSuffix)
 			} else {
-				labelName := labelConfig.TeamLabel
+				labelName := labelConfig.Sanitize(labelConfig.TeamLabel)
 				if labelValue, ok := labels[labelName]; ok {
 					names = append(names, labelValue)
 				} else {
@@ -1553,18 +1553,6 @@ func (a *Allocation) generateKey(aggregateBy []string, labelConfig *LabelConfig)
 	return strings.Join(names, "/")
 }
 
-// TODO:CLEANUP get rid of this
-// Helper function to check for slice membership. Not sure if repeated elsewhere in our codebase.
-func indexOf(v string, arr []string) int {
-	for i, s := range arr {
-		// This is caseless equivalence
-		if strings.EqualFold(v, s) {
-			return i
-		}
-	}
-	return -1
-}
-
 // Clone returns a new AllocationSet with a deep copy of the given
 // AllocationSet's allocations.
 func (as *AllocationSet) Clone() *AllocationSet {

+ 7 - 0
pkg/kubecost/config.go

@@ -169,6 +169,13 @@ func (lc *LabelConfig) Map() map[string]string {
 	return m
 }
 
+// Sanitize returns a sanitized version of the given string, which converts
+// all illegal characters to underscores. Illegal characters are those that
+// Prometheus does not support; i.e. [^a-zA-Z0-9_]
+func (lc *LabelConfig) Sanitize(label string) string {
+	return prom.SanitizeLabelName(strings.TrimSpace(label))
+}
+
 // GetExternalAllocationName derives an external allocation name from a set of
 // labels, given an aggregation property. If the aggregation property is,
 // itself, a label (e.g. label:app) then this function looks for a

+ 21 - 1
pkg/kubecost/config_test.go

@@ -104,7 +104,7 @@ func TestLabelConfig_GetExternalAllocationName(t *testing.T) {
 
 	// Change the external label for namespace and confirm it still works
 	lc.NamespaceExternalLabel = "kubens"
-	lc.ServiceExternalLabel = "prom-sanitization-test"
+	lc.ServiceExternalLabel = "prom/sanitization-test"
 	lc.PodExternalLabel = "Non__GlueFormattedLabel"
 	lc.OwnerExternalLabel = "kubeowner"
 	lc.DepartmentExternalLabel = "doesntexist,env"
@@ -130,3 +130,23 @@ func TestLabelConfig_GetExternalAllocationName(t *testing.T) {
 		}
 	}
 }
+
+func TestLabelConfig_Sanitize(t *testing.T) {
+	testCases := []struct {
+		label    string
+		expected string
+	}{
+		{"", ""},
+		{"simple", "simple"},
+		{"prom/sanitization-test", "prom_sanitization_test"},
+		{" prom/sanitization-test$  ", "prom_sanitization_test_"},
+	}
+
+	lc := NewLabelConfig()
+	for _, tc := range testCases {
+		actual := lc.Sanitize(tc.label)
+		if actual != tc.expected {
+			t.Fatalf("Sanitize failed; expected '%s'; got '%s'", tc.expected, actual)
+		}
+	}
+}