浏览代码

Add DisableGatewayService in JoinInfo to offering drivers the ability to disable default gateway

Signed-off-by: Chun Chen <ramichen@tencent.com>
Chun Chen 9 年之前
父节点
当前提交
141c51273f

+ 2 - 0
libnetwork/cmd/ovrouter/ovrouter.go

@@ -92,6 +92,8 @@ func (ep *endpoint) AddStaticRoute(destination *net.IPNet, routeType int,
 	return nil
 }
 
+func (ep *endpoint) DisableGatewayService() {}
+
 func main() {
 	if reexec.Init() {
 		return

+ 3 - 0
libnetwork/default_gateway.go

@@ -103,6 +103,9 @@ func (sb *sandbox) needDefaultGW() bool {
 		if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
 			continue
 		}
+		if ep.joinInfo.disableGatewayService {
+			return false
+		}
 		// TODO v6 needs to be handled.
 		if len(ep.Gateway()) > 0 {
 			return false

+ 3 - 0
libnetwork/driverapi/driverapi.go

@@ -91,6 +91,9 @@ type JoinInfo interface {
 	// AddStaticRoute adds a routes to the sandbox.
 	// It may be used in addtion to or instead of a default gateway (as above).
 	AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP) error
+
+	// DisableGatewayService tells libnetwork not to provide Default GW for the container
+	DisableGatewayService()
 }
 
 // DriverCallback provides a Callback interface for Drivers into LibNetwork

+ 2 - 0
libnetwork/drivers/bridge/bridge_test.go

@@ -416,6 +416,8 @@ func (te *testEndpoint) AddStaticRoute(destination *net.IPNet, routeType int, ne
 	return nil
 }
 
+func (te *testEndpoint) DisableGatewayService() {}
+
 func TestQueryEndpointInfo(t *testing.T) {
 	testQueryEndpointInfo(t, true)
 }

+ 5 - 4
libnetwork/drivers/remote/api/api.go

@@ -134,10 +134,11 @@ type StaticRoute struct {
 // JoinResponse is the response to a JoinRequest.
 type JoinResponse struct {
 	Response
-	InterfaceName *InterfaceName
-	Gateway       string
-	GatewayIPv6   string
-	StaticRoutes  []StaticRoute
+	InterfaceName         *InterfaceName
+	Gateway               string
+	GatewayIPv6           string
+	StaticRoutes          []StaticRoute
+	DisableGatewayService bool
 }
 
 // LeaveRequest describes the API for detaching an endpoint from a sandbox.

+ 3 - 0
libnetwork/drivers/remote/driver.go

@@ -231,6 +231,9 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
 			}
 		}
 	}
+	if res.DisableGatewayService {
+		jinfo.DisableGatewayService()
+	}
 	return nil
 }
 

+ 19 - 14
libnetwork/drivers/remote/driver_test.go

@@ -64,19 +64,20 @@ func setupPlugin(t *testing.T, name string, mux *http.ServeMux) func() {
 }
 
 type testEndpoint struct {
-	t              *testing.T
-	src            string
-	dst            string
-	address        string
-	addressIPv6    string
-	macAddress     string
-	gateway        string
-	gatewayIPv6    string
-	resolvConfPath string
-	hostsPath      string
-	nextHop        string
-	destination    string
-	routeType      int
+	t                     *testing.T
+	src                   string
+	dst                   string
+	address               string
+	addressIPv6           string
+	macAddress            string
+	gateway               string
+	gatewayIPv6           string
+	resolvConfPath        string
+	hostsPath             string
+	nextHop               string
+	destination           string
+	routeType             int
+	disableGatewayService bool
 }
 
 func (test *testEndpoint) Interface() driverapi.InterfaceInfo {
@@ -191,6 +192,10 @@ func (test *testEndpoint) AddStaticRoute(destination *net.IPNet, routeType int,
 	return nil
 }
 
+func (test *testEndpoint) DisableGatewayService() {
+	test.disableGatewayService = true
+}
+
 func TestGetEmptyCapabilities(t *testing.T) {
 	var plugin = "test-net-driver-empty-cap"
 
@@ -343,7 +348,7 @@ func TestRemoteDriver(t *testing.T) {
 				"DstPrefix": ep.dst,
 			},
 			"StaticRoutes": []map[string]interface{}{
-				map[string]interface{}{
+				{
 					"Destination": ep.destination,
 					"RouteType":   ep.routeType,
 					"NextHop":     ep.nextHop,

+ 11 - 3
libnetwork/endpoint_info.go

@@ -136,9 +136,10 @@ func (epi *endpointInterface) CopyTo(dstEpi *endpointInterface) error {
 }
 
 type endpointJoinInfo struct {
-	gw           net.IP
-	gw6          net.IP
-	StaticRoutes []*types.StaticRoute
+	gw                    net.IP
+	gw6                   net.IP
+	StaticRoutes          []*types.StaticRoute
+	disableGatewayService bool
 }
 
 func (ep *endpoint) Info() EndpointInfo {
@@ -340,3 +341,10 @@ func (ep *endpoint) retrieveFromStore() (*endpoint, error) {
 	}
 	return n.getEndpointFromStore(ep.ID())
 }
+
+func (ep *endpoint) DisableGatewayService() {
+	ep.Lock()
+	defer ep.Unlock()
+
+	ep.joinInfo.disableGatewayService = true
+}