github.com/containers/podman/v4@v4.9.4/pkg/specgen/pod_validate.go (about)

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