Skip to content

Commit 6d9e1f5

Browse files
committed
Replace keep_entry API with query API
1 parent 8693035 commit 6d9e1f5

File tree

5 files changed

+29
-146
lines changed

5 files changed

+29
-146
lines changed

docs/api.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ Following is the supported API format for filter transformations:
162162
remove_entry_if_equal: removes the entry if the field value equals specified value
163163
remove_entry_if_not_equal: removes the entry if the field value does not equal specified value
164164
remove_entry_all_satisfied: removes the entry if all of the defined rules are satisfied
165-
keep_entry_all_satisfied: keeps the entry if the set of rules are all satisfied
165+
keep_entry_query: keeps the entry if it matches the query
166166
add_field: adds (input) field to the entry; overrides previous value if present (key=input, value=value)
167167
add_field_if_doesnt_exist: adds a field to the entry if the field does not exist
168168
add_field_if: add output field set to assignee if input field satisfies criteria from parameters field
@@ -188,18 +188,7 @@ Following is the supported API format for filter transformations:
188188
input: entry input field
189189
value: specified value of input field:
190190
castInt: set true to cast the value field as an int (numeric values are float64 otherwise)
191-
keepEntryAllSatisfied: configuration for keep_entry rule
192-
type: (enum) one of the following:
193-
keep_entry_if_exists: keeps the entry if the field exists
194-
keep_entry_if_doesnt_exist: keeps the entry if the field does not exist
195-
keep_entry_if_equal: keeps the entry if the field value equals specified value
196-
keep_entry_if_not_equal: keeps the entry if the field value does not equal specified value
197-
keep_entry_if_regex_match: keeps the entry if the field value matches the specified regex
198-
keep_entry_if_not_regex_match: keeps the entry if the field value does not match the specified regex
199-
keepEntry: configuration for keep_entry_* rules
200-
input: entry input field
201-
value: specified value of input field:
202-
castInt: set true to cast the value field as an int (numeric values are float64 otherwise)
191+
keepEntryQuery: configuration for keep_entry rule
203192
keepEntrySampling: sampling value for keep_entry type: 1 flow on <sampling> is kept
204193
addField: configuration for add_field rule
205194
input: entry input field

