github.com/cilium/cilium@v1.16.2/pkg/hubble/filters/traffic_direction.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Hubble
     3  
     4  package filters
     5  
     6  import (
     7  	"context"
     8  
     9  	flowpb "github.com/cilium/cilium/api/v1/flow"
    10  	v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
    11  )
    12  
    13  func filterByTrafficDirection(directions []flowpb.TrafficDirection) FilterFunc {
    14  	return func(ev *v1.Event) bool {
    15  		flow := ev.GetFlow()
    16  		if flow == nil {
    17  			return false
    18  		}
    19  		for _, d := range directions {
    20  			if d == flow.GetTrafficDirection() {
    21  				return true
    22  			}
    23  		}
    24  		return false
    25  	}
    26  }
    27  
    28  // TrafficDirectionFilter implements filtering based on flow traffic direction
    29  // (e.g. ingress or egress).
    30  type TrafficDirectionFilter struct{}
    31  
    32  // OnBuildFilter builds a a flow traffic direction filter.
    33  func (e *TrafficDirectionFilter) OnBuildFilter(ctx context.Context, ff *flowpb.FlowFilter) ([]FilterFunc, error) {
    34  	var fs []FilterFunc
    35  
    36  	if directions := ff.GetTrafficDirection(); len(directions) > 0 {
    37  		fs = append(fs, filterByTrafficDirection(directions))
    38  	}
    39  
    40  	return fs, nil
    41  }