github.com/cilium/cilium@v1.16.2/pkg/hubble/filters/workload.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 filterByWorkload(wf []*flowpb.Workload, getEndpoint func(*v1.Event) *flowpb.Endpoint) FilterFunc {
    14  	return func(ev *v1.Event) bool {
    15  		for _, w := range getEndpoint(ev).GetWorkloads() {
    16  			for _, f := range wf {
    17  				if (f.GetName() == "" || f.GetName() == w.GetName()) &&
    18  					(f.GetKind() == "" || f.GetKind() == w.GetKind()) {
    19  					return true
    20  				}
    21  			}
    22  		}
    23  		return false
    24  	}
    25  }
    26  
    27  // WorkloadFilter implements filtering based on endpoint workload
    28  type WorkloadFilter struct{}
    29  
    30  // OnBuildFilter builds an endpoint workload filter
    31  func (*WorkloadFilter) OnBuildFilter(ctx context.Context, ff *flowpb.FlowFilter) ([]FilterFunc, error) {
    32  	var fs []FilterFunc
    33  
    34  	if wf := ff.GetSourceWorkload(); len(wf) > 0 {
    35  		fs = append(fs, filterByWorkload(wf, sourceEndpoint))
    36  	}
    37  
    38  	if wf := ff.GetDestinationWorkload(); len(wf) > 0 {
    39  		fs = append(fs, filterByWorkload(wf, destinationEndpoint))
    40  	}
    41  
    42  	return fs, nil
    43  }