github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/network/settings.go (about)

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