Skip to content

Commit

Permalink
Closes #15830 Support docker-specific network create options via CLI
Browse files Browse the repository at this point in the history
Signed-off-by: T K Chandra Hasan <t.k.chandra.hasan@ibm.com>
  • Loading branch information
hasan4791 committed Feb 9, 2023
1 parent ff4c7e6 commit 69e4db7
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
16 changes: 12 additions & 4 deletions libnetwork/netavark/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ func (n *netavarkNetwork) networkCreate(newNetwork *types.Network, defaultNet bo

switch newNetwork.Driver {
case types.BridgeNetworkDriver:
err = internalutil.CreateBridge(n, newNetwork, usedNetworks, n.defaultsubnetPools)
if err != nil {
return nil, err
}
// validate the given options, we do not need them but just check to make sure they are valid
for key, value := range newNetwork.Options {
switch key {
case "com.docker.network.driver.mtu":
newNetwork.Options[types.MTUOption] = value
fallthrough

case types.MTUOption:
_, err = internalutil.ParseMTU(value)
if err != nil {
Expand All @@ -186,10 +186,18 @@ func (n *netavarkNetwork) networkCreate(newNetwork *types.Network, defaultNet bo
if err != nil {
return nil, err
}

case "com.docker.network.bridge.name":
newNetwork.NetworkInterface = value

default:
return nil, fmt.Errorf("unsupported bridge network option %s", key)
}
}
err = internalutil.CreateBridge(n, newNetwork, usedNetworks, n.defaultsubnetPools)
if err != nil {
return nil, err
}
case types.MacVLANNetworkDriver:
err = createMacvlan(newNetwork)
if err != nil {
Expand Down
63 changes: 63 additions & 0 deletions libnetwork/netavark/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,32 @@ var _ = Describe("Config", func() {
Expect(network1.Internal).To(BeFalse())
})

It("create bridge config with com.docker.network.bridge.name", func() {
network := types.Network{
Driver: "bridge",
Options: map[string]string{
"com.docker.network.bridge.name": "foo",
},
}
network1, err := libpodNet.NetworkCreate(network, nil)
Expect(err).To(BeNil())
Expect(network1.Name).ToNot(BeEmpty())
Expect(filepath.Join(networkConfDir, network1.Name+".json")).To(BeARegularFile())
Expect(network1.ID).ToNot(BeEmpty())
Expect(network1.NetworkInterface).To(Equal("foo"))
Expect(network1.Driver).To(Equal("bridge"))
Expect(network1.Labels).To(BeEmpty())
Expect(network1.Options).To(HaveKeyWithValue("com.docker.network.bridge.name", "foo"))
Expect(network1.IPAMOptions).ToNot(BeEmpty())
Expect(network1.IPAMOptions).To(HaveKeyWithValue("driver", "host-local"))
Expect(network1.Subnets).To(HaveLen(1))
Expect(network1.Subnets[0].Subnet.String()).To(Equal("10.89.0.0/24"))
Expect(network1.Subnets[0].Gateway.String()).To(Equal("10.89.0.1"))
Expect(network1.Subnets[0].LeaseRange).To(BeNil())
Expect(network1.DNSEnabled).To(BeFalse())
Expect(network1.Internal).To(BeFalse())
})

It("create bridge with same name should fail", func() {
network := types.Network{
Driver: "bridge",
Expand Down Expand Up @@ -795,6 +821,43 @@ var _ = Describe("Config", func() {
Expect(err.Error()).To(ContainSubstring(`mtu -1 is less than zero`))
})

It("create network with com.docker.network.driver.mtu option", func() {
network := types.Network{
Options: map[string]string{
"com.docker.network.driver.mtu": "1500",
},
}
network1, err := libpodNet.NetworkCreate(network, nil)
Expect(err).To(BeNil())
Expect(network1.Driver).To(Equal("bridge"))
Expect(network1.Options).ToNot(BeNil())
path := filepath.Join(networkConfDir, network1.Name+".json")
Expect(path).To(BeARegularFile())
grepInFile(path, `"mtu": "1500"`)
Expect(network1.Options).To(HaveKeyWithValue("mtu", "1500"))
Expect(network1.Options).To(HaveKeyWithValue("com.docker.network.driver.mtu", "1500"))
})

It("create network with invalid com.docker.network.driver.mtu option", func() {
network := types.Network{
Options: map[string]string{
"com.docker.network.driver.mtu": "abc",
},
}
_, err := libpodNet.NetworkCreate(network, nil)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(`parsing "abc": invalid syntax`))

network = types.Network{
Options: map[string]string{
types.MTUOption: "-1",
},
}
_, err = libpodNet.NetworkCreate(network, nil)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(`mtu -1 is less than zero`))
})

It("create network with vlan option", func() {
network := types.Network{
Options: map[string]string{
Expand Down

0 comments on commit 69e4db7

Please sign in to comment.