Browse code

vndr libnetwork 64ae58878fc8f95e4a167499d654e13fa36abdc7

Signed-off-by: Pradip Dhara <pradipd@microsoft.com>

Pradip Dhara authored on 2017/12/01 06:02:56
Showing 4 changed files
... ...
@@ -30,7 +30,7 @@ github.com/moby/buildkit aaff9d591ef128560018433fe61beb802e149de8
30 30
 github.com/tonistiigi/fsutil dea3a0da73aee887fc02142d995be764106ac5e2
31 31
 
32 32
 #get libnetwork packages
33
-github.com/docker/libnetwork f7d21337cf1eb628ad54eecac0881fa23ec266df
33
+github.com/docker/libnetwork 64ae58878fc8f95e4a167499d654e13fa36abdc7
34 34
 github.com/docker/go-events 9461782956ad83b30282bf90e31fa6a70c255ba9
35 35
 github.com/armon/go-radix e39d623f12e8e41c7b5529e9a9dd67a1e2261f80
36 36
 github.com/armon/go-metrics eb0af217e5e9747e41dd5303755356b62d28e3ec
... ...
@@ -837,11 +837,34 @@ addToStore:
837 837
 	if err = c.updateToStore(network); err != nil {
838 838
 		return nil, err
839 839
 	}
840
+	defer func() {
841
+		if err != nil {
842
+			if e := c.deleteFromStore(network); e != nil {
843
+				logrus.Warnf("could not rollback from store, network %v on failure (%v): %v", network, err, e)
844
+			}
845
+		}
846
+	}()
847
+
840 848
 	if network.configOnly {
841 849
 		return network, nil
842 850
 	}
843 851
 
844 852
 	joinCluster(network)
