Skip to content

Commit 68e8a6a

Browse files
committed
PoC dsl (no parsing at this point)
1 parent e957d32 commit 68e8a6a

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

pkg/dsl/dsl.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package dsl
2+
3+
import (
4+
"github.com/netobserv/flowlogs-pipeline/pkg/api"
5+
"github.com/netobserv/flowlogs-pipeline/pkg/config"
6+
"github.com/netobserv/flowlogs-pipeline/pkg/utils/filters"
7+
)
8+
9+
type Node struct {
10+
op string
11+
leaf *api.KeepEntryRule
12+
children []*Node
13+
predicate filters.Predicate
14+
}
15+
16+
func Parse(s string) Node {
17+
return Node{}
18+
}
19+
20+
func (n *Node) Preprocess() error {
21+
if n.leaf != nil {
22+
p, err := filters.FromKeepEntry(n.leaf)
23+
if err != nil {
24+
return err
25+
}
26+
n.predicate = p
27+
return nil
28+
}
29+
for _, child := range n.children {
30+
err := child.Preprocess()
31+
if err != nil {
32+
return err
33+
}
34+
}
35+
return nil
36+
}
37+
38+
func (n *Node) Apply(flow config.GenericMap) bool {
39+
if n.leaf != nil {
40+
return n.predicate(flow)
41+
}
42+
if n.op == "and" {
43+
for _, child := range n.children {
44+
if !child.Apply(flow) {
45+
return false
46+
}
47+
}
48+
return true
49+
}
50+
for _, child := range n.children {
51+
if child.Apply(flow) {
52+
return true
53+
}
54+
}
55+
return false
56+
}

pkg/dsl/dsl_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package dsl
2+
3+
import (
4+
"testing"
5+
6+
"github.com/netobserv/flowlogs-pipeline/pkg/api"
7+
"github.com/netobserv/flowlogs-pipeline/pkg/config"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func Test(t *testing.T) {
12+
n := Node{
13+
op: "and",
14+
children: []*Node{
15+
{
16+
op: "or",
17+
children: []*Node{
18+
{
19+
leaf: &api.KeepEntryRule{
20+
Type: api.KeepEntryIfEqual,
21+
KeepEntry: &api.TransformFilterGenericRule{
22+
Input: "srcnamespace",
23+
Value: "netobserv",
24+
},
25+
},
26+
},
27+
{
28+
op: "and",
29+
children: []*Node{
30+
{
31+
leaf: &api.KeepEntryRule{
32+
Type: api.KeepEntryIfEqual,
33+
KeepEntry: &api.TransformFilterGenericRule{
34+
Input: "srcnamespace",
35+
Value: "ingress",
36+
},
37+
},
38+
},
39+
{
40+
leaf: &api.KeepEntryRule{
41+
Type: api.KeepEntryIfEqual,
42+
KeepEntry: &api.TransformFilterGenericRule{
43+
Input: "dstnamespace",
44+
Value: "netobserv",
45+
},
46+
},
47+
},
48+
},
49+
},
50+
},
51+
},
52+
{
53+
leaf: &api.KeepEntryRule{
54+
Type: api.KeepEntryIfEqual,
55+
KeepEntry: &api.TransformFilterGenericRule{
56+
Input: "srckind",
57+
Value: "pod",
58+
},
59+
},
60+
},
61+
},
62+
}
63+
n.Preprocess()
64+
65+
result := n.Apply(config.GenericMap{
66+
"srcnamespace": "plop",
67+
"dstnamespace": "netobserv",
68+
"srckind": "pod",
69+
})
70+
assert.False(t, result)
71+
72+
result = n.Apply(config.GenericMap{
73+
"srcnamespace": "ingress",
74+
"dstnamespace": "netobserv",
75+
"srckind": "pod",
76+
})
77+
assert.True(t, result)
78+
79+
result = n.Apply(config.GenericMap{
80+
"srcnamespace": "ingress",
81+
"dstnamespace": "netobserv",
82+
"srckind": "service",
83+
})
84+
assert.False(t, result)
85+
}

0 commit comments

Comments
 (0)