@@ -3,7 +3,6 @@ package libnetwork
33import (
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.
8071func (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+ }
0 commit comments