Browse Source

Add support for filterServices= filter

Michael Dresser 4 years ago
parent
commit
ad6010f239

+ 13 - 12
pkg/util/filterutil/allocationfilters.go

@@ -174,18 +174,19 @@ func AllocationFilterFromParamsV1(
 		filterV1DoubleValueFromList(qp.GetList("filterLabels", ","), kubecost.FilterLabel),
 	)
 
-	// TODO: filter service condition
-	// filterServices := qp.GetList("filterServices", ",")
-	// if len(filterServices) > 0 {
-	// 	subFilter := kubecost.AllocationFilterOr{
-	// 		Filters: []kubecost.AllocationFilter{},
-	// 	}
-
-	// 	for _, filter := range filterServices {
-	// 		ffs = append(ffs, GetServiceFilterFunc(filter))
-	// 	}
-	// 	filter.Filters = append(filter.Filters, subFilter)
-	// }
+	servicesFilter := kubecost.AllocationFilterOr{
+		Filters: []kubecost.AllocationFilter{},
+	}
+	for _, filterValue := range qp.GetList("filterServices", ",") {
+		servicesFilter.Filters = append(servicesFilter.Filters,
+			kubecost.AllocationFilterCondition{
+				Field: kubecost.FilterServices,
+				Op:    kubecost.FilterContains,
+				Value: filterValue,
+			},
+		)
+	}
+	filter.Filters = append(filter.Filters, servicesFilter)
 
 	return filter
 }

+ 37 - 0
pkg/util/filterutil/allocationfilters_test.go

@@ -269,6 +269,43 @@ func TestFiltersFromParamsV1(t *testing.T) {
 				}),
 			},
 		},
+		{
+			name: "single service",
+			qp: map[string]string{
+				"filterServices": "serv1",
+			},
+			shouldMatch: []kubecost.Allocation{
+				allocGenerator(kubecost.AllocationProperties{
+					Services: []string{"serv1"},
+				}),
+			},
+			shouldNotMatch: []kubecost.Allocation{
+				allocGenerator(kubecost.AllocationProperties{}),
+				allocGenerator(kubecost.AllocationProperties{
+					Services: []string{"serv2"},
+				}),
+			},
+		},
+		{
+			name: "multi service",
+			qp: map[string]string{
+				"filterServices": "serv1,serv3",
+			},
+			shouldMatch: []kubecost.Allocation{
+				allocGenerator(kubecost.AllocationProperties{
+					Services: []string{"serv1"},
+				}),
+				allocGenerator(kubecost.AllocationProperties{
+					Services: []string{"serv2", "serv3"},
+				}),
+			},
+			shouldNotMatch: []kubecost.Allocation{
+				allocGenerator(kubecost.AllocationProperties{}),
+				allocGenerator(kubecost.AllocationProperties{
+					Services: []string{"serv2"},
+				}),
+			},
+		},
 		{
 			name: "multi: namespaces, labels",
 			qp: map[string]string{