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

     1  package generate
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/containers/podman/v2/libpod"
     7  	"github.com/containers/podman/v2/pkg/specgen"
     8  	"github.com/pkg/errors"
     9  	"github.com/sirupsen/logrus"
    10  )
    11  
    12  func MakePod(p *specgen.PodSpecGenerator, rt *libpod.Runtime) (*libpod.Pod, error) {
    13  	if err := p.Validate(); err != nil {
    14  		return nil, err
    15  	}
    16  	options, err := createPodOptions(p, rt)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  	return rt.NewPod(context.Background(), options...)
    21  }
    22  
    23  func createPodOptions(p *specgen.PodSpecGenerator, rt *libpod.Runtime) ([]libpod.PodCreateOption, error) {
    24  	var (
    25  		options []libpod.PodCreateOption
    26  	)
    27  	if !p.NoInfra {
    28  		options = append(options, libpod.WithInfraContainer())
    29  		nsOptions, err := GetNamespaceOptions(p.SharedNamespaces)
    30  		if err != nil {
    31  			return nil, err
    32  		}
    33  		options = append(options, nsOptions...)
    34  
    35  		// Make our exit command
    36  		storageConfig := rt.StorageConfig()
    37  		runtimeConfig, err := rt.GetConfig()
    38  		if err != nil {
    39  			return nil, err
    40  		}
    41  		exitCommand, err := CreateExitCommandArgs(storageConfig, runtimeConfig, logrus.IsLevelEnabled(logrus.DebugLevel), false, false)
    42  		if err != nil {
    43  			return nil, errors.Wrapf(err, "error creating infra container exit command")
    44  		}
    45  		options = append(options, libpod.WithPodInfraExitCommand(exitCommand))
    46  	}
    47  	if len(p.CgroupParent) > 0 {
    48  		options = append(options, libpod.WithPodCgroupParent(p.CgroupParent))
    49  	}
    50  	if len(p.Labels) > 0 {
    51  		options = append(options, libpod.WithPodLabels(p.Labels))
    52  	}
    53  	if len(p.Name) > 0 {
    54  		options = append(options, libpod.WithPodName(p.Name))
    55  	}
    56  	if len(p.Hostname) > 0 {
    57  		options = append(options, libpod.WithPodHostname(p.Hostname))
    58  	}
    59  	if len(p.HostAdd) > 0 {
    60  		options = append(options, libpod.WithPodHosts(p.HostAdd))
    61  	}
    62  	if len(p.DNSServer) > 0 {
    63  		var dnsServers []string
    64  		for _, d := range p.DNSServer {
    65  			dnsServers = append(dnsServers, d.String())
    66  		}
    67  		options = append(options, libpod.WithPodDNS(dnsServers))
    68  	}
    69  	if len(p.DNSOption) > 0 {
    70  		options = append(options, libpod.WithPodDNSOption(p.DNSOption))
    71  	}
    72  	if len(p.DNSSearch) > 0 {
    73  		options = append(options, libpod.WithPodDNSSearch(p.DNSSearch))
    74  	}
    75  	if p.StaticIP != nil {
    76  		options = append(options, libpod.WithPodStaticIP(*p.StaticIP))
    77  	}
    78  	if p.StaticMAC != nil {
    79  		options = append(options, libpod.WithPodStaticMAC(*p.StaticMAC))
    80  	}
    81  	if p.NoManageResolvConf {
    82  		options = append(options, libpod.WithPodUseImageResolvConf())
    83  	}
    84  	if len(p.CNINetworks) > 0 {
    85  		options = append(options, libpod.WithPodNetworks(p.CNINetworks))
    86  	}
    87  
    88  	if len(p.InfraImage) > 0 {
    89  		options = append(options, libpod.WithInfraImage(p.InfraImage))
    90  	}
    91  
    92  	if len(p.InfraCommand) > 0 {
    93  		options = append(options, libpod.WithInfraCommand(p.InfraCommand))
    94  	}
    95  
    96  	switch p.NetNS.NSMode {
    97  	case specgen.Bridge, specgen.Default, "":
    98  		logrus.Debugf("Pod using default network mode")
    99  	case specgen.Host:
   100  		logrus.Debugf("Pod will use host networking")
   101  		options = append(options, libpod.WithPodHostNetwork())
   102  	case specgen.Slirp:
   103  		logrus.Debugf("Pod will use slirp4netns")
   104  		options = append(options, libpod.WithPodSlirp4netns(p.NetworkOptions))
   105  	default:
   106  		return nil, errors.Errorf("pods presently do not support network mode %s", p.NetNS.NSMode)
   107  	}
   108  
   109  	if p.NoManageHosts {
   110  		options = append(options, libpod.WithPodUseImageHosts())
   111  	}
   112  	if len(p.PortMappings) > 0 {
   113  		ports, _, _, err := parsePortMapping(p.PortMappings)
   114  		if err != nil {
   115  			return nil, err
   116  		}
   117  		options = append(options, libpod.WithInfraContainerPorts(ports))
   118  	}
   119  	options = append(options, libpod.WithPodCgroups())
   120  	if p.PodCreateCommand != nil {
   121  		options = append(options, libpod.WithPodCreateCommand(p.PodCreateCommand))
   122  	}
   123  	if len(p.InfraConmonPidFile) > 0 {
   124  		options = append(options, libpod.WithInfraConmonPidFile(p.InfraConmonPidFile))
   125  	}
   126  	return options, nil
   127  }