github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/libnetwork/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/containerd/log"
     8  	"github.com/Prakhar-Agarwal-byte/moby/libnetwork/cluster"
     9  	"github.com/Prakhar-Agarwal-byte/moby/libnetwork/datastore"
    10  	"github.com/Prakhar-Agarwal-byte/moby/libnetwork/ipamutils"
    11  	"github.com/Prakhar-Agarwal-byte/moby/libnetwork/netlabel"
    12  	"github.com/Prakhar-Agarwal-byte/moby/pkg/plugingetter"
    13  )
    14  
    15  const (
    16  	warningThNetworkControlPlaneMTU = 1500
    17  	minimumNetworkControlPlaneMTU   = 500
    18  )
    19  
    20  // Config encapsulates configurations of various Libnetwork components
    21  type Config struct {
    22  	DataDir string
    23  	// ExecRoot is the base-path for libnetwork external key listeners
    24  	// (created in "<ExecRoot>/libnetwork/<Controller-Short-ID>.sock"),
    25  	// and is passed as "-exec-root: argument for "libnetwork-setkey".
    26  	//
    27  	// It is only used on Linux, but referenced in some "unix" files
    28  	// (linux and freebsd).
    29  	//
    30  	// FIXME(thaJeztah): ExecRoot is only used for Controller.startExternalKeyListener(), but "libnetwork-setkey" is only implemented on Linux.
    31  	ExecRoot               string
    32  	DefaultNetwork         string
    33  	DefaultDriver          string
    34  	Labels                 []string
    35  	driverCfg              map[string]map[string]any
    36  	ClusterProvider        cluster.Provider
    37  	NetworkControlPlaneMTU int
    38  	DefaultAddressPool     []*ipamutils.NetworkToSplit
    39  	Scope                  datastore.ScopeCfg
    40  	ActiveSandboxes        map[string]interface{}
    41  	PluginGetter           plugingetter.PluginGetter
    42  }
    43  
    44  // New creates a new Config and initializes it with the given Options.
    45  func New(opts ...Option) *Config {
    46  	cfg := &Config{
    47  		driverCfg: make(map[string]map[string]any),
    48  	}
    49  
    50  	for _, opt := range opts {
    51  		if opt != nil {
    52  			opt(cfg)
    53  		}
    54  	}
    55  
    56  	// load default scope configs which don't have explicit user specified configs.
    57  	if cfg.Scope == (datastore.ScopeCfg{}) {
    58  		cfg.Scope = datastore.DefaultScope(cfg.DataDir)
    59  	}
    60  	return cfg
    61  }
    62  
    63  func (c *Config) DriverConfig(name string) map[string]any {
    64  	return c.driverCfg[name]
    65  }
    66  
    67  // Option is an option setter function type used to pass various configurations
    68  // to the controller
    69  type Option func(c *Config)
    70  
    71  // OptionDefaultNetwork function returns an option setter for a default network
    72  func OptionDefaultNetwork(dn string) Option {
    73  	return func(c *Config) {
    74  		log.G(context.TODO()).Debugf("Option DefaultNetwork: %s", dn)
    75  		c.DefaultNetwork = strings.TrimSpace(dn)
    76  	}
    77  }
    78  
    79  // OptionDefaultDriver function returns an option setter for default driver
    80  func OptionDefaultDriver(dd string) Option {
    81  	return func(c *Config) {
    82  		log.G(context.TODO()).Debugf("Option DefaultDriver: %s", dd)
    83  		c.DefaultDriver = strings.TrimSpace(dd)
    84  	}
    85  }
    86  
    87  // OptionDefaultAddressPoolConfig function returns an option setter for default address pool
    88  func OptionDefaultAddressPoolConfig(addressPool []*ipamutils.NetworkToSplit) Option {
    89  	return func(c *Config) {
    90  		c.DefaultAddressPool = addressPool
    91  	}
    92  }
    93  
    94  // OptionDriverConfig returns an option setter for driver configuration.
    95  func OptionDriverConfig(networkType string, config map[string]any) Option {
    96  	return func(c *Config) {
    97  		c.driverCfg[networkType] = config
    98  	}
    99  }
   100  
   101  // OptionLabels function returns an option setter for labels
   102  func OptionLabels(labels []string) Option {
   103  	return func(c *Config) {
   104  		for _, label := range labels {
   105  			if strings.HasPrefix(label, netlabel.Prefix) {
   106  				c.Labels = append(c.Labels, label)
   107  			}
   108  		}
   109  	}
   110  }
   111  
   112  // OptionDataDir function returns an option setter for data folder
   113  func OptionDataDir(dataDir string) Option {
   114  	return func(c *Config) {
   115  		c.DataDir = dataDir
   116  	}
   117  }
   118  
   119  // OptionExecRoot function returns an option setter for exec root folder.
   120  //
   121  // On Linux, it sets both the controller's ExecRoot and osl.basePath, whereas
   122  // on FreeBSD, it only sets the controller's ExecRoot. It is a no-op on other
   123  // platforms.
   124  func OptionExecRoot(execRoot string) Option {
   125  	return optionExecRoot(execRoot)
   126  }
   127  
   128  // OptionPluginGetter returns a plugingetter for remote drivers.
   129  func OptionPluginGetter(pg plugingetter.PluginGetter) Option {
   130  	return func(c *Config) {
   131  		c.PluginGetter = pg
   132  	}
   133  }
   134  
   135  // OptionNetworkControlPlaneMTU function returns an option setter for control plane MTU
   136  func OptionNetworkControlPlaneMTU(exp int) Option {
   137  	return func(c *Config) {
   138  		log.G(context.TODO()).Debugf("Network Control Plane MTU: %d", exp)
   139  		if exp < warningThNetworkControlPlaneMTU {
   140  			log.G(context.TODO()).Warnf("Received a MTU of %d, this value is very low, the network control plane can misbehave,"+
   141  				" defaulting to minimum value (%d)", exp, minimumNetworkControlPlaneMTU)
   142  			if exp < minimumNetworkControlPlaneMTU {
   143  				exp = minimumNetworkControlPlaneMTU
   144  			}
   145  		}
   146  		c.NetworkControlPlaneMTU = exp
   147  	}
   148  }
   149  
   150  // OptionActiveSandboxes function returns an option setter for passing the sandboxes
   151  // which were active during previous daemon life
   152  func OptionActiveSandboxes(sandboxes map[string]interface{}) Option {
   153  	return func(c *Config) {
   154  		c.ActiveSandboxes = sandboxes
   155  	}
   156  }