podman: fix --uts=host

Do not set any hostname value in the OCI configuration when --uts=host
is used and the user didn't specify any value.  This prevents an error
from the OCI runtime as it cannot set the hostname without a new UTS
namespace.

Differently, the HOSTNAME environment variable is always set.  When
--uts=host is used, HOSTNAME gets the value from the host.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>

Closes: #1280
Approved by: baude
This commit is contained in:
Giuseppe Scrivano 2018-08-15 17:27:26 +02:00 committed by Atomic Bot
parent bf741b3ea3
commit 50afe5b031
3 changed files with 36 additions and 11 deletions

View File

@ -215,8 +215,14 @@ func (c *Container) generateSpec(ctx context.Context) (*spec.Spec, error) {
g.AddAnnotation(crioAnnotations.Created, c.config.CreatedTime.Format(time.RFC3339Nano)) g.AddAnnotation(crioAnnotations.Created, c.config.CreatedTime.Format(time.RFC3339Nano))
g.AddAnnotation("org.opencontainers.image.stopSignal", fmt.Sprintf("%d", c.config.StopSignal)) g.AddAnnotation("org.opencontainers.image.stopSignal", fmt.Sprintf("%d", c.config.StopSignal))
g.SetHostname(c.Hostname()) for _, i := range c.config.Spec.Linux.Namespaces {
g.AddProcessEnv("HOSTNAME", g.Config.Hostname) if string(i.Type) == spec.UTSNamespace {
hostname := c.Hostname()
g.SetHostname(hostname)
g.AddProcessEnv("HOSTNAME", hostname)
break
}
}
// Only add container environment variable if not already present // Only add container environment variable if not already present
foundContainerEnv := false foundContainerEnv := false

View File

@ -74,18 +74,23 @@ func CreateConfigToOCISpec(config *CreateConfig) (*spec.Spec, error) { //nolint
g.AddAnnotation(key, val) g.AddAnnotation(key, val)
} }
g.SetRootReadonly(config.ReadOnlyRootfs) g.SetRootReadonly(config.ReadOnlyRootfs)
if config.Hostname == "" {
if config.NetMode.IsHost() { hostname := config.Hostname
config.Hostname, err = os.Hostname() if hostname == "" && (config.NetMode.IsHost() || config.UtsMode.IsHost()) {
if err != nil { hostname, err = os.Hostname()
return nil, errors.Wrap(err, "unable to retrieve hostname") if err != nil {
} return nil, errors.Wrap(err, "unable to retrieve hostname")
} }
} }
g.SetHostname(config.Hostname) g.RemoveHostname()
if config.Hostname != "" { if config.Hostname != "" || !config.UtsMode.IsHost() {
g.AddProcessEnv("HOSTNAME", config.Hostname) // Set the hostname in the OCI configuration only
// if specified by the user or if we are creating
// a new UTS namespace.
g.SetHostname(hostname)
} }
g.AddProcessEnv("HOSTNAME", hostname)
for sysctlKey, sysctlVal := range config.Sysctl { for sysctlKey, sysctlVal := range config.Sysctl {
g.AddLinuxSysctl(sysctlKey, sysctlVal) g.AddLinuxSysctl(sysctlKey, sysctlVal)
} }

View File

@ -114,6 +114,20 @@ var _ = Describe("Podman rmi", func() {
match, _ := session.GrepString(hostname) match, _ := session.GrepString(hostname)
Expect(match).Should(BeTrue()) Expect(match).Should(BeTrue())
}) })
It("podman run --net host --uts host hostname test", func() {
session := podmanTest.Podman([]string{"run", "--rm", "--net", "host", "--uts", "host", ALPINE, "printenv", "HOSTNAME"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
match, _ := session.GrepString(hostname)
Expect(match).Should(BeTrue())
})
It("podman run --uts host hostname test", func() {
session := podmanTest.Podman([]string{"run", "--rm", "--uts", "host", ALPINE, "printenv", "HOSTNAME"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
match, _ := session.GrepString(hostname)
Expect(match).Should(BeTrue())
})
It("podman run --net host --hostname ... hostname test", func() { It("podman run --net host --hostname ... hostname test", func() {
session := podmanTest.Podman([]string{"run", "--rm", "--net", "host", "--hostname", "foobar", ALPINE, "printenv", "HOSTNAME"}) session := podmanTest.Podman([]string{"run", "--rm", "--net", "host", "--hostname", "foobar", ALPINE, "printenv", "HOSTNAME"})