settings.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package network // import "github.com/docker/docker/daemon/network"
  2. import (
  3. "net"
  4. "sync"
  5. networktypes "github.com/docker/docker/api/types/network"
  6. clustertypes "github.com/docker/docker/daemon/cluster/provider"
  7. "github.com/docker/go-connections/nat"
  8. "github.com/pkg/errors"
  9. )
  10. // Settings stores configuration details about the daemon network config
  11. // TODO Windows. Many of these fields can be factored out.,
  12. type Settings struct {
  13. Bridge string
  14. SandboxID string
  15. SandboxKey string
  16. HairpinMode bool
  17. LinkLocalIPv6Address string
  18. LinkLocalIPv6PrefixLen int
  19. Networks map[string]*EndpointSettings
  20. Service *clustertypes.ServiceConfig
  21. Ports nat.PortMap
  22. SecondaryIPAddresses []networktypes.Address
  23. SecondaryIPv6Addresses []networktypes.Address
  24. IsAnonymousEndpoint bool
  25. HasSwarmEndpoint bool
  26. }
  27. // EndpointSettings is a package local wrapper for
  28. // networktypes.EndpointSettings which stores Endpoint state that
  29. // needs to be persisted to disk but not exposed in the api.
  30. type EndpointSettings struct {
  31. *networktypes.EndpointSettings
  32. IPAMOperational bool
  33. }
  34. // AttachmentStore stores the load balancer IP address for a network id.
  35. type AttachmentStore struct {
  36. sync.Mutex
  37. // key: networkd id
  38. // value: load balancer ip address
  39. networkToNodeLBIP map[string]net.IP
  40. }
  41. // ResetAttachments clears any existing load balancer IP to network mapping and
  42. // sets the mapping to the given attachments.
  43. func (store *AttachmentStore) ResetAttachments(attachments map[string]string) error {
  44. store.Lock()
  45. defer store.Unlock()
  46. store.clearAttachments()
  47. for nid, nodeIP := range attachments {
  48. ip, _, err := net.ParseCIDR(nodeIP)
  49. if err != nil {
  50. store.networkToNodeLBIP = make(map[string]net.IP)
  51. return errors.Wrapf(err, "Failed to parse load balancer address %s", nodeIP)
  52. }
  53. store.networkToNodeLBIP[nid] = ip
  54. }
  55. return nil
  56. }
  57. // ClearAttachments clears all the mappings of network to load balancer IP Address.
  58. func (store *AttachmentStore) ClearAttachments() {
  59. store.Lock()
  60. defer store.Unlock()
  61. store.clearAttachments()
  62. }
  63. func (store *AttachmentStore) clearAttachments() {
  64. store.networkToNodeLBIP = make(map[string]net.IP)
  65. }
  66. // GetIPForNetwork return the load balancer IP address for the given network.
  67. func (store *AttachmentStore) GetIPForNetwork(networkID string) (net.IP, bool) {
  68. store.Lock()
  69. defer store.Unlock()
  70. ip, exists := store.networkToNodeLBIP[networkID]
  71. return ip, exists
  72. }