853
+	defer func() {
854
+		if err != nil {
855
+			network.cancelDriverWatches()
856
+			if e := network.leaveCluster(); e != nil {
857
+				logrus.Warnf("Failed to leave agent cluster on network %s on failure (%v): %v", network.name, err, e)
858
+			}
859
+		}
860
+	}()
861
+
862
+	if len(network.loadBalancerIP) != 0 {
863
+		if err = network.createLoadBalancerSandbox(); err != nil {
864
+			return nil, err
865
+		}
866
+	}
867
+
845 868
 	if !c.isDistributedControl() {
846 869
 		c.Lock()
847 870
 		arrangeIngressFilterRule()
... ...
@@ -199,39 +199,40 @@ func (i *IpamInfo) UnmarshalJSON(data []byte) error {
199 199
 }
200 200
 
201 201
 type network struct {
202
-	ctrlr        *controller
203
-	name         string
204
-	networkType  string
205
-	id           string
206
-	created      time.Time
207
-	scope        string // network data scope
208
-	labels       map[string]string
209
-	ipamType     string
210
-	ipamOptions  map[string]string
211
-	addrSpace    string
212
-	ipamV4Config []*IpamConf
213
-	ipamV6Config []*IpamConf
214
-	ipamV4Info   []*IpamInfo
215
-	ipamV6Info   []*IpamInfo
216
-	enableIPv6   bool
217
-	postIPv6     bool
218
-	epCnt        *endpointCnt
219
-	generic      options.Generic
220
-	dbIndex      uint64
221
-	dbExists     bool
222
-	persist      bool
223
-	stopWatchCh  chan struct{}
224
-	drvOnce      *sync.Once
225
-	resolverOnce sync.Once
226
-	resolver     []Resolver
227
-	internal     bool
228
-	attachable   bool
229
-	inDelete     bool
230
-	ingress      bool
231
-	driverTables []networkDBTable
232
-	dynamic      bool
233
-	configOnly   bool
234
-	configFrom   string
202
+	ctrlr          *controller
203
+	name           string
204
+	networkType    string
205
+	id             string
206
+	created        time.Time
207
+	scope          string // network data scope
208
+	labels         map[string]string
209
+	ipamType       string
210
+	ipamOptions    map[string]string
211
+	addrSpace      string
212
+	ipamV4Config   []*IpamConf
213
+	ipamV6Config   []*IpamConf
214
+	ipamV4Info     []*IpamInfo
215
+	ipamV6Info     []*IpamInfo
216
+	enableIPv6     bool
217
+	postIPv6       bool
218
+	epCnt          *endpointCnt
219
+	generic        options.Generic
220
+	dbIndex        uint64
221
+	dbExists       bool
222
+	persist        bool
223
+	stopWatchCh    chan struct{}
224
+	drvOnce        *sync.Once
225
+	resolverOnce   sync.Once
226
+	resolver       []Resolver
227
+	internal       bool
228
+	attachable     bool
229
+	inDelete       bool
230
+	ingress        bool
231
+	driverTables   []networkDBTable
232
+	dynamic        bool
233
+	configOnly     bool
234
+	configFrom     string
235
+	loadBalancerIP net.IP
235 236
 	sync.Mutex
236 237
 }
237 238
 
... ...
@@ -473,6 +474,7 @@ func (n *network) CopyTo(o datastore.KVObject) error {
473 473
 	dstN.ingress = n.ingress
474 474
 	dstN.configOnly = n.configOnly
475 475
 	dstN.configFrom = n.configFrom
476
+	dstN.loadBalancerIP = n.loadBalancerIP
476 477
 
477 478
 	// copy labels
478 479
 	if dstN.labels == nil {
... ...
@@ -589,6 +591,7 @@ func (n *network) MarshalJSON() ([]byte, error) {
589 589
 	netMap["ingress"] = n.ingress
590 590
 	netMap["configOnly"] = n.configOnly
591 591
 	netMap["configFrom"] = n.configFrom
592
+	netMap["loadBalancerIP"] = n.loadBalancerIP
592 593
 	return json.Marshal(netMap)
593 594
 }
594 595
 
... ...
@@ -699,6 +702,9 @@ func (n *network) UnmarshalJSON(b []byte) (err error) {
699 699
 	if v, ok := netMap["configFrom"]; ok {
700 700
 		n.configFrom = v.(string)
701 701
 	}
702
+	if v, ok := netMap["loadBalancerIP"]; ok {
703
+		n.loadBalancerIP = net.ParseIP(v.(string))
704
+	}
702 705
 	// Reconcile old networks with the recently added `--ipv6` flag
703 706
 	if !n.enableIPv6 {
704 707
 		n.enableIPv6 = len(n.ipamV6Info) > 0
... ...
@@ -799,6 +805,13 @@ func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ip
799 799
 	}
800 800
 }
801 801
 
802
+// NetworkOptionLBEndpoint function returns an option setter for the configuration of the load balancer endpoint for this network
803
+func NetworkOptionLBEndpoint(ip net.IP) NetworkOption {
804
+	return func(n *network) {
805
+		n.loadBalancerIP = ip
806
+	}
807
+}
808
+
802 809
 // NetworkOptionDriverOpts function returns an option setter for any driver parameter described by a map
803 810
 func NetworkOptionDriverOpts(opts map[string]string) NetworkOption {
804 811
 	return func(n *network) {
... ...
@@ -944,6 +957,18 @@ func (n *network) delete(force bool) error {
944 944
 		return &UnknownNetworkError{name: name, id: id}
945 945
 	}
946 946
 
947
+	if len(n.loadBalancerIP) != 0 {
948
+		endpoints := n.Endpoints()
949
+		if force || len(endpoints) == 1 {
950
+			n.deleteLoadBalancerSandbox()
951
+		}
952
+		//Reload the network from the store to update the epcnt.
953
+		n, err = c.getNetworkFromStore(id)
954
+		if err != nil {
955
+			return &UnknownNetworkError{name: name, id: id}
956
+		}
957
+	}
958
+
947 959
 	if !force && n.getEpCnt().EndpointCnt() != 0 {
948 960
 		if n.configOnly {
949 961
 			return types.ForbiddenErrorf("configuration network %q is in use", n.Name())
... ...
@@ -1071,12 +1096,19 @@ func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoi
1071 1071
 		return nil, types.ForbiddenErrorf("endpoint with name %s already exists in network %s", name, n.Name())
1072 1072
 	}
1073 1073
 
1074
-	ep := &endpoint{name: name, generic: make(map[string]interface{}), iface: &endpointInterface{}}
1075
-	ep.id = stringid.GenerateRandomID()
1076
-
1077 1074
 	n.ctrlr.networkLocker.Lock(n.id)
1078 1075
 	defer n.ctrlr.networkLocker.Unlock(n.id)
1079 1076
 
1077
+	return n.createEndpoint(name, options...)
1078
+
1079
+}
1080
+
1081
+func (n *network) createEndpoint(name string, options ...EndpointOption) (Endpoint, error) {
1082
+	var err error
1083
+
1084
+	ep := &endpoint{name: name, generic: make(map[string]interface{}), iface: &endpointInterface{}}
1085
+	ep.id = stringid.GenerateRandomID()
1086
+
1080 1087
 	// Initialize ep.network with a possibly stale copy of n. We need this to get network from
1081 1088
 	// store. But once we get it from store we will have the most uptodate copy possibly.
1082 1089
 	ep.network = n
... ...
@@ -2021,3 +2053,80 @@ func (c *controller) getConfigNetwork(name string) (*network, error) {
2021 2021
 
2022 2022
 	return n.(*network), nil
2023 2023
 }
2024
+
2025
+func (n *network) createLoadBalancerSandbox() error {
2026
+	sandboxName := n.name + "-sbox"
2027
+	sbOptions := []SandboxOption{}
2028
+	if n.ingress {
2029
+		sbOptions = append(sbOptions, OptionIngress())
2030
+	}
2031
+	sb, err := n.ctrlr.NewSandbox(sandboxName, sbOptions...)
2032
+	if err != nil {
2033
+		return err
2034
+	}
2035
+	defer func() {
2036
+		if err != nil {
2037
+			if e := n.ctrlr.SandboxDestroy(sandboxName); e != nil {
2038
+				logrus.Warnf("could not delete sandbox %s on failure on failure (%v): %v", sandboxName, err, e)
2039
+			}
2040
+		}
2041
+	}()
2042
+
2043
+	endpointName := n.name + "-endpoint"
2044
+	epOptions := []EndpointOption{
2045
+		CreateOptionIpam(n.loadBalancerIP, nil, nil, nil),
2046
+		CreateOptionLoadBalancer(),
2047
+	}
2048
+	ep, err := n.createEndpoint(endpointName, epOptions...)
2049
+	if err != nil {
2050
+		return err
2051
+	}
2052
+	defer func() {
2053
+		if err != nil {
2054
+			if e := ep.Delete(true); e != nil {
2055
+				logrus.Warnf("could not delete endpoint %s on failure on failure (%v): %v", endpointName, err, e)
2056
+			}
2057
+		}
2058
+	}()
2059
+
2060
+	if err := ep.Join(sb, nil); err != nil {
2061
+		return err
2062
+	}
2063
+	return sb.EnableService()
2064
+}
2065
+
2066
+func (n *network) deleteLoadBalancerSandbox() {
2067
+	n.Lock()
2068
+	c := n.ctrlr
2069
+	name := n.name
2070
+	n.Unlock()
2071
+
2072
+	endpointName := name + "-endpoint"
2073
+	sandboxName := name + "-sbox"
2074
+
2075
+	endpoint, err := n.EndpointByName(endpointName)
2076
+	if err != nil {
2077
+		logrus.Warnf("Failed to find load balancer endpoint %s on network %s: %v", endpointName, name, err)
2078
+	} else {
2079
+
2080
+		info := endpoint.Info()
2081
+		if info != nil {
2082
+			sb := info.Sandbox()
2083
+			if sb != nil {
2084
+				if err := sb.DisableService(); err != nil {
2085
+					logrus.Warnf("Failed to disable service on sandbox %s: %v", sandboxName, err)
2086
+					//Ignore error and attempt to delete the load balancer endpoint
2087
+				}
2088
+			}
2089
+		}
2090
+
2091
+		if err := endpoint.Delete(true); err != nil {
2092
+			logrus.Warnf("Failed to delete endpoint %s (%s) in %s: %v", endpoint.Name(), endpoint.ID(), sandboxName, err)
2093
+			//Ignore error and attempt to delete the sandbox.
2094
+		}
2095
+	}
2096
+
2097
+	if err := c.SandboxDestroy(sandboxName); err != nil {
2098
+		logrus.Warnf("Failed to delete %s sandbox: %v", sandboxName, err)
2099
+	}
2100
+}
... ...
@@ -256,7 +256,7 @@ retry:
256 256
 			if err := cs.GetObject(datastore.Key(kvObject.Key()...), kvObject); err != nil {
257 257
 				return fmt.Errorf("could not update the kvobject to latest when trying to delete: %v", err)
258 258
 			}
259
-			logrus.Errorf("Error (%v) deleting object %v, retrying....", err, kvObject.Key())
259
+			logrus.Warnf("Error (%v) deleting object %v, retrying....", err, kvObject.Key())
260 260
 			goto retry
261 261
 		}
262 262
 		return err