Separate ExposedPorts from PortBindings in libnetwork API
- Fix missing code in tests in 64cceb37ad1c16884d709fd49fba34e8a99d8c41 Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
parent
8265de6325
commit
ec4e1da3c5
|
@ -171,6 +171,14 @@ func TestCreateLinkWithOptions(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getExposedPorts() []netutils.TransportPort {
|
||||||
|
return []netutils.TransportPort{
|
||||||
|
netutils.TransportPort{Proto: netutils.TCP, Port: uint16(5000)},
|
||||||
|
netutils.TransportPort{Proto: netutils.UDP, Port: uint16(400)},
|
||||||
|
netutils.TransportPort{Proto: netutils.TCP, Port: uint16(600)},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func getPortMapping() []netutils.PortBinding {
|
func getPortMapping() []netutils.PortBinding {
|
||||||
return []netutils.PortBinding{
|
return []netutils.PortBinding{
|
||||||
netutils.PortBinding{Proto: netutils.TCP, Port: uint16(230), HostPort: uint16(23000)},
|
netutils.PortBinding{Proto: netutils.TCP, Port: uint16(230), HostPort: uint16(23000)},
|
||||||
|
@ -201,9 +209,9 @@ func TestLinkContainers(t *testing.T) {
|
||||||
t.Fatalf("Failed to create bridge: %v", err)
|
t.Fatalf("Failed to create bridge: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
portMappings := getPortMapping()
|
exposedPorts := getExposedPorts()
|
||||||
epOptions := make(map[string]interface{})
|
epOptions := make(map[string]interface{})
|
||||||
epOptions[options.PortMap] = portMappings
|
epOptions[options.ExposedPorts] = exposedPorts
|
||||||
|
|
||||||
sinfo, err := d.CreateEndpoint("net1", "ep1", epOptions)
|
sinfo, err := d.CreateEndpoint("net1", "ep1", epOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -235,13 +243,12 @@ func TestLinkContainers(t *testing.T) {
|
||||||
t.Fatalf("Failed to link ep1 and ep2")
|
t.Fatalf("Failed to link ep1 and ep2")
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := iptables.Raw("-L", "DOCKER")
|
out, err := iptables.Raw("-L", DockerChain)
|
||||||
for _, pm := range portMappings {
|
for _, pm := range exposedPorts {
|
||||||
regex := fmt.Sprintf("%s dpt:%d", pm.Proto.String(), pm.Port)
|
regex := fmt.Sprintf("%s dpt:%d", pm.Proto.String(), pm.Port)
|
||||||
re := regexp.MustCompile(regex)
|
re := regexp.MustCompile(regex)
|
||||||
matches := re.FindAllString(string(out[:]), -1)
|
matches := re.FindAllString(string(out[:]), -1)
|
||||||
// There will be 2 matches : Port-Mapping and Linking table rules
|
if len(matches) != 1 {
|
||||||
if len(matches) < 2 {
|
|
||||||
t.Fatalf("IP Tables programming failed %s", string(out[:]))
|
t.Fatalf("IP Tables programming failed %s", string(out[:]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,13 +264,12 @@ func TestLinkContainers(t *testing.T) {
|
||||||
t.Fatalf("Failed to unlink ep1 and ep2")
|
t.Fatalf("Failed to unlink ep1 and ep2")
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err = iptables.Raw("-L", "DOCKER")
|
out, err = iptables.Raw("-L", DockerChain)
|
||||||
for _, pm := range portMappings {
|
for _, pm := range exposedPorts {
|
||||||
regex := fmt.Sprintf("%s dpt:%d", pm.Proto.String(), pm.Port)
|
regex := fmt.Sprintf("%s dpt:%d", pm.Proto.String(), pm.Port)
|
||||||
re := regexp.MustCompile(regex)
|
re := regexp.MustCompile(regex)
|
||||||
matches := re.FindAllString(string(out[:]), -1)
|
matches := re.FindAllString(string(out[:]), -1)
|
||||||
// There will be 1 match : Port-Mapping
|
if len(matches) != 0 {
|
||||||
if len(matches) > 1 {
|
|
||||||
t.Fatalf("Leave should have deleted relevant IPTables rules %s", string(out[:]))
|
t.Fatalf("Leave should have deleted relevant IPTables rules %s", string(out[:]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -282,13 +288,12 @@ func TestLinkContainers(t *testing.T) {
|
||||||
|
|
||||||
_, err = d.Join("net1", "ep2", "", genericOption)
|
_, err = d.Join("net1", "ep2", "", genericOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
out, err = iptables.Raw("-L", "DOCKER")
|
out, err = iptables.Raw("-L", DockerChain)
|
||||||
for _, pm := range portMappings {
|
for _, pm := range exposedPorts {
|
||||||
regex := fmt.Sprintf("%s dpt:%d", pm.Proto.String(), pm.Port)
|
regex := fmt.Sprintf("%s dpt:%d", pm.Proto.String(), pm.Port)
|
||||||
re := regexp.MustCompile(regex)
|
re := regexp.MustCompile(regex)
|
||||||
matches := re.FindAllString(string(out[:]), -1)
|
matches := re.FindAllString(string(out[:]), -1)
|
||||||
// There must be 1 match : Port-Mapping
|
if len(matches) != 0 {
|
||||||
if len(matches) > 1 {
|
|
||||||
t.Fatalf("Error handling should rollback relevant IPTables rules %s", string(out[:]))
|
t.Fatalf("Error handling should rollback relevant IPTables rules %s", string(out[:]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -298,6 +303,8 @@ func TestLinkContainers(t *testing.T) {
|
||||||
t.Fatalf("Error handling should rollback relevant IPTables rules %s", string(out[:]))
|
t.Fatalf("Error handling should rollback relevant IPTables rules %s", string(out[:]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
t.Fatalf("Expected Join to fail given link conditions are not satisfied")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -482,22 +482,27 @@ func JoinOptionUseDefaultSandbox() EndpointOption {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateOptionPortMapping function returns an option setter for the container exposed
|
// CreateOptionExposedPorts function returns an option setter for the container exposed
|
||||||
|
// ports option to be passed to network.CreateEndpoint() method.
|
||||||
|
func CreateOptionExposedPorts(exposedPorts []netutils.TransportPort) EndpointOption {
|
||||||
|
return func(ep *endpoint) {
|
||||||
|
// Defensive copy
|
||||||
|
eps := make([]netutils.TransportPort, len(exposedPorts))
|
||||||
|
copy(eps, exposedPorts)
|
||||||
|
// Store endpoint label and in generic because driver needs it
|
||||||
|
ep.exposedPorts = eps
|
||||||
|
ep.generic[options.ExposedPorts] = eps
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateOptionPortMapping function returns an option setter for the mapping
|
||||||
// ports option to be passed to network.CreateEndpoint() method.
|
// ports option to be passed to network.CreateEndpoint() method.
|
||||||
func CreateOptionPortMapping(portBindings []netutils.PortBinding) EndpointOption {
|
func CreateOptionPortMapping(portBindings []netutils.PortBinding) EndpointOption {
|
||||||
return func(ep *endpoint) {
|
return func(ep *endpoint) {
|
||||||
// Extract and store exposed ports as this is the only concern of libnetwork endpoint
|
|
||||||
// Store a copy of the bindings as generic data to pass to the driver
|
// Store a copy of the bindings as generic data to pass to the driver
|
||||||
pbs := make([]netutils.PortBinding, 0, len(portBindings))
|
pbs := make([]netutils.PortBinding, len(portBindings))
|
||||||
exp := make([]netutils.TransportPort, 0, len(portBindings))
|
copy(pbs, portBindings)
|
||||||
|
|
||||||
for _, b := range portBindings {
|
|
||||||
pbs = append(pbs, b.GetCopy())
|
|
||||||
exp = append(exp, netutils.TransportPort{Proto: b.Proto, Port: b.Port})
|
|
||||||
}
|
|
||||||
|
|
||||||
ep.generic[options.PortMap] = pbs
|
ep.generic[options.PortMap] = pbs
|
||||||
ep.exposedPorts = exp
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -179,7 +179,7 @@ func TestBridge(t *testing.T) {
|
||||||
t.Fatalf("Unexpected format for port mapping in endpoint operational data")
|
t.Fatalf("Unexpected format for port mapping in endpoint operational data")
|
||||||
}
|
}
|
||||||
if len(pm) != 3 {
|
if len(pm) != 3 {
|
||||||
t.Fatalf("Incomplete data for port mapping in endpoint operational data")
|
t.Fatalf("Incomplete data for port mapping in endpoint operational data: %d", len(pm))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ep.Delete(); err != nil {
|
if err := ep.Delete(); err != nil {
|
||||||
|
|
|
@ -39,6 +39,11 @@ type TransportPort struct {
|
||||||
Port uint16
|
Port uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetCopy returns a copy of this TransportPort structure instance
|
||||||
|
func (t *TransportPort) GetCopy() TransportPort {
|
||||||
|
return TransportPort{Proto: t.Proto, Port: t.Port}
|
||||||
|
}
|
||||||
|
|
||||||
// PortBinding represent a port binding between the container an the host
|
// PortBinding represent a port binding between the container an the host
|
||||||
type PortBinding struct {
|
type PortBinding struct {
|
||||||
Proto Protocol
|
Proto Protocol
|
||||||
|
|
Loading…
Reference in New Issue