1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

daemon: daemon.networkOptions(): don't pass Config as argument

This is a method on the daemon, which itself holds the Config, so
there's no need to pass the same configuration as an argument.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-04-23 23:12:55 +02:00
parent e78f6f9c68
commit 3b56c0663d
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
4 changed files with 43 additions and 47 deletions

View file

@ -471,7 +471,7 @@ func (daemon *Daemon) restore() error {
} }
group.Wait() group.Wait()
daemon.netController, err = daemon.initNetworkController(daemon.configStore, activeSandboxes) daemon.netController, err = daemon.initNetworkController(activeSandboxes)
if err != nil { if err != nil {
return fmt.Errorf("Error initializing network controller: %v", err) return fmt.Errorf("Error initializing network controller: %v", err)
} }
@ -1345,37 +1345,35 @@ func isBridgeNetworkDisabled(conf *config.Config) bool {
return conf.BridgeConfig.Iface == config.DisableNetworkBridge return conf.BridgeConfig.Iface == config.DisableNetworkBridge
} }
func (daemon *Daemon) networkOptions(dconfig *config.Config, pg plugingetter.PluginGetter, activeSandboxes map[string]interface{}) ([]nwconfig.Option, error) { func (daemon *Daemon) networkOptions(pg plugingetter.PluginGetter, activeSandboxes map[string]interface{}) ([]nwconfig.Option, error) {
options := []nwconfig.Option{} options := []nwconfig.Option{}
if dconfig == nil { if daemon.configStore == nil {
return options, nil return options, nil
} }
conf := daemon.configStore
options = append(options, nwconfig.OptionExperimental(dconfig.Experimental))
options = append(options, nwconfig.OptionDataDir(dconfig.Root))
options = append(options, nwconfig.OptionExecRoot(dconfig.GetExecRoot()))
dd := runconfig.DefaultDaemonNetworkMode() dd := runconfig.DefaultDaemonNetworkMode()
dn := runconfig.DefaultDaemonNetworkMode().NetworkName()
options = append(options, nwconfig.OptionDefaultDriver(string(dd)))
options = append(options, nwconfig.OptionDefaultNetwork(dn))
options = append(options, nwconfig.OptionLabels(dconfig.Labels))
options = append(options, driverOptions(dconfig))
if len(dconfig.NetworkConfig.DefaultAddressPools.Value()) > 0 { options = []nwconfig.Option{
options = append(options, nwconfig.OptionDefaultAddressPoolConfig(dconfig.NetworkConfig.DefaultAddressPools.Value())) nwconfig.OptionExperimental(conf.Experimental),
nwconfig.OptionDataDir(conf.Root),
nwconfig.OptionExecRoot(conf.GetExecRoot()),
nwconfig.OptionDefaultDriver(string(dd)),
nwconfig.OptionDefaultNetwork(dd.NetworkName()),
nwconfig.OptionLabels(conf.Labels),
nwconfig.OptionNetworkControlPlaneMTU(conf.NetworkControlPlaneMTU),
driverOptions(conf),
} }
if daemon.configStore != nil && daemon.configStore.LiveRestoreEnabled && len(activeSandboxes) != 0 { if len(conf.NetworkConfig.DefaultAddressPools.Value()) > 0 {
options = append(options, nwconfig.OptionDefaultAddressPoolConfig(conf.NetworkConfig.DefaultAddressPools.Value()))
}
if conf.LiveRestoreEnabled && len(activeSandboxes) != 0 {
options = append(options, nwconfig.OptionActiveSandboxes(activeSandboxes)) options = append(options, nwconfig.OptionActiveSandboxes(activeSandboxes))
} }
if pg != nil { if pg != nil {
options = append(options, nwconfig.OptionPluginGetter(pg)) options = append(options, nwconfig.OptionPluginGetter(pg))
} }
options = append(options, nwconfig.OptionNetworkControlPlaneMTU(dconfig.NetworkControlPlaneMTU))
return options, nil return options, nil
} }

View file

@ -836,8 +836,8 @@ func configureKernelSecuritySupport(config *config.Config, driverName string) er
return nil return nil
} }
func (daemon *Daemon) initNetworkController(config *config.Config, activeSandboxes map[string]interface{}) (libnetwork.NetworkController, error) { func (daemon *Daemon) initNetworkController(activeSandboxes map[string]interface{}) (libnetwork.NetworkController, error) {
netOptions, err := daemon.networkOptions(config, daemon.PluginStore, activeSandboxes) netOptions, err := daemon.networkOptions(daemon.PluginStore, activeSandboxes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -847,9 +847,10 @@ func (daemon *Daemon) initNetworkController(config *config.Config, activeSandbox
return nil, fmt.Errorf("error obtaining controller instance: %v", err) return nil, fmt.Errorf("error obtaining controller instance: %v", err)
} }
conf := daemon.configStore
if len(activeSandboxes) > 0 { if len(activeSandboxes) > 0 {
logrus.Info("There are old running containers, the network config will not take affect") logrus.Info("There are old running containers, the network config will not take affect")
setHostGatewayIP(daemon.configStore, controller) setHostGatewayIP(conf, controller)
return controller, nil return controller, nil
} }
@ -872,14 +873,14 @@ func (daemon *Daemon) initNetworkController(config *config.Config, activeSandbox
if err = n.Delete(); err != nil { if err = n.Delete(); err != nil {
return nil, fmt.Errorf("could not delete the default bridge network: %v", err) return nil, fmt.Errorf("could not delete the default bridge network: %v", err)
} }
if len(config.NetworkConfig.DefaultAddressPools.Value()) > 0 && !daemon.configStore.LiveRestoreEnabled { if len(conf.NetworkConfig.DefaultAddressPools.Value()) > 0 && !conf.LiveRestoreEnabled {
removeDefaultBridgeInterface() removeDefaultBridgeInterface()
} }
} }
if !config.DisableBridge { if !conf.DisableBridge {
// Initialize default driver "bridge" // Initialize default driver "bridge"
if err := initBridgeDriver(controller, config); err != nil { if err := initBridgeDriver(controller, conf); err != nil {
return nil, err return nil, err
} }
} else { } else {
@ -887,7 +888,7 @@ func (daemon *Daemon) initNetworkController(config *config.Config, activeSandbox
} }
// Set HostGatewayIP to the default bridge's IP if it is empty // Set HostGatewayIP to the default bridge's IP if it is empty
setHostGatewayIP(daemon.configStore, controller) setHostGatewayIP(conf, controller)
return controller, nil return controller, nil
} }

