func (a *Allocator) taskCreateNetworkAttachments(t *api.Task, s *api.Service) { // If task network attachments have already been filled in no // need to do anything else. if len(t.Networks) != 0 { return } var networks []*api.NetworkAttachment if isIngressNetworkNeeded(s) { networks = append(networks, &api.NetworkAttachment{Network: a.netCtx.ingressNetwork}) } a.store.View(func(tx store.ReadTx) { // Always prefer NetworkAttachmentConfig in the TaskSpec specNetworks := t.Spec.Networks if len(specNetworks) == 0 && s != nil && len(s.Spec.Networks) != 0 { specNetworks = s.Spec.Networks } for _, na := range specNetworks { n := store.GetNetwork(tx, na.Target) if n == nil { continue } attachment := api.NetworkAttachment{Network: n} attachment.Aliases = append(attachment.Aliases, na.Aliases...) attachment.Addresses = append(attachment.Addresses, na.Addresses...) networks = append(networks, &attachment) } }) taskUpdateNetworks(t, networks) }
func (a *Allocator) taskCreateNetworkAttachments(t *api.Task, s *api.Service) { // If task network attachments have already been filled in no // need to do anything else. if len(t.Networks) != 0 { return } var networks []*api.NetworkAttachment // The service to which this task belongs is trying to expose // ports to the external world. Automatically attach the task // to the ingress network. if s != nil && s.Spec.Endpoint != nil && len(s.Spec.Endpoint.Ports) != 0 { networks = append(networks, &api.NetworkAttachment{Network: a.netCtx.ingressNetwork}) } a.store.View(func(tx store.ReadTx) { // Always prefer NetworkAttachmentConfig in the TaskSpec specNetworks := t.Spec.Networks if len(specNetworks) == 0 && s != nil && len(s.Spec.Networks) != 0 { specNetworks = s.Spec.Networks } for _, na := range specNetworks { n := store.GetNetwork(tx, na.Target) if n == nil { continue } attachment := api.NetworkAttachment{Network: n} attachment.Aliases = append(attachment.Aliases, na.Aliases...) attachment.Addresses = append(attachment.Addresses, na.Addresses...) networks = append(networks, &attachment) } }) taskUpdateNetworks(t, networks) }