diff options
author | Paul Holzinger <pholzing@redhat.com> | 2022-04-19 16:22:39 +0200 |
---|---|---|
committer | Paul Holzinger <pholzing@redhat.com> | 2022-04-22 12:59:51 +0200 |
commit | 128086639c6317c324c32423765c72f38c115f74 (patch) | |
tree | c3e8022e71f651594acaae6edf4b1dbf6c20f15c | |
parent | 696bcd2773cf6c255855db9cf2ef724547626438 (diff) | |
download | podman-128086639c6317c324c32423765c72f38c115f74.tar.gz podman-128086639c6317c324c32423765c72f38c115f74.tar.bz2 podman-128086639c6317c324c32423765c72f38c115f74.zip |
libpod: fix c.Hostname() to respect the utsNsCtr
When we lookup the hostname for a given container we have to check if
the container is joined to another utsns and use this hostname then
instead.
This fixes a problem where the `hostname` command would use the correct
name but /etc/hostname would contain a different name.
Signed-off-by: Paul Holzinger <pholzing@redhat.com>
-rw-r--r-- | libpod/container.go | 9 | ||||
-rw-r--r-- | test/system/500-networking.bats | 8 |
2 files changed, 15 insertions, 2 deletions
diff --git a/libpod/container.go b/libpod/container.go index bc3cab439..6e2b7f528 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -628,6 +628,15 @@ func (c *Container) RuntimeName() string { // Hostname gets the container's hostname func (c *Container) Hostname() string { + if c.config.UTSNsCtr != "" { + utsNsCtr, err := c.runtime.GetContainer(c.config.UTSNsCtr) + if err != nil { + // should we return an error here? + logrus.Errorf("unable to lookup uts namespace for container %s: %v", c.ID(), err) + return "" + } + return utsNsCtr.Hostname() + } if c.config.Spec.Hostname != "" { return c.config.Spec.Hostname } diff --git a/test/system/500-networking.bats b/test/system/500-networking.bats index f5bb76d9a..f57c3dbd7 100644 --- a/test/system/500-networking.bats +++ b/test/system/500-networking.bats @@ -142,7 +142,7 @@ load helpers pid="$output" run_podman run --pod $pod_name --name $con1_name $IMAGE cat /etc/hosts is "$output" ".*\s$pod_name $infra_name.*" "Pod hostname in /etc/hosts" - is "$output" ".*127.0.0.1\s.*$con1_name.*" "Container1 name in /etc/hosts" + is "$output" ".*127.0.0.1\s$con1_name.*" "Container1 name in /etc/hosts" # get the length of the hosts file old_lines=${#lines[@]} @@ -150,9 +150,13 @@ load helpers # new host entry and the old one should be removed (lines check) run_podman run --pod $pod_name --name $con2_name $IMAGE cat /etc/hosts is "$output" ".*\s$pod_name $infra_name.*" "Pod hostname in /etc/hosts" - is "$output" ".*127.0.0.1\s.*$con2_name.*" "Container2 name in /etc/hosts" + is "$output" ".*127.0.0.1\s$con2_name.*" "Container2 name in /etc/hosts" is "${#lines[@]}" "$old_lines" "Number of hosts lines is equal" + run_podman run --pod $pod_name $IMAGE sh -c "hostname && cat /etc/hostname" + is "${lines[0]}" "$pod_name" "hostname is the pod hostname" + is "${lines[1]}" "$pod_name" "/etc/hostname contains correct pod hostname" + run_podman pod rm $pod_name is "$output" "$pid" "Only ID in output (no extra errors)" } |