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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package pool
     5  
     6  import (
     7  	"github.com/sirupsen/logrus"
     8  	"google.golang.org/grpc"
     9  	"google.golang.org/grpc/credentials/insecure"
    10  
    11  	"github.com/cilium/cilium/pkg/backoff"
    12  	peerTypes "github.com/cilium/cilium/pkg/hubble/peer/types"
    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  // defaultOptions is the reference point for default values.
    21  var defaultOptions = options{
    22  	peerServiceAddress: defaults.PeerTarget,
    23  	peerClientBuilder: peerTypes.LocalClientBuilder{
    24  		DialTimeout: defaults.DialTimeout,
    25  	},
    26  	clientConnBuilder: GRPCClientConnBuilder{
    27  		DialTimeout: defaults.DialTimeout,
    28  		Options: []grpc.DialOption{
    29  			grpc.WithTransportCredentials(insecure.NewCredentials()),
    30  			grpc.WithBlock(),
    31  			grpc.FailOnNonTempDialError(true),
    32  			grpc.WithReturnConnectionError(),
    33  		},
    34  	},
    35  	backoff: &backoff.Exponential{
    36  		Min:    time.Second,
    37  		Max:    time.Minute,
    38  		Factor: 2.0,
    39  	},
    40  	connCheckInterval:  2 * time.Minute,
    41  	connStatusInterval: 5 * time.Second,
    42  	retryTimeout:       defaults.RetryTimeout,
    43  	log:                logging.DefaultLogger.WithField(logfields.LogSubsys, "hubble-relay"),
    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  	peerServiceAddress string
    52  	peerClientBuilder  peerTypes.ClientBuilder
    53  	clientConnBuilder  poolTypes.ClientConnBuilder
    54  	backoff            BackoffDuration
    55  	connCheckInterval  time.Duration
    56  	connStatusInterval time.Duration
    57  	retryTimeout       time.Duration
    58  	log                logrus.FieldLogger
    59  }
    60  
    61  // WithPeerServiceAddress sets the address of the peer gRPC service.
    62  func WithPeerServiceAddress(a string) Option {
    63  	return func(o *options) error {
    64  		o.peerServiceAddress = a
    65  		return nil
    66  	}
    67  }
    68  
    69  // WithPeerClientBuilder sets the ClientBuilder that is used to create new Peer
    70  // service clients.
    71  func WithPeerClientBuilder(b peerTypes.ClientBuilder) Option {
    72  	return func(o *options) error {
    73  		o.peerClientBuilder = b
    74  		return nil
    75  	}
    76  }
    77  
    78  // WithClientConnBuilder sets the GRPCClientConnBuilder that is used to create
    79  // new gRPC connections to peers.
    80  func WithClientConnBuilder(b poolTypes.ClientConnBuilder) Option {
    81  	return func(o *options) error {
    82  		o.clientConnBuilder = b
    83  		return nil
    84  	}
    85  }
    86  
    87  // WithBackoff sets the backoff between after a failed connection attempt.
    88  func WithBackoff(b BackoffDuration) Option {
    89  	return func(o *options) error {
    90  		o.backoff = b
    91  		return nil
    92  	}
    93  }
    94  
    95  // WithConnCheckInterval sets the time interval between peer connections health
    96  // checks.
    97  func WithConnCheckInterval(i time.Duration) Option {
    98  	return func(o *options) error {
    99  		o.connCheckInterval = i
   100  		return nil
   101  	}
   102  }
   103  
   104  // WithConnStatusInterval sets the time interval between peer connection status
   105  // is reported through Prometheus gauge metrics.
   106  func WithConnStatusInterval(i time.Duration) Option {
   107  	return func(o *options) error {
   108  		o.connStatusInterval = i
   109  		return nil
   110  	}
   111  }
   112  
   113  // WithRetryTimeout sets the duration to wait before attempting to re-connect
   114  // to the peer gRPC service.
   115  func WithRetryTimeout(t time.Duration) Option {
   116  	return func(o *options) error {
   117  		o.retryTimeout = t
   118  		return nil
   119  	}
   120  }
   121  
   122  // WithLogger sets the logger to use for logging.
   123  func WithLogger(l logrus.FieldLogger) Option {
   124  	return func(o *options) error {
   125  		o.log = l
   126  		return nil
   127  	}
   128  }