service.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package libnetwork
  2. import (
  3. "fmt"
  4. "net"
  5. "sync"
  6. "github.com/docker/docker/libnetwork/internal/setmatrix"
  7. )
  8. var (
  9. // A global monotonic counter to assign firewall marks to
  10. // services.
  11. fwMarkCtr uint32 = 256
  12. fwMarkCtrMu sync.Mutex
  13. )
  14. type portConfigs []*PortConfig
  15. func (p portConfigs) String() string {
  16. if len(p) == 0 {
  17. return ""
  18. }
  19. pc := p[0]
  20. str := fmt.Sprintf("%d:%d/%s", pc.PublishedPort, pc.TargetPort, PortConfig_Protocol_name[int32(pc.Protocol)])
  21. for _, pc := range p[1:] {
  22. str = str + fmt.Sprintf(",%d:%d/%s", pc.PublishedPort, pc.TargetPort, PortConfig_Protocol_name[int32(pc.Protocol)])
  23. }
  24. return str
  25. }
  26. type serviceKey struct {
  27. id string
  28. ports string
  29. }
  30. type service struct {
  31. name string // Service Name
  32. id string // Service ID
  33. // Map of loadbalancers for the service one-per attached
  34. // network. It is keyed with network ID.
  35. loadBalancers map[string]*loadBalancer
  36. // List of ingress ports exposed by the service
  37. ingressPorts portConfigs
  38. // Service aliases
  39. aliases []string
  40. // This maps tracks for each IP address the list of endpoints ID
  41. // associated with it. At stable state the endpoint ID expected is 1
  42. // but during transition and service change it is possible to have
  43. // temporary more than 1
  44. ipToEndpoint setmatrix.SetMatrix[string]
  45. deleted bool
  46. sync.Mutex
  47. }
  48. // assignIPToEndpoint inserts the mapping between the IP and the endpoint identifier
  49. // returns true if the mapping was not present, false otherwise
  50. // returns also the number of endpoints associated to the IP
  51. func (s *service) assignIPToEndpoint(ip, eID string) (bool, int) {
  52. return s.ipToEndpoint.Insert(ip, eID)
  53. }
  54. // removeIPToEndpoint removes the mapping between the IP and the endpoint identifier
  55. // returns true if the mapping was deleted, false otherwise
  56. // returns also the number of endpoints associated to the IP
  57. func (s *service) removeIPToEndpoint(ip, eID string) (bool, int) {
  58. return s.ipToEndpoint.Remove(ip, eID)
  59. }
  60. func (s *service) printIPToEndpoint(ip string) (string, bool) {
  61. return s.ipToEndpoint.String(ip)
  62. }
  63. type lbBackend struct {
  64. ip net.IP
  65. disabled bool
  66. }
  67. type loadBalancer struct {
  68. vip net.IP
  69. fwMark uint32
  70. // Map of backend IPs backing this loadbalancer on this
  71. // network. It is keyed with endpoint ID.
  72. backEnds map[string]*lbBackend
  73. // Back pointer to service to which the loadbalancer belongs.
  74. service *service
  75. sync.Mutex
  76. }