9
"github.com/containers/podman/v5/libpod"
10
"github.com/containers/podman/v5/libpod/define"
11
"github.com/containers/podman/v5/pkg/specgen"
12
"github.com/containers/storage/pkg/fileutils"
13
spec "github.com/opencontainers/runtime-spec/specs-go"
14
"github.com/opencontainers/runtime-tools/generate"
15
"github.com/sirupsen/logrus"
18
func specConfigureNamespaces(s *specgen.SpecGenerator, g *generate.Generator, rt *libpod.Runtime, pod *libpod.Pod) error {
20
switch s.PidNS.NSMode {
22
if err := fileutils.Exists(s.PidNS.Value); err != nil {
23
return fmt.Errorf("cannot find specified PID namespace path: %w", err)
25
if err := g.AddOrReplaceLinuxNamespace(string(spec.PIDNamespace), s.PidNS.Value); err != nil {
29
if err := g.RemoveLinuxNamespace(string(spec.PIDNamespace)); err != nil {
33
if err := g.AddOrReplaceLinuxNamespace(string(spec.PIDNamespace), ""); err != nil {
39
switch s.IpcNS.NSMode {
41
if err := fileutils.Exists(s.IpcNS.Value); err != nil {
42
return fmt.Errorf("cannot find specified IPC namespace path: %w", err)
44
if err := g.AddOrReplaceLinuxNamespace(string(spec.IPCNamespace), s.IpcNS.Value); err != nil {
48
if err := g.RemoveLinuxNamespace(string(spec.IPCNamespace)); err != nil {
52
if err := g.AddOrReplaceLinuxNamespace(string(spec.IPCNamespace), ""); err != nil {
58
switch s.UtsNS.NSMode {
60
if err := fileutils.Exists(s.UtsNS.Value); err != nil {
61
return fmt.Errorf("cannot find specified UTS namespace path: %w", err)
63
if err := g.AddOrReplaceLinuxNamespace(string(spec.UTSNamespace), s.UtsNS.Value); err != nil {
67
if err := g.RemoveLinuxNamespace(string(spec.UTSNamespace)); err != nil {
71
if err := g.AddOrReplaceLinuxNamespace(string(spec.UTSNamespace), ""); err != nil {
76
hostname := s.Hostname
79
case s.UtsNS.NSMode == specgen.FromPod:
80
hostname = pod.Hostname()
81
case s.UtsNS.NSMode == specgen.FromContainer:
82
utsCtr, err := rt.LookupContainer(s.UtsNS.Value)
84
return fmt.Errorf("looking up container to share uts namespace with: %w", err)
86
hostname = utsCtr.Hostname()
87
case (s.NetNS.NSMode == specgen.Host && hostname == "") || s.UtsNS.NSMode == specgen.Host:
88
tmpHostname, err := os.Hostname()
90
return fmt.Errorf("unable to retrieve hostname of the host: %w", err)
92
hostname = tmpHostname
94
logrus.Debug("No hostname set; container's hostname will default to runtime default")
99
if s.Hostname != "" || s.UtsNS.NSMode != specgen.Host {
100
// Set the hostname in the OCI configuration only if specified by
101
// the user or if we are creating a new UTS namespace.
102
// TODO: Should we be doing this for pod or container shared
104
g.SetHostname(hostname)
106
if _, ok := s.Env["HOSTNAME"]; !ok && s.Hostname != "" {
107
g.AddProcessEnv("HOSTNAME", hostname)
111
if _, err := specgen.SetupUserNS(s.IDMappings, s.UserNS, g); err != nil {
116
switch s.CgroupNS.NSMode {
118
if err := fileutils.Exists(s.CgroupNS.Value); err != nil {
119
return fmt.Errorf("cannot find specified cgroup namespace path: %w", err)
121
if err := g.AddOrReplaceLinuxNamespace(string(spec.CgroupNamespace), s.CgroupNS.Value); err != nil {
125
if err := g.RemoveLinuxNamespace(string(spec.CgroupNamespace)); err != nil {
128
case specgen.Private:
129
if err := g.AddOrReplaceLinuxNamespace(string(spec.CgroupNamespace), ""); err != nil {
135
switch s.NetNS.NSMode {
137
if err := fileutils.Exists(s.NetNS.Value); err != nil {
138
return fmt.Errorf("cannot find specified network namespace path: %w", err)
140
if err := g.AddOrReplaceLinuxNamespace(string(spec.NetworkNamespace), s.NetNS.Value); err != nil {
144
if err := g.RemoveLinuxNamespace(string(spec.NetworkNamespace)); err != nil {
147
case specgen.Private, specgen.NoNetwork:
148
if err := g.AddOrReplaceLinuxNamespace(string(spec.NetworkNamespace), ""); err != nil {
153
if g.Config.Annotations == nil {
154
g.Config.Annotations = make(map[string]string)
156
if s.PublishExposedPorts != nil && *s.PublishExposedPorts {
157
g.Config.Annotations[define.InspectAnnotationPublishAll] = define.InspectResponseTrue
163
func needPostConfigureNetNS(s *specgen.SpecGenerator) bool {
164
return !s.UserNS.IsHost()