mirror of
https://github.com/containers/podman.git
synced 2025-06-03 20:33:20 +08:00

As part of this, make a major change to the type we use to represent port mappings in SpecGen (from using existing OCICNI structs to using our own custom one). This struct has the advantage of supporting ranges, massively reducing traffic over the wire for Podman commands using them (for example, the `podman run -p 5000-6000` command will now send only one struct instead of 1000). This struct also allows us to easily validate which ports are in use, and which are not, which is necessary for --expose. Once we have parsed the ports from the new struct, we can produce an accurate map including all currently requested ports, and use that to determine what ports need to be exposed (some requested exposed ports may already be included in a mapping from --publish and will be ignored) and what open ports on the host we can map them to. Signed-off-by: Matthew Heon <matthew.heon@pm.me>
95 lines
2.6 KiB
Go
95 lines
2.6 KiB
Go
package generate
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/containers/libpod/libpod"
|
|
"github.com/containers/libpod/pkg/specgen"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func MakePod(p *specgen.PodSpecGenerator, rt *libpod.Runtime) (*libpod.Pod, error) {
|
|
if err := p.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
options, err := createPodOptions(p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return rt.NewPod(context.Background(), options...)
|
|
}
|
|
|
|
func createPodOptions(p *specgen.PodSpecGenerator) ([]libpod.PodCreateOption, error) {
|
|
var (
|
|
options []libpod.PodCreateOption
|
|
)
|
|
if !p.NoInfra {
|
|
options = append(options, libpod.WithInfraContainer())
|
|
nsOptions, err := GetNamespaceOptions(p.SharedNamespaces)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
options = append(options, nsOptions...)
|
|
}
|
|
if len(p.CgroupParent) > 0 {
|
|
options = append(options, libpod.WithPodCgroupParent(p.CgroupParent))
|
|
}
|
|
if len(p.Labels) > 0 {
|
|
options = append(options, libpod.WithPodLabels(p.Labels))
|
|
}
|
|
if len(p.Name) > 0 {
|
|
options = append(options, libpod.WithPodName(p.Name))
|
|
}
|
|
if len(p.Hostname) > 0 {
|
|
options = append(options, libpod.WithPodHostname(p.Hostname))
|
|
}
|
|
if len(p.HostAdd) > 0 {
|
|
options = append(options, libpod.WithPodHosts(p.HostAdd))
|
|
}
|
|
if len(p.DNSServer) > 0 {
|
|
var dnsServers []string
|
|
for _, d := range p.DNSServer {
|
|
dnsServers = append(dnsServers, d.String())
|
|
}
|
|
options = append(options, libpod.WithPodDNS(dnsServers))
|
|
}
|
|
if len(p.DNSOption) > 0 {
|
|
options = append(options, libpod.WithPodDNSOption(p.DNSOption))
|
|
}
|
|
if len(p.DNSSearch) > 0 {
|
|
options = append(options, libpod.WithPodDNSSearch(p.DNSSearch))
|
|
}
|
|
if p.StaticIP != nil {
|
|
options = append(options, libpod.WithPodStaticIP(*p.StaticIP))
|
|
}
|
|
if p.StaticMAC != nil {
|
|
options = append(options, libpod.WithPodStaticMAC(*p.StaticMAC))
|
|
}
|
|
if p.NoManageResolvConf {
|
|
options = append(options, libpod.WithPodUseImageResolvConf())
|
|
}
|
|
switch p.NetNS.NSMode {
|
|
case specgen.Bridge:
|
|
logrus.Debugf("Pod using default network mode")
|
|
case specgen.Host:
|
|
logrus.Debugf("Pod will use host networking")
|
|
options = append(options, libpod.WithPodHostNetwork())
|
|
default:
|
|
logrus.Debugf("Pod joining CNI networks: %v", p.CNINetworks)
|
|
options = append(options, libpod.WithPodNetworks(p.CNINetworks))
|
|
}
|
|
|
|
if p.NoManageHosts {
|
|
options = append(options, libpod.WithPodUseImageHosts())
|
|
}
|
|
if len(p.PortMappings) > 0 {
|
|
ports, _, _, err := parsePortMapping(p.PortMappings)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
options = append(options, libpod.WithInfraContainerPorts(ports))
|
|
}
|
|
options = append(options, libpod.WithPodCgroups())
|
|
return options, nil
|
|
}
|