2015-09-25 17:02:31 +00:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-11-10 02:01:07 +00:00
|
|
|
"strings"
|
2015-09-25 17:02:31 +00:00
|
|
|
|
2016-11-10 02:01:07 +00:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
2015-09-25 17:02:31 +00:00
|
|
|
"github.com/docker/libnetwork/types"
|
2017-07-26 22:03:47 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-09-25 17:02:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-11-10 02:01:07 +00:00
|
|
|
gwEPlen = 12
|
2015-09-25 17:02:31 +00:00
|
|
|
)
|
|
|
|
|
2016-01-26 03:51:21 +00:00
|
|
|
var procGwNetwork = make(chan (bool), 1)
|
|
|
|
|
2015-09-25 17:02:31 +00:00
|
|
|
/*
|
2016-08-11 06:31:01 +00:00
|
|
|
libnetwork creates a bridge network "docker_gw_bridge" for providing
|
2015-09-25 17:02:31 +00:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2016-03-09 02:47:02 +00:00
|
|
|
func (sb *sandbox) setupDefaultGW() error {
|
2015-09-25 17:02:31 +00:00
|
|
|
|
2016-08-11 06:31:01 +00:00
|
|
|
// check if the container already has a GW endpoint
|
2015-09-25 17:02:31 +00:00
|
|
|
if ep := sb.getEndpointInGWNetwork(); ep != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-09 02:47:02 +00:00
|
|
|
c := sb.controller
|
|
|
|
|
2016-01-26 03:51:21 +00:00
|
|
|
// Look for default gw network. In case of error (includes not found),
|
|
|
|
// retry and create it if needed in a serialized execution.
|
2015-09-25 17:02:31 +00:00
|
|
|
n, err := c.NetworkByName(libnGWNetwork)
|
|
|
|
if err != nil {
|
2016-01-26 03:51:21 +00:00
|
|
|
if n, err = c.defaultGwNetwork(); err != nil {
|
2015-09-25 17:02:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-09 02:47:02 +00:00
|
|
|
createOptions := []EndpointOption{CreateOptionAnonymous()}
|
2015-10-26 20:56:56 +00:00
|
|
|
|
2015-09-25 17:02:31 +00:00
|
|
|
eplen := gwEPlen
|
|
|
|
if len(sb.containerID) < gwEPlen {
|
|
|
|
eplen = len(sb.containerID)
|
|
|
|
}
|
|
|
|
|
2016-11-10 02:01:07 +00:00
|
|
|
sbLabels := sb.Labels()
|
|
|
|
|
|
|
|
if sbLabels[netlabel.PortMap] != nil {
|
|
|
|
createOptions = append(createOptions, CreateOptionPortMapping(sbLabels[netlabel.PortMap].([]types.PortBinding)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if sbLabels[netlabel.ExposedPorts] != nil {
|
|
|
|
createOptions = append(createOptions, CreateOptionExposedPorts(sbLabels[netlabel.ExposedPorts].([]types.TransportPort)))
|
|
|
|
}
|
|
|
|
|
|
|
|
epOption := getPlatformOption()
|
|
|
|
if epOption != nil {
|
|
|
|
createOptions = append(createOptions, epOption)
|
|
|
|
}
|
|
|
|
|
2015-09-25 17:02:31 +00:00
|
|
|
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)
|
|
|
|
}
|
2017-04-14 23:49:39 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if err2 := newEp.Delete(true); err2 != nil {
|
|
|
|
logrus.Warnf("Failed to remove gw endpoint for container %s after failing to join the gateway network: %v",
|
|
|
|
sb.containerID, err2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-09-25 17:02:31 +00:00
|
|
|
epLocal := newEp.(*endpoint)
|
|
|
|
|
2017-04-14 23:49:39 +00:00
|
|
|
if err = epLocal.sbJoin(sb); err != nil {
|
2015-09-25 17:02:31 +00:00
|
|
|
return fmt.Errorf("container %s: endpoint join on GW Network failed: %v", sb.containerID, err)
|
|
|
|
}
|
2016-03-09 02:47:02 +00:00
|
|
|
|
2015-09-25 17:02:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-06 19:18:21 +00:00
|
|
|
// If present, detach and remove the endpoint connecting the sandbox to the default gw network.
|
2015-09-25 17:02:31 +00:00
|
|
|
func (sb *sandbox) clearDefaultGW() error {
|
|
|
|
var ep *endpoint
|
|
|
|
|
|
|
|
if ep = sb.getEndpointInGWNetwork(); ep == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-19 21:49:13 +00:00
|
|
|
if err := ep.sbLeave(sb, false); err != nil {
|
2015-09-25 17:02:31 +00:00
|
|
|
return fmt.Errorf("container %s: endpoint leaving GW Network failed: %v", sb.containerID, err)
|
|
|
|
}
|
2016-01-12 19:33:16 +00:00
|
|
|
if err := ep.Delete(false); err != nil {
|
2015-09-25 17:02:31 +00:00
|
|
|
return fmt.Errorf("container %s: deleting endpoint on GW Network failed: %v", sb.containerID, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-06 19:18:21 +00:00
|
|
|
// Evaluate whether the sandbox requires a default gateway based
|
|
|
|
// on the endpoints to which it is connected. It does not account
|
|
|
|
// for the default gateway network endpoint.
|
|
|
|
|
2015-09-25 17:02:31 +00:00
|
|
|
func (sb *sandbox) needDefaultGW() bool {
|
|
|
|
var needGW bool
|
|
|
|
|
|
|
|
for _, ep := range sb.getConnectedEndpoints() {
|
|
|
|
if ep.endpointInGWNetwork() {
|
2016-04-06 19:18:21 +00:00
|
|
|
continue
|
2015-09-25 17:02:31 +00:00
|
|
|
}
|
|
|
|
if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
|
|
|
|
continue
|
|
|
|
}
|
2015-12-18 19:03:57 +00:00
|
|
|
if ep.getNetwork().Internal() {
|
2016-04-06 19:18:21 +00:00
|
|
|
continue
|
2015-12-18 19:03:57 +00:00
|
|
|
}
|
2016-04-06 19:18:21 +00:00
|
|
|
// During stale sandbox cleanup, joinInfo may be nil
|
|
|
|
if ep.joinInfo != nil && ep.joinInfo.disableGatewayService {
|
|
|
|
continue
|
2015-12-07 23:46:14 +00:00
|
|
|
}
|
2015-09-25 17:02:31 +00:00
|
|
|
// TODO v6 needs to be handled.
|
|
|
|
if len(ep.Gateway()) > 0 {
|
|
|
|
return false
|
|
|
|
}
|
2015-12-07 23:46:14 +00:00
|
|
|
for _, r := range ep.StaticRoutes() {
|
2016-11-21 19:44:58 +00:00
|
|
|
if r.Destination != nil && r.Destination.String() == "0.0.0.0/0" {
|
2015-12-07 23:46:14 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 17:02:31 +00:00
|
|
|
needGW = true
|
|
|
|
}
|
2016-04-06 19:18:21 +00:00
|
|
|
|
2015-09-25 17:02:31 +00:00
|
|
|
return needGW
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *sandbox) getEndpointInGWNetwork() *endpoint {
|
|
|
|
for _, ep := range sb.getConnectedEndpoints() {
|
2016-11-10 02:01:07 +00:00
|
|
|
if ep.getNetwork().name == libnGWNetwork && strings.HasPrefix(ep.Name(), "gateway_") {
|
2015-09-25 17:02:31 +00:00
|
|
|
return ep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) endpointInGWNetwork() bool {
|
2016-11-10 02:01:07 +00:00
|
|
|
if ep.getNetwork().name == libnGWNetwork && strings.HasPrefix(ep.Name(), "gateway_") {
|
2015-09-25 17:02:31 +00:00
|
|
|
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
|
|
|
|
}
|
2016-01-26 03:51:21 +00:00
|
|
|
|
|
|
|
// Looks for the default gw network and creates it if not there.
|
|
|
|
// Parallel executions are serialized.
|
|
|
|
func (c *controller) defaultGwNetwork() (Network, error) {
|
|
|
|
procGwNetwork <- true
|
|
|
|
defer func() { <-procGwNetwork }()
|
|
|
|
|
|
|
|
n, err := c.NetworkByName(libnGWNetwork)
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(types.NotFoundError); ok {
|
|
|
|
n, err = c.createGWNetwork()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
2016-03-09 02:47:02 +00:00
|
|
|
|
|
|
|
// Returns the endpoint which is providing external connectivity to the sandbox
|
|
|
|
func (sb *sandbox) getGatewayEndpoint() *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
|
|
|
|
}
|