service.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package libnetwork
  2. import (
  3. "fmt"
  4. "net"
  5. "sync"
  6. )
  7. var (
  8. // A global monotonic counter to assign firewall marks to
  9. // services.
  10. fwMarkCtr uint32 = 256
  11. fwMarkCtrMu sync.Mutex
  12. )
  13. type portConfigs []*PortConfig
  14. func (p portConfigs) String() string {
  15. if len(p) == 0 {
  16. return ""
  17. }
  18. pc := p[0]
  19. str := fmt.Sprintf("%d:%d/%s", pc.PublishedPort, pc.TargetPort, PortConfig_Protocol_name[int32(pc.Protocol)])
  20. for _, pc := range p[1:] {
  21. str = str + fmt.Sprintf(",%d:%d/%s", pc.PublishedPort, pc.TargetPort, PortConfig_Protocol_name[int32(pc.Protocol)])
  22. }
  23. return str
  24. }
  25. type serviceKey struct {
  26. id string
  27. ports string
  28. }
  29. type service struct {
  30. name string // Service Name
  31. id string // Service ID
  32. // Map of loadbalancers for the service one-per attached
  33. // network. It is keyed with network ID.
  34. loadBalancers map[string]*loadBalancer
  35. // List of ingress ports exposed by the service
  36. ingressPorts portConfigs
  37. // Service aliases
  38. aliases []string
  39. sync.Mutex
  40. }
  41. type loadBalancer struct {
  42. vip net.IP
  43. fwMark uint32
  44. // Map of backend IPs backing this loadbalancer on this
  45. // network. It is keyed with endpoint ID.
  46. backEnds map[string]net.IP
  47. // Back pointer to service to which the loadbalancer belongs.
  48. service *service
  49. }