Skip to content

Commit 1ad6164

Browse files
authored
Merge pull request #1424 from rudyfly/network
feature: modify defaut bridge mode
2 parents 2a09b66 + 7e7f607 commit 1ad6164

File tree

12 files changed

+159
-76
lines changed

12 files changed

+159
-76
lines changed

apis/swagger.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3463,12 +3463,16 @@ definitions:
34633463
x-nullable: false
34643464
properties:
34653465
Subnet:
3466+
description: "subnet address for network"
34663467
type: "string"
34673468
IPRange:
3469+
description: "sub ip range in sub-network"
34683470
type: "string"
34693471
Gateway:
3472+
description: "gateway for sub-network"
34703473
type: "string"
34713474
AuxAddress:
3475+
description: "aux address in sub-network"
34723476
type: "object"
34733477
additionalProperties:
34743478
type: "string"

apis/types/ip_a_m_config.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

daemon/config/config.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ type Config struct {
2525
sync.Mutex
2626

2727
//Volume config
28-
VolumeConfig volume.Config `json:"volume-config"`
28+
VolumeConfig volume.Config `json:"volume-config,omitempty"`
2929

3030
// Network config
31-
NetworkConfg network.Config
31+
NetworkConfig network.Config `json:"network-config,omitempty"`
3232

3333
// Whether enable cri manager.
3434
IsCriEnabled bool `json:"enable-cri,omitempty"`
@@ -132,7 +132,7 @@ func (cfg *Config) Validate() error {
132132
}
133133

134134
//MergeConfigurations merges flagSet flags and config file flags into Config.
135-
func (cfg *Config) MergeConfigurations(config *Config, flagSet *pflag.FlagSet) error {
135+
func (cfg *Config) MergeConfigurations(flagSet *pflag.FlagSet) error {
136136
contents, err := ioutil.ReadFile(cfg.ConfigFile)
137137
if err != nil {
138138
if os.IsNotExist(err) {
@@ -206,7 +206,6 @@ func (cfg *Config) delValue(flagSet *pflag.FlagSet, fileFlags map[string]interfa
206206
r.Field(i).Set(reflect.MakeSlice(reflect.TypeOf([]string{}), 0, 0))
207207
}
208208
}
209-
210209
})
211210

212211
return cfg

daemon/daemon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func (d *Daemon) MetaStore() *meta.Store {
285285
}
286286

287287
func (d *Daemon) networkInit(ctx context.Context) error {
288-
return mode.NetworkModeInit(ctx, d.config.NetworkConfg, d.networkMgr)
288+
return mode.NetworkModeInit(ctx, d.config.NetworkConfig, d.networkMgr)
289289
}
290290

291291
// ContainerPlugin returns the container plugin fetched from shared file

daemon/mgr/network.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ type NetworkManager struct {
6767
// NewNetworkManager creates a brand new network manager.
6868
func NewNetworkManager(cfg *config.Config, store *meta.Store, ctrMgr ContainerMgr) (*NetworkManager, error) {
6969
// Create a new controller instance
70-
cfg.NetworkConfg.MetaPath = path.Dir(store.BaseDir)
71-
cfg.NetworkConfg.ExecRoot = network.DefaultExecRoot
70+
if cfg.NetworkConfig.MetaPath == "" {
71+
cfg.NetworkConfig.MetaPath = path.Dir(store.BaseDir)
72+
}
73+
74+
if cfg.NetworkConfig.ExecRoot == "" {
75+
cfg.NetworkConfig.ExecRoot = network.DefaultExecRoot
76+
}
7277

7378
initNetworkLog(cfg)
7479

@@ -81,17 +86,17 @@ func NewNetworkManager(cfg *config.Config, store *meta.Store, ctrMgr ContainerMg
8186
logrus.Errorf("failed to new network manager, can not get container list")
8287
return nil, errors.Wrap(err, "failed to get container list")
8388
}
84-
cfg.NetworkConfg.ActiveSandboxes = make(map[string]interface{})
89+
cfg.NetworkConfig.ActiveSandboxes = make(map[string]interface{})
8590
for _, c := range ctrs {
8691
endpoint := BuildContainerEndpoint(c)
87-
sbOptions, err := buildSandboxOptions(cfg.NetworkConfg, endpoint)
92+
sbOptions, err := buildSandboxOptions(cfg.NetworkConfig, endpoint)
8893
if err != nil {
8994
return nil, errors.Wrap(err, "failed to build sandbox options")
9095
}
91-
cfg.NetworkConfg.ActiveSandboxes[c.NetworkSettings.SandboxID] = sbOptions
96+
cfg.NetworkConfig.ActiveSandboxes[c.NetworkSettings.SandboxID] = sbOptions
9297
}
9398

94-
ctlOptions, err := controllerOptions(cfg.NetworkConfg)
99+
ctlOptions, err := controllerOptions(cfg.NetworkConfig)
95100
if err != nil {
96101
return nil, errors.Wrap(err, "failed to build network options")
97102
}
@@ -104,7 +109,7 @@ func NewNetworkManager(cfg *config.Config, store *meta.Store, ctrMgr ContainerMg
104109
return &NetworkManager{
105110
store: store,
106111
controller: controller,
107-
config: cfg.NetworkConfg,
112+
config: cfg.NetworkConfig,
108113
}, nil
109114
}
110115

@@ -449,16 +454,16 @@ func controllerOptions(cfg network.Config) ([]nwconfig.Option, error) {
449454
options = append(options, nwconfig.OptionDefaultNetwork("bridge"))
450455

451456
// set bridge options
452-
options = append(options, bridgeDriverOptions())
457+
options = append(options, bridgeDriverOptions(cfg.BridgeConfig))
453458

454459
return options, nil
455460
}
456461

457-
func bridgeDriverOptions() nwconfig.Option {
462+
func bridgeDriverOptions(cfg network.BridgeConfig) nwconfig.Option {
458463
bridgeConfig := options.Generic{
459-
"EnableIPForwarding": true,
460-
"EnableIPTables": true,
461-
"EnableUserlandProxy": true}
464+
"EnableIPForwarding": cfg.IPForward,
465+
"EnableIPTables": cfg.IPTables,
466+
"EnableUserlandProxy": cfg.UserlandProxy}
462467
bridgeOption := options.Generic{netlabel.GenericData: bridgeConfig}
463468

464469
return nwconfig.OptionDriverConfig("bridge", bridgeOption)

extra/libnetwork/drivers/bridge/port_mapping.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ var (
1515
)
1616

1717
func (n *bridgeNetwork) allocatePorts(ep *bridgeEndpoint, reqDefBindIP net.IP, ulPxyEnabled bool) ([]types.PortBinding, error) {
18+
if ep.addr == nil {
19+
return nil, fmt.Errorf("allocatePorts addr is null.")
20+
}
21+
1822
if ep.extConnConfig == nil || ep.extConnConfig.PortBindings == nil {
1923
return nil, nil
2024
}

extra/libnetwork/drivers/bridge/setup_ipv4.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package bridge
33
import (
44
"fmt"
55
"io/ioutil"
6+
"os"
67
"path/filepath"
78

89
log "github.com/Sirupsen/logrus"
@@ -16,13 +17,13 @@ func setupBridgeIPv4(config *networkConfiguration, i *bridgeInterface) error {
1617
return fmt.Errorf("failed to retrieve bridge interface addresses: %v", err)
1718
}
1819

19-
if !types.CompareIPNet(addrv4.IPNet, config.AddressIPv4) {
20+
if os.Getenv("SetBridgeIP") == "true" && !types.CompareIPNet(addrv4.IPNet, config.AddressIPv4) {
2021
if addrv4.IPNet != nil {
2122
if err := i.nlh.AddrDel(i.Link, &addrv4); err != nil {
2223
return fmt.Errorf("failed to remove current ip address from bridge: %v", err)
2324
}
2425
}
25-
log.Debugf("Assigning address to bridge interface %s: %s", config.BridgeName, config.AddressIPv4)
26+
log.Infof("Assigning address to bridge interface %s: %s", config.BridgeName, config.AddressIPv4)
2627
if err := i.nlh.AddrAdd(i.Link, &netlink.Addr{IPNet: config.AddressIPv4}); err != nil {
2728
return &IPv4AddrAddError{IP: config.AddressIPv4, Err: err}
2829
}

extra/libnetwork/drivers/bridge/setup_verify.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ func setupVerifyAndReconcile(config *networkConfiguration, i *bridgeInterface) e
1717

1818
// Verify that the bridge does have an IPv4 address.
1919
if addrv4.IPNet == nil {
20-
return &ErrNoIPAddr{}
20+
return nil
2121
}
2222

2323
// Verify that the bridge IPv4 address matches the requested configuration.
24-
if config.AddressIPv4 != nil && !addrv4.IP.Equal(config.AddressIPv4.IP) {
25-
return &IPv4AddrNoMatchError{IP: addrv4.IP, CfgIP: config.AddressIPv4.IP}
26-
}
24+
//if config.AddressIPv4 != nil && !addrv4.IP.Equal(config.AddressIPv4.IP) {
25+
// return &IPv4AddrNoMatchError{IP: addrv4.IP, CfgIP: config.AddressIPv4.IP}
26+
//}
2727

2828
// Verify that one of the bridge IPv6 addresses matches the requested
2929
// configuration.

main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ func setupFlags(cmd *cobra.Command) {
9999
// volume config
100100
flagSet.StringVar(&cfg.VolumeConfig.DriverAlias, "volume-driver-alias", "", "Set volume driver alias, <name=alias>[;name1=alias1]")
101101

102+
// network config
103+
flagSet.StringVar(&cfg.NetworkConfig.BridgeConfig.Name, "bridge-name", "", "Set default bridge name")
104+
flagSet.StringVar(&cfg.NetworkConfig.BridgeConfig.IP, "bip", "", "Set bridge IP")
105+
flagSet.StringVar(&cfg.NetworkConfig.BridgeConfig.GatewayIPv4, "default-gateway", "", "Set default bridge gateway")
106+
flagSet.StringVar(&cfg.NetworkConfig.BridgeConfig.FixedCIDR, "fixed-cidr", "", "Set bridge fixed CIDR")
107+
flagSet.IntVar(&cfg.NetworkConfig.BridgeConfig.Mtu, "mtu", 1500, "Set bridge MTU")
108+
flagSet.BoolVar(&cfg.NetworkConfig.BridgeConfig.IPTables, "iptables", true, "Enable iptables")
109+
flagSet.BoolVar(&cfg.NetworkConfig.BridgeConfig.IPForward, "ipforward", true, "Enable ipforward")
110+
flagSet.BoolVar(&cfg.NetworkConfig.BridgeConfig.UserlandProxy, "userland-proxy", false, "Enable userland proxy")
111+
102112
// cgroup-path flag is to set parent cgroup for all containers, default is "default" staying with containerd's configuration.
103113
flagSet.StringVar(&cfg.CgroupParent, "cgroup-parent", "default", "Set parent cgroup for all containers")
104114
flagSet.StringVar(&cfg.PluginPath, "plugin", "", "Set the path where plugin shared library file put")
@@ -275,5 +285,5 @@ func loadDaemonFile(cfg *config.Config, flagSet *pflag.FlagSet) error {
275285
return nil
276286
}
277287

278-
return cfg.MergeConfigurations(cfg, flagSet)
288+
return cfg.MergeConfigurations(flagSet)
279289
}

network/config.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,32 @@ var DefaultExecRoot = "/var/run/pouch"
55

66
// Config defines the network configuration.
77
type Config struct {
8-
Type string
8+
Type string `json:"-"`
99

10-
MetaPath string // meta store
11-
ExecRoot string // exec root
12-
DNS []string
13-
DNSOptions []string
14-
DNSSearch []string
10+
MetaPath string `json:"meta-path"` // meta store
11+
ExecRoot string `json:"exec-root-dir"` // exec root
12+
DNS []string `json:"dns"`
13+
DNSOptions []string `json:"dns-options"`
14+
DNSSearch []string `json:"dns-search"`
1515

1616
// bridge config
17-
BridgeConfig BridgeConfig
17+
BridgeConfig BridgeConfig `json:"bridge-config"`
1818

19-
ActiveSandboxes map[string]interface{}
19+
ActiveSandboxes map[string]interface{} `json:"-"`
2020
}
2121

2222
// BridgeConfig defines the bridge network configuration.
2323
type BridgeConfig struct {
24-
Name string
25-
IP string
26-
FixedCIDR string
27-
GatewayIPv4 string
28-
PreferredIP string
24+
Name string `json:"bridge-name"`
25+
IP string `json:"bip"`
26+
FixedCIDR string `json:"fixed-cidr"`
27+
GatewayIPv4 string `json:"default-gateway"`
28+
PreferredIP string `json:"preferred-ip"`
2929

30-
Mtu int
31-
ICC bool
32-
IPTables bool
33-
IPForward bool
34-
IPMasq bool
35-
UserlandProxy bool
36-
UserlandProxyPath string
30+
Mtu int `json:"mtu"`
31+
ICC bool `json:"icc"`
32+
IPTables bool `json:"iptables"`
33+
IPForward bool `json:"ipforward"`
34+
IPMasq bool `json:"ipmasq"`
35+
UserlandProxy bool `json:"userland-proxy"`
3736
}

0 commit comments

Comments
 (0)