github.com/cilium/cilium@v1.16.2/pkg/hubble/filters/reply.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 filterByReplyField(replyParams []bool) FilterFunc {
    14  	return func(ev *v1.Event) bool {
    15  		if len(replyParams) == 0 {
    16  			return true
    17  		}
    18  		switch f := ev.Event.(type) {
    19  		case *flowpb.Flow:
    20  			// FIXME: For dropped flows, we handle `is_reply=unknown` as
    21  			// `is_reply=false`. This is for compatibility with older clients
    22  			// (such as Hubble UI) which assume this filter applies to the
    23  			// deprecated `reply` field, where dropped flows always have
    24  			// `reply=false`.
    25  			if f.GetIsReply() == nil && f.GetVerdict() != flowpb.Verdict_DROPPED {
    26  				return false
    27  			}
    28  
    29  			reply := f.GetIsReply().GetValue()
    30  			for _, replyParam := range replyParams {
    31  				if reply == replyParam {
    32  					return true
    33  				}
    34  			}
    35  		}
    36  		return false
    37  	}
    38  }
    39  
    40  // ReplyFilter implements filtering for reply flows
    41  type ReplyFilter struct{}
    42  
    43  // OnBuildFilter builds a reply filter
    44  func (r *ReplyFilter) OnBuildFilter(ctx context.Context, ff *flowpb.FlowFilter) ([]FilterFunc, error) {
    45  	var fs []FilterFunc
    46  
    47  	if ff.GetReply() != nil {
    48  		fs = append(fs, filterByReplyField(ff.GetReply()))
    49  	}
    50  
    51  	return fs, nil
    52  }