Skip to content

Commit

Permalink
Add ExposedPorts to Inspect's ContainerConfig
Browse files Browse the repository at this point in the history
A field we missed versus Docker. Matches the format of our
existing Ports list in the NetworkConfig, but only includes
exposed ports (and maps these to struct{}, as they never go to
real ports on the host).

Fixes https://issues.redhat.com/browse/RHEL-60382

Signed-off-by: Matt Heon <[email protected]>
  • Loading branch information
mheon committed Sep 30, 2024
1 parent e1496c9 commit 400080c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
19 changes: 19 additions & 0 deletions libpod/container_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,25 @@ func (c *Container) generateInspectContainerConfig(spec *spec.Spec) *define.Insp

ctrConfig.SdNotifyMode = c.config.SdNotifyMode
ctrConfig.SdNotifySocket = c.config.SdNotifySocket

// Exosed ports consists of all exposed ports and all port mappings for
// this container. It does *NOT* follow to another container if we share
// the network namespace.
exposedPorts := make(map[string]struct{})
for port, protocols := range c.config.ExposedPorts {
for _, proto := range protocols {
exposedPorts[fmt.Sprintf("%d/%s", port, proto)] = struct{}{}
}
}
for _, mapping := range c.config.PortMappings {
for i := range mapping.Range {
exposedPorts[fmt.Sprintf("%d/%s", mapping.ContainerPort + i, mapping.Protocol)] = struct{}{}
}
}
if len(exposedPorts) > 0 {
ctrConfig.ExposedPorts = exposedPorts
}

return ctrConfig
}

Expand Down
2 changes: 2 additions & 0 deletions libpod/define/container_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ type InspectContainerConfig struct {
SdNotifyMode string `json:"sdNotifyMode,omitempty"`
// SdNotifySocket is the NOTIFY_SOCKET in use by/configured for the container.
SdNotifySocket string `json:"sdNotifySocket,omitempty"`
// ExposedPorts includes ports the container has exposed.
ExposedPorts map[string]struct{} `json:"ExposedPorts,omitempty"`

// V4PodmanCompatMarshal indicates that the json marshaller should
// use the old v4 inspect format to keep API compatibility.
Expand Down
23 changes: 10 additions & 13 deletions pkg/api/handlers/compat/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,19 +535,6 @@ func LibpodToContainerJSON(l *libpod.Container, sz bool) (*types.ContainerJSON,
}
stopTimeout := int(l.StopTimeout())

exposedPorts := make(nat.PortSet)
for ep := range inspect.NetworkSettings.Ports {
port, proto, ok := strings.Cut(ep, "/")
if !ok {
return nil, fmt.Errorf("PORT/PROTOCOL Format required for %q", ep)
}
exposedPort, err := nat.NewPort(proto, port)
if err != nil {
return nil, err
}
exposedPorts[exposedPort] = struct{}{}
}

var healthcheck *container.HealthConfig
if inspect.Config.Healthcheck != nil {
healthcheck = &container.HealthConfig{
Expand All @@ -559,6 +546,16 @@ func LibpodToContainerJSON(l *libpod.Container, sz bool) (*types.ContainerJSON,
}
}

// Apparently the compiler can't convert a map[string]struct{} into a nat.PortSet
// (Despite a nat.PortSet being that exact struct with some types added)
var exposedPorts nat.PortSet
if len(inspect.Config.ExposedPorts) > 0 {
exposedPorts = make(nat.PortSet)
for p := range inspect.Config.ExposedPorts {
exposedPorts[nat.Port(p)] = struct{}{}
}
}

config := container.Config{
Hostname: l.Hostname(),
Domainname: inspect.Config.DomainName,
Expand Down
23 changes: 23 additions & 0 deletions test/e2e/container_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var _ = Describe("Podman container inspect", func() {
Expect(data).To(HaveLen(1))
Expect(data[0].NetworkSettings.Ports).
To(Equal(map[string][]define.InspectHostPort{"8787/udp": nil, "99/sctp": nil}))
Expect(data[0].Config.ExposedPorts).
To(Equal(map[string]struct{}{"8787/udp": {}, "99/sctp": {}}))

session = podmanTest.Podman([]string{"ps", "--format", "{{.Ports}}"})
session.WaitWithDefaultTimeout()
Expand All @@ -61,6 +63,27 @@ var _ = Describe("Podman container inspect", func() {
Expect(session.OutputToString()).To(Equal("80/tcp, 8989/tcp"))
})

It("podman inspect exposed ports includes published ports", func() {
c1 := "ctr1"
c1s := podmanTest.Podman([]string{"run", "-d", "--expose", "22/tcp", "-p", "8080:80/tcp", "--name", c1, ALPINE, "top"})
c1s.WaitWithDefaultTimeout()
Expect(c1s).Should(ExitCleanly())

c2 := "ctr2"
c2s := podmanTest.Podman([]string{"run", "-d", "--net", fmt.Sprintf("container:%s", c1), "--name", c2, ALPINE, "top"})
c2s.WaitWithDefaultTimeout()
Expect(c2s).Should(ExitCleanly())

data1 := podmanTest.InspectContainer(c1)
Expect(data).To(HaveLen(1))
Expect(data1[0].Config.ExposedPorts).
To(Equal(map[string]struct{}{"22/tcp": {}, "8080/tcp": {}}))

data2 := podmanTest.InspectContainer(c2)
Expect(data).To(HaveLen(1))
Expect(data2[0].Config.ExposedPorts).To(BeNil())
})

It("podman inspect shows volumes-from with mount options", func() {
ctr1 := "volfctr"
ctr2 := "voltctr"
Expand Down

0 comments on commit 400080c

Please sign in to comment.