github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/specgen/pod_validate.go (about)

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