diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2020-05-08 16:35:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-08 16:35:33 +0200 |
commit | d3826d6eb3ec38116ee40b5081dcddab39bd3493 (patch) | |
tree | b469f7f41c6b84327f64ec7ff5a1dc8a171388d0 | |
parent | f4e3ec5fd69a6cc831b63619c5772dbf5c6a11a6 (diff) | |
parent | 2d68dc776d80cc5b71c609b8ddaf2069c97193e7 (diff) | |
download | podman-d3826d6eb3ec38116ee40b5081dcddab39bd3493.tar.gz podman-d3826d6eb3ec38116ee40b5081dcddab39bd3493.tar.bz2 podman-d3826d6eb3ec38116ee40b5081dcddab39bd3493.zip |
Merge pull request #6124 from mheon/fix_rootless_podcreate
Fix parsing of --network for `podman pod create`
-rw-r--r-- | cmd/podman/pods/create.go | 47 | ||||
-rw-r--r-- | pkg/specgen/pod_validate.go | 6 | ||||
-rw-r--r-- | pkg/specgen/podspecgen.go | 2 |
3 files changed, 26 insertions, 29 deletions
diff --git a/cmd/podman/pods/create.go b/cmd/podman/pods/create.go index 0a2016496..f97fa836a 100644 --- a/cmd/podman/pods/create.go +++ b/cmd/podman/pods/create.go @@ -17,6 +17,7 @@ import ( "github.com/containers/libpod/pkg/util" "github.com/pkg/errors" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) var ( @@ -59,6 +60,14 @@ func init() { flags.StringVarP(&createOptions.Hostname, "hostname", "", "", "Set a hostname to the pod") flags.StringVar(&podIDFile, "pod-id-file", "", "Write the pod ID to the file") flags.StringVar(&share, "share", createconfig.DefaultKernelNamespaces, "A comma delimited list of kernel namespaces the pod will share") + flags.SetNormalizeFunc(aliasNetworkFlag) +} + +func aliasNetworkFlag(_ *pflag.FlagSet, name string) pflag.NormalizedName { + if name == "net" { + name = "network" + } + return pflag.NormalizedName(name) } func create(cmd *cobra.Command, args []string) error { @@ -105,29 +114,21 @@ func create(cmd *cobra.Command, args []string) error { if err != nil { return err } - netInput, err := cmd.Flags().GetString("network") - if err != nil { - return err - } - n := specgen.Namespace{} - switch netInput { - case "bridge": - n.NSMode = specgen.Bridge - case "host": - n.NSMode = specgen.Host - case "slip4netns": - n.NSMode = specgen.Slirp - default: - if strings.HasPrefix(netInput, "container:") { // nolint - split := strings.Split(netInput, ":") - if len(split) != 2 { - return errors.Errorf("invalid network paramater: %q", netInput) - } - n.NSMode = specgen.FromContainer - n.Value = split[1] - } else if strings.HasPrefix(netInput, "ns:") { - return errors.New("the ns: network option is not supported for pods") - } else { + if cmd.Flag("network").Changed { + netInput, err := cmd.Flags().GetString("network") + if err != nil { + return err + } + n := specgen.Namespace{} + switch netInput { + case "bridge": + n.NSMode = specgen.Bridge + case "host": + n.NSMode = specgen.Host + case "slirp4netns": + n.NSMode = specgen.Slirp + default: + // Container and NS mode are presently unsupported n.NSMode = specgen.Bridge createOptions.Net.CNINetworks = strings.Split(netInput, ",") } diff --git a/pkg/specgen/pod_validate.go b/pkg/specgen/pod_validate.go index 98d59549e..08f1c0300 100644 --- a/pkg/specgen/pod_validate.go +++ b/pkg/specgen/pod_validate.go @@ -33,7 +33,7 @@ func (p *PodSpecGenerator) Validate() error { } // PodNetworkConfig - if err := p.NetNS.validate(); err != nil { + if err := validateNetNS(&p.NetNS); err != nil { return err } if p.NoInfra { @@ -85,10 +85,6 @@ func (p *PodSpecGenerator) Validate() error { return exclusivePodOptions("NoManageHosts", "HostAdd") } - if err := p.NetNS.validate(); err != nil { - return err - } - // Set Defaults if p.NetNS.Value == "" { if rootless.IsRootless() { diff --git a/pkg/specgen/podspecgen.go b/pkg/specgen/podspecgen.go index 682f3f215..11976233a 100644 --- a/pkg/specgen/podspecgen.go +++ b/pkg/specgen/podspecgen.go @@ -54,7 +54,7 @@ type PodNetworkConfig struct { // namespace. This network will, by default, be shared with all // containers in the pod. // Cannot be set to FromContainer and FromPod. - // Setting this to anything except "" conflicts with NoInfra=true. + // Setting this to anything except default conflicts with NoInfra=true. // Defaults to Bridge as root and Slirp as rootless. // Mandatory. NetNS Namespace `json:"netns,omitempty"` |