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

     1  package generate
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/containers/libpod/libpod"
     7  	"github.com/containers/libpod/pkg/specgen"
     8  	"github.com/sirupsen/logrus"
     9  )
    10  
    11  func MakePod(p *specgen.PodSpecGenerator, rt *libpod.Runtime) (*libpod.Pod, error) {
    12  	if err := p.Validate(); err != nil {
    13  		return nil, err
    14  	}
    15  	options, err := createPodOptions(p)
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  	return rt.NewPod(context.Background(), options...)
    20  }
    21  
    22  func createPodOptions(p *specgen.PodSpecGenerator) ([]libpod.PodCreateOption, error) {
    23  	var (
    24  		options []libpod.PodCreateOption
    25  	)
    26  	if !p.NoInfra {
    27  		options = append(options, libpod.WithInfraContainer())
    28  		nsOptions, err := GetNamespaceOptions(p.SharedNamespaces)
    29  		if err != nil {
    30  			return nil, err
    31  		}
    32  		options = append(options, nsOptions...)
    33  	}
    34  	if len(p.CgroupParent) > 0 {
    35  		options = append(options, libpod.WithPodCgroupParent(p.CgroupParent))
    36  	}
    37  	if len(p.Labels) > 0 {
    38  		options = append(options, libpod.WithPodLabels(p.Labels))
    39  	}
    40  	if len(p.Name) > 0 {
    41  		options = append(options, libpod.WithPodName(p.Name))
    42  	}
    43  	if len(p.Hostname) > 0 {
    44  		options = append(options, libpod.WithPodHostname(p.Hostname))
    45  	}
    46  	if len(p.HostAdd) > 0 {
    47  		options = append(options, libpod.WithPodHosts(p.HostAdd))
    48  	}
    49  	if len(p.DNSOption) > 0 {
    50  		options = append(options, libpod.WithPodDNSOption(p.DNSOption))
    51  	}
    52  	if len(p.DNSSearch) > 0 {
    53  		options = append(options, libpod.WithPodDNSSearch(p.DNSSearch))
    54  	}
    55  	if p.StaticIP != nil {
    56  		options = append(options, libpod.WithPodStaticIP(*p.StaticIP))
    57  	}
    58  	if p.StaticMAC != nil {
    59  		options = append(options, libpod.WithPodStaticMAC(*p.StaticMAC))
    60  	}
    61  	if p.NoManageResolvConf {
    62  		options = append(options, libpod.WithPodUseImageResolvConf())
    63  	}
    64  	switch p.NetNS.NSMode {
    65  	case specgen.Bridge:
    66  		logrus.Debugf("Pod using default network mode")
    67  	case specgen.Host:
    68  		logrus.Debugf("Pod will use host networking")
    69  		options = append(options, libpod.WithPodHostNetwork())
    70  	default:
    71  		logrus.Debugf("Pod joining CNI networks: %v", p.CNINetworks)
    72  		options = append(options, libpod.WithPodNetworks(p.CNINetworks))
    73  	}
    74  
    75  	if p.NoManageHosts {
    76  		options = append(options, libpod.WithPodUseImageHosts())
    77  	}
    78  	if len(p.PortMappings) > 0 {
    79  		options = append(options, libpod.WithInfraContainerPorts(p.PortMappings))
    80  	}
    81  	options = append(options, libpod.WithPodCgroups())
    82  	return options, nil
    83  }