service.go 2.5 KB

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