2015-09-06 21:34:50 -04:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/libnetwork/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
libnGWNetwork = "docker_gwbridge"
|
|
|
|
gwEPlen = 12
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
libnetwork creates a bridge network "docker_gw_bridge" for provding
|
|
|
|
default gateway for the containers if none of the container's endpoints
|
|
|
|
have GW set by the driver. ICC is set to false for the GW_bridge network.
|
|
|
|
|
|
|
|
If a driver can't provide external connectivity it can choose to not set
|
|
|
|
the GW IP for the endpoint.
|
|
|
|
|
|
|
|
endpoint on the GW_bridge network is managed dynamically by libnetwork.
|
|
|
|
ie:
|
|
|
|
- its created when an endpoint without GW joins the container
|
|
|
|
- its deleted when an endpoint with GW joins the container
|
|
|
|
*/
|
|
|
|
|
|
|
|
func (sb *sandbox) setupDefaultGW(srcEp *endpoint) error {
|
|
|
|
var createOptions []EndpointOption
|
|
|
|
c := srcEp.getNetwork().getController()
|
|
|
|
|
|
|
|
// check if the conitainer already has a GW endpoint
|
|
|
|
if ep := sb.getEndpointInGWNetwork(); ep != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := c.NetworkByName(libnGWNetwork)
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(types.NotFoundError); !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n, err = c.createGWNetwork()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt, ok := srcEp.generic[netlabel.PortMap]; ok {
|
|
|
|
if pb, ok := opt.([]types.PortBinding); ok {
|
|
|
|
createOptions = append(createOptions, CreateOptionPortMapping(pb))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt, ok := srcEp.generic[netlabel.ExposedPorts]; ok {
|
|
|
|
if exp, ok := opt.([]types.TransportPort); ok {
|
|
|
|
createOptions = append(createOptions, CreateOptionExposedPorts(exp))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-23 19:52:11 -04:00
|
|
|
createOptions = append(createOptions, CreateOptionAnonymous())
|
|
|
|
|
2015-09-06 21:34:50 -04:00
|
|
|
eplen := gwEPlen
|
|
|
|
if len(sb.containerID) < gwEPlen {
|
|
|
|
eplen = len(sb.containerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
newEp, err := n.CreateEndpoint("gateway_"+sb.containerID[0:eplen], createOptions...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("container %s: endpoint create on GW Network failed: %v", sb.containerID, err)
|
|
|
|
}
|
2015-09-18 20:33:55 -04:00
|
|
|
epLocal := newEp.(*endpoint)
|
2015-09-06 21:34:50 -04:00
|
|
|
|
2015-09-18 20:33:55 -04:00
|
|
|
if err := epLocal.sbJoin(sb); err != nil {
|
2015-09-06 21:34:50 -04:00
|
|
|
return fmt.Errorf("container %s: endpoint join on GW Network failed: %v", sb.containerID, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *sandbox) clearDefaultGW() error {
|
|
|
|
var ep *endpoint
|
|
|
|
|
|
|
|
if ep = sb.getEndpointInGWNetwork(); ep == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-16 17:24:44 -05:00
|
|
|
if err := ep.sbLeave(sb, false); err != nil {
|
2015-09-06 21:34:50 -04:00
|
|
|
return fmt.Errorf("container %s: endpoint leaving GW Network failed: %v", sb.containerID, err)
|
|
|
|
}
|
2016-01-08 14:24:14 -05:00
|
|
|
if err := ep.Delete(false); err != nil {
|
2015-09-06 21:34:50 -04:00
|
|
|
return fmt.Errorf("container %s: deleting endpoint on GW Network failed: %v", sb.containerID, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *sandbox) needDefaultGW() bool {
|
|
|
|
var needGW bool
|
|
|
|
|
|
|
|
for _, ep := range sb.getConnectedEndpoints() {
|
|
|
|
if ep.endpointInGWNetwork() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
|
|
|
|
continue
|
|
|
|
}
|
2015-12-21 20:29:39 -05:00
|
|
|
if ep.getNetwork().Internal() {
|
|
|
|
return false
|
|
|
|
}
|
2015-12-02 21:07:44 -05:00
|
|
|
if ep.joinInfo.disableGatewayService {
|
|
|
|
return false
|
|
|
|
}
|
2015-09-06 21:34:50 -04:00
|
|
|
// TODO v6 needs to be handled.
|
|
|
|
if len(ep.Gateway()) > 0 {
|
|
|
|
return false
|
|
|
|
}
|
2015-12-02 18:21:50 -05:00
|
|
|
for _, r := range ep.StaticRoutes() {
|
|
|
|
if r.Destination.String() == "0.0.0.0/0" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2015-09-06 21:34:50 -04:00
|
|
|
needGW = true
|
|
|
|
}
|
|
|
|
return needGW
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *sandbox) getEndpointInGWNetwork() *endpoint {
|
|
|
|
for _, ep := range sb.getConnectedEndpoints() {
|
|
|
|
if ep.getNetwork().name == libnGWNetwork {
|
|
|
|
return ep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) endpointInGWNetwork() bool {
|
|
|
|
if ep.getNetwork().name == libnGWNetwork {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *sandbox) getEPwithoutGateway() *endpoint {
|
|
|
|
for _, ep := range sb.getConnectedEndpoints() {
|
|
|
|
if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(ep.Gateway()) == 0 {
|
|
|
|
return ep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|