From dd74467fd8dcbdab35b4c7fc2dc183d12fb228f2 Mon Sep 17 00:00:00 2001 From: baude Date: Wed, 13 Feb 2019 13:22:58 -0600 Subject: show container ports of network namespace in cases where a container is part of a network namespace, we should show the network namespace's ports when dealing with ports. this impacts ps, kube, and port. fixes: #846 Signed-off-by: baude --- libpod/container.go | 12 ++++++++++-- libpod/kube.go | 6 +++++- 2 files changed, 15 insertions(+), 3 deletions(-) (limited to 'libpod') diff --git a/libpod/container.go b/libpod/container.go index b0589be3b..fec61533d 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -557,8 +557,16 @@ func (c *Container) NewNetNS() bool { // PortMappings returns the ports that will be mapped into a container if // a new network namespace is created // If NewNetNS() is false, this value is unused -func (c *Container) PortMappings() []ocicni.PortMapping { - return c.config.PortMappings +func (c *Container) PortMappings() ([]ocicni.PortMapping, error) { + // First check if the container belongs to a network namespace (like a pod) + if len(c.config.NetNsCtr) > 0 { + netNsCtr, err := c.runtime.LookupContainer(c.config.NetNsCtr) + if err != nil { + return nil, errors.Wrapf(err, "unable to lookup network namespace for container %s", c.ID()) + } + return netNsCtr.PortMappings() + } + return c.config.PortMappings, nil } // DNSServers returns DNS servers that will be used in the container's diff --git a/libpod/kube.go b/libpod/kube.go index 16cebf99b..484127870 100644 --- a/libpod/kube.go +++ b/libpod/kube.go @@ -228,7 +228,11 @@ func containerToV1Container(c *Container) (v1.Container, error) { return kubeContainer, nil } - ports, err := ocicniPortMappingToContainerPort(c.PortMappings()) + portmappings, err := c.PortMappings() + if err != nil { + return kubeContainer, err + } + ports, err := ocicniPortMappingToContainerPort(portmappings) if err != nil { return kubeContainer, nil } -- cgit v1.2.3-54-g00ecf