Add ExposedPorts to Inspect's ContainerConfig

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 <mheon@redhat.com>
This commit is contained in:
Matt Heon
2024-09-30 08:59:27 -04:00
parent e1496c992a
commit edc3dc5e11
5 changed files with 62 additions and 13 deletions

View File

@@ -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
}

View File

@@ -4,6 +4,7 @@ package libpod
import (
"fmt"
"strings"
"github.com/containers/image/v5/docker"
"github.com/containers/image/v5/pkg/shortnames"
@@ -175,6 +176,13 @@ func (c *Container) validate() error {
return fmt.Errorf("cannot set a startup healthcheck when there is no regular healthcheck: %w", define.ErrInvalidArg)
}
// Ensure all ports list a single protocol
for _, p := range c.config.PortMappings {
if strings.Contains(p.Protocol, ",") {
return fmt.Errorf("each port mapping must define a single protocol, got a comma-separated list for container port %d (protocols requested %q): %w", p.ContainerPort, p.Protocol, define.ErrInvalidArg)
}
}
return nil
}

View File

@@ -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.