|
|
@@ -0,0 +1,298 @@
|
|
|
+package allocationfilterutil
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "reflect"
|
|
|
+ "testing"
|
|
|
+
|
|
|
+ "github.com/kubecost/opencost/pkg/kubecost"
|
|
|
+)
|
|
|
+
|
|
|
+func allocGenerator(props kubecost.AllocationProperties) kubecost.Allocation {
|
|
|
+ a := kubecost.Allocation{
|
|
|
+ Properties: &props,
|
|
|
+ }
|
|
|
+
|
|
|
+ a.Name = a.Properties.String()
|
|
|
+ return a
|
|
|
+}
|
|
|
+
|
|
|
+func TestParse(t *testing.T) {
|
|
|
+ // TODO: unallocated cases
|
|
|
+ cases := []struct {
|
|
|
+ input string
|
|
|
+ expected kubecost.AllocationFilter
|
|
|
+ shouldMatch []kubecost.Allocation
|
|
|
+ shouldNotMatch []kubecost.Allocation
|
|
|
+ }{
|
|
|
+ {
|
|
|
+ input: `namespace:"kubecost"`,
|
|
|
+ expected: kubecost.AllocationFilterAnd{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterOr{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterNamespace,
|
|
|
+ Op: kubecost.FilterEquals,
|
|
|
+ Value: "kubecost",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ }},
|
|
|
+ shouldMatch: []kubecost.Allocation{
|
|
|
+ allocGenerator(kubecost.AllocationProperties{Namespace: "kubecost"}),
|
|
|
+ },
|
|
|
+ shouldNotMatch: []kubecost.Allocation{
|
|
|
+ allocGenerator(kubecost.AllocationProperties{Namespace: "kube-system"}),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ {
|
|
|
+ input: `namespace!:"kubecost","kube-system"`,
|
|
|
+ expected: kubecost.AllocationFilterAnd{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterAnd{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterNamespace,
|
|
|
+ Op: kubecost.FilterNotEquals,
|
|
|
+ Value: "kubecost",
|
|
|
+ },
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterNamespace,
|
|
|
+ Op: kubecost.FilterNotEquals,
|
|
|
+ Value: "kube-system",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ }},
|
|
|
+ shouldMatch: []kubecost.Allocation{
|
|
|
+ allocGenerator(kubecost.AllocationProperties{Namespace: "abc"}),
|
|
|
+ },
|
|
|
+ shouldNotMatch: []kubecost.Allocation{
|
|
|
+ allocGenerator(kubecost.AllocationProperties{Namespace: "kubecost"}),
|
|
|
+ allocGenerator(kubecost.AllocationProperties{Namespace: "kube-system"}),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ {
|
|
|
+ input: `namespace:"kubecost","kube-system"`,
|
|
|
+ expected: kubecost.AllocationFilterAnd{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterOr{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterNamespace,
|
|
|
+ Op: kubecost.FilterEquals,
|
|
|
+ Value: "kubecost",
|
|
|
+ },
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterNamespace,
|
|
|
+ Op: kubecost.FilterEquals,
|
|
|
+ Value: "kube-system",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ }},
|
|
|
+ shouldMatch: []kubecost.Allocation{
|
|
|
+ allocGenerator(kubecost.AllocationProperties{Namespace: "kubecost"}),
|
|
|
+ allocGenerator(kubecost.AllocationProperties{Namespace: "kube-system"}),
|
|
|
+ },
|
|
|
+ shouldNotMatch: []kubecost.Allocation{
|
|
|
+ allocGenerator(kubecost.AllocationProperties{Namespace: "abc"}),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ {
|
|
|
+ input: `label[app_abc]:"cost_analyzer"`,
|
|
|
+ expected: kubecost.AllocationFilterAnd{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterOr{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterLabel,
|
|
|
+ Key: "app_abc",
|
|
|
+ Op: kubecost.FilterEquals,
|
|
|
+ Value: "cost_analyzer",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ }},
|
|
|
+ },
|
|
|
+ {
|
|
|
+ input: `services:"123","abc"`,
|
|
|
+ expected: kubecost.AllocationFilterAnd{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterOr{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterServices,
|
|
|
+ Op: kubecost.FilterContains,
|
|
|
+ Value: "123",
|
|
|
+ },
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterServices,
|
|
|
+ Op: kubecost.FilterContains,
|
|
|
+ Value: "abc",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ }},
|
|
|
+ },
|
|
|
+ {
|
|
|
+ input: `services!:"123","abc"`,
|
|
|
+ expected: kubecost.AllocationFilterAnd{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterAnd{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterServices,
|
|
|
+ Op: kubecost.FilterNotContains,
|
|
|
+ Value: "123",
|
|
|
+ },
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterServices,
|
|
|
+ Op: kubecost.FilterNotContains,
|
|
|
+ Value: "abc",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ }},
|
|
|
+ },
|
|
|
+ {
|
|
|
+ input: `label[app_abc]:"cost_analyzer"`,
|
|
|
+ expected: kubecost.AllocationFilterAnd{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterOr{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterLabel,
|
|
|
+ Key: "app_abc",
|
|
|
+ Op: kubecost.FilterEquals,
|
|
|
+ Value: "cost_analyzer",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ }},
|
|
|
+ },
|
|
|
+ {
|
|
|
+ input: `label[app_abc]:"cost_analyzer"+label[foo]:"bar"`,
|
|
|
+ expected: kubecost.AllocationFilterAnd{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterOr{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterLabel,
|
|
|
+ Key: "app_abc",
|
|
|
+ Op: kubecost.FilterEquals,
|
|
|
+ Value: "cost_analyzer",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ kubecost.AllocationFilterOr{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterLabel,
|
|
|
+ Key: "foo",
|
|
|
+ Op: kubecost.FilterEquals,
|
|
|
+ Value: "bar",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ }},
|
|
|
+ },
|
|
|
+ {
|
|
|
+ input: `namespace:"kubecost"+label[app]:"cost_analyzer"+annotation[a1]:"b2"+cluster:"cluster-one"+node!:"node-123","node-456"+controllerName:"kubecost-cost-analyzer","kubecost-prometheus-server"+controllerKind!:"daemonset","statefulset","job"+container!:"123-abc_foo"+pod!:"aaaaaaaaaaaaaaaaaaaaaaaaa"+services!:"abc123"`,
|
|
|
+ expected: kubecost.AllocationFilterAnd{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterOr{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterNamespace,
|
|
|
+ Op: kubecost.FilterEquals,
|
|
|
+ Value: "kubecost",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ kubecost.AllocationFilterOr{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterLabel,
|
|
|
+ Key: "app",
|
|
|
+ Op: kubecost.FilterEquals,
|
|
|
+ Value: "cost_analyzer",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ kubecost.AllocationFilterOr{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterAnnotation,
|
|
|
+ Key: "a1",
|
|
|
+ Op: kubecost.FilterEquals,
|
|
|
+ Value: "b2",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ kubecost.AllocationFilterOr{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterClusterID,
|
|
|
+ Op: kubecost.FilterEquals,
|
|
|
+ Value: "cluster-one",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ kubecost.AllocationFilterAnd{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterNode,
|
|
|
+ Op: kubecost.FilterNotEquals,
|
|
|
+ Value: "node-123",
|
|
|
+ },
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterNode,
|
|
|
+ Op: kubecost.FilterNotEquals,
|
|
|
+ Value: "node-456",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ kubecost.AllocationFilterOr{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterControllerName,
|
|
|
+ Op: kubecost.FilterEquals,
|
|
|
+ Value: "kubecost-cost-analyzer",
|
|
|
+ },
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterControllerName,
|
|
|
+ Op: kubecost.FilterEquals,
|
|
|
+ Value: "kubecost-prometheus-server",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ kubecost.AllocationFilterAnd{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterControllerKind,
|
|
|
+ Op: kubecost.FilterNotEquals,
|
|
|
+ Value: "daemonset",
|
|
|
+ },
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterControllerKind,
|
|
|
+ Op: kubecost.FilterNotEquals,
|
|
|
+ Value: "statefulset",
|
|
|
+ },
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterControllerKind,
|
|
|
+ Op: kubecost.FilterNotEquals,
|
|
|
+ Value: "job",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ kubecost.AllocationFilterAnd{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterContainer,
|
|
|
+ Op: kubecost.FilterNotEquals,
|
|
|
+ Value: "123-abc_foo",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ kubecost.AllocationFilterAnd{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterPod,
|
|
|
+ Op: kubecost.FilterNotEquals,
|
|
|
+ Value: "aaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ kubecost.AllocationFilterAnd{[]kubecost.AllocationFilter{
|
|
|
+ kubecost.AllocationFilterCondition{
|
|
|
+ Field: kubecost.FilterServices,
|
|
|
+ Op: kubecost.FilterNotContains,
|
|
|
+ Value: "abc123",
|
|
|
+ },
|
|
|
+ }},
|
|
|
+ }},
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ for i, c := range cases {
|
|
|
+ t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
|
|
+ t.Logf("Query: %s", c.input)
|
|
|
+ result, err := ParseAllocationFilter(c.input)
|
|
|
+ t.Logf("Result: %s", result)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf("Unexpected parse error: %s", err)
|
|
|
+ }
|
|
|
+ if !reflect.DeepEqual(result, c.expected) {
|
|
|
+ t.Fatalf("Expected:\n%s\nGot:\n%s", c.expected, result)
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, shouldMatch := range c.shouldMatch {
|
|
|
+ if !result.Matches(&shouldMatch) {
|
|
|
+ t.Errorf("Failed to match %s", shouldMatch.Name)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for _, shouldNotMatch := range c.shouldNotMatch {
|
|
|
+ if result.Matches(&shouldNotMatch) {
|
|
|
+ t.Errorf("Incorrectly matched %s", shouldNotMatch.Name)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|