2018-02-05 21:05:59 +00:00
|
|
|
package network // import "github.com/docker/docker/daemon/network"
|
2014-04-14 06:15:31 +00:00
|
|
|
|
2015-12-09 21:38:39 +00:00
|
|
|
import (
|
2017-08-29 06:49:26 +00:00
|
|
|
"net"
|
2018-08-07 18:09:04 +00:00
|
|
|
"sync"
|
2017-08-29 06:49:26 +00:00
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
networktypes "github.com/docker/docker/api/types/network"
|
2016-06-14 02:52:49 +00:00
|
|
|
clustertypes "github.com/docker/docker/daemon/cluster/provider"
|
2015-12-18 17:58:48 +00:00
|
|
|
"github.com/docker/go-connections/nat"
|
2017-08-29 06:49:26 +00:00
|
|
|
"github.com/pkg/errors"
|
2015-12-09 21:38:39 +00:00
|
|
|
)
|
2015-10-09 18:21:48 +00:00
|
|
|
|
2015-07-21 11:34:57 +00:00
|
|
|
// Settings stores configuration details about the daemon network config
|
2015-08-07 02:21:00 +00:00
|
|
|
// TODO Windows. Many of these fields can be factored out.,
|
2015-04-04 04:06:48 +00:00
|
|
|
type Settings struct {
|
2015-05-06 22:39:29 +00:00
|
|
|
Bridge string
|
2015-09-02 23:43:28 +00:00
|
|
|
SandboxID string
|
2015-05-06 22:39:29 +00:00
|
|
|
HairpinMode bool
|
2015-01-08 23:03:19 +00:00
|
|
|
LinkLocalIPv6Address string
|
|
|
|
LinkLocalIPv6PrefixLen int
|
2016-08-23 23:50:15 +00:00
|
|
|
Networks map[string]*EndpointSettings
|
2016-06-14 02:52:49 +00:00
|
|
|
Service *clustertypes.ServiceConfig
|
2015-01-08 23:03:19 +00:00
|
|
|
Ports nat.PortMap
|
2015-05-06 22:39:29 +00:00
|
|
|
SandboxKey string
|
2015-12-09 21:38:39 +00:00
|
|
|
SecondaryIPAddresses []networktypes.Address
|
|
|
|
SecondaryIPv6Addresses []networktypes.Address
|
2015-10-21 16:08:19 +00:00
|
|
|
IsAnonymousEndpoint bool
|
2016-09-09 16:55:57 +00:00
|
|
|
HasSwarmEndpoint bool
|
2014-04-14 06:15:31 +00:00
|
|
|
}
|
2016-08-23 23:50:15 +00:00
|
|
|
|
|
|
|
// EndpointSettings is a package local wrapper for
|
|
|
|
// networktypes.EndpointSettings which stores Endpoint state that
|
|
|
|
// needs to be persisted to disk but not exposed in the api.
|
|
|
|
type EndpointSettings struct {
|
|
|
|
*networktypes.EndpointSettings
|
|
|
|
IPAMOperational bool
|
|
|
|
}
|
2017-08-29 06:49:26 +00:00
|
|
|
|
2017-09-22 06:04:34 +00:00
|
|
|
// AttachmentStore stores the load balancer IP address for a network id.
|
|
|
|
type AttachmentStore struct {
|
2018-08-07 18:09:04 +00:00
|
|
|
sync.Mutex
|
2019-11-27 14:43:53 +00:00
|
|
|
// key: networkd id
|
|
|
|
// value: load balancer ip address
|
2017-08-29 06:49:26 +00:00
|
|
|
networkToNodeLBIP map[string]net.IP
|
|
|
|
}
|
|
|
|
|
2017-10-17 03:30:05 +00:00
|
|
|
// ResetAttachments clears any existing load balancer IP to network mapping and
|
2017-09-22 06:04:34 +00:00
|
|
|
// sets the mapping to the given attachments.
|
|
|
|
func (store *AttachmentStore) ResetAttachments(attachments map[string]string) error {
|
2018-08-07 18:09:04 +00:00
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
store.clearAttachments()
|
2017-09-22 06:04:34 +00:00
|
|
|
for nid, nodeIP := range attachments {
|
2017-08-29 06:49:26 +00:00
|
|
|
ip, _, err := net.ParseCIDR(nodeIP)
|
|
|
|
if err != nil {
|
2017-09-22 06:04:34 +00:00
|
|
|
store.networkToNodeLBIP = make(map[string]net.IP)
|
2017-08-29 06:49:26 +00:00
|
|
|
return errors.Wrapf(err, "Failed to parse load balancer address %s", nodeIP)
|
|
|
|
}
|
2017-09-22 06:04:34 +00:00
|
|
|
store.networkToNodeLBIP[nid] = ip
|
2017-08-29 06:49:26 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-22 06:04:34 +00:00
|
|
|
// ClearAttachments clears all the mappings of network to load balancer IP Address.
|
|
|
|
func (store *AttachmentStore) ClearAttachments() {
|
2018-08-07 18:09:04 +00:00
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
store.clearAttachments()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *AttachmentStore) clearAttachments() {
|
2017-09-22 06:04:34 +00:00
|
|
|
store.networkToNodeLBIP = make(map[string]net.IP)
|
2017-08-29 06:49:26 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 06:04:34 +00:00
|
|
|
// GetIPForNetwork return the load balancer IP address for the given network.
|
|
|
|
func (store *AttachmentStore) GetIPForNetwork(networkID string) (net.IP, bool) {
|
2018-08-07 18:09:04 +00:00
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
2017-09-22 06:04:34 +00:00
|
|
|
ip, exists := store.networkToNodeLBIP[networkID]
|
2017-08-29 06:49:26 +00:00
|
|
|
return ip, exists
|
|
|
|
}
|