Skip to content

Commit 080bedd

Browse files
author
Santhosh Manohar
committed
Merge pull request #810 from aboch/se
Move exposed ports and port bindings from Endpoint to Sandbox
2 parents 6d1a65c + c0d1232 commit 080bedd

21 files changed

Lines changed: 546 additions & 230 deletions

api/api.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ func (sc *sandboxCreate) parseOptions() []libnetwork.SandboxOption {
251251
setFctList = append(setFctList, libnetwork.OptionExtraHost(e.Name, e.Address))
252252
}
253253
}
254+
if sc.ExposedPorts != nil {
255+
setFctList = append(setFctList, libnetwork.OptionExposedPorts(sc.ExposedPorts))
256+
}
257+
if sc.PortMapping != nil {
258+
setFctList = append(setFctList, libnetwork.OptionPortMapping(sc.PortMapping))
259+
}
254260
return setFctList
255261
}
256262

@@ -384,13 +390,6 @@ func procCreateEndpoint(c libnetwork.NetworkController, vars map[string]string,
384390
}
385391

386392
var setFctList []libnetwork.EndpointOption
387-
if ec.ExposedPorts != nil {
388-
setFctList = append(setFctList, libnetwork.CreateOptionExposedPorts(ec.ExposedPorts))
389-
}
390-
if ec.PortMapping != nil {
391-
setFctList = append(setFctList, libnetwork.CreateOptionPortMapping(ec.PortMapping))
392-
}
393-
394393
for _, str := range ec.MyAliases {
395394
setFctList = append(setFctList, libnetwork.CreateOptionMyAlias(str))
396395
}
@@ -633,13 +632,6 @@ func procPublishService(c libnetwork.NetworkController, vars map[string]string,
633632
}
634633

635634
var setFctList []libnetwork.EndpointOption
636-
if sp.ExposedPorts != nil {
637-
setFctList = append(setFctList, libnetwork.CreateOptionExposedPorts(sp.ExposedPorts))
638-
}
639-
if sp.PortMapping != nil {
640-
setFctList = append(setFctList, libnetwork.CreateOptionPortMapping(sp.PortMapping))
641-
}
642-
643635
for _, str := range sp.MyAliases {
644636
setFctList = append(setFctList, libnetwork.CreateOptionMyAlias(str))
645637
}

api/api_test.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,7 @@ func TestGetNetworksAndEndpoints(t *testing.T) {
293293
}
294294