pkg/api/transform_filter.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const (
3737
RemoveEntryIfEqual TransformFilterEnum = "remove_entry_if_equal" // removes the entry if the field value equals specified value
3838
RemoveEntryIfNotEqual TransformFilterEnum = "remove_entry_if_not_equal" // removes the entry if the field value does not equal specified value
3939
RemoveEntryAllSatisfied TransformFilterEnum = "remove_entry_all_satisfied" // removes the entry if all of the defined rules are satisfied
40-
KeepEntryAllSatisfied TransformFilterEnum = "keep_entry_all_satisfied" // keeps the entry if the set of rules are all satisfied
40+
KeepEntryQuery TransformFilterEnum = "keep_entry_query" // keeps the entry if it matches the query
4141
AddField TransformFilterEnum = "add_field" // adds (input) field to the entry; overrides previous value if present (key=input, value=value)
4242
AddFieldIfDoesntExist TransformFilterEnum = "add_field_if_doesnt_exist" // adds a field to the entry if the field does not exist
4343
AddFieldIf TransformFilterEnum = "add_field_if" // add output field set to assignee if input field satisfies criteria from parameters field
@@ -56,23 +56,12 @@ const (
5656
RemoveEntryIfNotEqualD TransformFilterRemoveEntryEnum = "remove_entry_if_not_equal" // removes the entry if the field value does not equal specified value
5757
)
5858

59-
type TransformFilterKeepEntryEnum string
60-
61-
const (
62-
KeepEntryIfExists TransformFilterKeepEntryEnum = "keep_entry_if_exists" // keeps the entry if the field exists
63-
KeepEntryIfDoesntExist TransformFilterKeepEntryEnum = "keep_entry_if_doesnt_exist" // keeps the entry if the field does not exist
64-
KeepEntryIfEqual TransformFilterKeepEntryEnum = "keep_entry_if_equal" // keeps the entry if the field value equals specified value
65-
KeepEntryIfNotEqual TransformFilterKeepEntryEnum = "keep_entry_if_not_equal" // keeps the entry if the field value does not equal specified value
66-
KeepEntryIfRegexMatch TransformFilterKeepEntryEnum = "keep_entry_if_regex_match" // keeps the entry if the field value matches the specified regex
67-
KeepEntryIfNotRegexMatch TransformFilterKeepEntryEnum = "keep_entry_if_not_regex_match" // keeps the entry if the field value does not match the specified regex
68-
)
69-
7059
type TransformFilterRule struct {
7160
Type TransformFilterEnum `yaml:"type,omitempty" json:"type,omitempty" doc:"(enum) one of the following:"`
7261
RemoveField *TransformFilterGenericRule `yaml:"removeField,omitempty" json:"removeField,omitempty" doc:"configuration for remove_field rule"`
7362
RemoveEntry *TransformFilterGenericRule `yaml:"removeEntry,omitempty" json:"removeEntry,omitempty" doc:"configuration for remove_entry_* rules"`
7463
RemoveEntryAllSatisfied []*RemoveEntryRule `yaml:"removeEntryAllSatisfied,omitempty" json:"removeEntryAllSatisfied,omitempty" doc:"configuration for remove_entry_all_satisfied rule"`
75-
KeepEntryAllSatisfied []*KeepEntryRule `yaml:"keepEntryAllSatisfied,omitempty" json:"keepEntryAllSatisfied,omitempty" doc:"configuration for keep_entry rule"`
64+
KeepEntryQuery string `yaml:"keepEntryQuery,omitempty" json:"keepEntryQuery,omitempty" doc:"configuration for keep_entry rule"`
7665
KeepEntrySampling uint16 `yaml:"keepEntrySampling,omitempty" json:"keepEntrySampling,omitempty" doc:"sampling value for keep_entry type: 1 flow on <sampling> is kept"`
7766
AddField *TransformFilterGenericRule `yaml:"addField,omitempty" json:"addField,omitempty" doc:"configuration for add_field rule"`
7867
AddFieldIfDoesntExist *TransformFilterGenericRule `yaml:"addFieldIfDoesntExist,omitempty" json:"addFieldIfDoesntExist,omitempty" doc:"configuration for add_field_if_doesnt_exist rule"`
@@ -93,9 +82,6 @@ func (r *TransformFilterRule) preprocess() {
9382
for i := range r.RemoveEntryAllSatisfied {
9483
r.RemoveEntryAllSatisfied[i].RemoveEntry.preprocess()
9584
}
96-
for i := range r.KeepEntryAllSatisfied {
97-
r.KeepEntryAllSatisfied[i].KeepEntry.preprocess()
98-
}
9985
for i := range r.ConditionalSampling {
10086
r.ConditionalSampling[i].preprocess()
10187
}
@@ -127,11 +113,6 @@ type RemoveEntryRule struct {
127113
RemoveEntry *TransformFilterGenericRule `yaml:"removeEntry,omitempty" json:"removeEntry,omitempty" doc:"configuration for remove_entry_* rules"`
128114
}
129115

130-
type KeepEntryRule struct {
131-
Type TransformFilterKeepEntryEnum `yaml:"type,omitempty" json:"type,omitempty" doc:"(enum) one of the following:"`
132-
KeepEntry *TransformFilterGenericRule `yaml:"keepEntry,omitempty" json:"keepEntry,omitempty" doc:"configuration for keep_entry_* rules"`
133-
}
134-
135116
type SamplingCondition struct {
136117
Value uint16 `yaml:"value,omitempty" json:"value,omitempty" doc:"sampling value: 1 flow on <sampling> is kept"`
137118
Rules []*RemoveEntryRule `yaml:"rules,omitempty" json:"rules,omitempty" doc:"rules to be satisfied for this sampling configuration"`

pkg/pipeline/transform/transform_filter.go

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/Knetic/govaluate"
2828
"github.com/netobserv/flowlogs-pipeline/pkg/api"
2929
"github.com/netobserv/flowlogs-pipeline/pkg/config"
30+
"github.com/netobserv/flowlogs-pipeline/pkg/dsl"
3031
"github.com/netobserv/flowlogs-pipeline/pkg/utils"
3132
"github.com/netobserv/flowlogs-pipeline/pkg/utils/filters"
3233
"github.com/sirupsen/logrus"
@@ -39,12 +40,12 @@ var (
3940

4041
type Filter struct {
4142
Rules []api.TransformFilterRule
42-
KeepRules []predicatesRule
43+
KeepRules []predicateRule
4344
}
4445

45-
type predicatesRule struct {
46-
predicates []filters.Predicate
47-
sampling uint16
46+
type predicateRule struct {
47+
predicate filters.Predicate
48+
sampling uint16
4849
}
4950

5051
// Transform transforms a flow; if false is returned as a second argument, the entry is dropped
@@ -55,7 +56,7 @@ func (f *Filter) Transform(entry config.GenericMap) (config.GenericMap, bool) {
5556
if len(f.KeepRules) > 0 {
5657
keep := false
5758
for _, r := range f.KeepRules {
58-
if applyPredicates(outputEntry, r) {
59+
if applyPredicate(outputEntry, r) {
5960
keep = true
6061
break
6162
}
@@ -162,9 +163,9 @@ func applyRule(entry config.GenericMap, labels map[string]string, rule *api.Tran
162163
return !isRemoveEntrySatisfied(entry, rule.RemoveEntryAllSatisfied)
163164
case api.ConditionalSampling:
164165
return sample(entry, rule.ConditionalSampling)
165-
case api.KeepEntryAllSatisfied:
166+
case api.KeepEntryQuery:
166167
// This should be processed only in "applyPredicates". Failure to do so is a bug.
167-
tlog.Panicf("unexpected KeepEntryAllSatisfied: %v", rule)
168+
tlog.Panicf("unexpected KeepEntryQuery: %v", rule)
168169
default:
169170
tlog.Panicf("unknown type %s for transform.Filter rule: %v", rule.Type, rule)
170171
}
@@ -181,16 +182,11 @@ func isRemoveEntrySatisfied(entry config.GenericMap, rules []*api.RemoveEntryRul
181182
return true
182183
}
183184

184-
func applyPredicates(entry config.GenericMap, rule predicatesRule) bool {
185+
func applyPredicate(entry config.GenericMap, rule predicateRule) bool {
185186
if !rollSampling(rule.sampling) {
186187
return false
187188
}
188-
for _, p := range rule.predicates {
189-
if !p(entry) {
190-
return false
191-
}
192-
}
193-
return true
189+
return rule.predicate(entry)
194190
}
195191

196192
func sample(entry config.GenericMap, rules []*api.SamplingCondition) bool {
@@ -209,22 +205,21 @@ func rollSampling(value uint16) bool {
209205
// NewTransformFilter create a new filter transform
210206
func NewTransformFilter(params config.StageParam) (Transformer, error) {
211207
tlog.Debugf("entering NewTransformFilter")
212-
keepRules := []predicatesRule{}
208+
keepRules := []predicateRule{}
213209
rules := []api.TransformFilterRule{}
214210
if params.Transform != nil && params.Transform.Filter != nil {
215211
params.Transform.Filter.Preprocess()
216212
for i := range params.Transform.Filter.Rules {
217213
baseRules := &params.Transform.Filter.Rules[i]
218-
if baseRules.Type == api.KeepEntryAllSatisfied {
219-
pr := predicatesRule{sampling: baseRules.KeepEntrySampling}
220-
for _, keepRule := range baseRules.KeepEntryAllSatisfied {
221-
pred, err := filters.FromKeepEntry(keepRule)
222-
if err != nil {
223-
return nil, err
224-
}
225-
pr.predicates = append(pr.predicates, pred)
214+
if baseRules.Type == api.KeepEntryQuery {
215+
predicate, err := dsl.Parse(baseRules.KeepEntryQuery)
216+
if err != nil {
217+
return nil, err
226218
}
227-
keepRules = append(keepRules, pr)
219+
keepRules = append(keepRules, predicateRule{
220+
sampling: baseRules.KeepEntrySampling,
221+
predicate: predicate,
222+
})
228223
} else {
229224
rules = append(rules, *baseRules)
230225
}

pkg/pipeline/transform/transform_filter_test.go

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -676,34 +676,8 @@ func Test_Transform_KeepEntry(t *testing.T) {
676676
newFilter := api.TransformFilter{
677677
Rules: []api.TransformFilterRule{
678678
{
679-
Type: api.KeepEntryAllSatisfied,
680-
KeepEntryAllSatisfied: []*api.KeepEntryRule{
681-
{
682-
Type: api.KeepEntryIfEqual,
683-
KeepEntry: &api.TransformFilterGenericRule{
684-
Input: "namespace",
685-
Value: "A",
686-
},
687-
},
688-
{
689-
Type: api.KeepEntryIfExists,
690-
KeepEntry: &api.TransformFilterGenericRule{
691-
Input: "workload",
692-
},
693-
},
694-
},
695-
},
696-
{
697-
Type: api.KeepEntryAllSatisfied,
698-
KeepEntryAllSatisfied: []*api.KeepEntryRule{
699-
{
700-
Type: api.KeepEntryIfRegexMatch,
701-
KeepEntry: &api.TransformFilterGenericRule{
702-
Input: "service",
703-
Value: "abc.+",
704-
},
705-
},
706-
},
679+
Type: api.KeepEntryQuery,
680+
KeepEntryQuery: `(namespace="A" and with(workload)) or service=~"abc.+"`,
707681
},
708682
},
709683
}
@@ -742,29 +716,13 @@ func Test_Transform_KeepEntrySampling(t *testing.T) {
742716
newFilter := api.TransformFilter{
743717
Rules: []api.TransformFilterRule{
744718
{
745-
Type: api.KeepEntryAllSatisfied,
746-
KeepEntryAllSatisfied: []*api.KeepEntryRule{
747-
{
748-
Type: api.KeepEntryIfEqual,
749-
KeepEntry: &api.TransformFilterGenericRule{
750-
Input: "namespace",
751-
Value: "A",
752-
},
753-
},
754-
},
719+
Type: api.KeepEntryQuery,
720+
KeepEntryQuery: `namespace="A"`,
755721
KeepEntrySampling: 10,
756722
},
757723
{
758-
Type: api.KeepEntryAllSatisfied,
759-
KeepEntryAllSatisfied: []*api.KeepEntryRule{
760-
{
761-
Type: api.KeepEntryIfEqual,
762-
KeepEntry: &api.TransformFilterGenericRule{
763-
Input: "namespace",
764-
Value: "B",
765-
},
766-
},
767-
},
724+
Type: api.KeepEntryQuery,
725+
KeepEntryQuery: `namespace="B"`,
768726
},
769727
},
770728
}

pkg/utils/filters/filters.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package filters
22

33
import (
4-
"fmt"
54
"regexp"
65
"strings"
76

8-
"github.com/netobserv/flowlogs-pipeline/pkg/api"
97
"github.com/netobserv/flowlogs-pipeline/pkg/config"
108
"github.com/netobserv/flowlogs-pipeline/pkg/utils"
119
)
@@ -113,41 +111,3 @@ func injectVars(flow config.GenericMap, filterValue string, varLookups [][]strin
113111
}
114112
return injected
115113
}
116-
117-
func FromKeepEntry(from *api.KeepEntryRule) (Predicate, error) {
118-
switch from.Type {
119-
case api.KeepEntryIfExists:
120-
return Presence(from.KeepEntry.Input), nil
121-
case api.KeepEntryIfDoesntExist:
122-
return Absence(from.KeepEntry.Input), nil
123-
case api.KeepEntryIfEqual:
124-
return Equal(from.KeepEntry.Input, from.KeepEntry.Value, true), nil
125-
case api.KeepEntryIfNotEqual:
126-
return NotEqual(from.KeepEntry.Input, from.KeepEntry.Value, true), nil
127-
case api.KeepEntryIfRegexMatch:
128-
if r, err := compileRegex(from.KeepEntry); err != nil {
129-
return nil, err
130-
} else {
131-
return Regex(from.KeepEntry.Input, r), nil
132-
}
133-
case api.KeepEntryIfNotRegexMatch:
134-
if r, err := compileRegex(from.KeepEntry); err != nil {
135-
return nil, err
136-
} else {
137-
return NotRegex(from.KeepEntry.Input, r), nil
138-
}
139-
}
140-
return nil, fmt.Errorf("keep entry rule type not recognized: %s", from.Type)
141-
}
142-
143-
func compileRegex(from *api.TransformFilterGenericRule) (*regexp.Regexp, error) {
144-
s, ok := from.Value.(string)
145-
if !ok {
146-
return nil, fmt.Errorf("invalid regex keep rule: rule value must be a string [%v]", from)
147-
}
148-
r, err := regexp.Compile(s)
149-
if err != nil {
150-
return nil, fmt.Errorf("invalid regex keep rule: cannot compile regex [%w]", err)
151-
}
152-
return r, nil
153-
}

0 commit comments

Comments
 (0)