mirror of https://github.com/docker/compose.git
Port parsing on the comand line
This commit is contained in:
parent
230cccff76
commit
52f7902d40
|
@ -9,6 +9,7 @@ ENV GO111MODULE=on
|
||||||
|
|
||||||
WORKDIR ${PWD}
|
WORKDIR ${PWD}
|
||||||
ADD go.* ${PWD}
|
ADD go.* ${PWD}
|
||||||
|
RUN go mod download
|
||||||
ADD . ${PWD}
|
ADD . ${PWD}
|
||||||
|
|
||||||
FROM golang:${GO_VERSION} AS protos-base
|
FROM golang:${GO_VERSION} AS protos-base
|
||||||
|
|
|
@ -141,8 +141,8 @@ func (cs *aciContainerService) Run(ctx context.Context, r containers.ContainerCo
|
||||||
var ports []types.ServicePortConfig
|
var ports []types.ServicePortConfig
|
||||||
for _, p := range r.Ports {
|
for _, p := range r.Ports {
|
||||||
ports = append(ports, types.ServicePortConfig{
|
ports = append(ports, types.ServicePortConfig{
|
||||||
Target: p.Destination,
|
Target: p.ContainerPort,
|
||||||
Published: p.Source,
|
Published: p.HostPort,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
project := compose.Project{
|
project := compose.Project{
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package run
|
package run
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
"github.com/docker/go-connections/nat"
|
||||||
|
|
||||||
"github.com/docker/api/containers"
|
"github.com/docker/api/containers"
|
||||||
)
|
)
|
||||||
|
@ -14,27 +14,34 @@ type runOpts struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func toPorts(ports []string) ([]containers.Port, error) {
|
func toPorts(ports []string) ([]containers.Port, error) {
|
||||||
|
_, bindings, err := nat.ParsePortSpecs(ports)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
var result []containers.Port
|
var result []containers.Port
|
||||||
|
|
||||||
for _, port := range ports {
|
for port, bind := range bindings {
|
||||||
parts := strings.Split(port, ":")
|
for _, portbind := range bind {
|
||||||
if len(parts) != 2 {
|
var hostPort uint32
|
||||||
return nil, fmt.Errorf("unable to parse ports %q", port)
|
if portbind.HostPort != "" {
|
||||||
}
|
hp, err := strconv.Atoi(portbind.HostPort)
|
||||||
source, err := strconv.Atoi(parts[0])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
destination, err := strconv.Atoi(parts[1])
|
hostPort = uint32(hp)
|
||||||
if err != nil {
|
} else {
|
||||||
return nil, err
|
hostPort = uint32(port.Int())
|
||||||
}
|
}
|
||||||
|
|
||||||
result = append(result, containers.Port{
|
result = append(result, containers.Port{
|
||||||
Source: uint32(source),
|
HostPort: hostPort,
|
||||||
Destination: uint32(destination),
|
ContainerPort: uint32(port.Int()),
|
||||||
|
Protocol: port.Proto(),
|
||||||
|
HostIP: portbind.HostIP,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,99 @@
|
||||||
|
package run
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
|
|
||||||
|
"github.com/docker/api/containers"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RunOptsSuite struct {
|
||||||
|
suite.Suite
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *RunOptsSuite) TestPortParse() {
|
||||||
|
testCases := []struct {
|
||||||
|
in string
|
||||||
|
expected []containers.Port
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
in: "80",
|
||||||
|
expected: []containers.Port{
|
||||||
|
{
|
||||||
|
HostPort: 80,
|
||||||
|
ContainerPort: 80,
|
||||||
|
Protocol: "tcp",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: "80:80",
|
||||||
|
expected: []containers.Port{
|
||||||
|
{
|
||||||
|
HostPort: 80,
|
||||||
|
ContainerPort: 80,
|
||||||
|
Protocol: "tcp",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: "80:80/udp",
|
||||||
|
expected: []containers.Port{
|
||||||
|
{
|
||||||
|
ContainerPort: 80,
|
||||||
|
HostPort: 80,
|
||||||
|
Protocol: "udp",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: "8080:80",
|
||||||
|
expected: []containers.Port{
|
||||||
|
{
|
||||||
|
HostPort: 8080,
|
||||||
|
ContainerPort: 80,
|
||||||
|
Protocol: "tcp",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: "192.168.0.2:8080:80",
|
||||||
|
expected: []containers.Port{
|
||||||
|
{
|
||||||
|
HostPort: 8080,
|
||||||
|
ContainerPort: 80,
|
||||||
|
Protocol: "tcp",
|
||||||
|
HostIP: "192.168.0.2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: "80-81:80-81",
|
||||||
|
expected: []containers.Port{
|
||||||
|
{
|
||||||
|
HostPort: 80,
|
||||||
|
ContainerPort: 80,
|
||||||
|
Protocol: "tcp",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
HostPort: 81,
|
||||||
|
ContainerPort: 81,
|
||||||
|
Protocol: "tcp",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range testCases {
|
||||||
|
result, err := toPorts([]string{testCase.in})
|
||||||
|
require.Nil(s.T(), err)
|
||||||
|
assert.ElementsMatch(s.T(), testCase.expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExampleTestSuite(t *testing.T) {
|
||||||
|
suite.Run(t, new(RunOptsSuite))
|
||||||
|
}
|
|
@ -50,7 +50,7 @@ func Command() *cobra.Command {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flags().StringArrayVarP(&opts.publish, "publish", "p", []string{}, "Publish a container's port(s)")
|
cmd.Flags().StringArrayVarP(&opts.publish, "publish", "p", []string{}, "Publish a container's port(s). [HOST_PORT:]CONTAINER_PORT")
|
||||||
cmd.Flags().StringVar(&opts.name, "name", getRandomName(), "Assign a name to the container")
|
cmd.Flags().StringVar(&opts.name, "name", getRandomName(), "Assign a name to the container")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
|
|
|
@ -17,14 +17,19 @@ type Container struct {
|
||||||
PidsCurrent uint64
|
PidsCurrent uint64
|
||||||
PidsLimit uint64
|
PidsLimit uint64
|
||||||
Labels []string
|
Labels []string
|
||||||
|
Ports []Port
|
||||||
}
|
}
|
||||||
|
|
||||||
// Port represents a published port of a container
|
// Port represents a published port of a container
|
||||||
type Port struct {
|
type Port struct {
|
||||||
// Source is the source port
|
// HostPort is the port number on the host
|
||||||
Source uint32
|
HostPort uint32
|
||||||
// Destination is the destination port
|
// ContainerPort is the port number inside the container
|
||||||
Destination uint32
|
ContainerPort uint32
|
||||||
|
/// Protocol is the protocol of the port mapping
|
||||||
|
Protocol string
|
||||||
|
// HostIP is the host ip to use
|
||||||
|
HostIP string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContainerConfig contains the configuration data about a container
|
// ContainerConfig contains the configuration data about a container
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -16,6 +16,7 @@ require (
|
||||||
github.com/containerd/containerd v1.3.4 // indirect
|
github.com/containerd/containerd v1.3.4 // indirect
|
||||||
github.com/docker/distribution v2.7.1+incompatible // indirect
|
github.com/docker/distribution v2.7.1+incompatible // indirect
|
||||||
github.com/docker/docker v17.12.0-ce-rc1.0.20200309214505-aa6a9891b09c+incompatible
|
github.com/docker/docker v17.12.0-ce-rc1.0.20200309214505-aa6a9891b09c+incompatible
|
||||||
|
github.com/docker/go-connections v0.4.0
|
||||||
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee // indirect
|
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee // indirect
|
||||||
github.com/gobwas/pool v0.2.0 // indirect
|
github.com/gobwas/pool v0.2.0 // indirect
|
||||||
github.com/gobwas/ws v1.0.3
|
github.com/gobwas/ws v1.0.3
|
||||||
|
|
|
@ -60,6 +60,7 @@ func (ms *mobyService) List(ctx context.Context) ([]containers.Container, error)
|
||||||
Image: container.Image,
|
Image: container.Image,
|
||||||
Status: container.Status,
|
Status: container.Status,
|
||||||
Command: container.Command,
|
Command: container.Command,
|
||||||
|
Ports: getPorts(container.Ports),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,3 +138,17 @@ func (ms *mobyService) Delete(ctx context.Context, containerID string, force boo
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getPorts(ports []types.Port) []containers.Port {
|
||||||
|
result := []containers.Port{}
|
||||||
|
for _, port := range ports {
|
||||||
|
result = append(result, containers.Port{
|
||||||
|
ContainerPort: uint32(port.PrivatePort),
|
||||||
|
HostPort: uint32(port.PublicPort),
|
||||||
|
HostIP: port.IP,
|
||||||
|
Protocol: port.Type,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue