@@ -37,6 +37,9 @@ type networkContext struct {
37
37
// the actual network allocation.
38
38
nwkAllocator networkallocator.NetworkAllocator
39
39
40
+ // The port allocator instance for allocating node ports
41
+ portAllocator * portAllocator
42
+
40
43
// A set of tasks which are ready to be allocated as a batch. This is
41
44
// distinct from "unallocatedTasks" which are tasks that failed to
42
45
// allocate on the first try, being held for a future retry.
@@ -95,6 +98,7 @@ func (a *Allocator) doNetworkInit(ctx context.Context) (err error) {
95
98
96
99
nc := & networkContext {
97
100
nwkAllocator : na ,
101
+ portAllocator : newPortAllocator (),
98
102
pendingTasks : make (map [string ]* api.Task ),
99
103
unallocatedTasks : make (map [string ]* api.Task ),
100
104
unallocatedServices : make (map [string ]* api.Service ),
@@ -233,7 +237,7 @@ func (a *Allocator) doNetworkAlloc(ctx context.Context, ev events.Event) {
233
237
break
234
238
}
235
239
236
- if nc .nwkAllocator . IsServiceAllocated (s ) {
240
+ if nc .isServiceAllocated (s ) {
237
241
break
238
242
}
239
243
@@ -261,8 +265,8 @@ func (a *Allocator) doNetworkAlloc(ctx context.Context, ev events.Event) {
261
265
break
262
266
}
263
267
264
- if nc .nwkAllocator . IsServiceAllocated (s ) {
265
- if ! nc .nwkAllocator . HostPublishPortsNeedUpdate (s ) {
268
+ if nc .isServiceAllocated (s ) {
269
+ if ! nc .portAllocator . hostPublishPortsNeedUpdate (s ) {
266
270
break
267
271
}
268
272
updatePortsInHostPublishMode (s )
@@ -287,6 +291,7 @@ func (a *Allocator) doNetworkAlloc(ctx context.Context, ev events.Event) {
287
291
if err := nc .nwkAllocator .DeallocateService (s ); err != nil {
288
292
log .G (ctx ).WithError (err ).Errorf ("Failed deallocation during delete of service %s" , s .ID )
289
293
} else {
294
+ nc .portAllocator .serviceDeallocatePorts (s )
290
295
nc .somethingWasDeallocated = true
291
296
}
292
297
@@ -681,7 +686,7 @@ func (a *Allocator) allocateServices(ctx context.Context, existingAddressesOnly
681
686
682
687
var allocatedServices []* api.Service
683
688
for _ , s := range services {
684
- if nc .nwkAllocator . IsServiceAllocated (s , networkallocator .OnInit ) {
689
+ if nc .isServiceAllocated (s , networkallocator .OnInit ) {
685
690
continue
686
691
}
687
692
if existingAddressesOnly &&
@@ -713,6 +718,23 @@ func (a *Allocator) allocateServices(ctx context.Context, existingAddressesOnly
713
718
return nil
714
719
}
715
720
721
+ // isServiceAllocated returns false if the passed service needs to have network resources allocated/updated.
722
+ func (nc * networkContext ) isServiceAllocated (s * api.Service , flags ... func (* networkallocator.ServiceAllocationOpts )) bool {
723
+ if ! nc .nwkAllocator .IsServiceAllocated (s , flags ... ) {
724
+ return false
725
+ }
726
+
727
+ var options networkallocator.ServiceAllocationOpts
728
+ for _ , flag := range flags {
729
+ flag (& options )
730
+ }
731
+ if (s .Spec .Endpoint != nil && len (s .Spec .Endpoint .Ports ) != 0 ) ||
732
+ (s .Endpoint != nil && len (s .Endpoint .Ports ) != 0 ) {
733
+ return nc .portAllocator .isPortsAllocatedOnInit (s , options .OnInit )
734
+ }
735
+ return true
736
+ }
737
+
716
738
// allocateTasks allocates tasks in the store so far before we started watching.
717
739
func (a * Allocator ) allocateTasks (ctx context.Context , existingAddressesOnly bool ) error {
718
740
var (
@@ -815,7 +837,7 @@ func taskReadyForNetworkVote(t *api.Task, s *api.Service, nc *networkContext) bo
815
837
// network configured or service endpoints have been
816
838
// allocated.
817
839
return (len (t .Networks ) == 0 || nc .nwkAllocator .IsTaskAllocated (t )) &&
818
- (s == nil || nc .nwkAllocator . IsServiceAllocated (s ))
840
+ (s == nil || nc .isServiceAllocated (s ))
819
841
}
820
842
821
843
func taskUpdateNetworks (t * api.Task , networks []* api.NetworkAttachment ) {
@@ -1203,10 +1225,15 @@ func (a *Allocator) allocateService(ctx context.Context, s *api.Service, existin
1203
1225
if err := nc .nwkAllocator .DeallocateService (s ); err != nil {
1204
1226
return err
1205
1227
}
1228
+ nc .portAllocator .serviceDeallocatePorts (s )
1206
1229
nc .somethingWasDeallocated = true
1207
1230
}
1208
1231
1232
+ if err := nc .portAllocator .serviceAllocatePorts (s ); err != nil {
1233
+ return err
1234
+ }
1209
1235
if err := nc .nwkAllocator .AllocateService (s ); err != nil {
1236
+ nc .portAllocator .serviceDeallocatePorts (s )
1210
1237
nc .unallocatedServices [s .ID ] = s
1211
1238
return err
1212
1239
}
@@ -1243,6 +1270,8 @@ func (a *Allocator) commitAllocatedService(ctx context.Context, batch *store.Bat
1243
1270
}); err != nil {
1244
1271
if err := a .netCtx .nwkAllocator .DeallocateService (s ); err != nil {
1245
1272
log .G (ctx ).WithError (err ).Errorf ("failed rolling back allocation of service %s" , s .ID )
1273
+ } else {
1274
+ a .netCtx .portAllocator .serviceDeallocatePorts (s )
1246
1275
}
1247
1276
1248
1277
return err
@@ -1298,7 +1327,7 @@ func (a *Allocator) allocateTask(ctx context.Context, t *api.Task) (err error) {
1298
1327
return
1299
1328
}
1300
1329
1301
- if ! nc .nwkAllocator . IsServiceAllocated (s ) {
1330
+ if ! nc .isServiceAllocated (s ) {
1302
1331
err = fmt .Errorf ("service %s to which task %s belongs has pending allocations" , s .ID , t .ID )
1303
1332
return
1304
1333
}
@@ -1423,7 +1452,7 @@ func (a *Allocator) procUnallocatedServices(ctx context.Context) {
1423
1452
nc := a .netCtx
1424
1453
var allocatedServices []* api.Service
1425
1454
for _ , s := range nc .unallocatedServices {
1426
- if ! nc .nwkAllocator . IsServiceAllocated (s ) {
1455
+ if ! nc .isServiceAllocated (s ) {
1427
1456
if err := a .allocateService (ctx , s , false ); err != nil {
1428
1457
log .G (ctx ).WithError (err ).Debugf ("Failed allocation of unallocated service %s" , s .ID )
1429
1458
continue
0 commit comments