summaryrefslogtreecommitdiff
path: root/cmd/podman/networks/create.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/podman/networks/create.go')
-rw-r--r--cmd/podman/networks/create.go60
1 files changed, 51 insertions, 9 deletions
diff --git a/cmd/podman/networks/create.go b/cmd/podman/networks/create.go
index b5ddd215f..21d720f55 100644
--- a/cmd/podman/networks/create.go
+++ b/cmd/podman/networks/create.go
@@ -8,7 +8,8 @@ import (
"github.com/containers/podman/v3/cmd/podman/common"
"github.com/containers/podman/v3/cmd/podman/parse"
"github.com/containers/podman/v3/cmd/podman/registry"
- "github.com/containers/podman/v3/libpod/define"
+ "github.com/containers/podman/v3/libpod/network/types"
+ "github.com/containers/podman/v3/libpod/network/util"
"github.com/containers/podman/v3/pkg/domain/entities"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
@@ -38,11 +39,11 @@ func networkCreateFlags(cmd *cobra.Command) {
flags := cmd.Flags()
driverFlagName := "driver"
- flags.StringVarP(&networkCreateOptions.Driver, driverFlagName, "d", "bridge", "driver to manage the network")
+ flags.StringVarP(&networkCreateOptions.Driver, driverFlagName, "d", types.DefaultNetworkDriver, "driver to manage the network")
_ = cmd.RegisterFlagCompletionFunc(driverFlagName, common.AutocompleteNetworkDriver)
optFlagName := "opt"
- flags.StringArrayVarP(&opts, optFlagName, "o", []string{}, "Set driver specific options (default [])")
+ flags.StringArrayVarP(&opts, optFlagName, "o", nil, "Set driver specific options (default [])")
_ = cmd.RegisterFlagCompletionFunc(optFlagName, completion.AutocompleteNone)
gatewayFlagName := "gateway"
@@ -55,6 +56,7 @@ func networkCreateFlags(cmd *cobra.Command) {
flags.IPNetVar(&networkCreateOptions.Range, ipRangeFlagName, net.IPNet{}, "allocate container IP from range")
_ = cmd.RegisterFlagCompletionFunc(ipRangeFlagName, completion.AutocompleteNone)
+ // TODO consider removing this for 4.0
macvlanFlagName := "macvlan"
flags.StringVar(&networkCreateOptions.MacVLAN, macvlanFlagName, "", "create a Macvlan connection based on this device")
// This option is deprecated
@@ -88,9 +90,6 @@ func networkCreate(cmd *cobra.Command, args []string) error {
name string
)
if len(args) > 0 {
- if !define.NameRegex.MatchString(args[0]) {
- return define.RegexError
- }
name = args[0]
}
var err error
@@ -100,17 +99,60 @@ func networkCreate(cmd *cobra.Command, args []string) error {
}
networkCreateOptions.Options, err = parse.GetAllLabels([]string{}, opts)
if err != nil {
- return errors.Wrapf(err, "unable to process options")
+ return errors.Wrapf(err, "unable to parse options")
+ }
+
+ network := types.Network{
+ Name: name,
+ Driver: networkCreateOptions.Driver,
+ Options: networkCreateOptions.Options,
+ Labels: networkCreateOptions.Labels,
+ IPv6Enabled: networkCreateOptions.IPv6,
+ DNSEnabled: !networkCreateOptions.DisableDNS,
+ Internal: networkCreateOptions.Internal,
}
+ // old --macvlan option
if networkCreateOptions.MacVLAN != "" {
logrus.Warn("The --macvlan option is deprecated, use `--driver macvlan --opt parent=<device>` instead")
+ network.Driver = types.MacVLANNetworkDriver
+ network.NetworkInterface = networkCreateOptions.MacVLAN
+ } else if networkCreateOptions.Driver == types.MacVLANNetworkDriver {
+ // new -d macvlan --opt parent=... syntax
+ if parent, ok := network.Options["parent"]; ok {
+ network.NetworkInterface = parent
+ delete(network.Options, "parent")
+ }
+ }
+
+ if networkCreateOptions.Subnet.IP != nil {
+ s := types.Subnet{
+ Subnet: types.IPNet{IPNet: networkCreateOptions.Subnet},
+ Gateway: networkCreateOptions.Gateway,
+ }
+ if networkCreateOptions.Range.IP != nil {
+ startIP, err := util.FirstIPInSubnet(&networkCreateOptions.Range)
+ if err != nil {
+ return errors.Wrap(err, "failed to get first ip in range")
+ }
+ lastIP, err := util.LastIPInSubnet(&networkCreateOptions.Range)
+ if err != nil {
+ return errors.Wrap(err, "failed to get last ip in range")
+ }
+ s.LeaseRange = &types.LeaseRange{
+ StartIP: startIP,
+ EndIP: lastIP,
+ }
+ }
+ network.Subnets = append(network.Subnets, s)
+ } else if networkCreateOptions.Range.IP != nil || networkCreateOptions.Gateway != nil {
+ return errors.New("cannot set gateway or range without subnet")
}
- response, err := registry.ContainerEngine().NetworkCreate(registry.Context(), name, networkCreateOptions)
+ response, err := registry.ContainerEngine().NetworkCreate(registry.Context(), network)
if err != nil {
return err
}
- fmt.Println(response.Filename)
+ fmt.Println(response.Name)
return nil
}