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