Browse code

Merge pull request #32929 from aaronlehmann/vendor-swarmkit-ae52d9d

[17.05] Vendor swarmkit ae52d9d - fix service port publishing regression

Victor Vieux authored on 2017/05/02 06:54:58
Showing 3 changed files
... ...
@@ -105,7 +105,7 @@ github.com/docker/containerd 9048e5e50717ea4497b757314bad98ea3763c145
105 105
 github.com/tonistiigi/fifo 1405643975692217d6720f8b54aeee1bf2cd5cf4
106 106
 
107 107
 # cluster
108
-github.com/docker/swarmkit bd105f8afe9609137a48f817ae124295df0e8ef1
108
+github.com/docker/swarmkit ae52d9de97b91eee978bc2fe411bc85b33eb82dd
109 109
 github.com/gogo/protobuf 8d70fb3182befc465c4a1eac8ad4d38ff49778e2
110 110
 github.com/cloudflare/cfssl 7fb22c8cba7ecaf98e4082d22d65800cf45e042a
111 111
 github.com/google/certificate-transparency d90e65c3a07988180c5b1ece71791c0b6506826e
... ...
@@ -1089,12 +1089,7 @@ func updateTaskStatus(t *api.Task, newStatus api.TaskState, message string) {
1089 1089
 
1090 1090
 // IsIngressNetwork returns whether the passed network is an ingress network.
1091 1091
 func IsIngressNetwork(nw *api.Network) bool {
1092
-	if nw.Spec.Ingress {
1093
-		return true
1094
-	}
1095
-	// Check if legacy defined ingress network
1096
-	_, ok := nw.Spec.Annotations.Labels["com.docker.swarm.internal"]
1097
-	return ok && nw.Spec.Annotations.Name == "ingress"
1092
+	return networkallocator.IsIngressNetwork(nw)
1098 1093
 }
1099 1094
 
1100 1095
 // GetIngressNetwork fetches the ingress network from store.
... ...
@@ -153,7 +153,7 @@ func (na *NetworkAllocator) Deallocate(n *api.Network) error {
153 153
 // IP and ports needed by the service.
154 154
 func (na *NetworkAllocator) ServiceAllocate(s *api.Service) (err error) {
155 155
 	if err = na.portAllocator.serviceAllocatePorts(s); err != nil {
156
-		return
156
+		return err
157 157
 	}
158 158
 	defer func() {
159 159
 		if err != nil {
... ...
@@ -183,7 +183,7 @@ func (na *NetworkAllocator) ServiceAllocate(s *api.Service) (err error) {
183 183
 		}
184 184
 
185 185
 		delete(na.services, s.ID)
186
-		return
186
+		return nil
187 187
 	}
188 188
 
189 189
 	// Always prefer NetworkAttachmentConfig in the TaskSpec
... ...
@@ -194,48 +194,55 @@ func (na *NetworkAllocator) ServiceAllocate(s *api.Service) (err error) {
194 194
 
195 195
 	// Allocate VIPs for all the pre-populated endpoint attachments
196 196
 	eVIPs := s.Endpoint.VirtualIPs[:0]
197
+
198
+vipLoop:
197 199
 	for _, eAttach := range s.Endpoint.VirtualIPs {
198
-		match := false
200
+		if na.IsVIPOnIngressNetwork(eAttach) {
201
+			if err = na.allocateVIP(eAttach); err != nil {
202
+				return err
203
+			}
204
+			eVIPs = append(eVIPs, eAttach)
205
+			continue vipLoop
206
+
207
+		}
199 208
 		for _, nAttach := range specNetworks {
200 209
 			if nAttach.Target == eAttach.NetworkID {
201
-				match = true
202 210
 				if err = na.allocateVIP(eAttach); err != nil {
203
-					return
211
+					return err
204 212
 				}
205 213
 				eVIPs = append(eVIPs, eAttach)
206
-				break
214
+				continue vipLoop
207 215
 			}
208 216
 		}
209
-		//If the network of the VIP is not part of the service spec,
210
-		//deallocate the vip
211
-		if !match {
212
-			na.deallocateVIP(eAttach)
213
-		}
217
+		// If the network of the VIP is not part of the service spec,
218
+		// deallocate the vip
219
+		na.deallocateVIP(eAttach)
214 220
 	}
215
-	s.Endpoint.VirtualIPs = eVIPs
216 221
 
217
-outer:
222
+networkLoop:
218 223
 	for _, nAttach := range specNetworks {
219 224
 		for _, vip := range s.Endpoint.VirtualIPs {
220 225
 			if vip.NetworkID == nAttach.Target {
221
-				continue outer
226
+				continue networkLoop
222 227
 			}
223 228
 		}
224 229
 
225 230
 		vip := &api.Endpoint_VirtualIP{NetworkID: nAttach.Target}
226 231
 		if err = na.allocateVIP(vip); err != nil {
227
-			return
232
+			return err
228 233
 		}
229 234
 
230
-		s.Endpoint.VirtualIPs = append(s.Endpoint.VirtualIPs, vip)
235
+		eVIPs = append(eVIPs, vip)
231 236
 	}
232 237
 
233
-	if len(s.Endpoint.VirtualIPs) > 0 {
238
+	if len(eVIPs) > 0 {
234 239
 		na.services[s.ID] = struct{}{}
235 240
 	} else {
236 241
 		delete(na.services, s.ID)
237 242
 	}
238
-	return
243
+
244
+	s.Endpoint.VirtualIPs = eVIPs
245
+	return nil
239 246
 }
240 247
 
241 248
 // ServiceDeallocate de-allocates all the network resources such as
... ...
@@ -253,6 +260,7 @@ func (na *NetworkAllocator) ServiceDeallocate(s *api.Service) error {
253 253
 				WithField("vip.addr", vip.Addr).Error("error deallocating vip")
254 254
 		}
255 255
 	}
256
+	s.Endpoint.VirtualIPs = nil
256 257
 
257 258
 	na.portAllocator.serviceDeallocatePorts(s)
258 259
 	delete(na.services, s.ID)
... ...
@@ -346,34 +354,33 @@ func (na *NetworkAllocator) ServiceNeedsAllocation(s *api.Service, flags ...func
346 346
 			return true
347 347
 		}
348 348
 
349
+		// If the spec has networks which don't have a corresponding VIP,
350
+		// the service needs to be allocated.
351
+	networkLoop:
349 352
 		for _, net := range specNetworks {
350
-			match := false
351 353
 			for _, vip := range s.Endpoint.VirtualIPs {
352 354
 				if vip.NetworkID == net.Target {
353
-					match = true
354
-					break
355
+					continue networkLoop
355 356
 				}
356 357
 			}
357
-			if !match {
358
-				return true
359
-			}
358
+			return true
360 359
 		}
361 360
 	}
362 361
 
363
-	//If the spec no longer has networks attached and has a vip allocated
364
-	//from previous spec the service needs to updated
362
+	// If the spec no longer has networks attached and has a vip allocated
363
+	// from previous spec the service needs to allocated.
365 364
 	if s.Endpoint != nil {
365
+	vipLoop:
366 366
 		for _, vip := range s.Endpoint.VirtualIPs {
367
-			match := false
367
+			if na.IsVIPOnIngressNetwork(vip) {
368
+				continue vipLoop
369
+			}
368 370
 			for _, net := range specNetworks {
369 371
 				if vip.NetworkID == net.Target {
370
-					match = true
371
-					break
372
+					continue vipLoop
372 373
 				}
373 374
 			}
374
-			if !match {
375
-				return true
376
-			}
375
+			return true
377 376
 		}
378 377
 	}
379 378
 
... ...
@@ -885,3 +892,26 @@ func initializeDrivers(reg *drvregistry.DrvRegistry) error {
885 885
 	}
886 886
 	return nil
887 887
 }
888
+
889
+// IsVIPOnIngressNetwork check if the vip is in ingress network
890
+func (na *NetworkAllocator) IsVIPOnIngressNetwork(vip *api.Endpoint_VirtualIP) bool {
891
+	if vip == nil {
892
+		return false
893
+	}
894
+
895
+	localNet := na.getNetwork(vip.NetworkID)
896
+	if localNet != nil && localNet.nw != nil {
897
+		return IsIngressNetwork(localNet.nw)
898
+	}
899
+	return false
900
+}
901
+
902
+// IsIngressNetwork check if the network is an ingress network
903
+func IsIngressNetwork(nw *api.Network) bool {
904
+	if nw.Spec.Ingress {
905
+		return true
906
+	}
907
+	// Check if legacy defined ingress network
908
+	_, ok := nw.Spec.Annotations.Labels["com.docker.swarm.internal"]
909
+	return ok && nw.Spec.Annotations.Name == "ingress"
910
+}