View file

@ -236,8 +236,8 @@ func configureMaxThreads(config *config.Config) error {
return nil return nil
} }
func (daemon *Daemon) initNetworkController(config *config.Config, activeSandboxes map[string]interface{}) (libnetwork.NetworkController, error) { func (daemon *Daemon) initNetworkController(activeSandboxes map[string]interface{}) (libnetwork.NetworkController, error) {
netOptions, err := daemon.networkOptions(config, nil, nil) netOptions, err := daemon.networkOptions(nil, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -253,8 +253,7 @@ func (daemon *Daemon) initNetworkController(config *config.Config, activeSandbox
// Remove networks not present in HNS // Remove networks not present in HNS
for _, v := range controller.Networks() { for _, v := range controller.Networks() {
options := v.Info().DriverOptions() hnsid := v.Info().DriverOptions()[winlibnetwork.HNSID]
hnsid := options[winlibnetwork.HNSID]
found := false found := false
for _, v := range hnsresponse { for _, v := range hnsresponse {
@ -283,9 +282,9 @@ func (daemon *Daemon) initNetworkController(config *config.Config, activeSandbox
defaultNetworkExists := false defaultNetworkExists := false
if network, err := controller.NetworkByName(runconfig.DefaultDaemonNetworkMode().NetworkName()); err == nil { if network, err := controller.NetworkByName(runconfig.DefaultDaemonNetworkMode().NetworkName()); err == nil {
options := network.Info().DriverOptions() hnsid := network.Info().DriverOptions()[winlibnetwork.HNSID]
for _, v := range hnsresponse { for _, v := range hnsresponse {
if options[winlibnetwork.HNSID] == v.Id { if hnsid == v.Id {
defaultNetworkExists = true defaultNetworkExists = true
break break
} }
@ -301,8 +300,8 @@ func (daemon *Daemon) initNetworkController(config *config.Config, activeSandbox
} }
var n libnetwork.Network var n libnetwork.Network
s := func(current libnetwork.Network) bool { s := func(current libnetwork.Network) bool {
options := current.Info().DriverOptions() hnsid := current.Info().DriverOptions()[winlibnetwork.HNSID]
if options[winlibnetwork.HNSID] == v.Id { if hnsid == v.Id {
n = current n = current
return true return true
} }
@ -372,9 +371,10 @@ func (daemon *Daemon) initNetworkController(config *config.Config, activeSandbox
} }
} }
if !config.DisableBridge { conf := daemon.configStore
if !conf.DisableBridge {
// Initialize default driver "bridge" // Initialize default driver "bridge"
if err := initBridgeDriver(controller, config); err != nil { if err := initBridgeDriver(controller, conf); err != nil {
return nil, err return nil, err
} }
} }

View file

@ -343,22 +343,19 @@ func TestDaemonReloadNetworkDiagnosticPort(t *testing.T) {
} }
daemon := &Daemon{ daemon := &Daemon{
imageService: images.NewImageService(images.ImageServiceConfig{}), imageService: images.NewImageService(images.ImageServiceConfig{}),
configStore: &config.Config{},
} }
daemon.configStore = &config.Config{}
valuesSet := make(map[string]interface{})
valuesSet["network-diagnostic-port"] = 2000
enableConfig := &config.Config{ enableConfig := &config.Config{
CommonConfig: config.CommonConfig{ CommonConfig: config.CommonConfig{
NetworkDiagnosticPort: 2000, NetworkDiagnosticPort: 2000,
ValuesSet: valuesSet, ValuesSet: map[string]interface{}{
"network-diagnostic-port": 2000,
},
}, },
} }
disableConfig := &config.Config{
CommonConfig: config.CommonConfig{},
}
netOptions, err := daemon.networkOptions(enableConfig, nil, nil) netOptions, err := daemon.networkOptions(nil, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -376,16 +373,16 @@ func TestDaemonReloadNetworkDiagnosticPort(t *testing.T) {
} }
// Check that the diagnostic is enabled // Check that the diagnostic is enabled
if !daemon.netController.IsDiagnosticEnabled() { if !daemon.netController.IsDiagnosticEnabled() {
t.Fatalf("diagnostic should be enable") t.Fatalf("diagnostic should be enabled")
} }
// Reload // Reload
if err := daemon.Reload(disableConfig); err != nil { if err := daemon.Reload(&config.Config{}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
// Check that the diagnostic is disabled // Check that the diagnostic is disabled
if daemon.netController.IsDiagnosticEnabled() { if daemon.netController.IsDiagnosticEnabled() {
t.Fatalf("diagnostic should be disable") t.Fatalf("diagnostic should be disabled")
} }
} }