Skip to content

Commit 053f20e

Browse files
milasnicksieger
andauthored
port: improve error-handling if port not found (docker#10039)
This method looked slightly incomplete. If the port wasn't found, it'd return `err`, but that was always `nil`, so we'd print out `:0`. Now, we construct a nice error message with the targeted port and the ones we found. The `--protocol` flag is also now case-insensitive to prevent any weirdness/confusion there. Co-authored-by: Nick Sieger <[email protected]> Signed-off-by: Milas Bowman <[email protected]>
1 parent 6ed9a79 commit 053f20e

File tree

6 files changed

+27
-10
lines changed

6 files changed

+27
-10
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ build-and-e2e-compose-standalone: build e2e-compose-standalone ## Compile the co
7777

7878
.PHONY: mocks
7979
mocks:
80+
mockgen --version >/dev/null 2>&1 || go install github.com/golang/mock/[email protected]
8081
mockgen -destination pkg/mocks/mock_docker_cli.go -package mocks github.com/docker/cli/cli/command Cli
8182
mockgen -destination pkg/mocks/mock_docker_api.go -package mocks github.com/docker/docker/client APIClient
8283
mockgen -destination pkg/mocks/mock_docker_compose_api.go -package mocks -source=./pkg/api/api.go Service

cmd/compose/port.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"strconv"
23+
"strings"
2324

2425
"github.com/spf13/cobra"
2526

@@ -28,7 +29,7 @@ import (
2829

2930
type portOptions struct {
3031
*projectOptions
31-
port int
32+
port uint16
3233
protocol string
3334
index int
3435
}
@@ -42,11 +43,12 @@ func portCommand(p *projectOptions, backend api.Service) *cobra.Command {
4243
Short: "Print the public port for a port binding.",
4344
Args: cobra.MinimumNArgs(2),
4445
PreRunE: Adapt(func(ctx context.Context, args []string) error {
45-
port, err := strconv.Atoi(args[1])
46+
port, err := strconv.ParseUint(args[1], 10, 16)
4647
if err != nil {
4748
return err
4849
}
49-
opts.port = port
50+
opts.port = uint16(port)
51+
opts.protocol = strings.ToLower(opts.protocol)
5052
return nil
5153
}),
5254
RunE: Adapt(func(ctx context.Context, args []string) error {

pkg/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type Service interface {
7272
// Events executes the equivalent to a `compose events`
7373
Events(ctx context.Context, projectName string, options EventsOptions) error
7474
// Port executes the equivalent to a `compose port`
75-
Port(ctx context.Context, projectName string, service string, port int, options PortOptions) (string, int, error)
75+
Port(ctx context.Context, projectName string, service string, port uint16, options PortOptions) (string, int, error)
7676
// Images executes the equivalent of a `compose images`
7777
Images(ctx context.Context, projectName string, options ImagesOptions) ([]ImageSummary, error)
7878
}

pkg/api/proxy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type ServiceProxy struct {
4848
UnPauseFn func(ctx context.Context, project string, options PauseOptions) error
4949
TopFn func(ctx context.Context, projectName string, services []string) ([]ContainerProcSummary, error)
5050
EventsFn func(ctx context.Context, project string, options EventsOptions) error
51-
PortFn func(ctx context.Context, project string, service string, port int, options PortOptions) (string, int, error)
51+
PortFn func(ctx context.Context, project string, service string, port uint16, options PortOptions) (string, int, error)
5252
ImagesFn func(ctx context.Context, projectName string, options ImagesOptions) ([]ImageSummary, error)
5353
interceptors []Interceptor
5454
}
@@ -294,7 +294,7 @@ func (s *ServiceProxy) Events(ctx context.Context, projectName string, options E
294294
}
295295

296296
// Port implements Service interface
297-
func (s *ServiceProxy) Port(ctx context.Context, projectName string, service string, port int, options PortOptions) (string, int, error) {
297+
func (s *ServiceProxy) Port(ctx context.Context, projectName string, service string, port uint16, options PortOptions) (string, int, error) {
298298
if s.PortFn == nil {
299299
return "", 0, ErrNotImplemented
300300
}

pkg/compose/port.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"github.com/docker/docker/api/types/filters"
2828
)
2929

30-
func (s *composeService) Port(ctx context.Context, projectName string, service string, port int, options api.PortOptions) (string, int, error) {
30+
func (s *composeService) Port(ctx context.Context, projectName string, service string, port uint16, options api.PortOptions) (string, int, error) {
3131
projectName = strings.ToLower(projectName)
3232
list, err := s.apiClient().ContainerList(ctx, moby.ContainerListOptions{
3333
Filters: filters.NewArgs(
@@ -44,9 +44,23 @@ func (s *composeService) Port(ctx context.Context, projectName string, service s
4444
}
4545
container := list[0]
4646
for _, p := range container.Ports {
47-
if p.PrivatePort == uint16(port) && p.Type == options.Protocol {
47+
if p.PrivatePort == port && p.Type == options.Protocol {
4848
return p.IP, int(p.PublicPort), nil
4949
}
5050
}
51-
return "", 0, err
51+
return "", 0, portNotFoundError(options.Protocol, port, container)
52+
}
53+
54+
func portNotFoundError(protocol string, port uint16, ctr moby.Container) error {
55+
formatPort := func(protocol string, port uint16) string {
56+
return fmt.Sprintf("%d/%s", port, protocol)
57+
}
58+
59+
var containerPorts []string
60+
for _, p := range ctr.Ports {
61+
containerPorts = append(containerPorts, formatPort(p.Type, p.PublicPort))
62+
}
63+
64+
name := strings.TrimPrefix(ctr.Names[0], "/")
65+
return fmt.Errorf("no port %s for container %s: %s", formatPort(protocol, port), name, strings.Join(containerPorts, ", "))
5266
}

pkg/mocks/mock_docker_compose_api.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)