mirror of https://github.com/containers/podman.git
Merge pull request #5875 from rhatdan/network
Cleanup network option parsing
This commit is contained in:
commit
8884f6a4f3
|
|
@ -1,7 +1,6 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/containers/libpod/cmd/podman/parse"
|
|
||||||
"github.com/containers/libpod/pkg/util"
|
"github.com/containers/libpod/pkg/util"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
@ -17,27 +16,7 @@ func (c *ContainerCLIOpts) validate() error {
|
||||||
if _, err := util.ValidatePullType(c.Pull); err != nil {
|
if _, err := util.ValidatePullType(c.Pull); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Verify the additional hosts are in correct format
|
|
||||||
for _, host := range c.Net.AddHosts {
|
|
||||||
if _, err := parse.ValidateExtraHost(host); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if dnsSearches := c.Net.DNSSearch; len(dnsSearches) > 0 {
|
|
||||||
// Validate domains are good
|
|
||||||
for _, dom := range dnsSearches {
|
|
||||||
if dom == "." {
|
|
||||||
if len(dnsSearches) > 1 {
|
|
||||||
return errors.Errorf("cannot pass additional search domains when also specifying '.'")
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if _, err := parse.ValidateDomain(dom); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var imageVolType = map[string]string{
|
var imageVolType = map[string]string{
|
||||||
"bind": "",
|
"bind": "",
|
||||||
"tmpfs": "",
|
"tmpfs": "",
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,9 @@ package common
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
"github.com/containers/libpod/cmd/podman/parse"
|
||||||
"github.com/containers/libpod/pkg/domain/entities"
|
"github.com/containers/libpod/pkg/domain/entities"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
|
@ -58,20 +60,60 @@ func NetFlagsToNetOptions(cmd *cobra.Command) (*entities.NetOptions, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
servers, err := cmd.Flags().GetStringSlice("dns")
|
// Verify the additional hosts are in correct format
|
||||||
if err != nil {
|
for _, host := range opts.AddHosts {
|
||||||
return nil, err
|
if _, err := parse.ValidateExtraHost(host); err != nil {
|
||||||
}
|
return nil, err
|
||||||
for _, d := range servers {
|
|
||||||
if d == "none" {
|
|
||||||
opts.DNSHost = true
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
opts.DNSServers = append(opts.DNSServers, net.ParseIP(d))
|
|
||||||
}
|
}
|
||||||
opts.DNSSearch, err = cmd.Flags().GetStringSlice("dns-search")
|
|
||||||
if err != nil {
|
if cmd.Flags().Changed("dns") {
|
||||||
return nil, err
|
servers, err := cmd.Flags().GetStringSlice("dns")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, d := range servers {
|
||||||
|
if d == "none" {
|
||||||
|
opts.UseImageResolvConf = true
|
||||||
|
if len(servers) > 1 {
|
||||||
|
return nil, errors.Errorf("%s is not allowed to be specified with other DNS ip addresses", d)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
dns := net.ParseIP(d)
|
||||||
|
if dns == nil {
|
||||||
|
return nil, errors.Errorf("%s is not an ip address", d)
|
||||||
|
}
|
||||||
|
opts.DNSServers = append(opts.DNSServers, dns)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cmd.Flags().Changed("dns-opt") {
|
||||||
|
options, err := cmd.Flags().GetStringSlice("dns-opt")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
opts.DNSOptions = options
|
||||||
|
}
|
||||||
|
|
||||||
|
if cmd.Flags().Changed("dns-search") {
|
||||||
|
dnsSearches, err := cmd.Flags().GetStringSlice("dns-search")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// Validate domains are good
|
||||||
|
for _, dom := range dnsSearches {
|
||||||
|
if dom == "." {
|
||||||
|
if len(dnsSearches) > 1 {
|
||||||
|
return nil, errors.Errorf("cannot pass additional search domains when also specifying '.'")
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, err := parse.ValidateDomain(dom); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
opts.DNSSearch = dnsSearches
|
||||||
}
|
}
|
||||||
|
|
||||||
m, err := cmd.Flags().GetString("mac-address")
|
m, err := cmd.Flags().GetString("mac-address")
|
||||||
|
|
@ -85,6 +127,7 @@ func NetFlagsToNetOptions(cmd *cobra.Command) (*entities.NetOptions, error) {
|
||||||
}
|
}
|
||||||
opts.StaticMAC = &mac
|
opts.StaticMAC = &mac
|
||||||
}
|
}
|
||||||
|
|
||||||
inputPorts, err := cmd.Flags().GetStringSlice("publish")
|
inputPorts, err := cmd.Flags().GetStringSlice("publish")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -95,6 +138,31 @@ func NetFlagsToNetOptions(cmd *cobra.Command) (*entities.NetOptions, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ip, err := cmd.Flags().GetString("ip")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if ip != "" {
|
||||||
|
staticIP := net.ParseIP(ip)
|
||||||
|
if staticIP == nil {
|
||||||
|
return nil, errors.Errorf("%s is not an ip address", ip)
|
||||||
|
}
|
||||||
|
opts.StaticIP = &staticIP
|
||||||
|
}
|
||||||
|
|
||||||
opts.NoHosts, err = cmd.Flags().GetBool("no-hosts")
|
opts.NoHosts, err = cmd.Flags().GetBool("no-hosts")
|
||||||
|
|
||||||
|
if cmd.Flags().Changed("network") {
|
||||||
|
network, err := cmd.Flags().GetString("network")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.Errorf("network %s is not yet supported", network)
|
||||||
|
// TODO How do I convert a string network to a Specgen.Namespace?
|
||||||
|
// opts.Network = specgen.Namespace{NSMode: network}
|
||||||
|
}
|
||||||
|
|
||||||
return &opts, err
|
return &opts, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -292,9 +292,12 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string
|
||||||
}
|
}
|
||||||
s.ShmSize = &shmSize
|
s.ShmSize = &shmSize
|
||||||
s.HostAdd = c.Net.AddHosts
|
s.HostAdd = c.Net.AddHosts
|
||||||
s.DNSServer = c.Net.DNSServers
|
s.UseImageResolvConf = c.Net.UseImageResolvConf
|
||||||
|
s.DNSServers = c.Net.DNSServers
|
||||||
s.DNSSearch = c.Net.DNSSearch
|
s.DNSSearch = c.Net.DNSSearch
|
||||||
s.DNSOption = c.Net.DNSOptions
|
s.DNSOptions = c.Net.DNSOptions
|
||||||
|
s.StaticIP = c.Net.StaticIP
|
||||||
|
s.StaticMAC = c.Net.StaticMAC
|
||||||
|
|
||||||
// deferred, must be added on libpod side
|
// deferred, must be added on libpod side
|
||||||
//var ImageVolumes map[string]struct{}
|
//var ImageVolumes map[string]struct{}
|
||||||
|
|
|
||||||
|
|
@ -134,7 +134,7 @@ func (p PodCreateOptions) ToPodSpecGen(s *specgen.PodSpecGenerator) {
|
||||||
s.StaticMAC = p.Net.StaticMAC
|
s.StaticMAC = p.Net.StaticMAC
|
||||||
s.PortMappings = p.Net.PublishPorts
|
s.PortMappings = p.Net.PublishPorts
|
||||||
s.CNINetworks = p.Net.CNINetworks
|
s.CNINetworks = p.Net.CNINetworks
|
||||||
if p.Net.DNSHost {
|
if p.Net.UseImageResolvConf {
|
||||||
s.NoManageResolvConf = true
|
s.NoManageResolvConf = true
|
||||||
}
|
}
|
||||||
s.DNSServer = p.Net.DNSServers
|
s.DNSServer = p.Net.DNSServers
|
||||||
|
|
|
||||||
|
|
@ -32,17 +32,17 @@ type VolumeDeleteReport struct{ Report }
|
||||||
// NetOptions reflect the shared network options between
|
// NetOptions reflect the shared network options between
|
||||||
// pods and containers
|
// pods and containers
|
||||||
type NetOptions struct {
|
type NetOptions struct {
|
||||||
AddHosts []string
|
AddHosts []string
|
||||||
CNINetworks []string
|
CNINetworks []string
|
||||||
DNSHost bool
|
UseImageResolvConf bool
|
||||||
DNSOptions []string
|
DNSOptions []string
|
||||||
DNSSearch []string
|
DNSSearch []string
|
||||||
DNSServers []net.IP
|
DNSServers []net.IP
|
||||||
Network specgen.Namespace
|
Network specgen.Namespace
|
||||||
NoHosts bool
|
NoHosts bool
|
||||||
PublishPorts []ocicni.PortMapping
|
PublishPorts []ocicni.PortMapping
|
||||||
StaticIP *net.IP
|
StaticIP *net.IP
|
||||||
StaticMAC *net.HardwareAddr
|
StaticMAC *net.HardwareAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
// All CLI inspect commands and inspect sub-commands use the same options
|
// All CLI inspect commands and inspect sub-commands use the same options
|
||||||
|
|
|
||||||
|
|
@ -91,13 +91,13 @@ func (s *SpecGenerator) Validate() error {
|
||||||
}
|
}
|
||||||
// useimageresolveconf conflicts with dnsserver, dnssearch, dnsoption
|
// useimageresolveconf conflicts with dnsserver, dnssearch, dnsoption
|
||||||
if s.UseImageResolvConf {
|
if s.UseImageResolvConf {
|
||||||
if len(s.DNSServer) > 0 {
|
if len(s.DNSServers) > 0 {
|
||||||
return exclusiveOptions("UseImageResolvConf", "DNSServer")
|
return exclusiveOptions("UseImageResolvConf", "DNSServer")
|
||||||
}
|
}
|
||||||
if len(s.DNSSearch) > 0 {
|
if len(s.DNSSearch) > 0 {
|
||||||
return exclusiveOptions("UseImageResolvConf", "DNSSearch")
|
return exclusiveOptions("UseImageResolvConf", "DNSSearch")
|
||||||
}
|
}
|
||||||
if len(s.DNSOption) > 0 {
|
if len(s.DNSOptions) > 0 {
|
||||||
return exclusiveOptions("UseImageResolvConf", "DNSOption")
|
return exclusiveOptions("UseImageResolvConf", "DNSOption")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -145,20 +145,19 @@ func GenerateNamespaceContainerOpts(s *specgen.SpecGenerator, rt *libpod.Runtime
|
||||||
if len(s.DNSSearch) > 0 {
|
if len(s.DNSSearch) > 0 {
|
||||||
options = append(options, libpod.WithDNSSearch(s.DNSSearch))
|
options = append(options, libpod.WithDNSSearch(s.DNSSearch))
|
||||||
}
|
}
|
||||||
if len(s.DNSServer) > 0 {
|
|
||||||
// TODO I'm not sure how we are going to handle this given the input
|
if s.UseImageResolvConf {
|
||||||
if len(s.DNSServer) == 1 { //&& strings.ToLower(s.DNSServer[0].) == "none" {
|
options = append(options, libpod.WithUseImageResolvConf())
|
||||||
options = append(options, libpod.WithUseImageResolvConf())
|
} else {
|
||||||
} else {
|
var dnsServers []string
|
||||||
var dnsServers []string
|
for _, d := range s.DNSServers {
|
||||||
for _, d := range s.DNSServer {
|
dnsServers = append(dnsServers, d.String())
|
||||||
dnsServers = append(dnsServers, d.String())
|
|
||||||
}
|
|
||||||
options = append(options, libpod.WithDNS(dnsServers))
|
|
||||||
}
|
}
|
||||||
|
options = append(options, libpod.WithDNS(dnsServers))
|
||||||
}
|
}
|
||||||
if len(s.DNSOption) > 0 {
|
|
||||||
options = append(options, libpod.WithDNSOption(s.DNSOption))
|
if len(s.DNSOptions) > 0 {
|
||||||
|
options = append(options, libpod.WithDNSOption(s.DNSOptions))
|
||||||
}
|
}
|
||||||
if s.StaticIP != nil {
|
if s.StaticIP != nil {
|
||||||
options = append(options, libpod.WithStaticIP(*s.StaticIP))
|
options = append(options, libpod.WithStaticIP(*s.StaticIP))
|
||||||
|
|
|
||||||
|
|
@ -319,24 +319,24 @@ type ContainerNetworkConfig struct {
|
||||||
// by Podman, but instead sourced from the image.
|
// by Podman, but instead sourced from the image.
|
||||||
// Conflicts with DNSServer, DNSSearch, DNSOption.
|
// Conflicts with DNSServer, DNSSearch, DNSOption.
|
||||||
UseImageResolvConf bool `json:"use_image_resolve_conf,omitempty"`
|
UseImageResolvConf bool `json:"use_image_resolve_conf,omitempty"`
|
||||||
// DNSServer is a set of DNS servers that will be used in the
|
// DNSServers is a set of DNS servers that will be used in the
|
||||||
// container's resolv.conf, replacing the host's DNS Servers which are
|
// container's resolv.conf, replacing the host's DNS Servers which are
|
||||||
// used by default.
|
// used by default.
|
||||||
// Conflicts with UseImageResolvConf.
|
// Conflicts with UseImageResolvConf.
|
||||||
// Optional.
|
// Optional.
|
||||||
DNSServer []net.IP `json:"dns_server,omitempty"`
|
DNSServers []net.IP `json:"dns_server,omitempty"`
|
||||||
// DNSSearch is a set of DNS search domains that will be used in the
|
// DNSSearch is a set of DNS search domains that will be used in the
|
||||||
// container's resolv.conf, replacing the host's DNS search domains
|
// container's resolv.conf, replacing the host's DNS search domains
|
||||||
// which are used by default.
|
// which are used by default.
|
||||||
// Conflicts with UseImageResolvConf.
|
// Conflicts with UseImageResolvConf.
|
||||||
// Optional.
|
// Optional.
|
||||||
DNSSearch []string `json:"dns_search,omitempty"`
|
DNSSearch []string `json:"dns_search,omitempty"`
|
||||||
// DNSOption is a set of DNS options that will be used in the
|
// DNSOptions is a set of DNS options that will be used in the
|
||||||
// container's resolv.conf, replacing the host's DNS options which are
|
// container's resolv.conf, replacing the host's DNS options which are
|
||||||
// used by default.
|
// used by default.
|
||||||
// Conflicts with UseImageResolvConf.
|
// Conflicts with UseImageResolvConf.
|
||||||
// Optional.
|
// Optional.
|
||||||
DNSOption []string `json:"dns_option,omitempty"`
|
DNSOptions []string `json:"dns_option,omitempty"`
|
||||||
// UseImageHosts indicates that /etc/hosts should not be managed by
|
// UseImageHosts indicates that /etc/hosts should not be managed by
|
||||||
// Podman, and instead sourced from the image.
|
// Podman, and instead sourced from the image.
|
||||||
// Conflicts with HostAdd.
|
// Conflicts with HostAdd.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue