github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/manager/allocator/networkallocator/networkallocator.go (about) 1 package networkallocator 2 3 import ( 4 "github.com/docker/swarmkit/api" 5 ) 6 7 const ( 8 // PredefinedLabel identifies internally allocated swarm networks 9 // corresponding to the node-local predefined networks on the host. 10 PredefinedLabel = "com.docker.swarm.predefined" 11 ) 12 13 // PredefinedNetworkData contains the minimum set of data needed 14 // to create the correspondent predefined network object in the store. 15 type PredefinedNetworkData struct { 16 Name string 17 Driver string 18 } 19 20 // ServiceAllocationOpts is struct used for functional options in 21 // IsServiceAllocated 22 type ServiceAllocationOpts struct { 23 OnInit bool 24 } 25 26 // OnInit is called for allocator initialization stage 27 func OnInit(options *ServiceAllocationOpts) { 28 options.OnInit = true 29 } 30 31 // NetworkAllocator provides network model specific allocation functionality. 32 type NetworkAllocator interface { 33 // 34 // Network Allocation 35 // 36 37 // IsAllocated returns if the passed network has been allocated or not. 38 IsAllocated(n *api.Network) bool 39 40 // Allocate allocates all the necessary resources both general 41 // and driver-specific which may be specified in the NetworkSpec 42 Allocate(n *api.Network) error 43 44 // Deallocate frees all the general and driver specific resources 45 // which were assigned to the passed network. 46 Deallocate(n *api.Network) error 47 48 // 49 // Service Allocation 50 // 51 52 // IsServiceAllocated returns false if the passed service 53 // needs to have network resources allocated/updated. 54 IsServiceAllocated(s *api.Service, flags ...func(*ServiceAllocationOpts)) bool 55 56 // AllocateService allocates all the network resources such as virtual 57 // IP and ports needed by the service. 58 AllocateService(s *api.Service) (err error) 59 60 // DeallocateService de-allocates all the network resources such as 61 // virtual IP and ports associated with the service. 62 DeallocateService(s *api.Service) error 63 64 // HostPublishPortsNeedUpdate returns true if the passed service needs 65 // allocations for its published ports in host (non ingress) mode 66 HostPublishPortsNeedUpdate(s *api.Service) bool 67 68 // 69 // Task Allocation 70 // 71 72 // IsTaskAllocated returns if the passed task has its network 73 // resources allocated or not. 74 IsTaskAllocated(t *api.Task) bool 75 76 // AllocateTask allocates all the endpoint resources for all the 77 // networks that a task is attached to. 78 AllocateTask(t *api.Task) error 79 80 // DeallocateTask releases all the endpoint resources for all the 81 // networks that a task is attached to. 82 DeallocateTask(t *api.Task) error 83 84 // AllocateAttachment Allocates a load balancer endpoint for the node 85 AllocateAttachment(node *api.Node, networkAttachment *api.NetworkAttachment) error 86 87 // DeallocateAttachment Deallocates a load balancer endpoint for the node 88 DeallocateAttachment(node *api.Node, networkAttachment *api.NetworkAttachment) error 89 90 // IsAttachmentAllocated If lb endpoint is allocated on the node 91 IsAttachmentAllocated(node *api.Node, networkAttachment *api.NetworkAttachment) bool 92 } 93 94 // IsIngressNetwork check if the network is an ingress network 95 func IsIngressNetwork(nw *api.Network) bool { 96 if nw.Spec.Ingress { 97 return true 98 } 99 // Check if legacy defined ingress network 100 _, ok := nw.Spec.Annotations.Labels["com.docker.swarm.internal"] 101 return ok && nw.Spec.Annotations.Name == "ingress" 102 } 103 104 // IsIngressNetworkNeeded checks whether the service requires the routing-mesh 105 func IsIngressNetworkNeeded(s *api.Service) bool { 106 if s == nil { 107 return false 108 } 109 110 if s.Spec.Endpoint == nil { 111 return false 112 } 113 114 for _, p := range s.Spec.Endpoint.Ports { 115 // The service to which this task belongs is trying to 116 // expose ports with PublishMode as Ingress to the 117 // external world. Automatically attach the task to 118 // the ingress network. 119 if p.PublishMode == api.PublishModeIngress { 120 return true 121 } 122 } 123 124 return false 125 }