From 02be831ce745d33cd590f8a63e4d5f776e4da4b7 Mon Sep 17 00:00:00 2001 From: Colin Bendell Date: Mon, 15 Nov 2021 13:57:25 -0500 Subject: [PATCH] Support EXPOSE with port ranges Fixes issue #12293. EXPOSE directive in images should mirror the --expose parameter. Specifically `EXPOSE 20000-20100/tcp` should work the same as `--expose 20000-20100/tcp` Signed-off-by: Colin Bendell --- pkg/specgen/generate/ports.go | 36 +++++++++-------------------------- pkg/specgenutil/specgen.go | 2 +- pkg/specgenutil/util.go | 4 ++-- 3 files changed, 12 insertions(+), 30 deletions(-) diff --git a/pkg/specgen/generate/ports.go b/pkg/specgen/generate/ports.go index 53a5e56977..b60cc1e980 100644 --- a/pkg/specgen/generate/ports.go +++ b/pkg/specgen/generate/ports.go @@ -5,7 +5,6 @@ import ( "fmt" "net" "sort" - "strconv" "strings" "github.com/containers/common/libimage" @@ -13,6 +12,7 @@ import ( "github.com/containers/podman/v3/utils" "github.com/containers/podman/v3/pkg/specgen" + "github.com/containers/podman/v3/pkg/specgenutil" "github.com/containers/podman/v3/pkg/util" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -410,31 +410,13 @@ func checkProtocol(protocol string, allowSCTP bool) ([]string, error) { } func GenExposedPorts(exposedPorts map[string]struct{}) (map[uint16]string, error) { - expose := make(map[uint16]string, len(exposedPorts)) - for imgExpose := range exposedPorts { - // Expose format is portNumber[/protocol] - splitExpose := strings.SplitN(imgExpose, "/", 2) - num, err := strconv.Atoi(splitExpose[0]) - if err != nil { - return nil, errors.Wrapf(err, "unable to convert image EXPOSE statement %q to port number", imgExpose) - } - if num > 65535 || num < 1 { - return nil, errors.Errorf("%d from image EXPOSE statement %q is not a valid port number", num, imgExpose) - } - - // No need to validate protocol, we'll do it later. - newProto := "tcp" - if len(splitExpose) == 2 { - newProto = splitExpose[1] - } - - proto := expose[uint16(num)] - if len(proto) > 1 { - proto = proto + "," + newProto - } else { - proto = newProto - } - expose[uint16(num)] = proto + expose := make([]string, 0, len(exposedPorts)) + for e := range exposedPorts { + expose = append(expose, e) + } + toReturn, err := specgenutil.CreateExpose(expose) + if err != nil { + return nil, errors.Wrapf(err, "unable to convert image EXPOSE") } - return expose, nil + return toReturn, nil } diff --git a/pkg/specgenutil/specgen.go b/pkg/specgenutil/specgen.go index c110b9e97b..7a572e730a 100644 --- a/pkg/specgenutil/specgen.go +++ b/pkg/specgenutil/specgen.go @@ -314,7 +314,7 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *entities.ContainerCreateOptions s.Pod = podID } - expose, err := createExpose(c.Expose) + expose, err := CreateExpose(c.Expose) if err != nil { return err } diff --git a/pkg/specgenutil/util.go b/pkg/specgenutil/util.go index b47082b7f4..534374e71f 100644 --- a/pkg/specgenutil/util.go +++ b/pkg/specgenutil/util.go @@ -53,11 +53,11 @@ func ParseFilters(filter []string) (map[string][]string, error) { return filters, nil } -// createExpose parses user-provided exposed port definitions and converts them +// CreateExpose parses user-provided exposed port definitions and converts them // into SpecGen format. // TODO: The SpecGen format should really handle ranges more sanely - we could // be massively inflating what is sent over the wire with a large range. -func createExpose(expose []string) (map[uint16]string, error) { +func CreateExpose(expose []string) (map[uint16]string, error) { toReturn := make(map[uint16]string) for _, e := range expose {