295295
ec1 := endpointCreate{
296-
Name: "ep1",
297-
ExposedPorts: getExposedPorts(),
298-
PortMapping: getPortMapping(),
296+
Name: "ep1",
299297
}
300298
b1, err := json.Marshal(ec1)
301299
if err != nil {
@@ -823,10 +821,8 @@ func TestProcPublishUnpublishService(t *testing.T) {
823821
}
824822

825823
sp := servicePublish{
826-
Name: "web",
827-
Network: "network",
828-
ExposedPorts: getExposedPorts(),
829-
PortMapping: getPortMapping(),
824+
Name: "web",
825+
Network: "network",
830826
}
831827
b, err = json.Marshal(sp)
832828
if err != nil {
@@ -2087,7 +2083,10 @@ func TestEndToEnd(t *testing.T) {
20872083
cpid1 := string(chars[0 : len(chars)/2])
20882084

20892085
// Create sandboxes
2090-
sb1, err := json.Marshal(sandboxCreate{ContainerID: cid1})
2086+
sb1, err := json.Marshal(sandboxCreate{
2087+
ContainerID: cid1,
2088+
PortMapping: getPortMapping(),
2089+
})
20912090
if err != nil {
20922091
t.Fatal(err)
20932092
}
@@ -2111,7 +2110,10 @@ func TestEndToEnd(t *testing.T) {
21112110
t.Fatal(err)
21122111
}
21132112

2114-
sb2, err := json.Marshal(sandboxCreate{ContainerID: cid2})
2113+
sb2, err := json.Marshal(sandboxCreate{
2114+
ContainerID: cid2,
2115+
ExposedPorts: getExposedPorts(),
2116+
})
21152117
if err != nil {
21162118
t.Fatal(err)
21172119
}

api/types.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,23 @@ type networkCreate struct {
4242

4343
// endpointCreate represents the body of the "create endpoint" http request message
4444
type endpointCreate struct {
45-
Name string `json:"name"`
46-
MyAliases []string `json:"my_aliases"`
47-
ExposedPorts []types.TransportPort `json:"exposed_ports"`
48-
PortMapping []types.PortBinding `json:"port_mapping"`
45+
Name string `json:"name"`
46+
MyAliases []string `json:"my_aliases"`
4947
}
5048

5149
// sandboxCreate is the expected body of the "create sandbox" http request message
5250
type sandboxCreate struct {
53-
ContainerID string `json:"container_id"`
54-
HostName string `json:"host_name"`
55-
DomainName string `json:"domain_name"`
56-
HostsPath string `json:"hosts_path"`
57-
ResolvConfPath string `json:"resolv_conf_path"`
58-
DNS []string `json:"dns"`
59-
ExtraHosts []extraHost `json:"extra_hosts"`
60-
UseDefaultSandbox bool `json:"use_default_sandbox"`
61-
UseExternalKey bool `json:"use_external_key"`
51+
ContainerID string `json:"container_id"`
52+
HostName string `json:"host_name"`
53+
DomainName string `json:"domain_name"`
54+
HostsPath string `json:"hosts_path"`
55+
ResolvConfPath string `json:"resolv_conf_path"`
56+
DNS []string `json:"dns"`
57+
ExtraHosts []extraHost `json:"extra_hosts"`
58+
UseDefaultSandbox bool `json:"use_default_sandbox"`
59+
UseExternalKey bool `json:"use_external_key"`
60+
ExposedPorts []types.TransportPort `json:"exposed_ports"`
61+
PortMapping []types.PortBinding `json:"port_mapping"`
6262
}
6363

6464
// endpointJoin represents the expected body of the "join endpoint" or "leave endpoint" http request messages
@@ -69,11 +69,9 @@ type endpointJoin struct {
6969

7070
// servicePublish represents the body of the "publish service" http request message
7171
type servicePublish struct {
72-
Name string `json:"name"`
73-
MyAliases []string `json:"my_aliases"`
74-
Network string `json:"network_name"`
75-
ExposedPorts []types.TransportPort `json:"exposed_ports"`
76-
PortMapping []types.PortBinding `json:"port_mapping"`
72+
Name string `json:"name"`
73+
MyAliases []string `json:"my_aliases"`
74+
Network string `json:"network_name"`
7775
}
7876

7977
// serviceDelete represents the body of the "unpublish service" http request message

client/types.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,9 @@ type networkCreate struct {
4242

4343
// serviceCreate represents the body of the "publish service" http request message
4444
type serviceCreate struct {
45-
Name string `json:"name"`
46-
MyAliases []string `json:"my_aliases"`
47-
Network string `json:"network_name"`
48-
ExposedPorts []types.TransportPort `json:"exposed_ports"`
49-
PortMapping []types.PortBinding `json:"port_mapping"`
45+
Name string `json:"name"`
46+
MyAliases []string `json:"my_aliases"`
47+
Network string `json:"network_name"`
5048
}
5149

5250
// serviceDelete represents the body of the "unpublish service" http request message
@@ -63,14 +61,16 @@ type serviceAttach struct {
6361

6462
// SandboxCreate is the body of the "post /sandboxes" http request message
6563
type SandboxCreate struct {
66-
ContainerID string `json:"container_id"`
67-
HostName string `json:"host_name"`
68-
DomainName string `json:"domain_name"`
69-
HostsPath string `json:"hosts_path"`
70-
ResolvConfPath string `json:"resolv_conf_path"`
71-
DNS []string `json:"dns"`
72-
ExtraHosts []extraHost `json:"extra_hosts"`
73-
UseDefaultSandbox bool `json:"use_default_sandbox"`
64+
ContainerID string `json:"container_id"`
65+
HostName string `json:"host_name"`
66+
DomainName string `json:"domain_name"`
67+
HostsPath string `json:"hosts_path"`
68+
ResolvConfPath string `json:"resolv_conf_path"`
69+
DNS []string `json:"dns"`
70+
ExtraHosts []extraHost `json:"extra_hosts"`
71+
UseDefaultSandbox bool `json:"use_default_sandbox"`
72+
ExposedPorts []types.TransportPort `json:"exposed_ports"`
73+
PortMapping []types.PortBinding `json:"port_mapping"`
7474
}
7575

7676
// extraHost represents the extra host object

default_gateway.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package libnetwork
33
import (
44
"fmt"
55

6-
"github.com/docker/libnetwork/netlabel"
76
"github.com/docker/libnetwork/types"
87
)
98

@@ -28,15 +27,15 @@ var procGwNetwork = make(chan (bool), 1)
2827
- its deleted when an endpoint with GW joins the container
2928
*/
3029

31-
func (sb *sandbox) setupDefaultGW(srcEp *endpoint) error {
32-
var createOptions []EndpointOption
33-
c := srcEp.getNetwork().getController()
30+
func (sb *sandbox) setupDefaultGW() error {
3431

3532
// check if the conitainer already has a GW endpoint
3633
if ep := sb.getEndpointInGWNetwork(); ep != nil {
3734
return nil
3835
}
3936

37+
c := sb.controller
38+
4039
// Look for default gw network. In case of error (includes not found),
4140
// retry and create it if needed in a serialized execution.
4241
n, err := c.NetworkByName(libnGWNetwork)
@@ -46,19 +45,7 @@ func (sb *sandbox) setupDefaultGW(srcEp *endpoint) error {
4645
}
4746
}
4847

49-
if opt, ok := srcEp.generic[netlabel.PortMap]; ok {
50-
if pb, ok := opt.([]types.PortBinding); ok {
51-
createOptions = append(createOptions, CreateOptionPortMapping(pb))
52-
}
53-
}
54-
55-
if opt, ok := srcEp.generic[netlabel.ExposedPorts]; ok {
56-
if exp, ok := opt.([]types.TransportPort); ok {
57-
createOptions = append(createOptions, CreateOptionExposedPorts(exp))
58-
}
59-
}
60-
61-
createOptions = append(createOptions, CreateOptionAnonymous())
48+
createOptions := []EndpointOption{CreateOptionAnonymous()}
6249

6350
eplen := gwEPlen
6451
if len(sb.containerID) < gwEPlen {
@@ -74,16 +61,24 @@ func (sb *sandbox) setupDefaultGW(srcEp *endpoint) error {
7461
if err := epLocal.sbJoin(sb); err != nil {
7562
return fmt.Errorf("container %s: endpoint join on GW Network failed: %v", sb.containerID, err)
7663
}
64+
7765
return nil
7866
}
7967

68+
// If present, removes the endpoint connecting the sandbox to the default gw network.
69+
// Unless it is the endpoint designated to provide the external connectivity.
70+
// If the sandbox is being deleted, removes the endpoint unconditionally.
8071
func (sb *sandbox) clearDefaultGW() error {
8172
var ep *endpoint
8273

8374
if ep = sb.getEndpointInGWNetwork(); ep == nil {
8475
return nil
8576
}
8677

78+
if ep == sb.getGatewayEndpoint() && !sb.inDelete {
79+
return nil
80+
}
81+
8782
if err := ep.sbLeave(sb, false); err != nil {
8883
return fmt.Errorf("container %s: endpoint leaving GW Network failed: %v", sb.containerID, err)
8984
}
@@ -98,7 +93,7 @@ func (sb *sandbox) needDefaultGW() bool {
9893

9994
for _, ep := range sb.getConnectedEndpoints() {
10095
if ep.endpointInGWNetwork() {
101-
continue
96+
return false
10297
}
10398
if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
10499
continue
@@ -165,3 +160,16 @@ func (c *controller) defaultGwNetwork() (Network, error) {
165160
}
166161
return n, err
167162
}
163+
164+
// Returns the endpoint which is providing external connectivity to the sandbox
165+
func (sb *sandbox) getGatewayEndpoint() *endpoint {
166+
for _, ep := range sb.getConnectedEndpoints() {
167+
if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
168+
continue
169+
}
170+
if len(ep.Gateway()) != 0 {
171+
return ep
172+
}
173+
}
174+
return nil
175+
}

driverapi/driverapi.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ type Driver interface {
4242
// Leave method is invoked when a Sandbox detaches from an endpoint.
4343
Leave(nid, eid string) error
4444

45+
// ProgramExternalConnectivity invokes the driver method which does the necessary
46+
// programming to allow the external connectivity dictated by the passed options
47+
ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error
48+
49+
// RevokeExternalConnectivity aks the driver to remove any external connectivity
50+
// programming that was done so far
51+
RevokeExternalConnectivity(nid, eid string) error
52+
4553
// Type returns the the type of this driver, the network type this driver manages
4654
Type() string
4755
}

0 commit comments

Comments
 (0)