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

Merge pull request #1392 from opencost/mmd/sanitize-label-name-for-aliased-filters

Sanitize label name when filtering on alias
Michael Dresser 3 лет назад
Родитель
Сommit
a1a0977c5c

+ 3 - 2
pkg/util/allocationfilterutil/queryfilters.go

@@ -282,6 +282,7 @@ func filterV1LabelMappedFromList(rawFilterValues []string, labelName string) kub
 	filter := kubecost.AllocationFilterOr{
 		Filters: []kubecost.AllocationFilter{},
 	}
+	labelName = prom.SanitizeLabelName(labelName)
 
 	for _, filterValue := range rawFilterValues {
 		filterValue = strings.TrimSpace(filterValue)
@@ -322,7 +323,7 @@ func filterV1DoubleValueFromList(rawFilterValuesUnsplit []string, filterField ku
 				log.Warnf("illegal key/value filter (ignoring): %s", unsplit)
 				continue
 			}
-			key := prom.SanitizeLabelName(strings.TrimSpace(split[0]))
+			labelName := prom.SanitizeLabelName(strings.TrimSpace(split[0]))
 			val := strings.TrimSpace(split[1])
 			val, wildcard := parseWildcardEnd(val)
 
@@ -330,7 +331,7 @@ func filterV1DoubleValueFromList(rawFilterValuesUnsplit []string, filterField ku
 				Field: filterField,
 				// All v1 filters are equality comparisons
 				Op:    kubecost.FilterEquals,
-				Key:   key,
+				Key:   labelName,
 				Value: val,
 			}
 

+ 9 - 9
pkg/util/allocationfilterutil/queryfilters_test.go

@@ -365,21 +365,21 @@ func TestFiltersFromParamsV1(t *testing.T) {
 			},
 		},
 		{
-			name: "single department",
+			name: "single department, sanitization required",
 			qp: map[string]string{
 				"filterDepartments": "pa-1",
 			},
 			shouldMatch: []kubecost.Allocation{
 				allocGenerator(kubecost.AllocationProperties{
 					Labels: map[string]string{
-						"internal-product-umbrella": "pa-1",
+						"internal_product_umbrella": "pa-1",
 					},
 				}),
 			},
 			shouldNotMatch: []kubecost.Allocation{
 				allocGenerator(kubecost.AllocationProperties{
 					Labels: map[string]string{
-						"internal-product-umbrella": "ps-N",
+						"internal_product_umbrella": "ps-N",
 					},
 				}),
 			},
@@ -392,34 +392,34 @@ func TestFiltersFromParamsV1(t *testing.T) {
 			shouldMatch: []kubecost.Allocation{
 				allocGenerator(kubecost.AllocationProperties{
 					Labels: map[string]string{
-						"internal-product-umbrella": "pa-1",
+						"internal_product_umbrella": "pa-1",
 					},
 				}),
 			},
 			shouldNotMatch: []kubecost.Allocation{
 				allocGenerator(kubecost.AllocationProperties{
 					Labels: map[string]string{
-						"internal-product-umbrella": "ps-N",
+						"internal_product_umbrella": "ps-N",
 					},
 				}),
 			},
 		},
 		{
-			name: "single label",
+			name: "single label, sanitization required",
 			qp: map[string]string{
-				"filterLabels": "app:cost-analyzer",
+				"filterLabels": "app-a:cost-analyzer",
 			},
 			shouldMatch: []kubecost.Allocation{
 				allocGenerator(kubecost.AllocationProperties{
 					Labels: map[string]string{
-						"app": "cost-analyzer",
+						"app_a": "cost-analyzer",
 					},
 				}),
 			},
 			shouldNotMatch: []kubecost.Allocation{
 				allocGenerator(kubecost.AllocationProperties{
 					Labels: map[string]string{
-						"app": "foo",
+						"app_a": "foo",
 					},
 				}),
 				allocGenerator(kubecost.AllocationProperties{