Skip to content

Commit c6200c6

Browse files
committed
Add documentation and make target
1 parent 6781b9d commit c6200c6

File tree

6 files changed

+141
-56
lines changed

6 files changed

+141
-56
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,11 @@ else
197197
DOCKER_BUILDKIT=1 $(OCI_BIN) manifest push ${IMAGE} docker://${IMAGE};
198198
endif
199199

200+
.PHONY: goyacc
201+
goyacc: ## Regenerate filters query langage
202+
@echo "### Regenerate filters query langage"
203+
GOFLAGS="" go install golang.org/x/tools/cmd/[email protected]
204+
goyacc -o pkg/dsl/expr.y.go pkg/dsl/expr.y
205+
200206
include .mk/development.mk
201207
include .mk/shortcuts.mk

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,16 @@ removal of only the `SrcPort` key and value
386386
Using `remove_entry_if_equal` will remove the entry if the specified field exists and is equal to the specified value.
387387
Using `remove_entry_if_not_equal` will remove the entry if the specified field exists and is not equal to the specified value.
388388

389+
#### Transform Filter: query language
390+
391+
Alternatively, a query language allows to filter flows, keeping entries rather than removing them.
392+
393+
```
394+
(srcnamespace="netobserv" OR (srcnamespace="ingress" AND dstnamespace="netobserv")) AND srckind!="service"
395+
```
396+
397+
[See here](./docs/filtering.md) for more information about this language.
398+
389399
### Transform Network
390400

391401
`transform network` provides specific functionality that is useful for transformation of network flow-logs:

docs/filtering.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# FLP filtering language
2+
3+
Flowlogs-pipeline uses a simple query language to filter network flows:
4+
5+
```
6+
(srcnamespace="netobserv" OR (srcnamespace="ingress" AND dstnamespace="netobserv")) AND srckind!="service"
7+
```
8+
9+
The syntax includes:
10+
11+
- Logical boolean operators (case insensitive)
12+
- `and`
13+
- `or`
14+
- String comparison operators
15+
- equals `=`
16+
- not equals `!=`
17+
- matches regexp `=~`
18+
- not matches regexp `!~`
19+
- Unary operations
20+
- field is present: `with(field)`
21+
- field is absent: `without(field)`
22+
- Parenthesis-based priority
23+
24+
## API integration
25+
26+
The language is currently integrated in the "keep_entry" transform/filtering API. Example:
27+
28+
```yaml
29+
transform:
30+
type: filter
31+
filter:
32+
rules:
33+
- type: keep_entry_query
34+
keepEntryQuery: (namespace="A" and with(workload)) or service=~"abc.+"
35+
keepEntrySampling: 10 # Optionally, a sampling ratio can be associated with the filter
36+
```
37+
38+
## Integration with the NetObserv operator
39+
40+
In the [NetObserv operator](https://github.com/netobserv/network-observability-operator), the filtering query language is used in `FlowCollector` `spec.processor.filters`. Example:
41+
42+
```yaml
43+
spec:
44+
processor:
45+
filters:
46+
- query: |
47+
(SrcK8S_Namespace="netobserv" OR (SrcK8S_Namespace="openshift-ingress" AND DstK8S_Namespace="netobserv"))
48+
outputTarget: Loki # The filter can target a specific output (such as Loki logs or exported data), or all outputs.
49+
sampling: 10 # Optionally, a sampling ratio can be associated with the filter
50+
```
51+
52+
See also the [list of field names](https://github.com/netobserv/network-observability-operator/blob/main/docs/flows-format.adoc) that are available for queries, and the [API documentation](https://github.com/netobserv/network-observability-operator/blob/main/docs/FlowCollector.md#flowcollectorspecprocessorfiltersindex-1).
53+
54+
## Internals
55+
56+
This language is designed using [Yacc](https://en.wikipedia.org/wiki/Yacc) / goyacc.
57+
58+
The [definition file](../pkg/dsl/expr.y) describes the syntax based on a list of tokens. It is derived to a [go source file](../pkg/dsl/expr.y.go) using [goyacc](https://pkg.go.dev/golang.org/x/tools/cmd/goyacc), which defines constants for the tokens, among other things. The [lexer](../pkg/dsl/lexer.go) file defines structures and helpers that can be used from `expr.y`, the logic used to interpret the language in a structured way, and is also where actual characters/strings are mapped to syntax tokens. Finally, [eval.go](../pkg/dsl/eval.go) runs the desired query on actual data.
59+
60+
When adding features to the language, you'll likely have to change `expr.y` and `lexer.go`.
61+
62+
To regenerate `expr.y.go`, run:
63+
64+
```bash
65+
make goyacc
66+
```

pkg/dsl/expr.y

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ package dsl
1010
%type <expr> root
1111
%type <expr> expr
1212

13-
%token <value> VAR STRING NUMBER AND OR EQ NEQ REG NREG OPEN_PARENTHESIS CLOSE_PARENTHESIS WITH WITHOUT
13+
%token <value> NF_FIELD STRING NUMBER AND OR EQ NEQ REG NREG OPEN_PARENTHESIS CLOSE_PARENTHESIS WITH WITHOUT
1414
%left AND
1515
%left OR
1616
%%
@@ -25,10 +25,10 @@ expr:
2525
OPEN_PARENTHESIS expr CLOSE_PARENTHESIS { $$ = ParenthesisExpr{inner: $2} }
2626
| expr AND expr { $$ = LogicalExpr{left: $1, operator: operatorAnd, right: $3} }
2727
| expr OR expr { $$ = LogicalExpr{left: $1, operator: operatorOr, right: $3} }
28-
| WITH OPEN_PARENTHESIS VAR CLOSE_PARENTHESIS { $$ = WithExpr{key: $3} }
29-
| WITHOUT OPEN_PARENTHESIS VAR CLOSE_PARENTHESIS { $$ = WithoutExpr{key: $3} }
30-
| VAR EQ STRING { $$ = EqExpr{key: $1, value: $3} }
31-
| VAR NEQ STRING { $$ = NEqExpr{key: $1, value: $3} }
32-
| VAR REG STRING { $$ = RegExpr{key: $1, value: $3} }
33-
| VAR NREG STRING { $$ = NRegExpr{key: $1, value: $3} }
28+
| WITH OPEN_PARENTHESIS NF_FIELD CLOSE_PARENTHESIS { $$ = WithExpr{key: $3} }
29+
| WITHOUT OPEN_PARENTHESIS NF_FIELD CLOSE_PARENTHESIS { $$ = WithoutExpr{key: $3} }
30+
| NF_FIELD EQ STRING { $$ = EqExpr{key: $1, value: $3} }
31+
| NF_FIELD NEQ STRING { $$ = NEqExpr{key: $1, value: $3} }
32+
| NF_FIELD REG STRING { $$ = RegExpr{key: $1, value: $3} }
33+
| NF_FIELD NREG STRING { $$ = NRegExpr{key: $1, value: $3} }
3434
%%

pkg/dsl/expr.y.go

Lines changed: 40 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)