Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --if-not-exists option to podman network create #8987

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/podman/networks/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func networkCreateFlags(cmd *cobra.Command) {
flags.IPVar(&networkCreateOptions.Gateway, gatewayFlagName, nil, "IPv4 or IPv6 gateway for the subnet")
_ = cmd.RegisterFlagCompletionFunc(gatewayFlagName, completion.AutocompleteNone)

if !registry.IsRemote() {
flags.BoolVar(&networkCreateOptions.IfNotExists, "if-not-exists", false, "do not error if the network name already exists")
}
flags.BoolVar(&networkCreateOptions.Internal, "internal", false, "restrict external access from this network")

ipRangeFlagName := "ip-range"
Expand Down
6 changes: 6 additions & 0 deletions docs/source/markdown/podman-network-create.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ The `vlan` option assign VLAN tag and enables vlan\_filtering. Defaults to none.
Define a gateway for the subnet. If you want to provide a gateway address, you must also provide a
*subnet* option.

#### **--if-not-exists**

Do not error if a network with the given name already exists. Instead do nothing.

This option is not supported on the remote client.

#### **--internal**

Restrict external access of this network
Expand Down
10 changes: 7 additions & 3 deletions libpod/network/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/containernetworking/cni/pkg/version"
"github.com/containers/common/pkg/config"
"github.com/containers/podman/v2/libpod/define"
"github.com/containers/podman/v2/pkg/domain/entities"
"github.com/containers/podman/v2/pkg/rootless"
"github.com/containers/podman/v2/pkg/util"
Expand All @@ -33,7 +34,8 @@ func Create(name string, options entities.NetworkCreateOptions, runtimeConfig *c
} else {
fileName, err = createBridge(name, options, runtimeConfig)
}
if err != nil {
// do not error if IfNotExists is true and the error cause is ErrNetworkExists
if err != nil && !(options.IfNotExists && errors.Cause(err) == define.ErrNetworkExists) {
return nil, err
}
return &entities.NetworkCreateReport{Filename: fileName}, nil
Expand Down Expand Up @@ -207,7 +209,8 @@ func createBridge(name string, options entities.NetworkCreateOptions, runtimeCon
return "", err
}
if util.StringInSlice(name, netNames) {
return "", errors.Errorf("the network name %s is already used", name)
path := filepath.Join(GetCNIConfDir(runtimeConfig), fmt.Sprintf("%s.conflist", name))
return path, errors.Wrapf(define.ErrNetworkExists, "the network name %s is already used", name)
}
} else {
// If no name is given, we give the name of the bridge device
Expand Down Expand Up @@ -261,7 +264,8 @@ func createMacVLAN(name string, options entities.NetworkCreateOptions, runtimeCo
return "", err
}
if util.StringInSlice(name, netNames) {
return "", errors.Errorf("the network name %s is already used", name)
path := filepath.Join(GetCNIConfDir(runtimeConfig), fmt.Sprintf("%s.conflist", name))
return path, errors.Wrapf(define.ErrNetworkExists, "the network name %s is already used", name)
}
} else {
name, err = GetFreeDeviceName(runtimeConfig)
Expand Down
14 changes: 8 additions & 6 deletions pkg/domain/entities/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ type NetworkCreateOptions struct {
DisableDNS bool
Driver string
Gateway net.IP
Internal bool
Labels map[string]string
MacVLAN string
Range net.IPNet
Subnet net.IPNet
IPv6 bool
// Do not error if the network name is already used.
IfNotExists bool
Internal bool
Labels map[string]string
MacVLAN string
Range net.IPNet
Subnet net.IPNet
IPv6 bool
// Mapping of driver options and values.
Options map[string]string
}
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/network_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
cniversion "github.com/containernetworking/cni/pkg/version"
"github.com/containers/podman/v2/libpod/network"
. "github.com/containers/podman/v2/test/utils"
"github.com/containers/storage/pkg/stringid"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
Expand Down Expand Up @@ -365,4 +366,19 @@ var _ = Describe("Podman network create", func() {
Expect(nc).To(ExitWithError())
})

It("podman network create with if not exists", func() {
SkipIfRemote("--if-not-exists is not supported on remote")
net := stringid.GenerateNonCryptoID()
nc := podmanTest.Podman([]string{"network", "create", "--if-not-exists", net})
nc.WaitWithDefaultTimeout()
defer podmanTest.removeCNINetwork(net)
Expect(nc.ExitCode()).To(BeZero())
Expect(nc.OutputToString()).To(ContainSubstring(net))

nc = podmanTest.Podman([]string{"network", "create", "--if-not-exists", net})
nc.WaitWithDefaultTimeout()
Expect(nc.ExitCode()).To(BeZero())
Expect(nc.OutputToString()).To(ContainSubstring(net))
})

})