@@ -28,6 +28,7 @@ import (
2828 "github.com/netobserv/flowlogs-pipeline/pkg/api"
2929 "github.com/netobserv/flowlogs-pipeline/pkg/config"
3030 "github.com/netobserv/flowlogs-pipeline/pkg/utils"
31+ "github.com/netobserv/flowlogs-pipeline/pkg/utils/filters"
3132 "github.com/sirupsen/logrus"
3233)
3334
3839
3940type Filter struct {
4041 Rules []api.TransformFilterRule
41- KeepRules []api.TransformFilterRule
42+ KeepRules []predicatesRule
43+ }
44+
45+ type predicatesRule struct {
46+ predicates []filters.Predicate
47+ sampling uint16
4248}
4349
4450// Transform transforms a flow; if false is returned as a second argument, the entry is dropped
@@ -48,9 +54,8 @@ func (f *Filter) Transform(entry config.GenericMap) (config.GenericMap, bool) {
4854 labels := make (map [string ]string )
4955 if len (f .KeepRules ) > 0 {
5056 keep := false
51- for i := range f .KeepRules {
52- tlog .Tracef ("keep rule = %v" , f .KeepRules [i ])
53- if applyRule (outputEntry , labels , & f .KeepRules [i ]) {
57+ for _ , r := range f .KeepRules {
58+ if applyPredicates (outputEntry , r ) {
5459 keep = true
5560 break
5661 }
@@ -158,7 +163,8 @@ func applyRule(entry config.GenericMap, labels map[string]string, rule *api.Tran
158163 case api .ConditionalSampling :
159164 return sample (entry , rule .ConditionalSampling )
160165 case api .KeepEntryAllSatisfied :
161- return rollSampling (rule .KeepEntrySampling ) && isKeepEntrySatisfied (entry , rule .KeepEntryAllSatisfied )
166+ // This should be processed only in "applyPredicates". Failure to do so is a bug.
167+ tlog .Panicf ("unexpected KeepEntryAllSatisfied: %v" , rule )
162168 default :
163169 tlog .Panicf ("unknown type %s for transform.Filter rule: %v" , rule .Type , rule )
164170 }
@@ -175,58 +181,18 @@ func isRemoveEntrySatisfied(entry config.GenericMap, rules []*api.RemoveEntryRul
175181 return true
176182}
177183
178- func isKeepEntrySatisfied (entry config.GenericMap , rules []* api.KeepEntryRule ) bool {
179- for _ , r := range rules {
180- val , ok := entry [r .KeepEntry .Input ]
181- switch r .Type {
182- case api .KeepEntryIfExists :
183- if ! ok {
184- return false
185- }
186- case api .KeepEntryIfDoesntExist :
187- if ok {
188- return false
189- }
190- case api .KeepEntryIfEqual :
191- if ! ok || val != r .KeepEntry .Value {
192- return false
193- }
194- case api .KeepEntryIfNotEqual :
195- if ok && val == r .KeepEntry .Value {
196- return false
197- }
198- case api .KeepEntryIfRegexMatch :
199- if ok {
200- match , ok := checkRegex (r .KeepEntry .Value , val )
201- if ! ok || ! match {
202- return false
203- }
204- } else {
205- return false
206- }
207- case api .KeepEntryIfNotRegexMatch :
208- if ok {
209- match , ok := checkRegex (r .KeepEntry .Value , val )
210- if ! ok || match {
211- return false
212- }
213- } else {
214- return false
215- }
184+ func applyPredicates (entry config.GenericMap , rule predicatesRule ) bool {
185+ if ! rollSampling (rule .sampling ) {
186+ return false
187+ }
188+ for _ , p := range rule .predicates {
189+ if ! p (entry ) {
190+ return false
216191 }
217192 }
218193 return true
219194}
220195
221- // Returns (valid, match)
222- func checkRegex (maybeReg any , value any ) (bool , bool ) {
223- reg , ok := maybeReg .(* regexp.Regexp )
224- if ! ok {
225- return false , false
226- }
227- return true , reg .MatchString (utils .ConvertToString (value ))
228- }
229-
230196func sample (entry config.GenericMap , rules []* api.SamplingCondition ) bool {
231197 for _ , r := range rules {
232198 if isRemoveEntrySatisfied (entry , r .Rules ) {
@@ -243,17 +209,24 @@ func rollSampling(value uint16) bool {
243209// NewTransformFilter create a new filter transform
244210func NewTransformFilter (params config.StageParam ) (Transformer , error ) {
245211 tlog .Debugf ("entering NewTransformFilter" )
246- keepRules := []api. TransformFilterRule {}
212+ keepRules := []predicatesRule {}
247213 rules := []api.TransformFilterRule {}
248214 if params .Transform != nil && params .Transform .Filter != nil {
249- if err := params .Transform .Filter .Preprocess (); err != nil {
250- return nil , err
251- }
215+ params .Transform .Filter .Preprocess ()
252216 for i := range params .Transform .Filter .Rules {
253- if params .Transform .Filter .Rules [i ].Type == api .KeepEntryAllSatisfied {
254- keepRules = append (keepRules , params .Transform .Filter .Rules [i ])
217+ 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 )
226+ }
227+ keepRules = append (keepRules , pr )
255228 } else {
256- rules = append (rules , params . Transform . Filter . Rules [ i ] )
229+ rules = append (rules , * baseRules )
257230 }
258231 }
259232 }
0 commit comments