Files
podman/pkg/specgen/pod_validate.go
Paul Holzinger 535818414c support advanced network configuration via cli
Rework the --network parse logic to support multiple networks with
specific network configuration settings.
--network can now be set multiple times. For bridge network mode the
following options have been added:
  - **alias=name**: Add network-scoped alias for the container.
  - **ip=IPv4**: Specify a static ipv4 address for this container.
  - **ip=IPv6**: Specify a static ipv6 address for this container.
  - **mac=MAC**: Specify a static mac address address for this container.
  - **interface_name**: Specify a name for the created network interface inside the container.

So now you can set --network bridge:ip=10.88.0.10,mac=44:33:22:11:00:99
for the default bridge network as well as for network names.
This is better than using --ip because we can set the ip per network
without any confusion which network the ip address should be assigned
to.
The --ip, --mac-address and --network-alias options are still supported
but --ip or --mac-address can only be set when only one network is set.
This limitation already existed previously.

The ability to specify a custom network interface name is new
Fixes #11534

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2021-12-14 15:23:39 +01:00

91 lines
2.7 KiB
Go

package specgen
import (
"github.com/containers/podman/v3/pkg/util"
"github.com/pkg/errors"
)
var (
// ErrInvalidPodSpecConfig describes an error given when the podspecgenerator is invalid
ErrInvalidPodSpecConfig = errors.New("invalid pod spec")
// containerConfig has the default configurations defined in containers.conf
containerConfig = util.DefaultContainerConfig()
)
func exclusivePodOptions(opt1, opt2 string) error {
return errors.Wrapf(ErrInvalidPodSpecConfig, "%s and %s are mutually exclusive pod options", opt1, opt2)
}
// Validate verifies the input is valid
func (p *PodSpecGenerator) Validate() error {
// PodBasicConfig
if p.NoInfra {
if len(p.InfraCommand) > 0 {
return exclusivePodOptions("NoInfra", "InfraCommand")
}
if len(p.InfraImage) > 0 {
return exclusivePodOptions("NoInfra", "InfraImage")
}
if len(p.InfraName) > 0 {
return exclusivePodOptions("NoInfra", "InfraName")
}
if len(p.SharedNamespaces) > 0 {
return exclusivePodOptions("NoInfra", "SharedNamespaces")
}
}
// PodNetworkConfig
if err := validateNetNS(&p.NetNS); err != nil {
return err
}
if p.NoInfra {
if p.NetNS.NSMode != Default && p.NetNS.NSMode != "" {
return errors.New("NoInfra and network modes cannot be used together")
}
// Note that networks might be set when --ip or --mac was set
// so we need to check that no networks are set without the infra
if len(p.Networks) > 0 {
return errors.New("cannot set networks options without infra container")
}
if len(p.DNSOption) > 0 {
return exclusivePodOptions("NoInfra", "DNSOption")
}
if len(p.DNSSearch) > 0 {
return exclusivePodOptions("NoInfo", "DNSSearch")
}
if len(p.DNSServer) > 0 {
return exclusivePodOptions("NoInfra", "DNSServer")
}
if len(p.HostAdd) > 0 {
return exclusivePodOptions("NoInfra", "HostAdd")
}
if p.NoManageResolvConf {
return exclusivePodOptions("NoInfra", "NoManageResolvConf")
}
}
if p.NetNS.NSMode != "" && p.NetNS.NSMode != Bridge && p.NetNS.NSMode != Slirp && p.NetNS.NSMode != Default {
if len(p.PortMappings) > 0 {
return errors.New("PortMappings can only be used with Bridge or slirp4netns networking")
}
if len(p.Networks) > 0 {
return errors.New("Networks can only be used with Bridge mode networking")
}
}
if p.NoManageResolvConf {
if len(p.DNSServer) > 0 {
return exclusivePodOptions("NoManageResolvConf", "DNSServer")
}
if len(p.DNSSearch) > 0 {
return exclusivePodOptions("NoManageResolvConf", "DNSSearch")
}
if len(p.DNSOption) > 0 {
return exclusivePodOptions("NoManageResolvConf", "DNSOption")
}
}
if p.NoManageHosts && len(p.HostAdd) > 0 {
return exclusivePodOptions("NoManageHosts", "HostAdd")
}
return nil
}