github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/specgen/pod_validate.go (about)

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