Add internal network option

Signed-off-by: Chun Chen <ramichen@tencent.com>
This commit is contained in:
Chun Chen 2015-12-22 09:29:39 +08:00
parent b464d40ce6
commit 186a32acab
5 changed files with 44 additions and 0 deletions

View file

@ -103,6 +103,9 @@ func (sb *sandbox) needDefaultGW() bool {
if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
continue
}
if ep.getNetwork().Internal() {
return false
}
if ep.joinInfo.disableGatewayService {
return false
}

View file

@ -490,6 +490,12 @@ func parseNetworkOptions(id string, option options.Generic) (*networkConfigurati
config.EnableIPv6 = val.(bool)
}
if val, ok := option[netlabel.Internal]; ok {
if internal, ok := val.(bool); ok && internal {
return nil, &driverapi.ErrNotImplemented{}
}
}
// Finally validate the configuration
if err = config.Validate(); err != nil {
return nil, err

View file

@ -2327,3 +2327,10 @@ func TestParallel2(t *testing.T) {
func TestParallel3(t *testing.T) {
runParallelTests(t, 3)
}
func TestNetworkInternal(t *testing.T) {
_, err := controller.NewNetwork(bridgeNetType, "testnetworkinternal", libnetwork.NetworkOptionInternalNetwork())
if err == nil || err.Error() != (&driverapi.ErrNotImplemented{}).Error() {
t.Fatal("bridge network can't be internal")
}
}

View file

@ -41,6 +41,9 @@ const (
// Gateway represents the gateway for the network
Gateway = Prefix + ".gateway"
// Internal constant represents that the network is internal which disables default gateway service
Internal = Prefix + ".internal"
)
var (

View file

@ -163,6 +163,7 @@ type network struct {
persist bool
stopWatchCh chan struct{}
drvOnce *sync.Once
internal bool
sync.Mutex
}
@ -305,6 +306,7 @@ func (n *network) CopyTo(o datastore.KVObject) error {
dstN.dbIndex = n.dbIndex
dstN.dbExists = n.dbExists
dstN.drvOnce = n.drvOnce
dstN.internal = n.internal
for _, v4conf := range n.ipamV4Config {
dstV4Conf := &IpamConf{}
@ -391,6 +393,7 @@ func (n *network) MarshalJSON() ([]byte, error) {
}
netMap["ipamV6Info"] = string(iis)
}
netMap["internal"] = n.internal
return json.Marshal(netMap)
}
@ -454,6 +457,9 @@ func (n *network) UnmarshalJSON(b []byte) (err error) {
return err
}
}
if v, ok := netMap["internal"]; ok {
n.internal = v.(bool)
}
return nil
}
@ -480,6 +486,18 @@ func NetworkOptionPersist(persist bool) NetworkOption {
}
}
// NetworkOptionInternalNetwork returns an option setter to config the network
// to be internal which disables default gateway service
func NetworkOptionInternalNetwork() NetworkOption {
return func(n *network) {
n.internal = true
if n.generic == nil {
n.generic = make(map[string]interface{})
}
n.generic[netlabel.Internal] = true
}
}
// NetworkOptionIpam function returns an option setter for the ipam configuration for this network
func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ipV6 []*IpamConf) NetworkOption {
return func(n *network) {
@ -1187,3 +1205,10 @@ func (n *network) IpamInfo() ([]*IpamInfo, []*IpamInfo) {
return v4Info, v6Info
}
func (n *network) Internal() bool {
n.Lock()
defer n.Unlock()
return n.internal
}