From 528428919ecd0c60f04400c67b49820ac21b575d Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 23 Sep 2022 19:40:11 +0200 Subject: [PATCH] libnetwork/config: merge DaemonCfg into Config It was unclear what the distinction was between these configuration structs, so merging them to simplify. Signed-off-by: Sebastiaan van Stijn --- builder/builder-next/executor_unix.go | 2 +- daemon/container_operations.go | 2 +- daemon/network.go | 4 +-- libnetwork/agent.go | 8 +++--- libnetwork/config/config.go | 35 +++++++++++--------------- libnetwork/config/config_test.go | 6 ++--- libnetwork/controller.go | 26 +++++++++---------- libnetwork/firewall_linux_test.go | 2 +- libnetwork/sandbox_externalkey_unix.go | 2 +- 9 files changed, 40 insertions(+), 47 deletions(-) diff --git a/builder/builder-next/executor_unix.go b/builder/builder-next/executor_unix.go index 55209d8c8a..919e2bb2cf 100644 --- a/builder/builder-next/executor_unix.go +++ b/builder/builder-next/executor_unix.go @@ -128,7 +128,7 @@ func (iface *lnInterface) Set(s *specs.Spec) error { s.Hooks = &specs.Hooks{ Prestart: []specs.Hook{{ Path: filepath.Join("/proc", strconv.Itoa(os.Getpid()), "exe"), - Args: []string{"libnetwork-setkey", "-exec-root=" + iface.provider.Config().Daemon.ExecRoot, iface.sbx.ContainerID(), shortNetCtlrID}, + Args: []string{"libnetwork-setkey", "-exec-root=" + iface.provider.Config().ExecRoot, iface.sbx.ContainerID(), shortNetCtlrID}, }}, } return nil diff --git a/daemon/container_operations.go b/daemon/container_operations.go index 4c3d37468a..d8f7411a37 100644 --- a/daemon/container_operations.go +++ b/daemon/container_operations.go @@ -465,7 +465,7 @@ func (daemon *Daemon) updateContainerNetworkSettings(container *container.Contai networkName := mode.NetworkName() if mode.IsDefault() { - networkName = daemon.netController.Config().Daemon.DefaultNetwork + networkName = daemon.netController.Config().DefaultNetwork } if mode.IsUserDefined() { diff --git a/daemon/network.go b/daemon/network.go index 438d13d71a..c468d7f6e7 100644 --- a/daemon/network.go +++ b/daemon/network.go @@ -108,7 +108,7 @@ func (daemon *Daemon) GetNetworkByName(name string) (libnetwork.Network, error) return nil, libnetwork.ErrNoSuchNetwork(name) } if name == "" { - name = c.Config().Daemon.DefaultNetwork + name = c.Config().DefaultNetwork } return c.NetworkByName(name) } @@ -316,7 +316,7 @@ func (daemon *Daemon) createNetwork(create types.NetworkCreateRequest, id string c := daemon.netController driver := create.Driver if driver == "" { - driver = c.Config().Daemon.DefaultDriver + driver = c.Config().DefaultDriver } nwOptions := []libnetwork.NetworkOption{ diff --git a/libnetwork/agent.go b/libnetwork/agent.go index feca147ea8..9cf678d465 100644 --- a/libnetwork/agent.go +++ b/libnetwork/agent.go @@ -223,7 +223,7 @@ func (c *controller) agentSetup(clusterProvider cluster.Provider) error { listenAddr, _, _ := net.SplitHostPort(listen) logrus.Infof("Initializing Libnetwork Agent Listen-Addr=%s Local-addr=%s Adv-addr=%s Data-addr=%s Remote-addr-list=%v MTU=%d", - listenAddr, bindAddr, advAddr, dataAddr, remoteAddrList, c.Config().Daemon.NetworkControlPlaneMTU) + listenAddr, bindAddr, advAddr, dataAddr, remoteAddrList, c.Config().NetworkControlPlaneMTU) if advAddr != "" && agent == nil { if err := c.agentInit(listenAddr, bindAddr, advAddr, dataAddr); err != nil { logrus.Errorf("error in agentInit: %v", err) @@ -295,12 +295,12 @@ func (c *controller) agentInit(listenAddr, bindAddrOrInterface, advertiseAddr, d netDBConf.BindAddr = listenAddr netDBConf.AdvertiseAddr = advertiseAddr netDBConf.Keys = keys - if c.Config().Daemon.NetworkControlPlaneMTU != 0 { + if c.Config().NetworkControlPlaneMTU != 0 { // Consider the MTU remove the IP hdr (IPv4 or IPv6) and the TCP/UDP hdr. // To be on the safe side let's cut 100 bytes - netDBConf.PacketBufferSize = (c.Config().Daemon.NetworkControlPlaneMTU - 100) + netDBConf.PacketBufferSize = (c.Config().NetworkControlPlaneMTU - 100) logrus.Debugf("Control plane MTU: %d will initialize NetworkDB with: %d", - c.Config().Daemon.NetworkControlPlaneMTU, netDBConf.PacketBufferSize) + c.Config().NetworkControlPlaneMTU, netDBConf.PacketBufferSize) } nDB, err := networkdb.New(netDBConf) if err != nil { diff --git a/libnetwork/config/config.go b/libnetwork/config/config.go index 3c7da720e7..115c4e14fe 100644 --- a/libnetwork/config/config.go +++ b/libnetwork/config/config.go @@ -20,14 +20,6 @@ const ( // Config encapsulates configurations of various Libnetwork components type Config struct { - Daemon DaemonCfg - Scopes map[string]*datastore.ScopeCfg - ActiveSandboxes map[string]interface{} - PluginGetter plugingetter.PluginGetter -} - -// DaemonCfg represents libnetwork core configuration -type DaemonCfg struct { DataDir string ExecRoot string DefaultNetwork string @@ -37,6 +29,9 @@ type DaemonCfg struct { ClusterProvider cluster.Provider NetworkControlPlaneMTU int DefaultAddressPool []*ipamutils.NetworkToSplit + Scopes map[string]*datastore.ScopeCfg + ActiveSandboxes map[string]interface{} + PluginGetter plugingetter.PluginGetter } // LoadDefaultScopes loads default scope configs for scopes which @@ -53,10 +48,8 @@ func (c *Config) LoadDefaultScopes(dataDir string) { // a reference to the corresponding Config structure func ParseConfigOptions(opts ...Option) *Config { cfg := &Config{ - Daemon: DaemonCfg{ - DriverCfg: make(map[string]interface{}), - }, - Scopes: make(map[string]*datastore.ScopeCfg), + DriverCfg: make(map[string]interface{}), + Scopes: make(map[string]*datastore.ScopeCfg), } for _, opt := range opts { if opt != nil { @@ -64,7 +57,7 @@ func ParseConfigOptions(opts ...Option) *Config { } } - cfg.LoadDefaultScopes(cfg.Daemon.DataDir) + cfg.LoadDefaultScopes(cfg.DataDir) return cfg } @@ -77,7 +70,7 @@ type Option func(c *Config) func OptionDefaultNetwork(dn string) Option { return func(c *Config) { logrus.Debugf("Option DefaultNetwork: %s", dn) - c.Daemon.DefaultNetwork = strings.TrimSpace(dn) + c.DefaultNetwork = strings.TrimSpace(dn) } } @@ -85,21 +78,21 @@ func OptionDefaultNetwork(dn string) Option { func OptionDefaultDriver(dd string) Option { return func(c *Config) { logrus.Debugf("Option DefaultDriver: %s", dd) - c.Daemon.DefaultDriver = strings.TrimSpace(dd) + c.DefaultDriver = strings.TrimSpace(dd) } } // OptionDefaultAddressPoolConfig function returns an option setter for default address pool func OptionDefaultAddressPoolConfig(addressPool []*ipamutils.NetworkToSplit) Option { return func(c *Config) { - c.Daemon.DefaultAddressPool = addressPool + c.DefaultAddressPool = addressPool } } // OptionDriverConfig returns an option setter for driver configuration. func OptionDriverConfig(networkType string, config map[string]interface{}) Option { return func(c *Config) { - c.Daemon.DriverCfg[networkType] = config + c.DriverCfg[networkType] = config } } @@ -108,7 +101,7 @@ func OptionLabels(labels []string) Option { return func(c *Config) { for _, label := range labels { if strings.HasPrefix(label, netlabel.Prefix) { - c.Daemon.Labels = append(c.Daemon.Labels, label) + c.Labels = append(c.Labels, label) } } } @@ -117,14 +110,14 @@ func OptionLabels(labels []string) Option { // OptionDataDir function returns an option setter for data folder func OptionDataDir(dataDir string) Option { return func(c *Config) { - c.Daemon.DataDir = dataDir + c.DataDir = dataDir } } // OptionExecRoot function returns an option setter for exec root folder func OptionExecRoot(execRoot string) Option { return func(c *Config) { - c.Daemon.ExecRoot = execRoot + c.ExecRoot = execRoot osl.SetBasePath(execRoot) } } @@ -147,7 +140,7 @@ func OptionNetworkControlPlaneMTU(exp int) Option { exp = minimumNetworkControlPlaneMTU } } - c.Daemon.NetworkControlPlaneMTU = exp + c.NetworkControlPlaneMTU = exp } } diff --git a/libnetwork/config/config_test.go b/libnetwork/config/config_test.go index c0decffc43..cfd5a5a955 100644 --- a/libnetwork/config/config_test.go +++ b/libnetwork/config/config_test.go @@ -17,10 +17,10 @@ func TestOptionsLabels(t *testing.T) { } f := OptionLabels(l) f(c) - if len(c.Daemon.Labels) != 3 { - t.Fatalf("Expecting 3 labels, seen %d", len(c.Daemon.Labels)) + if len(c.Labels) != 3 { + t.Fatalf("Expecting 3 labels, seen %d", len(c.Labels)) } - for _, l := range c.Daemon.Labels { + for _, l := range c.Labels { if !strings.HasPrefix(l, netlabel.Prefix) { t.Fatalf("config must accept only libnetwork labels. Not : %s", l) } diff --git a/libnetwork/controller.go b/libnetwork/controller.go index fe83dffd8b..a09685f07c 100644 --- a/libnetwork/controller.go +++ b/libnetwork/controller.go @@ -219,7 +219,7 @@ func New(cfgOptions ...config.Option) (NetworkController, error) { } } - if err = initIPAMDrivers(drvRegistry, nil, c.getStore(datastore.GlobalScope), c.cfg.Daemon.DefaultAddressPool); err != nil { + if err = initIPAMDrivers(drvRegistry, nil, c.getStore(datastore.GlobalScope), c.cfg.DefaultAddressPool); err != nil { return nil, err } @@ -249,12 +249,12 @@ func (c *controller) SetClusterProvider(provider cluster.Provider) { var sameProvider bool c.Lock() // Avoids to spawn multiple goroutine for the same cluster provider - if c.cfg.Daemon.ClusterProvider == provider { + if c.cfg.ClusterProvider == provider { // If the cluster provider is already set, there is already a go routine spawned // that is listening for events, so nothing to do here sameProvider = true } else { - c.cfg.Daemon.ClusterProvider = provider + c.cfg.ClusterProvider = provider } c.Unlock() @@ -301,7 +301,7 @@ func (c *controller) getAgent() *agent { } func (c *controller) clusterAgentInit() { - clusterProvider := c.cfg.Daemon.ClusterProvider + clusterProvider := c.cfg.ClusterProvider var keysAvailable bool for { eventType := <-clusterProvider.ListenClusterEvents() @@ -408,7 +408,7 @@ func (c *controller) makeDriverConfig(ntype string) map[string]interface{} { config := make(map[string]interface{}) - for _, label := range c.cfg.Daemon.Labels { + for _, label := range c.cfg.Labels { if !strings.HasPrefix(netlabel.Key(label), netlabel.DriverPrefix+"."+ntype) { continue } @@ -416,7 +416,7 @@ func (c *controller) makeDriverConfig(ntype string) map[string]interface{} { config[netlabel.Key(label)] = netlabel.Value(label) } - drvCfg, ok := c.cfg.Daemon.DriverCfg[ntype] + drvCfg, ok := c.cfg.DriverCfg[ntype] if ok { for k, v := range drvCfg.(map[string]interface{}) { config[k] = v @@ -580,19 +580,19 @@ func (c *controller) Config() config.Config { func (c *controller) isManager() bool { c.Lock() defer c.Unlock() - if c.cfg == nil || c.cfg.Daemon.ClusterProvider == nil { + if c.cfg == nil || c.cfg.ClusterProvider == nil { return false } - return c.cfg.Daemon.ClusterProvider.IsManager() + return c.cfg.ClusterProvider.IsManager() } func (c *controller) isAgent() bool { c.Lock() defer c.Unlock() - if c.cfg == nil || c.cfg.Daemon.ClusterProvider == nil { + if c.cfg == nil || c.cfg.ClusterProvider == nil { return false } - return c.cfg.Daemon.ClusterProvider.IsAgent() + return c.cfg.ClusterProvider.IsAgent() } func (c *controller) isDistributedControl() bool { @@ -1038,8 +1038,8 @@ func (c *controller) NewSandbox(containerID string, options ...SandboxOption) (S if sb.ingress { c.ingressSandbox = sb - sb.config.hostsPath = filepath.Join(c.cfg.Daemon.DataDir, "/network/files/hosts") - sb.config.resolvConfPath = filepath.Join(c.cfg.Daemon.DataDir, "/network/files/resolv.conf") + sb.config.hostsPath = filepath.Join(c.cfg.DataDir, "/network/files/hosts") + sb.config.resolvConfPath = filepath.Join(c.cfg.DataDir, "/network/files/resolv.conf") sb.id = "ingress_sbox" } else if sb.loadBalancerNID != "" { sb.id = "lb_" + sb.loadBalancerNID @@ -1287,7 +1287,7 @@ func (c *controller) iptablesEnabled() bool { return false } // parse map cfg["bridge"]["generic"]["EnableIPTable"] - cfgBridge, ok := c.cfg.Daemon.DriverCfg["bridge"].(map[string]interface{}) + cfgBridge, ok := c.cfg.DriverCfg["bridge"].(map[string]interface{}) if !ok { return false } diff --git a/libnetwork/firewall_linux_test.go b/libnetwork/firewall_linux_test.go index e712900aaf..37fcda1b2a 100644 --- a/libnetwork/firewall_linux_test.go +++ b/libnetwork/firewall_linux_test.go @@ -52,7 +52,7 @@ func TestUserChain(t *testing.T) { tc := tc t.Run(fmt.Sprintf("iptables=%v,insert=%v", tc.iptables, tc.insert), func(t *testing.T) { c := nc.(*controller) - c.cfg.Daemon.DriverCfg["bridge"] = map[string]interface{}{ + c.cfg.DriverCfg["bridge"] = map[string]interface{}{ netlabel.GenericData: options.Generic{ "EnableIPTables": tc.iptables, }, diff --git a/libnetwork/sandbox_externalkey_unix.go b/libnetwork/sandbox_externalkey_unix.go index 963deecb81..2c0355ed33 100644 --- a/libnetwork/sandbox_externalkey_unix.go +++ b/libnetwork/sandbox_externalkey_unix.go @@ -114,7 +114,7 @@ func processReturn(r io.Reader) error { func (c *controller) startExternalKeyListener() error { execRoot := defaultExecRoot - if v := c.Config().Daemon.ExecRoot; v != "" { + if v := c.Config().ExecRoot; v != "" { execRoot = v } udsBase := filepath.Join(execRoot, execSubdir)