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

Filters v2: Unallocated != logic and tests

Michael Dresser 4 лет назад
Родитель
Сommit
18c551f420
2 измененных файлов с 52 добавлено и 6 удалено
  1. 7 6
      pkg/kubecost/allocationfilter.go
  2. 45 0
      pkg/kubecost/allocationfilter_test.go

+ 7 - 6
pkg/kubecost/allocationfilter.go

@@ -32,8 +32,6 @@ type FilterOp int
 // does not enforce exhaustive pattern matching on "enum" types.
 const (
 	FilterEquals FilterOp = iota
-
-	// TODO: what is the != behavior for __unallocated__?
 	FilterNotEquals
 )
 
@@ -147,9 +145,9 @@ func (filter AllocationFilterCondition) Matches(a *Allocation) bool {
 			return false
 		}
 
-		// namespace="__unallocated__" should match a.Properties.Namespace = ""
-		if valueToCompare == "" && filter.Value == UnallocatedSuffix {
-			return true
+		// namespace:"__unallocated__" should match a.Properties.Namespace = ""
+		if valueToCompare == "" {
+			return filter.Value == UnallocatedSuffix
 		}
 
 		if valueToCompare == filter.Value {
@@ -160,7 +158,10 @@ func (filter AllocationFilterCondition) Matches(a *Allocation) bool {
 			return true
 		}
 
-		// TODO: __unallocated__ behavior?
+		// namespace!:"__unallocated__" should match a.Properties.Namespace != ""
+		if filter.Value == UnallocatedSuffix {
+			return valueToCompare != ""
+		}
 
 		if valueToCompare != filter.Value {
 			return true

+ 45 - 0
pkg/kubecost/allocationfilter_test.go

@@ -57,6 +57,51 @@ func Test_AllocationFilterCondition_Matches(t *testing.T) {
 
 			expected: false,
 		},
+		{
+			name: "Namespace NotEquals Unallocated -> true",
+			a: &Allocation{
+				Properties: &AllocationProperties{
+					Namespace: "kube-system",
+				},
+			},
+			filter: AllocationFilterCondition{
+				Field: FilterNamespace,
+				Op:    FilterNotEquals,
+				Value: UnallocatedSuffix,
+			},
+
+			expected: true,
+		},
+		{
+			name: "Namespace NotEquals Unallocated -> false",
+			a: &Allocation{
+				Properties: &AllocationProperties{
+					Namespace: "",
+				},
+			},
+			filter: AllocationFilterCondition{
+				Field: FilterNamespace,
+				Op:    FilterNotEquals,
+				Value: UnallocatedSuffix,
+			},
+
+			expected: false,
+		},
+		{
+			name: "Namespace Equals Unallocated -> true",
+			a: &Allocation{
+				Properties: &AllocationProperties{
+					Namespace: "",
+				},
+			},
+			filter: AllocationFilterCondition{
+				Field: FilterNamespace,
+				Op:    FilterEquals,
+				Value: UnallocatedSuffix,
+			},
+
+			expected: true,
+		},
 		{
 			name: "ControllerKind Equals -> true",
 			a: &Allocation{