@@ -371,12 +371,15 @@ func (a *Allocator) doNetworkAlloc(ctx context.Context, ev events.Event) {
371371 s := v .Service .Copy ()
372372
373373 if nc .nwkAllocator .IsServiceAllocated (s ) {
374- break
375- }
376-
377- if err := a .allocateService (ctx , s ); err != nil {
378- log .G (ctx ).WithError (err ).Errorf ("Failed allocation during update of service %s" , s .ID )
379- break
374+ if nc .nwkAllocator .PortsAllocatedInHostPublishMode (s ) {
375+ break
376+ }
377+ updatePortsInHostPublishMode (s )
378+ } else {
379+ if err := a .allocateService (ctx , s ); err != nil {
380+ log .G (ctx ).WithError (err ).Errorf ("Failed allocation during update of service %s" , s .ID )
381+ break
382+ }
380383 }
381384
382385 if _ , err := a .store .Batch (func (batch * store.Batch ) error {
@@ -641,6 +644,36 @@ func (a *Allocator) commitAllocatedNode(ctx context.Context, batch *store.Batch,
641644 return nil
642645}
643646
647+ // This function prepares the service object for being updated when the change regards
648+ // the published ports in host mode: It resets the runtime state ports (s.Endpoint.Ports)
649+ // to the current ingress mode runtime state ports plus the newly configured publish mode ports,
650+ // so that the service allocation invoked on this new service object will trigger the deallocation
651+ // of any old publish mode port and allocation of any new one.
652+ func updatePortsInHostPublishMode (s * api.Service ) {
653+ if s .Endpoint != nil {
654+ var portConfigs []* api.PortConfig
655+ for _ , portConfig := range s .Endpoint .Ports {
656+ if portConfig .PublishMode == api .PublishModeIngress {
657+ portConfigs = append (portConfigs , portConfig )
658+ }
659+ }
660+ s .Endpoint .Ports = portConfigs
661+ }
662+
663+ if s .Spec .Endpoint != nil {
664+ if s .Endpoint == nil {
665+ s .Endpoint = & api.Endpoint {}
666+ }
667+ for _ , portConfig := range s .Spec .Endpoint .Ports {
668+ if portConfig .PublishMode == api .PublishModeIngress {
669+ continue
670+ }
671+ s .Endpoint .Ports = append (s .Endpoint .Ports , portConfig .Copy ())
672+ }
673+ s .Endpoint .Spec = s .Spec .Endpoint .Copy ()
674+ }
675+ }
676+
644677func (a * Allocator ) allocateService (ctx context.Context , s * api.Service ) error {
645678 nc := a .netCtx
646679
0 commit comments