Files
moby/daemon/network/settings.go
Cory Snider 19f4c27d81 api/t/network: represent MAC addrs as byte slices
Make invalid states unrepresentable by moving away from stringly-typed
MAC address values in API structs. As go.dev/issue/29678 has not yet
been implemented, provide our own HardwareAddr byte-slice type which
implements TextMarshaler and TextUnmarshaler to retain compatibility
with the API wire format.

When stdlib's net.HardwareAddr type implements TextMarshaler and
TextUnmarshaler and GODEBUG=netmarshal becomes the default, we should be
able to make the type a straight alias for stdlib net.HardwareAddr as a
non-breaking change.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2025-10-30 17:11:38 -04:00

77 lines
2.3 KiB
Go

package network
import (
"net"
"sync"
networktypes "github.com/moby/moby/api/types/network"
clustertypes "github.com/moby/moby/v2/daemon/cluster/provider"
"github.com/pkg/errors"
)
// Settings stores configuration details about the daemon network config
// TODO Windows. Many of these fields can be factored out.,
type Settings struct {
SandboxID string
SandboxKey string
Networks map[string]*EndpointSettings
Service *clustertypes.ServiceConfig
Ports networktypes.PortMap
HasSwarmEndpoint bool
}
// 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
// DesiredMacAddress is the configured value, it's copied from MacAddress (the
// API param field) when the container is created.
DesiredMacAddress networktypes.HardwareAddr
}
// AttachmentStore stores the load balancer IP address for a network id.
type AttachmentStore struct {
sync.Mutex
// key: networkd id
// value: load balancer ip address
networkToNodeLBIP map[string]net.IP
}
// ResetAttachments clears any existing load balancer IP to network mapping and
// sets the mapping to the given attachments.
func (store *AttachmentStore) ResetAttachments(attachments map[string]string) error {
store.Lock()
defer store.Unlock()
store.clearAttachments()
for nid, nodeIP := range attachments {
ip, _, err := net.ParseCIDR(nodeIP)
if err != nil {
store.networkToNodeLBIP = make(map[string]net.IP)
return errors.Wrapf(err, "Failed to parse load balancer address %s", nodeIP)
}
store.networkToNodeLBIP[nid] = ip
}
return nil
}
// ClearAttachments clears all the mappings of network to load balancer IP Address.
func (store *AttachmentStore) ClearAttachments() {
store.Lock()
defer store.Unlock()
store.clearAttachments()
}
func (store *AttachmentStore) clearAttachments() {
store.networkToNodeLBIP = make(map[string]net.IP)
}
// GetIPForNetwork return the load balancer IP address for the given network.
func (store *AttachmentStore) GetIPForNetwork(networkID string) (net.IP, bool) {
store.Lock()
defer store.Unlock()
ip, exists := store.networkToNodeLBIP[networkID]
return ip, exists
}