Skip to content

Commit 4021bd7

Browse files
authored
Merge pull request moby#50864 from akerouanton/bridge-driver-config
libnet/d/bridge: Register: pass a Configuration struct
2 parents 50d281f + f37094a commit 4021bd7

27 files changed

+155
-368
lines changed

daemon/daemon.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,6 @@ func (daemon *Daemon) networkOptions(conf *config.Config, pg plugingetter.Plugin
16491649
nwconfig.OptionExecRoot(conf.GetExecRoot()),
16501650
nwconfig.OptionDefaultDriver(network.DefaultNetwork),
16511651
nwconfig.OptionDefaultNetwork(network.DefaultNetwork),
1652-
nwconfig.OptionLabels(conf.Labels),
16531652
nwconfig.OptionNetworkControlPlaneMTU(conf.NetworkControlPlaneMTU),
16541653
nwconfig.OptionFirewallBackend(conf.FirewallBackend),
16551654
}

daemon/daemon_unix.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"github.com/moby/moby/v2/daemon/libnetwork/drivers/bridge"
3636
"github.com/moby/moby/v2/daemon/libnetwork/netlabel"
3737
"github.com/moby/moby/v2/daemon/libnetwork/nlwrap"
38-
"github.com/moby/moby/v2/daemon/libnetwork/options"
3938
lntypes "github.com/moby/moby/v2/daemon/libnetwork/types"
4039
"github.com/moby/moby/v2/daemon/pkg/opts"
4140
volumemounts "github.com/moby/moby/v2/daemon/volume/mounts"
@@ -927,17 +926,15 @@ func networkPlatformOptions(conf *config.Config) []nwconfig.Option {
927926
return []nwconfig.Option{
928927
nwconfig.OptionRootless(conf.Rootless),
929928
nwconfig.OptionUserlandProxy(conf.EnableUserlandProxy, conf.UserlandProxyPath),
930-
nwconfig.OptionDriverConfig("bridge", options.Generic{
931-
netlabel.GenericData: options.Generic{
932-
"EnableIPForwarding": conf.BridgeConfig.EnableIPForward,
933-
"DisableFilterForwardDrop": conf.BridgeConfig.DisableFilterForwardDrop,
934-
"EnableIPTables": conf.BridgeConfig.EnableIPTables,
935-
"EnableIP6Tables": conf.BridgeConfig.EnableIP6Tables,
936-
"EnableProxy": conf.EnableUserlandProxy && conf.UserlandProxyPath != "",
937-
"ProxyPath": conf.UserlandProxyPath,
938-
"AllowDirectRouting": conf.BridgeConfig.AllowDirectRouting,
939-
"AcceptFwMark": conf.BridgeConfig.BridgeAcceptFwMark,
940-
},
929+
nwconfig.OptionBridgeConfig(bridge.Configuration{
930+
EnableIPForwarding: conf.BridgeConfig.EnableIPForward,
931+
DisableFilterForwardDrop: conf.BridgeConfig.DisableFilterForwardDrop,
932+
EnableIPTables: conf.BridgeConfig.EnableIPTables,
933+
EnableIP6Tables: conf.BridgeConfig.EnableIP6Tables,
934+
EnableProxy: conf.EnableUserlandProxy && conf.UserlandProxyPath != "",
935+
ProxyPath: conf.UserlandProxyPath,
936+
AllowDirectRouting: conf.BridgeConfig.AllowDirectRouting,
937+
AcceptFwMark: conf.BridgeConfig.BridgeAcceptFwMark,
941938
}),
942939
}
943940
}

daemon/libnetwork/config/config.go

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/moby/moby/v2/daemon/libnetwork/cluster"
99
"github.com/moby/moby/v2/daemon/libnetwork/datastore"
1010
"github.com/moby/moby/v2/daemon/libnetwork/ipamutils"
11-
"github.com/moby/moby/v2/daemon/libnetwork/netlabel"
1211
"github.com/moby/moby/v2/pkg/plugingetter"
1312
)
1413

@@ -19,6 +18,8 @@ const (
1918

2019
// Config encapsulates configurations of various Libnetwork components
2120
type Config struct {
21+
PlatformConfig
22+
2223
DataDir string
2324
// ExecRoot is the base-path for libnetwork external key listeners
2425
// (created in "<ExecRoot>/libnetwork/<Controller-Short-ID>.sock"),
@@ -32,7 +33,6 @@ type Config struct {
3233
DefaultNetwork string
3334
DefaultDriver string
3435
Labels []string
35-
driverCfg map[string]map[string]any
3636
ClusterProvider cluster.Provider
3737
NetworkControlPlaneMTU int
3838
DefaultAddressPool []*ipamutils.NetworkToSplit
@@ -48,7 +48,6 @@ type Config struct {
4848
// New creates a new Config and initializes it with the given Options.
4949
func New(opts ...Option) *Config {
5050
cfg := &Config{
51-
driverCfg: make(map[string]map[string]any),
5251
DatastoreBucket: datastore.DefaultBucket,
5352
}
5453

@@ -61,10 +60,6 @@ func New(opts ...Option) *Config {
6160
return cfg
6261
}
6362

64-
func (c *Config) DriverConfig(name string) map[string]any {
65-
return c.driverCfg[name]
66-
}
67-
6863
// Option is an option setter function type used to pass various configurations
6964
// to the controller
7065
type Option func(c *Config)
@@ -92,24 +87,6 @@ func OptionDefaultAddressPoolConfig(addressPool []*ipamutils.NetworkToSplit) Opt
9287
}
9388
}
9489

95-
// OptionDriverConfig returns an option setter for driver configuration.
96-
func OptionDriverConfig(networkType string, config map[string]any) Option {
97-
return func(c *Config) {
98-
c.driverCfg[networkType] = config
99-
}
100-
}
101-
102-
// OptionLabels function returns an option setter for labels
103-
func OptionLabels(labels []string) Option {
104-
return func(c *Config) {
105-
for _, label := range labels {
106-
if strings.HasPrefix(label, netlabel.Prefix) {
107-
c.Labels = append(c.Labels, label)
108-
}
109-
}
110-
}
111-
}
112-
11390
// OptionDataDir function returns an option setter for data folder
11491
func OptionDataDir(dataDir string) Option {
11592
return func(c *Config) {

daemon/libnetwork/config/config_freebsd.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

daemon/libnetwork/config/config_linux.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
package config
22

3-
import "github.com/moby/moby/v2/daemon/libnetwork/osl"
3+
import (
4+
"github.com/moby/moby/v2/daemon/libnetwork/drivers/bridge"
5+
"github.com/moby/moby/v2/daemon/libnetwork/osl"
6+
)
7+
8+
// PlatformConfig defines platform-specific configuration.
9+
type PlatformConfig struct {
10+
BridgeConfig bridge.Configuration
11+
}
12+
13+
// OptionBridgeConfig returns an option setter for bridge driver config.
14+
func OptionBridgeConfig(config bridge.Configuration) Option {
15+
return func(c *Config) {
16+
c.BridgeConfig = config
17+
}
18+
}
419

520
// optionExecRoot on Linux sets both the controller's ExecRoot and osl.basePath.
621
func optionExecRoot(execRoot string) Option {

daemon/libnetwork/config/config_test.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

daemon/libnetwork/config/config_unsupported.go renamed to daemon/libnetwork/config/config_windows.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
//go:build !linux && !freebsd
2-
31
package config
42

3+
// PlatformConfig defines platform-specific configuration.
4+
type PlatformConfig struct{}
5+
56
// optionExecRoot is a no-op on non-unix platforms.
67
func optionExecRoot(execRoot string) Option {
78
return func(*Config) {}

daemon/libnetwork/controller.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func New(ctx context.Context, cfgOptions ...config.Option) (_ *Controller, retEr
187187
return nil, err
188188
}
189189

190-
if err := registerNetworkDrivers(&c.drvRegistry, c.store, &c.pmRegistry, c.makeDriverConfig); err != nil {
190+
if err := registerNetworkDrivers(&c.drvRegistry, c.cfg, c.store, &c.pmRegistry); err != nil {
191191
return nil, err
192192
}
193193

@@ -383,29 +383,6 @@ func (c *Controller) agentStopComplete() {
383383
c.mu.Unlock()
384384
}
385385

386-
func (c *Controller) makeDriverConfig(ntype string) map[string]any {
387-
if c.cfg == nil {
388-
return nil
389-
}
390-
391-
cfg := map[string]any{}
392-
for _, label := range c.cfg.Labels {
393-
key, val, _ := strings.Cut(label, "=")
394-
if !strings.HasPrefix(key, netlabel.DriverPrefix+"."+ntype) {
395-
continue
396-
}
397-
398-
cfg[key] = val
399-
}
400-
401-
// Merge in the existing config for this driver.
402-
for k, v := range c.cfg.DriverConfig(ntype) {
403-
cfg[k] = v
404-
}
405-
406-
return cfg
407-
}
408-
409386
// ID returns the controller's unique identity.
410387
func (c *Controller) ID() string {
411388
return c.id

daemon/libnetwork/controller_linux.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"github.com/moby/moby/api/types/system"
1111
"github.com/moby/moby/v2/daemon/libnetwork/internal/nftables"
1212
"github.com/moby/moby/v2/daemon/libnetwork/iptables"
13-
"github.com/moby/moby/v2/daemon/libnetwork/netlabel"
14-
"github.com/moby/moby/v2/daemon/libnetwork/options"
1513
"github.com/moby/moby/v2/daemon/libnetwork/osl"
1614
)
1715

@@ -34,24 +32,11 @@ func (c *Controller) FirewallBackend() *system.FirewallInfo {
3432
// enabledIptablesVersions returns the iptables versions that are enabled
3533
// for the controller.
3634
func (c *Controller) enabledIptablesVersions() []iptables.IPVersion {
37-
c.mu.Lock()
38-
defer c.mu.Unlock()
39-
if c.cfg == nil {
40-
return nil
41-
}
42-
// parse map cfg["bridge"]["generic"]["EnableIPTable"]
43-
cfgBridge := c.cfg.DriverConfig("bridge")
44-
cfgGeneric, ok := cfgBridge[netlabel.GenericData].(options.Generic)
45-
if !ok {
46-
return nil
47-
}
48-
4935
var versions []iptables.IPVersion
50-
if enabled, ok := cfgGeneric["EnableIPTables"].(bool); enabled || !ok {
51-
// iptables is enabled unless user explicitly disabled it
36+
if c.cfg.BridgeConfig.EnableIPTables {
5237
versions = append(versions, iptables.IPv4)
5338
}
54-
if enabled, _ := cfgGeneric["EnableIP6Tables"].(bool); enabled {
39+
if c.cfg.BridgeConfig.EnableIP6Tables {
5540
versions = append(versions, iptables.IPv6)
5641
}
5742
return versions

daemon/libnetwork/drivers/bridge/bridge_linux.go

Lines changed: 30 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ const spanPrefix = "libnetwork.drivers.bridge"
6363
// FIXME(robmry) - it doesn't belong here.
6464
const DockerForwardChain = iptabler.DockerForwardChain
6565

66-
// configuration info for the "bridge" driver.
67-
type configuration struct {
66+
// Configuration info for the "bridge" driver.
67+
type Configuration struct {
6868
EnableIPForwarding bool
6969
DisableFilterForwardDrop bool
7070
EnableIPTables bool
@@ -156,7 +156,7 @@ type bridgeNetwork struct {
156156
}
157157

158158
type driver struct {
159-
config configuration
159+
config Configuration
160160
networks map[string]*bridgeNetwork
161161
store *datastore.Store
162162
nlh nlwrap.Handle
@@ -178,19 +178,40 @@ const (
178178
)
179179

180180
// New constructs a new bridge driver
181-
func newDriver(store *datastore.Store, pms *drvregistry.PortMappers) *driver {
182-
return &driver{
181+
func newDriver(store *datastore.Store, config Configuration, pms *drvregistry.PortMappers) (*driver, error) {
182+
fw, err := newFirewaller(context.Background(), firewaller.Config{
183+
IPv4: config.EnableIPTables,
184+
IPv6: config.EnableIP6Tables,
185+
Hairpin: !config.EnableProxy,
186+
AllowDirectRouting: config.AllowDirectRouting,
187+
WSL2Mirrored: isRunningUnderWSL2MirroredMode(context.Background()),
188+
})
189+
if err != nil {
190+
return nil, err
191+
}
192+
193+
d := &driver{
183194
store: store,
195+
config: config,
184196
nlh: ns.NlHandle(),
185197
networks: map[string]*bridgeNetwork{},
198+
firewaller: fw,
186199
portmappers: pms,
187200
}
201+
202+
if err := d.initStore(); err != nil {
203+
return nil, err
204+
}
205+
206+
iptables.OnReloaded(d.handleFirewalldReload)
207+
208+
return d, nil
188209
}
189210

190211
// Register registers a new instance of bridge driver.
191-
func Register(r driverapi.Registerer, store *datastore.Store, pms *drvregistry.PortMappers, config map[string]any) error {
192-
d := newDriver(store, pms)
193-
if err := d.configure(config); err != nil {
212+
func Register(r driverapi.Registerer, store *datastore.Store, pms *drvregistry.PortMappers, config Configuration) error {
213+
d, err := newDriver(store, config, pms)
214+
if err != nil {
194215
return err
195216
}
196217
return r.RegisterDriver(NetworkType, d, driverapi.Capability{
@@ -496,48 +517,6 @@ func (n *bridgeNetwork) getEndpoint(eid string) (*bridgeEndpoint, error) {
496517
return nil, nil
497518
}
498519

499-
func (d *driver) configure(option map[string]any) error {
500-
var config configuration
501-
switch opt := option[netlabel.GenericData].(type) {
502-
case options.Generic:
503-
opaqueConfig, err := options.GenerateFromModel(opt, &configuration{})
504-
if err != nil {
505-
return err
506-
}
507-
config = *opaqueConfig.(*configuration)
508-
case *configuration:
509-
config = *opt
510-
case nil:
511-
// No GenericData option set. Use defaults.
512-
default:
513-
return errdefs.InvalidParameter(fmt.Errorf("invalid configuration type (%T) passed", opt))
514-
}
515-
516-
var err error
517-
d.firewaller, err = newFirewaller(context.Background(), firewaller.Config{
518-
IPv4: config.EnableIPTables,
519-
IPv6: config.EnableIP6Tables,
520-
Hairpin: !config.EnableProxy,
521-
AllowDirectRouting: config.AllowDirectRouting,
522-
WSL2Mirrored: isRunningUnderWSL2MirroredMode(context.Background()),
523-
})
524-
if err != nil {
525-
return err
526-
}
527-
528-
d.mu.Lock()
529-
d.config = config
530-
d.mu.Unlock()
531-
532-
// Register for an event when firewalld is reloaded, but take the config lock so
533-
// that events won't be processed until the initial load from Store is complete.
534-
d.configNetwork.Lock()
535-
defer d.configNetwork.Unlock()
536-
iptables.OnReloaded(d.handleFirewalldReload)
537-
538-
return d.initStore()
539-
}
540-
541520
var newFirewaller = func(ctx context.Context, config firewaller.Config) (firewaller.Firewaller, error) {
542521
if nftables.Enabled() {
543522
fw, err := nftabler.NewNftabler(ctx, config)
@@ -1057,7 +1036,6 @@ func (d *driver) CreateEndpoint(ctx context.Context, nid, eid string, ifInfo dri
10571036
// Get the network handler and make sure it exists
10581037
d.mu.Lock()
10591038
n, ok := d.networks[nid]
1060-
dconfig := d.config
10611039
d.mu.Unlock()
10621040

10631041
if !ok {
@@ -1169,7 +1147,7 @@ func (d *driver) CreateEndpoint(ctx context.Context, nid, eid string, ifInfo dri
11691147
return fmt.Errorf("adding interface %s to bridge %s failed: %v", hostIfName, config.BridgeName, err)
11701148
}
11711149

1172-
if !dconfig.EnableProxy {
1150+
if !d.config.EnableProxy {
11731151
err = setHairpinMode(d.nlh, host, true)
11741152
if err != nil {
11751153
return err

0 commit comments

Comments
 (0)