github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/specgen/pod_validate.go (about)

     1  package specgen
     2  
     3  import (
     4  	"github.com/containers/podman/v2/pkg/rootless"
     5  	"github.com/containers/podman/v2/pkg/util"
     6  	"github.com/pkg/errors"
     7  )
     8  
     9  var (
    10  	// ErrInvalidPodSpecConfig describes an error given when the podspecgenerator is invalid
    11  	ErrInvalidPodSpecConfig = errors.New("invalid pod spec")
    12  	// containerConfig has the default configurations defined in containers.conf
    13  	containerConfig = util.DefaultContainerConfig()
    14  )
    15  
    16  func exclusivePodOptions(opt1, opt2 string) error {
    17  	return errors.Wrapf(ErrInvalidPodSpecConfig, "%s and %s are mutually exclusive pod options", opt1, opt2)
    18  }
    19  
    20  // Validate verifies the input is valid
    21  func (p *PodSpecGenerator) Validate() error {
    22  
    23  	if rootless.IsRootless() {
    24  		if p.StaticIP != nil {
    25  			return ErrNoStaticIPRootless
    26  		}
    27  		if p.StaticMAC != nil {
    28  			return ErrNoStaticMACRootless
    29  		}
    30  	}
    31  
    32  	// PodBasicConfig
    33  	if p.NoInfra {
    34  		if len(p.InfraCommand) > 0 {
    35  			return exclusivePodOptions("NoInfra", "InfraCommand")
    36  		}
    37  		if len(p.InfraImage) > 0 {
    38  			return exclusivePodOptions("NoInfra", "InfraImage")
    39  		}
    40  		if len(p.SharedNamespaces) > 0 {
    41  			return exclusivePodOptions("NoInfra", "SharedNamespaces")
    42  		}
    43  	}
    44  
    45  	// PodNetworkConfig
    46  	if err := validateNetNS(&p.NetNS); err != nil {
    47  		return err
    48  	}
    49  	if p.NoInfra {
    50  		if p.NetNS.NSMode != Default && p.NetNS.NSMode != "" {
    51  			return errors.New("NoInfra and network modes cannot be used toegther")
    52  		}
    53  		if p.StaticIP != nil {
    54  			return exclusivePodOptions("NoInfra", "StaticIP")
    55  		}
    56  		if p.StaticMAC != nil {
    57  			return exclusivePodOptions("NoInfra", "StaticMAC")
    58  		}
    59  		if len(p.DNSOption) > 0 {
    60  			return exclusivePodOptions("NoInfra", "DNSOption")
    61  		}
    62  		if len(p.DNSSearch) > 0 {
    63  			return exclusivePodOptions("NoInfo", "DNSSearch")
    64  		}
    65  		if len(p.DNSServer) > 0 {
    66  			return exclusivePodOptions("NoInfra", "DNSServer")
    67  		}
    68  		if len(p.HostAdd) > 0 {
    69  			return exclusivePodOptions("NoInfra", "HostAdd")
    70  		}
    71  		if p.NoManageResolvConf {
    72  			return exclusivePodOptions("NoInfra", "NoManageResolvConf")
    73  		}
    74  	}
    75  	if p.NetNS.NSMode != "" && p.NetNS.NSMode != Bridge && p.NetNS.NSMode != Slirp && p.NetNS.NSMode != Default {
    76  		if len(p.PortMappings) > 0 {
    77  			return errors.New("PortMappings can only be used with Bridge or slirp4netns networking")
    78  		}
    79  		if len(p.CNINetworks) > 0 {
    80  			return errors.New("CNINetworks can only be used with Bridge mode networking")
    81  		}
    82  	}
    83  	if p.NoManageResolvConf {
    84  		if len(p.DNSServer) > 0 {
    85  			return exclusivePodOptions("NoManageResolvConf", "DNSServer")
    86  		}
    87  		if len(p.DNSSearch) > 0 {
    88  			return exclusivePodOptions("NoManageResolvConf", "DNSSearch")
    89  		}
    90  		if len(p.DNSOption) > 0 {
    91  			return exclusivePodOptions("NoManageResolvConf", "DNSOption")
    92  		}
    93  	}
    94  	if p.NoManageHosts && len(p.HostAdd) > 0 {
    95  		return exclusivePodOptions("NoManageHosts", "HostAdd")
    96  	}
    97  
    98  	return nil
    99  }