github.com/cilium/cilium@v1.16.2/pkg/hubble/relay/observer/option.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package observer
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/sirupsen/logrus"
    10  	"google.golang.org/grpc"
    11  
    12  	observerpb "github.com/cilium/cilium/api/v1/observer"
    13  	"github.com/cilium/cilium/pkg/hubble/relay/defaults"
    14  	poolTypes "github.com/cilium/cilium/pkg/hubble/relay/pool/types"
    15  	"github.com/cilium/cilium/pkg/logging"
    16  	"github.com/cilium/cilium/pkg/logging/logfields"
    17  	"github.com/cilium/cilium/pkg/time"
    18  )
    19  
    20  type observerClientBuilder interface {
    21  	observerClient(p *poolTypes.Peer) observerpb.ObserverClient
    22  }
    23  
    24  type defaultObserverClientBuilder struct{}
    25  
    26  func (d defaultObserverClientBuilder) observerClient(p *poolTypes.Peer) observerpb.ObserverClient {
    27  	if p == nil {
    28  		return nil
    29  	}
    30  	if conn, ok := p.Conn.(*grpc.ClientConn); ok {
    31  		return observerpb.NewObserverClient(conn)
    32  	}
    33  	return nil
    34  }
    35  
    36  // DefaultOptions is the reference point for default values.
    37  var defaultOptions = options{
    38  	sortBufferMaxLen:       defaults.SortBufferMaxLen,
    39  	sortBufferDrainTimeout: defaults.SortBufferDrainTimeout,
    40  	errorAggregationWindow: defaults.ErrorAggregationWindow,
    41  	peerUpdateInterval:     defaults.PeerUpdateInterval,
    42  	log:                    logging.DefaultLogger.WithField(logfields.LogSubsys, "hubble-relay"),
    43  	ocb:                    defaultObserverClientBuilder{},
    44  }
    45  
    46  // Option customizes the configuration of the Manager.
    47  type Option func(o *options) error
    48  
    49  // Options stores all the configuration values for peer manager.
    50  type options struct {
    51  	sortBufferMaxLen       int
    52  	sortBufferDrainTimeout time.Duration
    53  	errorAggregationWindow time.Duration
    54  	peerUpdateInterval     time.Duration
    55  	log                    logrus.FieldLogger
    56  
    57  	// this is not meant to be user configurable as it's only useful to
    58  	// override when testing
    59  	ocb observerClientBuilder
    60  }
    61  
    62  // WithSortBufferMaxLen sets the maximum number of flows that can be buffered
    63  // for sorting before being sent to the client. The provided value must be
    64  // greater than 0 and is to be understood per client request. Therefore, it is
    65  // advised to keep the value moderate (a value between 30 and 100 should
    66  // constitute a good choice in most cases).
    67  func WithSortBufferMaxLen(i int) Option {
    68  	return func(o *options) error {
    69  		if i <= 0 {
    70  			return fmt.Errorf("value for SortBufferMaxLen must be greater than 0: %d", i)
    71  		}
    72  		o.sortBufferMaxLen = i
    73  		return nil
    74  	}
    75  }
    76  
    77  // WithSortBufferDrainTimeout sets the sort buffer drain timeout value. For
    78  // flows requests where the total number of flows cannot be determined
    79  // (typically for flows requests in follow mode), a flow is taken out of the
    80  // buffer and sent to the client after duration d if the buffer is not full.
    81  // This value must be greater than 0. Setting this value too low would render
    82  // the flows sorting operation ineffective. A value between 500 milliseconds
    83  // and 3 seconds should be constitute a good choice in most cases.
    84  func WithSortBufferDrainTimeout(d time.Duration) Option {
    85  	return func(o *options) error {
    86  		if d <= 0 {
    87  			return fmt.Errorf("value for SortBufferDrainTimeout must be greater than 0: %d", d)
    88  		}
    89  		o.sortBufferDrainTimeout = d
    90  		return nil
    91  	}
    92  }
    93  
    94  // WithErrorAggregationWindow sets a time window during which errors with the
    95  // same error message are coalesced. The aggregated error is forwarded to the
    96  // downstream consumer either when the window expires or when a new, different
    97  // error occurs (whichever happens first)
    98  func WithErrorAggregationWindow(d time.Duration) Option {
    99  	return func(o *options) error {
   100  		if d <= 0 {
   101  			return fmt.Errorf("value for ErrorAggregationWindow must be greater than 0: %d", d)
   102  		}
   103  		o.errorAggregationWindow = d
   104  		return nil
   105  	}
   106  }
   107  
   108  // WithLogger sets the logger to use for logging.
   109  func WithLogger(l logrus.FieldLogger) Option {
   110  	return func(o *options) error {
   111  		o.log = l
   112  		return nil
   113  	}
   114  }
   115  
   116  // withObserverClientBuilder sets the observerClientBuilder that is used to
   117  // create a new ObserverClient from a poolTypes.ClientConn. This is private as
   118  // it is only useful to override the default in the context of implemeting unit
   119  // tests.
   120  func withObserverClientBuilder(b observerClientBuilder) Option {
   121  	return func(o *options) error {
   122  		o.ocb = b
   123  		return nil
   124  	}
   125  }