add //go:build directives to prevent downgrading to go1.16 language
This repository is not yet a module (i.e., does not have a `go.mod`). This
is not problematic when building the code in GOPATH or "vendor" mode, but
when using the code as a module-dependency (in module-mode), different semantics
are applied since Go1.21, which switches Go _language versions_ on a per-module,
per-package, or even per-file base.
A condensed summary of that logic [is as follows][1]:
- For modules that have a go.mod containing a go version directive; that
version is considered a minimum _required_ version (starting with the
go1.19.13 and go1.20.8 patch releases: before those, it was only a
recommendation).
- For dependencies that don't have a go.mod (not a module), go language
version go1.16 is assumed.
- Likewise, for modules that have a go.mod, but the file does not have a
go version directive, go language version go1.16 is assumed.
- If a go.work file is present, but does not have a go version directive,
language version go1.17 is assumed.
When switching language versions, Go _downgrades_ the language version,
which means that language features (such as generics, and `any`) are not
available, and compilation fails. For example:
# github.com/docker/cli/cli/context/store
/go/pkg/mod/github.com/docker/cli@v25.0.0-beta.2+incompatible/cli/context/store/storeconfig.go:6:24: predeclared any requires go1.18 or later (-lang was set to go1.16; check go.mod)
/go/pkg/mod/github.com/docker/cli@v25.0.0-beta.2+incompatible/cli/context/store/store.go:74:12: predeclared any requires go1.18 or later (-lang was set to go1.16; check go.mod)
Note that these fallbacks are per-module, per-package, and can even be
per-file, so _(indirect) dependencies_ can still use modern language
features, as long as their respective go.mod has a version specified.
Unfortunately, these failures do not occur when building locally (using
vendor / GOPATH mode), but will affect consumers of the module.
Obviously, this situation is not ideal, and the ultimate solution is to
move to go modules (add a go.mod), but this comes with a non-insignificant
risk in other areas (due to our complex dependency tree).
We can revert to using go1.16 language features only, but this may be
limiting, and may still be problematic when (e.g.) matching signatures
of dependencies.
There is an escape hatch: adding a `//go:build` directive to files that
make use of go language features. From the [go toolchain docs][2]:
> The go line for each module sets the language version the compiler enforces
> when compiling packages in that module. The language version can be changed
> on a per-file basis by using a build constraint.
>
> For example, a module containing code that uses the Go 1.21 language version
> should have a `go.mod` file with a go line such as `go 1.21` or `go 1.21.3`.
> If a specific source file should be compiled only when using a newer Go
> toolchain, adding `//go:build go1.22` to that source file both ensures that
> only Go 1.22 and newer toolchains will compile the file and also changes
> the language version in that file to Go 1.22.
This patch adds `//go:build` directives to those files using recent additions
to the language. It's currently using go1.19 as version to match the version
in our "vendor.mod", but we can consider being more permissive ("any" requires
go1.18 or up), or more "optimistic" (force go1.21, which is the version we
currently use to build).
For completeness sake, note that any file _without_ a `//go:build` directive
will continue to use go1.16 language version when used as a module.
[1]: https://github.com/golang/go/blob/58c28ba286dd0e98fe4cca80f5d64bbcb824a685/src/cmd/go/internal/gover/version.go#L9-L56
[2]: https://go.dev/doc/toolchain
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-12-15 13:26:31 +00:00
|
|
|
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
|
|
|
|
//go:build go1.19
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
package libnetwork
|
2015-02-27 17:34:30 +00:00
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
import (
|
2023-06-23 00:33:17 +00:00
|
|
|
"context"
|
2015-05-08 13:26:35 +00:00
|
|
|
"encoding/json"
|
2015-10-05 11:21:15 +00:00
|
|
|
"fmt"
|
2015-06-19 06:40:17 +00:00
|
|
|
"net"
|
2021-02-04 08:31:33 +00:00
|
|
|
"runtime"
|
2015-10-23 17:09:00 +00:00
|
|
|
"strings"
|
2015-04-13 18:40:42 +00:00
|
|
|
"sync"
|
2016-09-20 15:59:32 +00:00
|
|
|
"time"
|
2015-02-27 17:34:30 +00:00
|
|
|
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/containerd/log"
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/datastore"
|
|
|
|
"github.com/docker/docker/libnetwork/driverapi"
|
|
|
|
"github.com/docker/docker/libnetwork/etchosts"
|
|
|
|
"github.com/docker/docker/libnetwork/internal/setmatrix"
|
|
|
|
"github.com/docker/docker/libnetwork/ipamapi"
|
|
|
|
"github.com/docker/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/docker/libnetwork/netutils"
|
|
|
|
"github.com/docker/docker/libnetwork/networkdb"
|
|
|
|
"github.com/docker/docker/libnetwork/options"
|
2023-07-28 17:11:00 +00:00
|
|
|
"github.com/docker/docker/libnetwork/scope"
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2021-05-28 00:15:56 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2023-09-23 09:44:55 +00:00
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2015-04-13 18:40:42 +00:00
|
|
|
)
|
2015-02-27 17:34:30 +00:00
|
|
|
|
2015-04-20 16:46:11 +00:00
|
|
|
// EndpointWalker is a client provided function which will be used to walk the Endpoints.
|
|
|
|
// When the function returns true, the walk will stop.
|
2023-01-12 01:42:24 +00:00
|
|
|
type EndpointWalker func(ep *Endpoint) bool
|
2015-02-27 17:34:30 +00:00
|
|
|
|
2016-12-06 22:56:24 +00:00
|
|
|
// ipInfo is the reverse mapping from IP to service name to serve the PTR query.
|
2017-05-22 02:25:52 +00:00
|
|
|
// extResolver is set if an external server resolves a service name to this IP.
|
2021-08-26 08:02:04 +00:00
|
|
|
// It's an indication to defer PTR queries also to that external server.
|
2016-12-06 22:56:24 +00:00
|
|
|
type ipInfo struct {
|
|
|
|
name string
|
2017-06-18 12:25:58 +00:00
|
|
|
serviceID string
|
2016-12-06 22:56:24 +00:00
|
|
|
extResolver bool
|
|
|
|
}
|
|
|
|
|
2017-06-18 12:25:58 +00:00
|
|
|
// svcMapEntry is the body of the element into the svcMap
|
|
|
|
// The ip is a string because the SetMatrix does not accept non hashable values
|
|
|
|
type svcMapEntry struct {
|
|
|
|
ip string
|
|
|
|
serviceID string
|
|
|
|
}
|
|
|
|
|
2015-12-24 09:51:32 +00:00
|
|
|
type svcInfo struct {
|
2023-03-29 17:31:12 +00:00
|
|
|
svcMap setmatrix.SetMatrix[svcMapEntry]
|
|
|
|
svcIPv6Map setmatrix.SetMatrix[svcMapEntry]
|
|
|
|
ipMap setmatrix.SetMatrix[ipInfo]
|
2016-05-08 07:48:04 +00:00
|
|
|
service map[string][]servicePorts
|
2015-12-24 09:51:32 +00:00
|
|
|
}
|
2015-06-19 06:40:17 +00:00
|
|
|
|
2016-05-25 05:46:18 +00:00
|
|
|
// backing container or host's info
|
|
|
|
type serviceTarget struct {
|
|
|
|
name string
|
|
|
|
ip net.IP
|
|
|
|
port uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
type servicePorts struct {
|
|
|
|
portName string
|
|
|
|
proto string
|
|
|
|
target []serviceTarget
|
|
|
|
}
|
|
|
|
|
2017-03-02 07:57:37 +00:00
|
|
|
type networkDBTable struct {
|
|
|
|
name string
|
|
|
|
objType driverapi.ObjectType
|
|
|
|
}
|
|
|
|
|
2015-10-03 23:11:50 +00:00
|
|
|
// IpamConf contains all the ipam related configurations for a network
|
2023-09-15 10:44:30 +00:00
|
|
|
//
|
|
|
|
// TODO(aker): use proper net/* structs instead of string literals.
|
2015-10-03 23:11:50 +00:00
|
|
|
type IpamConf struct {
|
2021-08-26 08:02:04 +00:00
|
|
|
// PreferredPool is the master address pool for containers and network interfaces.
|
2015-10-03 23:11:50 +00:00
|
|
|
PreferredPool string
|
2021-08-26 08:02:04 +00:00
|
|
|
// SubPool is a subset of the master pool. If specified,
|
|
|
|
// this becomes the container pool.
|
2015-10-07 03:29:30 +00:00
|
|
|
SubPool string
|
2021-08-26 08:02:04 +00:00
|
|
|
// Gateway is the preferred Network Gateway address (optional).
|
2015-10-07 03:29:30 +00:00
|
|
|
Gateway string
|
2021-08-26 08:02:04 +00:00
|
|
|
// AuxAddresses contains auxiliary addresses for network driver. Must be within the master pool.
|
|
|
|
// libnetwork will reserve them if they fall into the container pool.
|
2015-10-07 03:29:30 +00:00
|
|
|
AuxAddresses map[string]string
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate checks whether the configuration is valid
|
|
|
|
func (c *IpamConf) Validate() error {
|
|
|
|
if c.Gateway != "" && nil == net.ParseIP(c.Gateway) {
|
2023-08-08 11:35:05 +00:00
|
|
|
return types.InvalidParameterErrorf("invalid gateway address %s in Ipam configuration", c.Gateway)
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-15 10:44:30 +00:00
|
|
|
// Contains checks whether the ipamSubnet contains [addr].
|
|
|
|
func (c *IpamConf) Contains(addr net.IP) bool {
|
|
|
|
if c == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.PreferredPool == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
_, allowedRange, _ := net.ParseCIDR(c.PreferredPool)
|
|
|
|
if c.SubPool != "" {
|
|
|
|
_, allowedRange, _ = net.ParseCIDR(c.SubPool)
|
|
|
|
}
|
|
|
|
|
|
|
|
return allowedRange.Contains(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsStatic checks whether the subnet was statically allocated (ie. user-defined).
|
|
|
|
func (c *IpamConf) IsStatic() bool {
|
|
|
|
return c != nil && c.PreferredPool != ""
|
|
|
|
}
|
|
|
|
|
2015-10-03 23:11:50 +00:00
|
|
|
// IpamInfo contains all the ipam related operational info for a network
|
|
|
|
type IpamInfo struct {
|
|
|
|
PoolID string
|
|
|
|
Meta map[string]string
|
|
|
|
driverapi.IPAMData
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes IpamInfo into json message
|
|
|
|
func (i *IpamInfo) MarshalJSON() ([]byte, error) {
|
|
|
|
m := map[string]interface{}{
|
|
|
|
"PoolID": i.PoolID,
|
|
|
|
}
|
|
|
|
v, err := json.Marshal(&i.IPAMData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m["IPAMData"] = string(v)
|
|
|
|
|
|
|
|
if i.Meta != nil {
|
|
|
|
m["Meta"] = i.Meta
|
|
|
|
}
|
|
|
|
return json.Marshal(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes json message into PoolData
|
|
|
|
func (i *IpamInfo) UnmarshalJSON(data []byte) error {
|
|
|
|
var (
|
|
|
|
m map[string]interface{}
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if err = json.Unmarshal(data, &m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i.PoolID = m["PoolID"].(string)
|
|
|
|
if v, ok := m["Meta"]; ok {
|
|
|
|
b, _ := json.Marshal(v)
|
|
|
|
if err = json.Unmarshal(b, &i.Meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := m["IPAMData"]; ok {
|
|
|
|
if err = json.Unmarshal([]byte(v.(string)), &i.IPAMData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
// Network represents a logical connectivity zone that containers may
|
|
|
|
// join using the Link method. A network is managed by a specific driver.
|
|
|
|
type Network struct {
|
2023-01-11 22:43:32 +00:00
|
|
|
ctrlr *Controller
|
2018-10-09 14:04:31 +00:00
|
|
|
name string
|
|
|
|
networkType string
|
|
|
|
id string
|
|
|
|
created time.Time
|
|
|
|
scope string // network data scope
|
|
|
|
labels map[string]string
|
2023-09-15 14:32:01 +00:00
|
|
|
ipamType string // ipamType is the name of the IPAM driver
|
2018-10-09 14:04:31 +00:00
|
|
|
ipamOptions map[string]string
|
|
|
|
addrSpace string
|
|
|
|
ipamV4Config []*IpamConf
|
|
|
|
ipamV6Config []*IpamConf
|
|
|
|
ipamV4Info []*IpamInfo
|
|
|
|
ipamV6Info []*IpamInfo
|
|
|
|
enableIPv6 bool
|
|
|
|
postIPv6 bool
|
|
|
|
epCnt *endpointCnt
|
|
|
|
generic options.Generic
|
|
|
|
dbIndex uint64
|
|
|
|
dbExists bool
|
|
|
|
persist bool
|
|
|
|
drvOnce *sync.Once
|
2022-09-03 21:20:23 +00:00
|
|
|
resolverOnce sync.Once //nolint:nolintlint,unused // only used on windows
|
2023-01-20 21:58:23 +00:00
|
|
|
resolver []*Resolver
|
2018-10-09 14:04:31 +00:00
|
|
|
internal bool
|
|
|
|
attachable bool
|
|
|
|
inDelete bool
|
|
|
|
ingress bool
|
|
|
|
driverTables []networkDBTable
|
|
|
|
dynamic bool
|
|
|
|
configOnly bool
|
|
|
|
configFrom string
|
|
|
|
loadBalancerIP net.IP
|
|
|
|
loadBalancerMode string
|
2023-01-11 23:00:34 +00:00
|
|
|
mu sync.Mutex
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 14:04:31 +00:00
|
|
|
const (
|
|
|
|
loadBalancerModeNAT = "NAT"
|
|
|
|
loadBalancerModeDSR = "DSR"
|
|
|
|
loadBalancerModeDefault = loadBalancerModeNAT
|
|
|
|
)
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
// Name returns a user chosen name for this network.
|
|
|
|
func (n *Network) Name() string {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-05-28 23:29:21 +00:00
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
return n.name
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
// ID returns a system generated id for this network.
|
|
|
|
func (n *Network) ID() string {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-05-28 23:29:21 +00:00
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
return n.id
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) Created() time.Time {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2016-09-20 15:59:32 +00:00
|
|
|
|
|
|
|
return n.created
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
// Type returns the type of network, which corresponds to its managing driver.
|
|
|
|
func (n *Network) Type() string {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-05-28 23:29:21 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
return n.networkType
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) Key() []string {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-07-02 05:00:48 +00:00
|
|
|
return []string{datastore.NetworkKeyPrefix, n.id}
|
2015-05-08 13:26:35 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) KeyPrefix() []string {
|
2015-06-01 16:43:24 +00:00
|
|
|
return []string{datastore.NetworkKeyPrefix}
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) Value() []byte {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-05-08 13:26:35 +00:00
|
|
|
b, err := json.Marshal(n)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) SetValue(value []byte) error {
|
2015-06-18 22:13:38 +00:00
|
|
|
return json.Unmarshal(value, n)
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) Index() uint64 {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-05-08 13:26:35 +00:00
|
|
|
return n.dbIndex
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) SetIndex(index uint64) {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
2015-05-08 13:26:35 +00:00
|
|
|
n.dbIndex = index
|
2015-06-18 22:13:38 +00:00
|
|
|
n.dbExists = true
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Unlock()
|
2015-06-05 20:31:12 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) Exists() bool {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-06-18 22:13:38 +00:00
|
|
|
return n.dbExists
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) Skip() bool {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-09-22 14:09:39 +00:00
|
|
|
return !n.persist
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) New() datastore.KVObject {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-10-05 11:21:15 +00:00
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
return &Network{
|
2015-10-05 11:21:15 +00:00
|
|
|
ctrlr: n.ctrlr,
|
|
|
|
drvOnce: &sync.Once{},
|
2016-01-17 20:43:41 +00:00
|
|
|
scope: n.scope,
|
2015-10-05 11:21:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-09 20:46:33 +00:00
|
|
|
// CopyTo deep copies to the destination IpamConfig
|
|
|
|
func (c *IpamConf) CopyTo(dstC *IpamConf) error {
|
|
|
|
dstC.PreferredPool = c.PreferredPool
|
|
|
|
dstC.SubPool = c.SubPool
|
|
|
|
dstC.Gateway = c.Gateway
|
|
|
|
if c.AuxAddresses != nil {
|
|
|
|
dstC.AuxAddresses = make(map[string]string, len(c.AuxAddresses))
|
|
|
|
for k, v := range c.AuxAddresses {
|
|
|
|
dstC.AuxAddresses[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
// CopyTo deep copies to the destination IpamInfo
|
|
|
|
func (i *IpamInfo) CopyTo(dstI *IpamInfo) error {
|
|
|
|
dstI.PoolID = i.PoolID
|
|
|
|
if i.Meta != nil {
|
|
|
|
dstI.Meta = make(map[string]string)
|
|
|
|
for k, v := range i.Meta {
|
|
|
|
dstI.Meta[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dstI.AddressSpace = i.AddressSpace
|
|
|
|
dstI.Pool = types.GetIPNetCopy(i.Pool)
|
|
|
|
dstI.Gateway = types.GetIPNetCopy(i.Gateway)
|
|
|
|
|
|
|
|
if i.AuxAddresses != nil {
|
|
|
|
dstI.AuxAddresses = make(map[string]*net.IPNet)
|
|
|
|
for k, v := range i.AuxAddresses {
|
|
|
|
dstI.AuxAddresses[k] = types.GetIPNetCopy(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) validateConfiguration() error {
|
2017-04-07 17:51:39 +00:00
|
|
|
if n.configOnly {
|
|
|
|
// Only supports network specific configurations.
|
|
|
|
// Network operator configurations are not supported.
|
2017-05-18 16:17:24 +00:00
|
|
|
if n.ingress || n.internal || n.attachable || n.scope != "" {
|
2017-04-07 17:51:39 +00:00
|
|
|
return types.ForbiddenErrorf("configuration network can only contain network " +
|
|
|
|
"specific fields. Network operator fields like " +
|
2017-05-18 16:17:24 +00:00
|
|
|
"[ ingress | internal | attachable | scope ] are not supported.")
|
2017-04-07 17:51:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if n.configFrom != "" {
|
|
|
|
if n.configOnly {
|
|
|
|
return types.ForbiddenErrorf("a configuration network cannot depend on another configuration network")
|
|
|
|
}
|
|
|
|
if n.ipamType != "" &&
|
|
|
|
n.ipamType != defaultIpamForNetworkType(n.networkType) ||
|
|
|
|
n.enableIPv6 ||
|
|
|
|
len(n.labels) > 0 || len(n.ipamOptions) > 0 ||
|
|
|
|
len(n.ipamV4Config) > 0 || len(n.ipamV6Config) > 0 {
|
|
|
|
return types.ForbiddenErrorf("user specified configurations are not supported if the network depends on a configuration network")
|
|
|
|
}
|
|
|
|
if len(n.generic) > 0 {
|
|
|
|
if data, ok := n.generic[netlabel.GenericData]; ok {
|
|
|
|
var (
|
|
|
|
driverOptions map[string]string
|
|
|
|
opts interface{}
|
|
|
|
)
|
2019-01-22 23:57:03 +00:00
|
|
|
switch t := data.(type) {
|
|
|
|
case map[string]interface{}, map[string]string:
|
|
|
|
opts = t
|
2017-04-07 17:51:39 +00:00
|
|
|
}
|
|
|
|
ba, err := json.Marshal(opts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to validate network configuration: %v", err)
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(ba, &driverOptions); err != nil {
|
|
|
|
return fmt.Errorf("failed to validate network configuration: %v", err)
|
|
|
|
}
|
|
|
|
if len(driverOptions) > 0 {
|
|
|
|
return types.ForbiddenErrorf("network driver options are not supported if the network depends on a configuration network")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-26 08:02:04 +00:00
|
|
|
// applyConfigurationTo applies network specific configurations.
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) applyConfigurationTo(to *Network) error {
|
2017-04-07 17:51:39 +00:00
|
|
|
to.enableIPv6 = n.enableIPv6
|
|
|
|
if len(n.labels) > 0 {
|
|
|
|
to.labels = make(map[string]string, len(n.labels))
|
|
|
|
for k, v := range n.labels {
|
|
|
|
if _, ok := to.labels[k]; !ok {
|
|
|
|
to.labels[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-30 23:01:15 +00:00
|
|
|
if len(n.ipamType) != 0 {
|
|
|
|
to.ipamType = n.ipamType
|
|
|
|
}
|
2017-04-07 17:51:39 +00:00
|
|
|
if len(n.ipamOptions) > 0 {
|
|
|
|
to.ipamOptions = make(map[string]string, len(n.ipamOptions))
|
|
|
|
for k, v := range n.ipamOptions {
|
|
|
|
if _, ok := to.ipamOptions[k]; !ok {
|
|
|
|
to.ipamOptions[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(n.ipamV4Config) > 0 {
|
|
|
|
to.ipamV4Config = make([]*IpamConf, 0, len(n.ipamV4Config))
|
2017-07-06 16:42:38 +00:00
|
|
|
to.ipamV4Config = append(to.ipamV4Config, n.ipamV4Config...)
|
2017-04-07 17:51:39 +00:00
|
|
|
}
|
|
|
|
if len(n.ipamV6Config) > 0 {
|
|
|
|
to.ipamV6Config = make([]*IpamConf, 0, len(n.ipamV6Config))
|
2017-07-06 16:42:38 +00:00
|
|
|
to.ipamV6Config = append(to.ipamV6Config, n.ipamV6Config...)
|
2017-04-07 17:51:39 +00:00
|
|
|
}
|
|
|
|
if len(n.generic) > 0 {
|
|
|
|
to.generic = options.Generic{}
|
|
|
|
for k, v := range n.generic {
|
|
|
|
to.generic[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) CopyTo(o datastore.KVObject) error {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-10-05 11:21:15 +00:00
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
dstN := o.(*Network)
|
2015-10-05 11:21:15 +00:00
|
|
|
dstN.name = n.name
|
|
|
|
dstN.id = n.id
|
2016-09-20 15:59:32 +00:00
|
|
|
dstN.created = n.created
|
2015-10-05 11:21:15 +00:00
|
|
|
dstN.networkType = n.networkType
|
2016-01-17 20:43:41 +00:00
|
|
|
dstN.scope = n.scope
|
2016-07-12 15:57:50 +00:00
|
|
|
dstN.dynamic = n.dynamic
|
2015-10-05 11:21:15 +00:00
|
|
|
dstN.ipamType = n.ipamType
|
|
|
|
dstN.enableIPv6 = n.enableIPv6
|
|
|
|
dstN.persist = n.persist
|
2015-11-06 07:50:10 +00:00
|
|
|
dstN.postIPv6 = n.postIPv6
|
2015-10-05 11:21:15 +00:00
|
|
|
dstN.dbIndex = n.dbIndex
|
|
|
|
dstN.dbExists = n.dbExists
|
|
|
|
dstN.drvOnce = n.drvOnce
|
2015-12-22 01:29:39 +00:00
|
|
|
dstN.internal = n.internal
|
2016-11-08 23:06:47 +00:00
|
|
|
dstN.attachable = n.attachable
|
2016-03-05 10:00:31 +00:00
|
|
|
dstN.inDelete = n.inDelete
|
2016-05-31 06:55:51 +00:00
|
|
|
dstN.ingress = n.ingress
|
2017-04-07 17:51:39 +00:00
|
|
|
dstN.configOnly = n.configOnly
|
|
|
|
dstN.configFrom = n.configFrom
|
2017-11-04 20:58:54 +00:00
|
|
|
dstN.loadBalancerIP = n.loadBalancerIP
|
2018-10-09 14:04:31 +00:00
|
|
|
dstN.loadBalancerMode = n.loadBalancerMode
|
2015-10-05 11:21:15 +00:00
|
|
|
|
2016-03-17 00:43:47 +00:00
|
|
|
// copy labels
|
|
|
|
if dstN.labels == nil {
|
|
|
|
dstN.labels = make(map[string]string, len(n.labels))
|
|
|
|
}
|
|
|
|
for k, v := range n.labels {
|
|
|
|
dstN.labels[k] = v
|
|
|
|
}
|
|
|
|
|
2016-04-07 16:36:53 +00:00
|
|
|
if n.ipamOptions != nil {
|
|
|
|
dstN.ipamOptions = make(map[string]string, len(n.ipamOptions))
|
|
|
|
for k, v := range n.ipamOptions {
|
|
|
|
dstN.ipamOptions[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-09 20:46:33 +00:00
|
|
|
for _, v4conf := range n.ipamV4Config {
|
|
|
|
dstV4Conf := &IpamConf{}
|
2021-05-28 00:15:56 +00:00
|
|
|
if err := v4conf.CopyTo(dstV4Conf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-09 20:46:33 +00:00
|
|
|
dstN.ipamV4Config = append(dstN.ipamV4Config, dstV4Conf)
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
for _, v4info := range n.ipamV4Info {
|
|
|
|
dstV4Info := &IpamInfo{}
|
2021-05-28 00:15:56 +00:00
|
|
|
if err := v4info.CopyTo(dstV4Info); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
dstN.ipamV4Info = append(dstN.ipamV4Info, dstV4Info)
|
|
|
|
}
|
|
|
|
|
2015-10-09 20:46:33 +00:00
|
|
|
for _, v6conf := range n.ipamV6Config {
|
|
|
|
dstV6Conf := &IpamConf{}
|
2021-05-28 00:15:56 +00:00
|
|
|
if err := v6conf.CopyTo(dstV6Conf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-09 20:46:33 +00:00
|
|
|
dstN.ipamV6Config = append(dstN.ipamV6Config, dstV6Conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v6info := range n.ipamV6Info {
|
|
|
|
dstV6Info := &IpamInfo{}
|
2021-05-28 00:15:56 +00:00
|
|
|
if err := v6info.CopyTo(dstV6Info); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-09 20:46:33 +00:00
|
|
|
dstN.ipamV6Info = append(dstN.ipamV6Info, dstV6Info)
|
2015-10-04 04:25:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
dstN.generic = options.Generic{}
|
|
|
|
for k, v := range n.generic {
|
|
|
|
dstN.generic[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) getEpCnt() *endpointCnt {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-10-10 02:31:44 +00:00
|
|
|
|
2015-10-12 05:28:26 +00:00
|
|
|
return n.epCnt
|
2015-05-08 13:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO : Can be made much more generic with the help of reflection (but has some golang limitations)
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) MarshalJSON() ([]byte, error) {
|
2015-05-08 13:26:35 +00:00
|
|
|
netMap := make(map[string]interface{})
|
|
|
|
netMap["name"] = n.name
|
2015-07-02 05:00:48 +00:00
|
|
|
netMap["id"] = n.id
|
2016-09-20 15:59:32 +00:00
|
|
|
netMap["created"] = n.created
|
2015-05-08 13:26:35 +00:00
|
|
|
netMap["networkType"] = n.networkType
|
2016-01-17 20:43:41 +00:00
|
|
|
netMap["scope"] = n.scope
|
2016-03-17 00:43:47 +00:00
|
|
|
netMap["labels"] = n.labels
|
2015-09-22 20:20:55 +00:00
|
|
|
netMap["ipamType"] = n.ipamType
|
2016-04-07 16:36:53 +00:00
|
|
|
netMap["ipamOptions"] = n.ipamOptions
|
2015-10-03 23:11:50 +00:00
|
|
|
netMap["addrSpace"] = n.addrSpace
|
2015-05-08 13:26:35 +00:00
|
|
|
netMap["enableIPv6"] = n.enableIPv6
|
2015-10-03 23:11:50 +00:00
|
|
|
if n.generic != nil {
|
|
|
|
netMap["generic"] = n.generic
|
|
|
|
}
|
2015-09-22 14:09:39 +00:00
|
|
|
netMap["persist"] = n.persist
|
2015-11-06 07:50:10 +00:00
|
|
|
netMap["postIPv6"] = n.postIPv6
|
2015-10-03 23:11:50 +00:00
|
|
|
if len(n.ipamV4Config) > 0 {
|
|
|
|
ics, err := json.Marshal(n.ipamV4Config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
netMap["ipamV4Config"] = string(ics)
|
|
|
|
}
|
|
|
|
if len(n.ipamV4Info) > 0 {
|
|
|
|
iis, err := json.Marshal(n.ipamV4Info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
netMap["ipamV4Info"] = string(iis)
|
|
|
|
}
|
|
|
|
if len(n.ipamV6Config) > 0 {
|
|
|
|
ics, err := json.Marshal(n.ipamV6Config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
netMap["ipamV6Config"] = string(ics)
|
|
|
|
}
|
|
|
|
if len(n.ipamV6Info) > 0 {
|
|
|
|
iis, err := json.Marshal(n.ipamV6Info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
netMap["ipamV6Info"] = string(iis)
|
|
|
|
}
|
2015-12-22 01:29:39 +00:00
|
|
|
netMap["internal"] = n.internal
|
2016-11-08 23:06:47 +00:00
|
|
|
netMap["attachable"] = n.attachable
|
2016-03-05 10:00:31 +00:00
|
|
|
netMap["inDelete"] = n.inDelete
|
2016-05-31 06:55:51 +00:00
|
|
|
netMap["ingress"] = n.ingress
|
2017-04-07 17:51:39 +00:00
|
|
|
netMap["configOnly"] = n.configOnly
|
|
|
|
netMap["configFrom"] = n.configFrom
|
2017-11-04 20:58:54 +00:00
|
|
|
netMap["loadBalancerIP"] = n.loadBalancerIP
|
2018-10-09 14:04:31 +00:00
|
|
|
netMap["loadBalancerMode"] = n.loadBalancerMode
|
2015-05-08 13:26:35 +00:00
|
|
|
return json.Marshal(netMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO : Can be made much more generic with the help of reflection (but has some golang limitations)
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) UnmarshalJSON(b []byte) (err error) {
|
2015-05-08 13:26:35 +00:00
|
|
|
var netMap map[string]interface{}
|
|
|
|
if err := json.Unmarshal(b, &netMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.name = netMap["name"].(string)
|
2015-07-02 05:00:48 +00:00
|
|
|
n.id = netMap["id"].(string)
|
2016-09-20 15:59:32 +00:00
|
|
|
// "created" is not available in older versions
|
|
|
|
if v, ok := netMap["created"]; ok {
|
|
|
|
// n.created is time.Time but marshalled as string
|
|
|
|
if err = n.created.UnmarshalText([]byte(v.(string))); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("failed to unmarshal creation time %v: %v", v, err)
|
2016-09-20 15:59:32 +00:00
|
|
|
n.created = time.Time{}
|
|
|
|
}
|
|
|
|
}
|
2015-05-08 13:26:35 +00:00
|
|
|
n.networkType = netMap["networkType"].(string)
|
|
|
|
n.enableIPv6 = netMap["enableIPv6"].(bool)
|
2015-10-07 20:04:21 +00:00
|
|
|
|
2016-03-17 00:43:47 +00:00
|
|
|
// if we weren't unmarshaling to netMap we could simply set n.labels
|
|
|
|
// unfortunately, we can't because map[string]interface{} != map[string]string
|
|
|
|
if labels, ok := netMap["labels"].(map[string]interface{}); ok {
|
|
|
|
n.labels = make(map[string]string, len(labels))
|
|
|
|
for label, value := range labels {
|
|
|
|
n.labels[label] = value.(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 16:36:53 +00:00
|
|
|
if v, ok := netMap["ipamOptions"]; ok {
|
|
|
|
if iOpts, ok := v.(map[string]interface{}); ok {
|
|
|
|
n.ipamOptions = make(map[string]string, len(iOpts))
|
|
|
|
for k, v := range iOpts {
|
|
|
|
n.ipamOptions[k] = v.(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-03 23:11:50 +00:00
|
|
|
if v, ok := netMap["generic"]; ok {
|
|
|
|
n.generic = v.(map[string]interface{})
|
2015-10-14 23:38:46 +00:00
|
|
|
// Restore opts in their map[string]string form
|
2015-10-06 19:08:54 +00:00
|
|
|
if v, ok := n.generic[netlabel.GenericData]; ok {
|
|
|
|
var lmap map[string]string
|
|
|
|
ba, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(ba, &lmap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.generic[netlabel.GenericData] = lmap
|
|
|
|
}
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
if v, ok := netMap["persist"]; ok {
|
|
|
|
n.persist = v.(bool)
|
|
|
|
}
|
2015-11-06 07:50:10 +00:00
|
|
|
if v, ok := netMap["postIPv6"]; ok {
|
|
|
|
n.postIPv6 = v.(bool)
|
|
|
|
}
|
2015-10-07 20:04:21 +00:00
|
|
|
if v, ok := netMap["ipamType"]; ok {
|
|
|
|
n.ipamType = v.(string)
|
|
|
|
} else {
|
|
|
|
n.ipamType = ipamapi.DefaultIPAM
|
|
|
|
}
|
|
|
|
if v, ok := netMap["addrSpace"]; ok {
|
|
|
|
n.addrSpace = v.(string)
|
|
|
|
}
|
2015-10-03 23:11:50 +00:00
|
|
|
if v, ok := netMap["ipamV4Config"]; ok {
|
|
|
|
if err := json.Unmarshal([]byte(v.(string)), &n.ipamV4Config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := netMap["ipamV4Info"]; ok {
|
|
|
|
if err := json.Unmarshal([]byte(v.(string)), &n.ipamV4Info); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := netMap["ipamV6Config"]; ok {
|
|
|
|
if err := json.Unmarshal([]byte(v.(string)), &n.ipamV6Config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-13 15:41:45 +00:00
|
|
|
}
|
2015-10-03 23:11:50 +00:00
|
|
|
if v, ok := netMap["ipamV6Info"]; ok {
|
|
|
|
if err := json.Unmarshal([]byte(v.(string)), &n.ipamV6Info); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-22 14:09:39 +00:00
|
|
|
}
|
2015-12-22 01:29:39 +00:00
|
|
|
if v, ok := netMap["internal"]; ok {
|
|
|
|
n.internal = v.(bool)
|
|
|
|
}
|
2016-11-08 23:06:47 +00:00
|
|
|
if v, ok := netMap["attachable"]; ok {
|
|
|
|
n.attachable = v.(bool)
|
|
|
|
}
|
2016-01-17 20:43:41 +00:00
|
|
|
if s, ok := netMap["scope"]; ok {
|
|
|
|
n.scope = s.(string)
|
|
|
|
}
|
2016-03-05 10:00:31 +00:00
|
|
|
if v, ok := netMap["inDelete"]; ok {
|
|
|
|
n.inDelete = v.(bool)
|
|
|
|
}
|
2016-05-31 06:55:51 +00:00
|
|
|
if v, ok := netMap["ingress"]; ok {
|
|
|
|
n.ingress = v.(bool)
|
|
|
|
}
|
2017-04-07 17:51:39 +00:00
|
|
|
if v, ok := netMap["configOnly"]; ok {
|
|
|
|
n.configOnly = v.(bool)
|
|
|
|
}
|
|
|
|
if v, ok := netMap["configFrom"]; ok {
|
|
|
|
n.configFrom = v.(string)
|
|
|
|
}
|
2017-11-04 20:58:54 +00:00
|
|
|
if v, ok := netMap["loadBalancerIP"]; ok {
|
|
|
|
n.loadBalancerIP = net.ParseIP(v.(string))
|
|
|
|
}
|
2018-10-09 14:04:31 +00:00
|
|
|
n.loadBalancerMode = loadBalancerModeDefault
|
|
|
|
if v, ok := netMap["loadBalancerMode"]; ok {
|
|
|
|
n.loadBalancerMode = v.(string)
|
|
|
|
}
|
2016-02-23 17:23:53 +00:00
|
|
|
// Reconcile old networks with the recently added `--ipv6` flag
|
|
|
|
if !n.enableIPv6 {
|
|
|
|
n.enableIPv6 = len(n.ipamV6Info) > 0
|
|
|
|
}
|
2015-05-08 13:26:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-11 10:15:20 +00:00
|
|
|
// NetworkOption is an option setter function type used to pass various options to
|
2015-05-01 00:57:06 +00:00
|
|
|
// NewNetwork method. The various setter functions of type NetworkOption are
|
|
|
|
// provided by libnetwork, they look like NetworkOptionXXXX(...)
|
2023-07-21 22:38:57 +00:00
|
|
|
type NetworkOption func(n *Network)
|
2015-05-01 00:57:06 +00:00
|
|
|
|
|
|
|
// NetworkOptionGeneric function returns an option setter for a Generic option defined
|
|
|
|
// in a Dictionary of Key-Value pair
|
|
|
|
func NetworkOptionGeneric(generic map[string]interface{}) NetworkOption {
|
2023-07-21 22:38:57 +00:00
|
|
|
return func(n *Network) {
|
2016-01-30 00:54:57 +00:00
|
|
|
if n.generic == nil {
|
|
|
|
n.generic = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
if val, ok := generic[netlabel.EnableIPv6]; ok {
|
|
|
|
n.enableIPv6 = val.(bool)
|
|
|
|
}
|
|
|
|
if val, ok := generic[netlabel.Internal]; ok {
|
|
|
|
n.internal = val.(bool)
|
|
|
|
}
|
|
|
|
for k, v := range generic {
|
|
|
|
n.generic[k] = v
|
2015-05-06 06:41:20 +00:00
|
|
|
}
|
2015-05-01 00:57:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-31 06:55:51 +00:00
|
|
|
// NetworkOptionIngress returns an option setter to indicate if a network is
|
|
|
|
// an ingress network.
|
2017-03-02 03:09:39 +00:00
|
|
|
func NetworkOptionIngress(ingress bool) NetworkOption {
|
2023-07-21 22:38:57 +00:00
|
|
|
return func(n *Network) {
|
2017-03-02 03:09:39 +00:00
|
|
|
n.ingress = ingress
|
2016-05-31 06:55:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 14:09:39 +00:00
|
|
|
// NetworkOptionPersist returns an option setter to set persistence policy for a network
|
|
|
|
func NetworkOptionPersist(persist bool) NetworkOption {
|
2023-07-21 22:38:57 +00:00
|
|
|
return func(n *Network) {
|
2015-09-22 14:09:39 +00:00
|
|
|
n.persist = persist
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 00:54:57 +00:00
|
|
|
// NetworkOptionEnableIPv6 returns an option setter to explicitly configure IPv6
|
|
|
|
func NetworkOptionEnableIPv6(enableIPv6 bool) NetworkOption {
|
2023-07-21 22:38:57 +00:00
|
|
|
return func(n *Network) {
|
2016-01-30 00:54:57 +00:00
|
|
|
if n.generic == nil {
|
|
|
|
n.generic = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
n.enableIPv6 = enableIPv6
|
|
|
|
n.generic[netlabel.EnableIPv6] = enableIPv6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-22 01:29:39 +00:00
|
|
|
// NetworkOptionInternalNetwork returns an option setter to config the network
|
|
|
|
// to be internal which disables default gateway service
|
|
|
|
func NetworkOptionInternalNetwork() NetworkOption {
|
2023-07-21 22:38:57 +00:00
|
|
|
return func(n *Network) {
|
2015-12-22 01:29:39 +00:00
|
|
|
if n.generic == nil {
|
|
|
|
n.generic = make(map[string]interface{})
|
|
|
|
}
|
2016-01-30 00:54:57 +00:00
|
|
|
n.internal = true
|
2015-12-22 01:29:39 +00:00
|
|
|
n.generic[netlabel.Internal] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 23:06:47 +00:00
|
|
|
// NetworkOptionAttachable returns an option setter to set attachable for a network
|
|
|
|
func NetworkOptionAttachable(attachable bool) NetworkOption {
|
2023-07-21 22:38:57 +00:00
|
|
|
return func(n *Network) {
|
2016-11-08 23:06:47 +00:00
|
|
|
n.attachable = attachable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 20:31:44 +00:00
|
|
|
// NetworkOptionScope returns an option setter to overwrite the network's scope.
|
|
|
|
// By default the network's scope is set to the network driver's datascope.
|
|
|
|
func NetworkOptionScope(scope string) NetworkOption {
|
2023-07-21 22:38:57 +00:00
|
|
|
return func(n *Network) {
|
2017-04-07 20:31:44 +00:00
|
|
|
n.scope = scope
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-03 23:11:50 +00:00
|
|
|
// NetworkOptionIpam function returns an option setter for the ipam configuration for this network
|
2016-01-07 20:57:22 +00:00
|
|
|
func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ipV6 []*IpamConf, opts map[string]string) NetworkOption {
|
2023-07-21 22:38:57 +00:00
|
|
|
return func(n *Network) {
|
2015-10-09 20:57:49 +00:00
|
|
|
if ipamDriver != "" {
|
|
|
|
n.ipamType = ipamDriver
|
2016-10-12 23:55:20 +00:00
|
|
|
if ipamDriver == ipamapi.DefaultIPAM {
|
|
|
|
n.ipamType = defaultIpamForNetworkType(n.Type())
|
|
|
|
}
|
2015-10-09 20:57:49 +00:00
|
|
|
}
|
2016-01-07 20:57:22 +00:00
|
|
|
n.ipamOptions = opts
|
2015-10-03 23:11:50 +00:00
|
|
|
n.addrSpace = addrSpace
|
|
|
|
n.ipamV4Config = ipV4
|
|
|
|
n.ipamV6Config = ipV6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 20:58:54 +00:00
|
|
|
// NetworkOptionLBEndpoint function returns an option setter for the configuration of the load balancer endpoint for this network
|
|
|
|
func NetworkOptionLBEndpoint(ip net.IP) NetworkOption {
|
2023-07-21 22:38:57 +00:00
|
|
|
return func(n *Network) {
|
2017-11-04 20:58:54 +00:00
|
|
|
n.loadBalancerIP = ip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 00:43:47 +00:00
|
|
|
// NetworkOptionDriverOpts function returns an option setter for any driver parameter described by a map
|
2015-10-14 23:38:46 +00:00
|
|
|
func NetworkOptionDriverOpts(opts map[string]string) NetworkOption {
|
2023-07-21 22:38:57 +00:00
|
|
|
return func(n *Network) {
|
2015-10-06 19:08:54 +00:00
|
|
|
if n.generic == nil {
|
|
|
|
n.generic = make(map[string]interface{})
|
|
|
|
}
|
2015-10-14 23:38:46 +00:00
|
|
|
if opts == nil {
|
|
|
|
opts = make(map[string]string)
|
2015-10-10 10:49:09 +00:00
|
|
|
}
|
2015-10-06 19:08:54 +00:00
|
|
|
// Store the options
|
2015-10-14 23:38:46 +00:00
|
|
|
n.generic[netlabel.GenericData] = opts
|
2015-10-06 19:08:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 00:43:47 +00:00
|
|
|
// NetworkOptionLabels function returns an option setter for labels specific to a network
|
|
|
|
func NetworkOptionLabels(labels map[string]string) NetworkOption {
|
2023-07-21 22:38:57 +00:00
|
|
|
return func(n *Network) {
|
2016-03-17 00:43:47 +00:00
|
|
|
n.labels = labels
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 23:17:19 +00:00
|
|
|
// NetworkOptionDynamic function returns an option setter for dynamic option for a network
|
|
|
|
func NetworkOptionDynamic() NetworkOption {
|
2023-07-21 22:38:57 +00:00
|
|
|
return func(n *Network) {
|
2016-05-24 23:17:19 +00:00
|
|
|
n.dynamic = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 07:50:10 +00:00
|
|
|
// NetworkOptionDeferIPv6Alloc instructs the network to defer the IPV6 address allocation until after the endpoint has been created
|
|
|
|
// It is being provided to support the specific docker daemon flags where user can deterministically assign an IPv6 address
|
|
|
|
// to a container as combination of fixed-cidr-v6 + mac-address
|
|
|
|
// TODO: Remove this option setter once we support endpoint ipam options
|
|
|
|
func NetworkOptionDeferIPv6Alloc(enable bool) NetworkOption {
|
2023-07-21 22:38:57 +00:00
|
|
|
return func(n *Network) {
|
2015-11-06 07:50:10 +00:00
|
|
|
n.postIPv6 = enable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 17:51:39 +00:00
|
|
|
// NetworkOptionConfigOnly tells controller this network is
|
|
|
|
// a configuration only network. It serves as a configuration
|
|
|
|
// for other networks.
|
|
|
|
func NetworkOptionConfigOnly() NetworkOption {
|
2023-07-21 22:38:57 +00:00
|
|
|
return func(n *Network) {
|
2017-04-07 17:51:39 +00:00
|
|
|
n.configOnly = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkOptionConfigFrom tells controller to pick the
|
|
|
|
// network configuration from a configuration only network
|
|
|
|
func NetworkOptionConfigFrom(name string) NetworkOption {
|
2023-07-21 22:38:57 +00:00
|
|
|
return func(n *Network) {
|
2017-04-07 17:51:39 +00:00
|
|
|
n.configFrom = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) processOptions(options ...NetworkOption) {
|
2015-05-01 00:57:06 +00:00
|
|
|
for _, opt := range options {
|
|
|
|
if opt != nil {
|
|
|
|
opt(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 22:08:39 +00:00
|
|
|
type networkDeleteParams struct {
|
|
|
|
rmLBEndpoint bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkDeleteOption is a type for optional parameters to pass to the
|
2023-07-21 22:38:57 +00:00
|
|
|
// Network.Delete() function.
|
2018-04-09 22:08:39 +00:00
|
|
|
type NetworkDeleteOption func(p *networkDeleteParams)
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
// NetworkDeleteOptionRemoveLB informs a Network.Delete() operation that should
|
2018-04-09 22:08:39 +00:00
|
|
|
// remove the load balancer endpoint for this network. Note that the Delete()
|
|
|
|
// method will automatically remove a load balancing endpoint for most networks
|
|
|
|
// when the network is otherwise empty. However, this does not occur for some
|
|
|
|
// networks. In particular, networks marked as ingress (which are supposed to
|
|
|
|
// be more permanent than other overlay networks) won't automatically remove
|
|
|
|
// the LB endpoint on Delete(). This method allows for explicit removal of
|
|
|
|
// such networks provided there are no other endpoints present in the network.
|
|
|
|
// If the network still has non-LB endpoints present, Delete() will not
|
|
|
|
// remove the LB endpoint and will return an error.
|
|
|
|
func NetworkDeleteOptionRemoveLB(p *networkDeleteParams) {
|
|
|
|
p.rmLBEndpoint = true
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) resolveDriver(name string, load bool) (driverapi.Driver, driverapi.Capability, error) {
|
2015-10-05 11:21:15 +00:00
|
|
|
c := n.getController()
|
|
|
|
|
|
|
|
// Check if a driver for the specified network type is available
|
2023-07-28 10:49:22 +00:00
|
|
|
d, capabilities := c.drvRegistry.Driver(name)
|
2016-03-01 02:17:04 +00:00
|
|
|
if d == nil {
|
|
|
|
if load {
|
2017-07-06 16:42:38 +00:00
|
|
|
err := c.loadDriver(name)
|
2016-03-01 02:17:04 +00:00
|
|
|
if err != nil {
|
2023-01-24 23:19:26 +00:00
|
|
|
return nil, driverapi.Capability{}, err
|
2016-03-01 02:17:04 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
|
2023-07-28 10:49:22 +00:00
|
|
|
d, capabilities = c.drvRegistry.Driver(name)
|
2016-03-01 02:17:04 +00:00
|
|
|
if d == nil {
|
2023-01-24 23:19:26 +00:00
|
|
|
return nil, driverapi.Capability{}, fmt.Errorf("could not resolve driver %s in registry", name)
|
2016-03-01 02:17:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// don't fail if driver loading is not required
|
2023-01-24 23:19:26 +00:00
|
|
|
return nil, driverapi.Capability{}, nil
|
2015-10-05 11:21:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 10:49:22 +00:00
|
|
|
return d, capabilities, nil
|
2015-10-05 11:21:15 +00:00
|
|
|
}
|
2015-06-05 20:31:12 +00:00
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) driverIsMultihost() bool {
|
2023-07-28 10:49:22 +00:00
|
|
|
_, capabilities, err := n.resolveDriver(n.networkType, true)
|
2017-04-07 20:31:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2023-07-28 10:49:22 +00:00
|
|
|
return capabilities.ConnectivityScope == scope.Global
|
2017-04-07 20:31:44 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) driver(load bool) (driverapi.Driver, error) {
|
2023-07-28 10:49:22 +00:00
|
|
|
d, capabilities, err := n.resolveDriver(n.networkType, load)
|
2016-03-01 02:17:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-10-05 11:21:15 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
2016-05-16 01:10:50 +00:00
|
|
|
// If load is not required, driver, cap and err may all be nil
|
2023-01-24 23:19:26 +00:00
|
|
|
if n.scope == "" {
|
2023-07-28 10:49:22 +00:00
|
|
|
n.scope = capabilities.DataScope
|
2016-05-16 01:10:50 +00:00
|
|
|
}
|
2017-05-18 22:06:27 +00:00
|
|
|
if n.dynamic {
|
|
|
|
// If the network is dynamic, then it is swarm
|
|
|
|
// scoped regardless of the backing driver.
|
2023-07-28 17:11:00 +00:00
|
|
|
n.scope = scope.Swarm
|
2016-03-30 21:42:58 +00:00
|
|
|
}
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Unlock()
|
2016-03-01 02:17:04 +00:00
|
|
|
return d, nil
|
2015-10-05 11:21:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
// Delete the network.
|
|
|
|
func (n *Network) Delete(options ...NetworkDeleteOption) error {
|
2018-04-09 22:08:39 +00:00
|
|
|
var params networkDeleteParams
|
|
|
|
for _, opt := range options {
|
|
|
|
opt(¶ms)
|
|
|
|
}
|
|
|
|
return n.delete(false, params.rmLBEndpoint)
|
2016-03-05 10:00:31 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 22:08:39 +00:00
|
|
|
// This function gets called in 3 ways:
|
2022-07-08 16:27:07 +00:00
|
|
|
// - Delete() -- (false, false)
|
|
|
|
// remove if endpoint count == 0 or endpoint count == 1 and
|
|
|
|
// there is a load balancer IP
|
|
|
|
// - Delete(libnetwork.NetworkDeleteOptionRemoveLB) -- (false, true)
|
|
|
|
// remove load balancer and network if endpoint count == 1
|
|
|
|
// - controller.networkCleanup() -- (true, true)
|
|
|
|
// remove the network no matter what
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) delete(force bool, rmLBEndpoint bool) error {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
2015-10-05 11:21:15 +00:00
|
|
|
c := n.ctrlr
|
|
|
|
name := n.name
|
|
|
|
id := n.id
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Unlock()
|
2015-10-05 11:21:15 +00:00
|
|
|
|
2017-06-18 12:25:58 +00:00
|
|
|
c.networkLocker.Lock(id)
|
2022-07-13 20:30:47 +00:00
|
|
|
defer c.networkLocker.Unlock(id) //nolint:errcheck
|
2017-06-18 12:25:58 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
n, err := c.getNetworkFromStore(id)
|
|
|
|
if err != nil {
|
|
|
|
return &UnknownNetworkError{name: name, id: id}
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 22:08:39 +00:00
|
|
|
// Only remove ingress on force removal or explicit LB endpoint removal
|
|
|
|
if n.ingress && !force && !rmLBEndpoint {
|
|
|
|
return &ActiveEndpointsError{name: n.name, id: n.id}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the network is empty
|
2018-04-10 16:34:41 +00:00
|
|
|
var emptyCount uint64
|
|
|
|
if n.hasLoadBalancerEndpoint() {
|
2018-04-09 22:08:39 +00:00
|
|
|
emptyCount = 1
|
|
|
|
}
|
|
|
|
if !force && n.getEpCnt().EndpointCnt() > emptyCount {
|
|
|
|
if n.configOnly {
|
|
|
|
return types.ForbiddenErrorf("configuration network %q is in use", n.Name())
|
|
|
|
}
|
|
|
|
return &ActiveEndpointsError{name: n.name, id: n.id}
|
|
|
|
}
|
|
|
|
|
2018-04-10 16:34:41 +00:00
|
|
|
if n.hasLoadBalancerEndpoint() {
|
2018-04-09 22:08:39 +00:00
|
|
|
// If we got to this point, then the following must hold:
|
|
|
|
// * force is true OR endpoint count == 1
|
|
|
|
if err := n.deleteLoadBalancerSandbox(); err != nil {
|
|
|
|
if !force {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// continue deletion when force is true even on error
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("Error deleting load balancer sandbox: %v", err)
|
2017-11-04 20:58:54 +00:00
|
|
|
}
|
2022-07-13 20:30:47 +00:00
|
|
|
// Reload the network from the store to update the epcnt.
|
2017-11-04 20:58:54 +00:00
|
|
|
n, err = c.getNetworkFromStore(id)
|
|
|
|
if err != nil {
|
|
|
|
return &UnknownNetworkError{name: name, id: id}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 22:08:39 +00:00
|
|
|
// Up to this point, errors that we returned were recoverable.
|
|
|
|
// From here on, any errors leave us in an inconsistent state.
|
|
|
|
// This is unfortunate, but there isn't a safe way to
|
|
|
|
// reconstitute a load-balancer endpoint after removing it.
|
2015-06-01 16:43:24 +00:00
|
|
|
|
2016-03-05 10:00:31 +00:00
|
|
|
// Mark the network for deletion
|
|
|
|
n.inDelete = true
|
|
|
|
if err = c.updateToStore(n); err != nil {
|
|
|
|
return fmt.Errorf("error marking network %s (%s) for deletion: %v", n.Name(), n.ID(), err)
|
2015-10-05 11:21:15 +00:00
|
|
|
}
|
2016-03-05 10:00:31 +00:00
|
|
|
|
2017-04-07 17:51:39 +00:00
|
|
|
if n.ConfigFrom() != "" {
|
|
|
|
if t, err := c.getConfigNetwork(n.ConfigFrom()); err == nil {
|
|
|
|
if err := t.getEpCnt().DecEndpointCnt(); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("Failed to update reference count for configuration network %q on removal of network %q: %v",
|
2017-04-07 17:51:39 +00:00
|
|
|
t.Name(), n.Name(), err)
|
|
|
|
}
|
|
|
|
} else {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("Could not find configuration network %q during removal of network %q", n.configFrom, n.Name())
|
2017-04-07 17:51:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.configOnly {
|
|
|
|
goto removeFromStore
|
|
|
|
}
|
|
|
|
|
2016-03-05 10:00:31 +00:00
|
|
|
n.ipamRelease()
|
2015-10-05 11:21:15 +00:00
|
|
|
|
2016-08-20 00:50:37 +00:00
|
|
|
// We are about to delete the network. Leave the gossip
|
|
|
|
// cluster for the network to stop all incoming network
|
|
|
|
// specific gossip updates before cleaning up all the service
|
|
|
|
// bindings for the network. But cleanup service binding
|
|
|
|
// before deleting the network from the store since service
|
|
|
|
// bindings cleanup requires the network in the store.
|
|
|
|
n.cancelDriverWatches()
|
|
|
|
if err = n.leaveCluster(); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Errorf("Failed leaving network %s from the agent cluster: %v", n.Name(), err)
|
2016-08-20 00:50:37 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 04:41:29 +00:00
|
|
|
// Cleanup the service discovery for this network
|
|
|
|
c.cleanupServiceDiscovery(n.ID())
|
|
|
|
|
2020-03-04 00:16:51 +00:00
|
|
|
// Cleanup the load balancer. On Windows this call is required
|
2021-02-04 08:21:45 +00:00
|
|
|
// to remove remote loadbalancers in VFP, and must be performed before
|
|
|
|
// dataplane network deletion.
|
2021-02-04 08:31:33 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
c.cleanupServiceBindings(n.ID())
|
|
|
|
}
|
2020-03-04 00:16:51 +00:00
|
|
|
|
2021-02-04 08:21:45 +00:00
|
|
|
// Delete the network from the dataplane
|
|
|
|
if err = n.deleteNetwork(); err != nil {
|
|
|
|
if !force {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debugf("driver failed to delete stale network %s (%s): %v", n.Name(), n.ID(), err)
|
2021-02-04 08:21:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 17:51:39 +00:00
|
|
|
removeFromStore:
|
2015-10-05 11:21:15 +00:00
|
|
|
// deleteFromStore performs an atomic delete operation and the
|
2023-07-21 22:38:57 +00:00
|
|
|
// Network.epCnt will help prevent any possible
|
2015-10-05 11:21:15 +00:00
|
|
|
// race between endpoint join and network delete
|
2016-03-05 10:00:31 +00:00
|
|
|
if err = c.deleteFromStore(n.getEpCnt()); err != nil {
|
|
|
|
if !force {
|
|
|
|
return fmt.Errorf("error deleting network endpoint count from store: %v", err)
|
|
|
|
}
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debugf("Error deleting endpoint count from store for stale network %s (%s) for deletion: %v", n.Name(), n.ID(), err)
|
2015-10-12 05:28:26 +00:00
|
|
|
}
|
2015-12-02 01:53:09 +00:00
|
|
|
|
2016-03-05 10:00:31 +00:00
|
|
|
if err = c.deleteFromStore(n); err != nil {
|
2015-10-12 05:28:26 +00:00
|
|
|
return fmt.Errorf("error deleting network from store: %v", err)
|
2015-06-01 16:43:24 +00:00
|
|
|
}
|
2015-04-13 18:40:42 +00:00
|
|
|
|
2015-06-01 16:43:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) deleteNetwork() error {
|
2016-01-16 22:24:44 +00:00
|
|
|
d, err := n.driver(true)
|
2015-10-05 11:21:15 +00:00
|
|
|
if err != nil {
|
2023-07-21 22:38:57 +00:00
|
|
|
return fmt.Errorf("failed deleting Network: %v", err)
|
2015-10-05 11:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.DeleteNetwork(n.ID()); err != nil {
|
2015-06-10 20:27:23 +00:00
|
|
|
// Forbidden Errors should be honored
|
|
|
|
if _, ok := err.(types.ForbiddenError); ok {
|
|
|
|
return err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
2015-10-10 16:00:35 +00:00
|
|
|
|
|
|
|
if _, ok := err.(types.MaskableError); !ok {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("driver error deleting network %s : %v", n.name, err)
|
2015-10-10 16:00:35 +00:00
|
|
|
}
|
2015-06-10 20:27:23 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
|
2016-09-19 22:48:06 +00:00
|
|
|
for _, resolver := range n.resolver {
|
|
|
|
resolver.Stop()
|
|
|
|
}
|
2015-06-10 20:27:23 +00:00
|
|
|
return nil
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) addEndpoint(ep *Endpoint) error {
|
2016-01-16 22:24:44 +00:00
|
|
|
d, err := n.driver(true)
|
2015-10-05 11:21:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to add endpoint: %v", err)
|
|
|
|
}
|
2015-05-31 18:49:11 +00:00
|
|
|
|
2023-08-27 12:37:10 +00:00
|
|
|
err = d.CreateEndpoint(n.id, ep.id, ep.Iface(), ep.generic)
|
2015-05-31 18:49:11 +00:00
|
|
|
if err != nil {
|
2015-10-05 11:21:15 +00:00
|
|
|
return types.InternalErrorf("failed to create endpoint %s on network %s: %v",
|
|
|
|
ep.Name(), n.Name(), err)
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
2015-06-19 06:40:17 +00:00
|
|
|
|
2015-05-31 18:49:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
// CreateEndpoint creates a new endpoint to this network symbolically identified by the
|
|
|
|
// specified unique name. The options parameter carries driver specific options.
|
|
|
|
func (n *Network) CreateEndpoint(name string, options ...EndpointOption) (*Endpoint, error) {
|
2015-06-05 20:31:12 +00:00
|
|
|
var err error
|
2023-07-16 16:52:41 +00:00
|
|
|
if strings.TrimSpace(name) == "" {
|
2017-02-02 02:17:29 +00:00
|
|
|
return nil, ErrInvalidName(name)
|
2015-05-08 02:59:06 +00:00
|
|
|
}
|
2015-06-04 16:41:42 +00:00
|
|
|
|
2017-04-07 17:51:39 +00:00
|
|
|
if n.ConfigOnly() {
|
|
|
|
return nil, types.ForbiddenErrorf("cannot create endpoint on configuration-only network")
|
|
|
|
}
|
|
|
|
|
2015-06-05 20:31:12 +00:00
|
|
|
if _, err = n.EndpointByName(name); err == nil {
|
2016-12-27 11:46:10 +00:00
|
|
|
return nil, types.ForbiddenErrorf("endpoint with name %s already exists in network %s", name, n.Name())
|
2015-06-04 16:41:42 +00:00
|
|
|
}
|
|
|
|
|
2017-06-18 12:25:58 +00:00
|
|
|
n.ctrlr.networkLocker.Lock(n.id)
|
2022-07-13 20:30:47 +00:00
|
|
|
defer n.ctrlr.networkLocker.Unlock(n.id) //nolint:errcheck
|
2017-06-18 12:25:58 +00:00
|
|
|
|
2017-11-04 20:58:54 +00:00
|
|
|
return n.createEndpoint(name, options...)
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) createEndpoint(name string, options ...EndpointOption) (*Endpoint, error) {
|
2017-11-04 20:58:54 +00:00
|
|
|
var err error
|
|
|
|
|
2023-08-20 17:03:05 +00:00
|
|
|
ep := &Endpoint{name: name, generic: make(map[string]interface{}), iface: &EndpointInterface{}}
|
2017-11-04 20:58:54 +00:00
|
|
|
ep.id = stringid.GenerateRandomID()
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
// Initialize ep.network with a possibly stale copy of n. We need this to get network from
|
2016-01-11 10:15:20 +00:00
|
|
|
// store. But once we get it from store we will have the most uptodate copy possibly.
|
2015-04-13 18:40:42 +00:00
|
|
|
ep.network = n
|
2015-10-05 11:21:15 +00:00
|
|
|
ep.network, err = ep.getNetworkFromStore()
|
|
|
|
if err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Errorf("failed to get network during CreateEndpoint: %v", err)
|
2020-05-22 07:22:36 +00:00
|
|
|
return nil, err
|
2015-10-05 11:21:15 +00:00
|
|
|
}
|
|
|
|
n = ep.network
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
ep.processOptions(options...)
|
2015-04-13 18:40:42 +00:00
|
|
|
|
2016-05-25 03:04:49 +00:00
|
|
|
for _, llIPNet := range ep.Iface().LinkLocalAddresses() {
|
|
|
|
if !llIPNet.IP.IsLinkLocalUnicast() {
|
2023-08-08 11:35:05 +00:00
|
|
|
return nil, types.InvalidParameterErrorf("invalid link local IP address: %v", llIPNet.IP)
|
2016-05-25 03:04:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 21:17:43 +00:00
|
|
|
if opt, ok := ep.generic[netlabel.MacAddress]; ok {
|
|
|
|
if mac, ok := opt.(net.HardwareAddr); ok {
|
|
|
|
ep.iface.mac = mac
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 08:02:04 +00:00
|
|
|
ipam, capability, err := n.getController().getIPAMDriver(n.ipamType)
|
2015-12-10 02:09:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-26 08:02:04 +00:00
|
|
|
if capability.RequiresMACAddress {
|
2015-12-10 02:09:58 +00:00
|
|
|
if ep.iface.mac == nil {
|
|
|
|
ep.iface.mac = netutils.GenerateRandomMAC()
|
|
|
|
}
|
|
|
|
if ep.ipamOptions == nil {
|
|
|
|
ep.ipamOptions = make(map[string]string)
|
|
|
|
}
|
|
|
|
ep.ipamOptions[netlabel.MacAddress] = ep.iface.mac.String()
|
|
|
|
}
|
|
|
|
|
2016-03-01 02:17:04 +00:00
|
|
|
if err = ep.assignAddress(ipam, true, n.enableIPv6 && !n.postIPv6); err != nil {
|
2015-10-03 23:11:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
ep.releaseAddress()
|
|
|
|
}
|
|
|
|
}()
|
2018-06-26 10:26:00 +00:00
|
|
|
|
|
|
|
if err = n.addEndpoint(ep); err != nil {
|
2015-05-31 18:49:11 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-05 20:31:12 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2018-06-26 10:26:00 +00:00
|
|
|
if e := ep.deleteEndpoint(false); e != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("cleaning up endpoint failed %s : %v", name, e)
|
2015-06-05 20:31:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-10-05 11:21:15 +00:00
|
|
|
|
2018-06-26 10:26:00 +00:00
|
|
|
// We should perform updateToStore call right after addEndpoint
|
|
|
|
// in order to have iface properly configured
|
|
|
|
if err = n.getController().updateToStore(ep); err != nil {
|
2015-06-05 20:31:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2018-06-26 10:26:00 +00:00
|
|
|
if e := n.getController().deleteFromStore(ep); e != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("error rolling back endpoint %s from store: %v", name, e)
|
2015-06-05 20:31:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-05-31 18:49:11 +00:00
|
|
|
|
2017-12-06 22:45:00 +00:00
|
|
|
if err = ep.assignAddress(ipam, false, n.enableIPv6 && n.postIPv6); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-17 23:43:12 +00:00
|
|
|
if !n.getController().isSwarmNode() || n.Scope() != scope.Swarm || !n.driverIsMultihost() {
|
|
|
|
n.updateSvcRecord(ep, true)
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
n.updateSvcRecord(ep, false)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2015-10-23 17:09:00 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
// Increment endpoint count to indicate completion of endpoint addition
|
2015-10-12 05:28:26 +00:00
|
|
|
if err = n.getEpCnt().IncEndpointCnt(); err != nil {
|
2015-10-05 11:21:15 +00:00
|
|
|
return nil, err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 22:42:23 +00:00
|
|
|
return ep, nil
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
// Endpoints returns the list of Endpoint(s) in this network.
|
|
|
|
func (n *Network) Endpoints() []*Endpoint {
|
2015-10-05 11:21:15 +00:00
|
|
|
endpoints, err := n.getEndpointsFromStore()
|
|
|
|
if err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Error(err)
|
2015-10-05 11:21:15 +00:00
|
|
|
}
|
2023-01-12 01:42:24 +00:00
|
|
|
return endpoints
|
2015-04-18 06:13:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
// WalkEndpoints uses the provided function to walk the Endpoints.
|
|
|
|
func (n *Network) WalkEndpoints(walker EndpointWalker) {
|
2015-04-20 16:46:11 +00:00
|
|
|
for _, e := range n.Endpoints() {
|
|
|
|
if walker(e) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
// EndpointByName returns the Endpoint which has the passed name. If not found,
|
|
|
|
// the error ErrNoSuchEndpoint is returned.
|
|
|
|
func (n *Network) EndpointByName(name string) (*Endpoint, error) {
|
2015-05-11 23:13:27 +00:00
|
|
|
if name == "" {
|
2015-05-14 21:56:15 +00:00
|
|
|
return nil, ErrInvalidName(name)
|
2015-05-11 23:13:27 +00:00
|
|
|
}
|
2023-01-12 01:42:24 +00:00
|
|
|
var e *Endpoint
|
2015-04-24 19:05:33 +00:00
|
|
|
|
2023-01-12 01:42:24 +00:00
|
|
|
s := func(current *Endpoint) bool {
|
2015-05-11 23:13:27 +00:00
|
|
|
if current.Name() == name {
|
|
|
|
e = current
|
|
|
|
return true
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
2015-05-11 23:13:27 +00:00
|
|
|
return false
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
n.WalkEndpoints(s)
|
|
|
|
|
2015-05-15 23:04:09 +00:00
|
|
|
if e == nil {
|
2015-05-14 21:56:15 +00:00
|
|
|
return nil, ErrNoSuchEndpoint(name)
|
2015-05-15 23:04:09 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
return e, nil
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
// EndpointByID returns the Endpoint which has the passed id. If not found,
|
|
|
|
// the error ErrNoSuchEndpoint is returned.
|
|
|
|
func (n *Network) EndpointByID(id string) (*Endpoint, error) {
|
2015-05-11 23:13:27 +00:00
|
|
|
if id == "" {
|
2015-05-14 21:56:15 +00:00
|
|
|
return nil, ErrInvalidID(id)
|
2015-05-11 23:13:27 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
ep, err := n.getEndpointFromStore(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrNoSuchEndpoint(id)
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
2015-05-13 21:12:57 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
return ep, nil
|
2015-05-13 21:12:57 +00:00
|
|
|
}
|
2015-06-19 06:40:17 +00:00
|
|
|
|
2023-11-04 14:15:54 +00:00
|
|
|
// updateSvcRecord adds or deletes local DNS records for a given Endpoint.
|
2023-09-13 23:44:43 +00:00
|
|
|
func (n *Network) updateSvcRecord(ep *Endpoint, isAdd bool) {
|
2023-11-04 14:15:54 +00:00
|
|
|
iface := ep.Iface()
|
|
|
|
if iface == nil || iface.Address() == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-13 04:20:31 +00:00
|
|
|
var ipv6 net.IP
|
2016-01-08 03:19:31 +00:00
|
|
|
epName := ep.Name()
|
2023-11-04 14:15:54 +00:00
|
|
|
myAliases := ep.MyAliases()
|
|
|
|
if iface.AddressIPv6() != nil {
|
|
|
|
ipv6 = iface.AddressIPv6().IP
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceID := ep.svcID
|
|
|
|
if serviceID == "" {
|
|
|
|
serviceID = ep.ID()
|
|
|
|
}
|
|
|
|
if isAdd {
|
|
|
|
// If anonymous endpoint has an alias use the first alias
|
|
|
|
// for ip->name mapping. Not having the reverse mapping
|
|
|
|
// breaks some apps
|
|
|
|
if ep.isAnonymous() {
|
|
|
|
if len(myAliases) > 0 {
|
|
|
|
n.addSvcRecords(ep.ID(), myAliases[0], serviceID, iface.Address().IP, ipv6, true, "updateSvcRecord")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
n.addSvcRecords(ep.ID(), epName, serviceID, iface.Address().IP, ipv6, true, "updateSvcRecord")
|
2016-01-13 04:20:31 +00:00
|
|
|
}
|
2023-11-04 14:15:54 +00:00
|
|
|
for _, alias := range myAliases {
|
|
|
|
n.addSvcRecords(ep.ID(), alias, serviceID, iface.Address().IP, ipv6, false, "updateSvcRecord")
|
2017-06-18 12:25:58 +00:00
|
|
|
}
|
2023-11-04 14:15:54 +00:00
|
|
|
} else {
|
|
|
|
if ep.isAnonymous() {
|
|
|
|
if len(myAliases) > 0 {
|
|
|
|
n.deleteSvcRecords(ep.ID(), myAliases[0], serviceID, iface.Address().IP, ipv6, true, "updateSvcRecord")
|
2016-01-08 03:19:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-11-04 14:15:54 +00:00
|
|
|
n.deleteSvcRecords(ep.ID(), epName, serviceID, iface.Address().IP, ipv6, true, "updateSvcRecord")
|
|
|
|
}
|
|
|
|
for _, alias := range myAliases {
|
|
|
|
n.deleteSvcRecords(ep.ID(), alias, serviceID, iface.Address().IP, ipv6, false, "updateSvcRecord")
|
2016-01-08 03:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 17:31:12 +00:00
|
|
|
func addIPToName(ipMap *setmatrix.SetMatrix[ipInfo], name, serviceID string, ip net.IP) {
|
2016-01-13 04:20:31 +00:00
|
|
|
reverseIP := netutils.ReverseIP(ip.String())
|
2017-06-06 23:04:50 +00:00
|
|
|
ipMap.Insert(reverseIP, ipInfo{
|
2017-06-18 12:25:58 +00:00
|
|
|
name: name,
|
|
|
|
serviceID: serviceID,
|
2017-06-06 23:04:50 +00:00
|
|
|
})
|
2016-01-13 04:20:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 17:31:12 +00:00
|
|
|
func delIPToName(ipMap *setmatrix.SetMatrix[ipInfo], name, serviceID string, ip net.IP) {
|
2017-06-18 12:25:58 +00:00
|
|
|
reverseIP := netutils.ReverseIP(ip.String())
|
|
|
|
ipMap.Remove(reverseIP, ipInfo{
|
|
|
|
name: name,
|
|
|
|
serviceID: serviceID,
|
|
|
|
})
|
2016-01-13 04:20:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 17:31:12 +00:00
|
|
|
func addNameToIP(svcMap *setmatrix.SetMatrix[svcMapEntry], name, serviceID string, epIP net.IP) {
|
2019-06-14 22:37:38 +00:00
|
|
|
// Since DNS name resolution is case-insensitive, Use the lower-case form
|
|
|
|
// of the name as the key into svcMap
|
|
|
|
lowerCaseName := strings.ToLower(name)
|
|
|
|
svcMap.Insert(lowerCaseName, svcMapEntry{
|
2017-06-18 12:25:58 +00:00
|
|
|
ip: epIP.String(),
|
|
|
|
serviceID: serviceID,
|
|
|
|
})
|
|
|
|
}
|
2016-01-13 04:20:31 +00:00
|
|
|
|
2023-03-29 17:31:12 +00:00
|
|
|
func delNameToIP(svcMap *setmatrix.SetMatrix[svcMapEntry], name, serviceID string, epIP net.IP) {
|
2019-06-14 22:37:38 +00:00
|
|
|
lowerCaseName := strings.ToLower(name)
|
|
|
|
svcMap.Remove(lowerCaseName, svcMapEntry{
|
2017-06-18 12:25:58 +00:00
|
|
|
ip: epIP.String(),
|
|
|
|
serviceID: serviceID,
|
|
|
|
})
|
2016-01-13 04:20:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) addSvcRecords(eID, name, serviceID string, epIP, epIPv6 net.IP, ipMapUpdate bool, method string) {
|
2016-10-03 22:15:09 +00:00
|
|
|
// Do not add service names for ingress network as this is a
|
|
|
|
// routing only network
|
|
|
|
if n.ingress {
|
|
|
|
return
|
|
|
|
}
|
2021-04-21 13:49:12 +00:00
|
|
|
networkID := n.ID()
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debugf("%s (%.7s).addSvcRecords(%s, %s, %s, %t) %s sid:%s", eID, networkID, name, epIP, epIPv6, ipMapUpdate, method, serviceID)
|
2017-02-24 18:04:38 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
c := n.getController()
|
2023-01-11 20:56:50 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2017-06-06 23:04:50 +00:00
|
|
|
|
2021-04-21 13:49:12 +00:00
|
|
|
sr, ok := c.svcRecords[networkID]
|
2015-10-05 11:21:15 +00:00
|
|
|
if !ok {
|
2023-03-29 17:31:12 +00:00
|
|
|
sr = &svcInfo{}
|
2021-04-21 13:49:12 +00:00
|
|
|
c.svcRecords[networkID] = sr
|
2015-10-05 11:21:15 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 03:19:31 +00:00
|
|
|
if ipMapUpdate {
|
2023-03-29 17:31:12 +00:00
|
|
|
addIPToName(&sr.ipMap, name, serviceID, epIP)
|
2016-01-13 04:20:31 +00:00
|
|
|
if epIPv6 != nil {
|
2023-03-29 17:31:12 +00:00
|
|
|
addIPToName(&sr.ipMap, name, serviceID, epIPv6)
|
2016-01-08 03:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-24 09:51:32 +00:00
|
|
|
|
2023-03-29 17:31:12 +00:00
|
|
|
addNameToIP(&sr.svcMap, name, serviceID, epIP)
|
2016-01-13 04:20:31 +00:00
|
|
|
if epIPv6 != nil {
|
2023-03-29 17:31:12 +00:00
|
|
|
addNameToIP(&sr.svcIPv6Map, name, serviceID, epIPv6)
|
2016-01-08 03:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-23 17:09:00 +00:00
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) deleteSvcRecords(eID, name, serviceID string, epIP net.IP, epIPv6 net.IP, ipMapUpdate bool, method string) {
|
2016-10-03 22:15:09 +00:00
|
|
|
// Do not delete service names from ingress network as this is a
|
|
|
|
// routing only network
|
|
|
|
if n.ingress {
|
|
|
|
return
|
|
|
|
}
|
2021-04-21 13:49:12 +00:00
|
|
|
networkID := n.ID()
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debugf("%s (%.7s).deleteSvcRecords(%s, %s, %s, %t) %s sid:%s ", eID, networkID, name, epIP, epIPv6, ipMapUpdate, method, serviceID)
|
2017-02-24 18:04:38 +00:00
|
|
|
|
2016-01-08 03:19:31 +00:00
|
|
|
c := n.getController()
|
2023-01-11 20:56:50 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2017-06-06 23:04:50 +00:00
|
|
|
|
2021-04-21 13:49:12 +00:00
|
|
|
sr, ok := c.svcRecords[networkID]
|
2016-01-08 03:19:31 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ipMapUpdate {
|
2023-03-29 17:31:12 +00:00
|
|
|
delIPToName(&sr.ipMap, name, serviceID, epIP)
|
2016-01-08 03:19:31 +00:00
|
|
|
|
2016-01-13 04:20:31 +00:00
|
|
|
if epIPv6 != nil {
|
2023-03-29 17:31:12 +00:00
|
|
|
delIPToName(&sr.ipMap, name, serviceID, epIPv6)
|
2015-06-19 06:40:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-08 03:19:31 +00:00
|
|
|
|
2023-03-29 17:31:12 +00:00
|
|
|
delNameToIP(&sr.svcMap, name, serviceID, epIP)
|
2016-01-13 04:20:31 +00:00
|
|
|
|
|
|
|
if epIPv6 != nil {
|
2023-03-29 17:31:12 +00:00
|
|
|
delNameToIP(&sr.svcIPv6Map, name, serviceID, epIPv6)
|
2016-01-08 03:19:31 +00:00
|
|
|
}
|
2015-06-19 06:40:17 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) getSvcRecords(ep *Endpoint) []etchosts.Record {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-06-19 06:40:17 +00:00
|
|
|
|
2016-06-17 22:41:16 +00:00
|
|
|
if ep == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-19 06:40:17 +00:00
|
|
|
var recs []etchosts.Record
|
2016-07-20 16:49:59 +00:00
|
|
|
|
2016-06-17 22:41:16 +00:00
|
|
|
epName := ep.Name()
|
2015-10-05 11:21:15 +00:00
|
|
|
|
2023-01-11 20:56:50 +00:00
|
|
|
n.ctrlr.mu.Lock()
|
|
|
|
defer n.ctrlr.mu.Unlock()
|
2017-06-18 12:25:58 +00:00
|
|
|
sr, ok := n.ctrlr.svcRecords[n.id]
|
2023-03-29 17:31:12 +00:00
|
|
|
if !ok {
|
2017-06-18 12:25:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-07-20 16:49:59 +00:00
|
|
|
|
2017-06-18 12:25:58 +00:00
|
|
|
svcMapKeys := sr.svcMap.Keys()
|
|
|
|
// Loop on service names on this network
|
|
|
|
for _, k := range svcMapKeys {
|
|
|
|
if strings.Split(k, ".")[0] == epName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Get all the IPs associated to this service
|
|
|
|
mapEntryList, ok := sr.svcMap.Get(k)
|
|
|
|
if !ok {
|
|
|
|
// The key got deleted
|
2016-06-17 22:41:16 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-06-18 12:25:58 +00:00
|
|
|
if len(mapEntryList) == 0 {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("Found empty list of IP addresses for service %s on network %s (%s)", k, n.name, n.id)
|
2015-10-23 17:09:00 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-06-18 12:25:58 +00:00
|
|
|
|
2015-06-19 06:40:17 +00:00
|
|
|
recs = append(recs, etchosts.Record{
|
2017-06-18 12:25:58 +00:00
|
|
|
Hosts: k,
|
2023-03-29 17:31:12 +00:00
|
|
|
IP: mapEntryList[0].ip,
|
2015-06-19 06:40:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return recs
|
|
|
|
}
|
2015-07-02 05:00:48 +00:00
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) getController() *Controller {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-07-02 05:00:48 +00:00
|
|
|
return n.ctrlr
|
|
|
|
}
|
2015-10-03 23:11:50 +00:00
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) ipamAllocate() error {
|
2016-06-15 04:34:44 +00:00
|
|
|
if n.hasSpecialDriver() {
|
2015-10-04 04:25:57 +00:00
|
|
|
return nil
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 02:17:04 +00:00
|
|
|
ipam, _, err := n.getController().getIPAMDriver(n.ipamType)
|
2015-10-03 23:11:50 +00:00
|
|
|
if err != nil {
|
2015-10-04 04:25:57 +00:00
|
|
|
return err
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if n.addrSpace == "" {
|
|
|
|
if n.addrSpace, err = n.deriveAddressSpace(); err != nil {
|
2015-10-04 04:25:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = n.ipamAllocateVersion(4, ipam)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
n.ipamReleaseVersion(4, ipam)
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
2015-10-04 04:25:57 +00:00
|
|
|
}()
|
|
|
|
|
2016-02-23 17:23:53 +00:00
|
|
|
if !n.enableIPv6 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-04 23:33:53 +00:00
|
|
|
err = n.ipamAllocateVersion(6, ipam)
|
2017-07-06 16:42:38 +00:00
|
|
|
return err
|
2015-10-04 04:25:57 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 15:22:20 +00:00
|
|
|
func (n *Network) requestPoolHelper(ipam ipamapi.Ipam, addressSpace, requestedPool, requestedSubPool string, options map[string]string, v6 bool) (poolID string, pool *net.IPNet, meta map[string]string, err error) {
|
2023-07-22 15:36:54 +00:00
|
|
|
var tmpPoolLeases []string
|
|
|
|
defer func() {
|
|
|
|
// Prevent repeated lock/unlock in the loop.
|
|
|
|
nwName := n.Name()
|
|
|
|
// Release all pools we held on to.
|
|
|
|
for _, pID := range tmpPoolLeases {
|
|
|
|
if err := ipam.ReleasePool(pID); err != nil {
|
|
|
|
log.G(context.TODO()).Warnf("Failed to release overlapping pool %s while returning from pool request helper for network %s", pool, nwName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-02-26 22:58:11 +00:00
|
|
|
for {
|
2023-07-22 15:22:20 +00:00
|
|
|
poolID, pool, meta, err = ipam.RequestPool(addressSpace, requestedPool, requestedSubPool, options, v6)
|
2016-02-26 22:58:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, nil, err
|
|
|
|
}
|
|
|
|
|
libnetwork: network.requestPoolHelper: slightly optimize order of checks
Check the preferredPool first, as other checks could be doing more
(such as locking, or validating / parsing). Also adding a note, as
it's unclear why we're ignoring invalid pools here.
The "invalid" conditions was added in [libnetwork#1095][1], which
moved code to reduce os-specific dependencies in the ipam package,
but also introduced a types.IsIPNetValid() function, which considers
"0.0.0.0/0" invalid, and added it to the condition to return early.
Unfortunately review does not mention this change, so there's no
context why. Possibly this was done to prevent errors further down
the line (when checking for overlaps), but returning an error here
instead would likely have avoided that as well, so we can only guess.
To make this code slightly more transparent, this patch also inlines
the "types.IsIPNetValid" function, as it's not used anywhere else,
and inlining it makes it more visible.
[1]: https://github.com/moby/libnetwork/commit/5ca79d6b87873264516323a7b76f0af7d0298492#diff-bdcd879439d041827d334846f9aba01de6e3683ed8fdd01e63917dae6df23846
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-22 17:44:24 +00:00
|
|
|
// If the network pool was explicitly chosen, the network belongs to
|
|
|
|
// global scope, or it is invalid ("0.0.0.0/0"), then we don't perform
|
|
|
|
// check for overlaps.
|
|
|
|
//
|
|
|
|
// FIXME(thaJeztah): why are we ignoring invalid pools here?
|
|
|
|
//
|
|
|
|
// The "invalid" conditions was added in [libnetwork#1095][1], which
|
|
|
|
// moved code to reduce os-specific dependencies in the ipam package,
|
|
|
|
// but also introduced a types.IsIPNetValid() function, which considers
|
|
|
|
// "0.0.0.0/0" invalid, and added it to the conditions below.
|
|
|
|
//
|
|
|
|
// Unfortunately review does not mention this change, so there's no
|
|
|
|
// context why. Possibly this was done to prevent errors further down
|
|
|
|
// the line (when checking for overlaps), but returning an error here
|
|
|
|
// instead would likely have avoided that as well, so we can only guess.
|
|
|
|
//
|
|
|
|
// [1]: https://github.com/moby/libnetwork/commit/5ca79d6b87873264516323a7b76f0af7d0298492#diff-bdcd879439d041827d334846f9aba01de6e3683ed8fdd01e63917dae6df23846
|
|
|
|
if requestedPool != "" || n.Scope() == scope.Global || pool.String() == "0.0.0.0/0" {
|
2016-02-26 22:58:11 +00:00
|
|
|
return poolID, pool, meta, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for overlap and if none found, we have found the right pool.
|
|
|
|
if _, err := netutils.FindAvailableNetwork([]*net.IPNet{pool}); err == nil {
|
|
|
|
return poolID, pool, meta, nil
|
|
|
|
}
|
|
|
|
|
2023-07-22 15:36:54 +00:00
|
|
|
// Pool obtained in this iteration is overlapping. Hold onto the pool
|
|
|
|
// and don't release it yet, because we don't want IPAM to give us back
|
|
|
|
// the same pool over again. But make sure we still do a deferred release
|
|
|
|
// when we have either obtained a non-overlapping pool or ran out of
|
|
|
|
// pre-defined pools.
|
|
|
|
tmpPoolLeases = append(tmpPoolLeases, poolID)
|
2016-02-26 22:58:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) ipamAllocateVersion(ipVer int, ipam ipamapi.Ipam) error {
|
2015-10-04 04:25:57 +00:00
|
|
|
var (
|
|
|
|
cfgList *[]*IpamConf
|
|
|
|
infoList *[]*IpamInfo
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
switch ipVer {
|
|
|
|
case 4:
|
|
|
|
cfgList = &n.ipamV4Config
|
|
|
|
infoList = &n.ipamV4Info
|
|
|
|
case 6:
|
|
|
|
cfgList = &n.ipamV6Config
|
|
|
|
infoList = &n.ipamV6Info
|
|
|
|
default:
|
|
|
|
return types.InternalErrorf("incorrect ip version passed to ipam allocate: %d", ipVer)
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
2015-10-10 00:06:52 +00:00
|
|
|
if len(*cfgList) == 0 {
|
2016-02-12 13:07:00 +00:00
|
|
|
*cfgList = []*IpamConf{{}}
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
2015-10-04 04:25:57 +00:00
|
|
|
*infoList = make([]*IpamInfo, len(*cfgList))
|
|
|
|
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debugf("Allocating IPv%d pools for network %s (%s)", ipVer, n.Name(), n.ID())
|
2015-10-03 23:11:50 +00:00
|
|
|
|
2015-10-04 04:25:57 +00:00
|
|
|
for i, cfg := range *cfgList {
|
2015-10-03 23:11:50 +00:00
|
|
|
if err = cfg.Validate(); err != nil {
|
2015-10-04 04:25:57 +00:00
|
|
|
return err
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
d := &IpamInfo{}
|
2015-10-04 04:25:57 +00:00
|
|
|
(*infoList)[i] = d
|
2015-10-03 23:11:50 +00:00
|
|
|
|
2016-06-01 08:13:00 +00:00
|
|
|
d.AddressSpace = n.addrSpace
|
2016-02-26 22:58:11 +00:00
|
|
|
d.PoolID, d.Pool, d.Meta, err = n.requestPoolHelper(ipam, n.addrSpace, cfg.PreferredPool, cfg.SubPool, n.ipamOptions, ipVer == 6)
|
2015-10-03 23:11:50 +00:00
|
|
|
if err != nil {
|
2015-10-04 04:25:57 +00:00
|
|
|
return err
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if err := ipam.ReleasePool(d.PoolID); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("Failed to release address pool %s after failure to create network %s (%s)", d.PoolID, n.Name(), n.ID())
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if gws, ok := d.Meta[netlabel.Gateway]; ok {
|
|
|
|
if d.Gateway, err = types.ParseCIDR(gws); err != nil {
|
2023-08-08 11:35:05 +00:00
|
|
|
return types.InvalidParameterErrorf("failed to parse gateway address (%v) returned by ipam driver: %v", gws, err)
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If user requested a specific gateway, libnetwork will allocate it
|
|
|
|
// irrespective of whether ipam driver returned a gateway already.
|
|
|
|
// If none of the above is true, libnetwork will allocate one.
|
|
|
|
if cfg.Gateway != "" || d.Gateway == nil {
|
2022-01-20 13:10:24 +00:00
|
|
|
gatewayOpts := map[string]string{
|
2015-12-04 03:20:42 +00:00
|
|
|
ipamapi.RequestAddressType: netlabel.Gateway,
|
|
|
|
}
|
|
|
|
if d.Gateway, _, err = ipam.RequestAddress(d.PoolID, net.ParseIP(cfg.Gateway), gatewayOpts); err != nil {
|
2015-10-04 04:25:57 +00:00
|
|
|
return types.InternalErrorf("failed to allocate gateway (%v): %v", cfg.Gateway, err)
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-07 03:29:30 +00:00
|
|
|
// Auxiliary addresses must be part of the master address pool
|
|
|
|
// If they fall into the container addressable pool, libnetwork will reserve them
|
2015-10-03 23:11:50 +00:00
|
|
|
if cfg.AuxAddresses != nil {
|
|
|
|
var ip net.IP
|
|
|
|
d.IPAMData.AuxAddresses = make(map[string]*net.IPNet, len(cfg.AuxAddresses))
|
|
|
|
for k, v := range cfg.AuxAddresses {
|
|
|
|
if ip = net.ParseIP(v); ip == nil {
|
2023-08-08 11:35:05 +00:00
|
|
|
return types.InvalidParameterErrorf("non parsable secondary ip address (%s:%s) passed for network %s", k, v, n.Name())
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
2015-10-07 03:29:30 +00:00
|
|
|
if !d.Pool.Contains(ip) {
|
2017-05-22 02:25:52 +00:00
|
|
|
return types.ForbiddenErrorf("auxiliary address: (%s:%s) must belong to the master pool: %s", k, v, d.Pool)
|
2015-10-07 03:29:30 +00:00
|
|
|
}
|
|
|
|
// Attempt reservation in the container addressable pool, silent the error if address does not belong to that pool
|
|
|
|
if d.IPAMData.AuxAddresses[k], _, err = ipam.RequestAddress(d.PoolID, ip, nil); err != nil && err != ipamapi.ErrIPOutOfRange {
|
2015-10-04 04:25:57 +00:00
|
|
|
return types.InternalErrorf("failed to allocate secondary ip address (%s:%s): %v", k, v, err)
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 04:25:57 +00:00
|
|
|
return nil
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) ipamRelease() {
|
2016-06-15 04:34:44 +00:00
|
|
|
if n.hasSpecialDriver() {
|
2015-10-03 23:11:50 +00:00
|
|
|
return
|
|
|
|
}
|
2016-03-01 02:17:04 +00:00
|
|
|
ipam, _, err := n.getController().getIPAMDriver(n.ipamType)
|
2015-10-03 23:11:50 +00:00
|
|
|
if err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("Failed to retrieve ipam driver to release address pool(s) on delete of network %s (%s): %v", n.Name(), n.ID(), err)
|
2015-10-03 23:11:50 +00:00
|
|
|
return
|
|
|
|
}
|
2015-10-04 04:25:57 +00:00
|
|
|
n.ipamReleaseVersion(4, ipam)
|
|
|
|
n.ipamReleaseVersion(6, ipam)
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) ipamReleaseVersion(ipVer int, ipam ipamapi.Ipam) {
|
2016-03-05 10:00:31 +00:00
|
|
|
var infoList *[]*IpamInfo
|
2015-10-04 04:25:57 +00:00
|
|
|
|
|
|
|
switch ipVer {
|
|
|
|
case 4:
|
2016-03-05 10:00:31 +00:00
|
|
|
infoList = &n.ipamV4Info
|
2015-10-04 04:25:57 +00:00
|
|
|
case 6:
|
2016-03-05 10:00:31 +00:00
|
|
|
infoList = &n.ipamV6Info
|
2015-10-04 04:25:57 +00:00
|
|
|
default:
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("incorrect ip version passed to ipam release: %d", ipVer)
|
2015-10-04 04:25:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-23 17:23:53 +00:00
|
|
|
if len(*infoList) == 0 {
|
2015-10-04 04:25:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debugf("releasing IPv%d pools from network %s (%s)", ipVer, n.Name(), n.ID())
|
2015-10-04 04:25:57 +00:00
|
|
|
|
2016-03-05 10:00:31 +00:00
|
|
|
for _, d := range *infoList {
|
2015-10-03 23:11:50 +00:00
|
|
|
if d.Gateway != nil {
|
|
|
|
if err := ipam.ReleaseAddress(d.PoolID, d.Gateway.IP); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("Failed to release gateway ip address %s on delete of network %s (%s): %v", d.Gateway.IP, n.Name(), n.ID(), err)
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if d.IPAMData.AuxAddresses != nil {
|
|
|
|
for k, nw := range d.IPAMData.AuxAddresses {
|
2015-10-07 03:29:30 +00:00
|
|
|
if d.Pool.Contains(nw.IP) {
|
|
|
|
if err := ipam.ReleaseAddress(d.PoolID, nw.IP); err != nil && err != ipamapi.ErrIPOutOfRange {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("Failed to release secondary ip address %s (%v) on delete of network %s (%s): %v", k, nw.IP, n.Name(), n.ID(), err)
|
2015-10-07 03:29:30 +00:00
|
|
|
}
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := ipam.ReleasePool(d.PoolID); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("Failed to release address pool %s on delete of network %s (%s): %v", d.PoolID, n.Name(), n.ID(), err)
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-05 10:00:31 +00:00
|
|
|
|
|
|
|
*infoList = nil
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) getIPInfo(ipVer int) []*IpamInfo {
|
2015-10-04 04:25:57 +00:00
|
|
|
var info []*IpamInfo
|
|
|
|
switch ipVer {
|
|
|
|
case 4:
|
|
|
|
info = n.ipamV4Info
|
|
|
|
case 6:
|
|
|
|
info = n.ipamV6Info
|
|
|
|
default:
|
|
|
|
return nil
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
2015-10-04 04:25:57 +00:00
|
|
|
l := make([]*IpamInfo, 0, len(info))
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
2017-07-06 16:42:38 +00:00
|
|
|
l = append(l, info...)
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Unlock()
|
2015-10-03 23:11:50 +00:00
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) getIPData(ipVer int) []driverapi.IPAMData {
|
2015-10-04 04:25:57 +00:00
|
|
|
var info []*IpamInfo
|
|
|
|
switch ipVer {
|
|
|
|
case 4:
|
|
|
|
info = n.ipamV4Info
|
|
|
|
case 6:
|
|
|
|
info = n.ipamV6Info
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
l := make([]driverapi.IPAMData, 0, len(info))
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
2015-10-04 04:25:57 +00:00
|
|
|
for _, d := range info {
|
2015-10-03 23:11:50 +00:00
|
|
|
l = append(l, d.IPAMData)
|
|
|
|
}
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Unlock()
|
2015-10-03 23:11:50 +00:00
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) deriveAddressSpace() (string, error) {
|
2023-01-24 23:19:26 +00:00
|
|
|
ipam, _ := n.getController().ipamRegistry.IPAM(n.ipamType)
|
|
|
|
if ipam == nil {
|
|
|
|
return "", types.NotFoundErrorf("failed to get default address space: unknown ipam type %q", n.ipamType)
|
|
|
|
}
|
|
|
|
local, global, err := ipam.GetDefaultAddressSpaces()
|
2016-03-01 02:17:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", types.NotFoundErrorf("failed to get default address space: %v", err)
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
2023-10-19 16:10:44 +00:00
|
|
|
if n.Scope() == scope.Global {
|
2016-03-01 02:17:04 +00:00
|
|
|
return global, nil
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
2016-03-01 02:17:04 +00:00
|
|
|
return local, nil
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
2015-10-06 19:08:54 +00:00
|
|
|
|
2023-07-25 15:38:30 +00:00
|
|
|
// Peers returns a slice of PeerInfo structures which has the information about the peer
|
|
|
|
// nodes participating in the same overlay network. This is currently the per-network
|
|
|
|
// gossip cluster. For non-dynamic overlay networks and bridge networks it returns an
|
|
|
|
// empty slice
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) Peers() []networkdb.PeerInfo {
|
2016-10-25 21:52:36 +00:00
|
|
|
if !n.Dynamic() {
|
|
|
|
return []networkdb.PeerInfo{}
|
|
|
|
}
|
|
|
|
|
2023-07-28 10:49:22 +00:00
|
|
|
a := n.getController().getAgent()
|
|
|
|
if a == nil {
|
2016-11-22 07:38:03 +00:00
|
|
|
return []networkdb.PeerInfo{}
|
2016-10-25 21:52:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 10:49:22 +00:00
|
|
|
return a.networkDB.Peers(n.ID())
|
2016-10-25 21:52:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) DriverOptions() map[string]string {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-10-06 19:08:54 +00:00
|
|
|
if n.generic != nil {
|
|
|
|
if m, ok := n.generic[netlabel.GenericData]; ok {
|
2015-10-09 15:59:40 +00:00
|
|
|
return m.(map[string]string)
|
2015-10-06 19:08:54 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-09 15:59:40 +00:00
|
|
|
return map[string]string{}
|
2015-10-06 19:08:54 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) Scope() string {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2016-01-17 20:43:41 +00:00
|
|
|
return n.scope
|
2015-10-06 19:08:54 +00:00
|
|
|
}
|
2015-10-09 20:46:33 +00:00
|
|
|
|
2023-07-22 15:31:29 +00:00
|
|
|
func (n *Network) IpamConfig() (ipamType string, ipamOptions map[string]string, ipamV4Config []*IpamConf, ipamV6Config []*IpamConf) {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-10-09 20:46:33 +00:00
|
|
|
|
2023-07-22 15:31:29 +00:00
|
|
|
ipamV4Config = make([]*IpamConf, len(n.ipamV4Config))
|
2015-10-09 20:46:33 +00:00
|
|
|
for i, c := range n.ipamV4Config {
|
|
|
|
cc := &IpamConf{}
|
2021-05-28 00:15:56 +00:00
|
|
|
if err := c.CopyTo(cc); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).WithError(err).Error("Error copying ipam ipv4 config")
|
2021-05-28 00:15:56 +00:00
|
|
|
}
|
2023-07-22 15:31:29 +00:00
|
|
|
ipamV4Config[i] = cc
|
2015-10-09 20:46:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 15:31:29 +00:00
|
|
|
ipamV6Config = make([]*IpamConf, len(n.ipamV6Config))
|
2015-10-09 20:46:33 +00:00
|
|
|
for i, c := range n.ipamV6Config {
|
|
|
|
cc := &IpamConf{}
|
2021-05-28 00:15:56 +00:00
|
|
|
if err := c.CopyTo(cc); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).WithError(err).Debug("Error copying ipam ipv6 config")
|
2021-05-28 00:15:56 +00:00
|
|
|
}
|
2023-07-22 15:31:29 +00:00
|
|
|
ipamV6Config[i] = cc
|
2015-10-09 20:46:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 15:31:29 +00:00
|
|
|
return n.ipamType, n.ipamOptions, ipamV4Config, ipamV6Config
|
2015-10-09 20:46:33 +00:00
|
|
|
}
|
2015-12-16 09:30:21 +00:00
|
|
|
|
2023-07-22 15:31:29 +00:00
|
|
|
func (n *Network) IpamInfo() (ipamV4Info []*IpamInfo, ipamV6Info []*IpamInfo) {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-12-16 09:30:21 +00:00
|
|
|
|
2023-07-22 15:31:29 +00:00
|
|
|
ipamV4Info = make([]*IpamInfo, len(n.ipamV4Info))
|
2015-12-16 09:30:21 +00:00
|
|
|
for i, info := range n.ipamV4Info {
|
|
|
|
ic := &IpamInfo{}
|
2021-05-28 00:15:56 +00:00
|
|
|
if err := info.CopyTo(ic); err != nil {
|
2023-07-22 15:31:29 +00:00
|
|
|
log.G(context.TODO()).WithError(err).Error("Error copying IPv4 IPAM config")
|
2021-05-28 00:15:56 +00:00
|
|
|
}
|
2023-07-22 15:31:29 +00:00
|
|
|
ipamV4Info[i] = ic
|
2015-12-16 09:30:21 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 15:31:29 +00:00
|
|
|
ipamV6Info = make([]*IpamInfo, len(n.ipamV6Info))
|
2015-12-16 09:30:21 +00:00
|
|
|
for i, info := range n.ipamV6Info {
|
|
|
|
ic := &IpamInfo{}
|
2021-05-28 00:15:56 +00:00
|
|
|
if err := info.CopyTo(ic); err != nil {
|
2023-07-22 15:31:29 +00:00
|
|
|
log.G(context.TODO()).WithError(err).Error("Error copying IPv6 IPAM config")
|
2021-05-28 00:15:56 +00:00
|
|
|
}
|
2023-07-22 15:31:29 +00:00
|
|
|
ipamV6Info[i] = ic
|
2015-12-16 09:30:21 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 15:31:29 +00:00
|
|
|
return ipamV4Info, ipamV6Info
|
2015-12-16 09:30:21 +00:00
|
|
|
}
|
2015-12-22 01:29:39 +00:00
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) Internal() bool {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2015-12-22 01:29:39 +00:00
|
|
|
|
|
|
|
return n.internal
|
|
|
|
}
|
2016-01-29 18:10:06 +00:00
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) Attachable() bool {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2016-11-08 23:06:47 +00:00
|
|
|
|
|
|
|
return n.attachable
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) Ingress() bool {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2017-03-02 03:09:39 +00:00
|
|
|
|
|
|
|
return n.ingress
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) Dynamic() bool {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2016-05-24 23:17:19 +00:00
|
|
|
|
|
|
|
return n.dynamic
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) IPv6Enabled() bool {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2016-01-29 18:10:06 +00:00
|
|
|
|
|
|
|
return n.enableIPv6
|
|
|
|
}
|
2016-03-17 00:43:47 +00:00
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) ConfigFrom() string {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2017-04-07 17:51:39 +00:00
|
|
|
|
|
|
|
return n.configFrom
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) ConfigOnly() bool {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2017-04-07 17:51:39 +00:00
|
|
|
|
|
|
|
return n.configOnly
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) Labels() map[string]string {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2016-03-17 00:43:47 +00:00
|
|
|
|
2022-01-20 13:10:24 +00:00
|
|
|
lbls := make(map[string]string, len(n.labels))
|
2016-03-17 00:43:47 +00:00
|
|
|
for k, v := range n.labels {
|
|
|
|
lbls[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return lbls
|
|
|
|
}
|
2016-03-30 21:42:58 +00:00
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) TableEventRegister(tableName string, objType driverapi.ObjectType) error {
|
2017-03-02 07:57:37 +00:00
|
|
|
if !driverapi.IsValidType(objType) {
|
|
|
|
return fmt.Errorf("invalid object type %v in registering table, %s", objType, tableName)
|
|
|
|
}
|
|
|
|
|
|
|
|
t := networkDBTable{
|
|
|
|
name: tableName,
|
|
|
|
objType: objType,
|
|
|
|
}
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2017-03-02 07:57:37 +00:00
|
|
|
n.driverTables = append(n.driverTables, t)
|
2016-03-30 21:42:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-06-15 04:34:44 +00:00
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) UpdateIpamConfig(ipV4Data []driverapi.IPAMData) {
|
2019-08-07 21:28:54 +00:00
|
|
|
ipamV4Config := make([]*IpamConf, len(ipV4Data))
|
|
|
|
|
|
|
|
for i, data := range ipV4Data {
|
|
|
|
ic := &IpamConf{}
|
|
|
|
ic.PreferredPool = data.Pool.String()
|
|
|
|
ic.Gateway = data.Gateway.IP.String()
|
|
|
|
ipamV4Config[i] = ic
|
|
|
|
}
|
|
|
|
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2019-08-07 21:28:54 +00:00
|
|
|
n.ipamV4Config = ipamV4Config
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
// Special drivers are ones which do not need to perform any Network plumbing
|
|
|
|
func (n *Network) hasSpecialDriver() bool {
|
2016-06-15 04:34:44 +00:00
|
|
|
return n.Type() == "host" || n.Type() == "null"
|
|
|
|
}
|
2016-09-19 22:48:06 +00:00
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) hasLoadBalancerEndpoint() bool {
|
2018-04-10 16:34:41 +00:00
|
|
|
return len(n.loadBalancerIP) != 0
|
|
|
|
}
|
|
|
|
|
2023-09-23 09:44:55 +00:00
|
|
|
func (n *Network) ResolveName(ctx context.Context, req string, ipType int) ([]net.IP, bool) {
|
2016-09-19 22:48:06 +00:00
|
|
|
var ipv6Miss bool
|
|
|
|
|
|
|
|
c := n.getController()
|
2021-04-21 13:49:12 +00:00
|
|
|
networkID := n.ID()
|
2023-09-23 09:44:55 +00:00
|
|
|
|
|
|
|
_, span := otel.Tracer("").Start(ctx, "Network.ResolveName", trace.WithAttributes(
|
|
|
|
attribute.String("libnet.network.name", n.Name()),
|
|
|
|
attribute.String("libnet.network.id", networkID),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2023-01-11 20:56:50 +00:00
|
|
|
c.mu.Lock()
|
2023-09-23 09:44:55 +00:00
|
|
|
// TODO(aker): release the lock earlier
|
2023-01-11 20:56:50 +00:00
|
|
|
defer c.mu.Unlock()
|
2021-04-21 13:49:12 +00:00
|
|
|
sr, ok := c.svcRecords[networkID]
|
2016-09-19 22:48:06 +00:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
req = strings.TrimSuffix(req, ".")
|
2019-06-14 22:37:38 +00:00
|
|
|
req = strings.ToLower(req)
|
2017-06-18 12:25:58 +00:00
|
|
|
ipSet, ok := sr.svcMap.Get(req)
|
2016-09-19 22:48:06 +00:00
|
|
|
|
|
|
|
if ipType == types.IPv6 {
|
|
|
|
// If the name resolved to v4 address then its a valid name in
|
|
|
|
// the docker network domain. If the network is not v6 enabled
|
|
|
|
// set ipv6Miss to filter the DNS query from going to external
|
|
|
|
// resolvers.
|
2017-07-06 16:42:38 +00:00
|
|
|
if ok && !n.enableIPv6 {
|
2016-09-19 22:48:06 +00:00
|
|
|
ipv6Miss = true
|
|
|
|
}
|
2017-06-18 12:25:58 +00:00
|
|
|
ipSet, ok = sr.svcIPv6Map.Get(req)
|
2016-09-19 22:48:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-18 12:25:58 +00:00
|
|
|
if ok && len(ipSet) > 0 {
|
|
|
|
// this map is to avoid IP duplicates, this can happen during a transition period where 2 services are using the same IP
|
|
|
|
noDup := make(map[string]bool)
|
|
|
|
var ipLocal []net.IP
|
|
|
|
for _, ip := range ipSet {
|
2023-03-29 17:31:12 +00:00
|
|
|
if _, dup := noDup[ip.ip]; !dup {
|
|
|
|
noDup[ip.ip] = true
|
|
|
|
ipLocal = append(ipLocal, net.ParseIP(ip.ip))
|
2017-06-18 12:25:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ipLocal, ok
|
2016-09-19 22:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ipv6Miss
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) HandleQueryResp(name string, ip net.IP) {
|
2021-04-21 13:49:12 +00:00
|
|
|
networkID := n.ID()
|
2016-12-06 22:56:24 +00:00
|
|
|
c := n.getController()
|
2023-01-11 20:56:50 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2021-04-21 13:49:12 +00:00
|
|
|
sr, ok := c.svcRecords[networkID]
|
2016-09-19 22:48:06 +00:00
|
|
|
|
2016-12-06 22:56:24 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ipStr := netutils.ReverseIP(ip.String())
|
2017-06-06 23:04:50 +00:00
|
|
|
// If an object with extResolver == true is already in the set this call will fail
|
|
|
|
// but anyway it means that has already been inserted before
|
|
|
|
if ok, _ := sr.ipMap.Contains(ipStr, ipInfo{name: name}); ok {
|
|
|
|
sr.ipMap.Remove(ipStr, ipInfo{name: name})
|
|
|
|
sr.ipMap.Insert(ipStr, ipInfo{name: name, extResolver: true})
|
2016-12-06 22:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-23 09:44:55 +00:00
|
|
|
func (n *Network) ResolveIP(_ context.Context, ip string) string {
|
2021-04-21 13:49:12 +00:00
|
|
|
networkID := n.ID()
|
2016-09-19 22:48:06 +00:00
|
|
|
c := n.getController()
|
2023-01-11 20:56:50 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2021-04-21 13:49:12 +00:00
|
|
|
sr, ok := c.svcRecords[networkID]
|
2016-09-19 22:48:06 +00:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
nwName := n.Name()
|
|
|
|
|
2017-06-06 23:04:50 +00:00
|
|
|
elemSet, ok := sr.ipMap.Get(ip)
|
|
|
|
if !ok || len(elemSet) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
// NOTE it is possible to have more than one element in the Set, this will happen
|
2017-06-12 18:30:30 +00:00
|
|
|
// because of interleave of different events from different sources (local container create vs
|
2017-06-06 23:04:50 +00:00
|
|
|
// network db notifications)
|
|
|
|
// In such cases the resolution will be based on the first element of the set, and can vary
|
|
|
|
// during the system stabilitation
|
2023-03-29 17:31:12 +00:00
|
|
|
elem := elemSet[0]
|
2017-06-06 23:04:50 +00:00
|
|
|
if elem.extResolver {
|
2016-12-06 22:56:24 +00:00
|
|
|
return ""
|
2016-09-19 22:48:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 23:04:50 +00:00
|
|
|
return elem.name + "." + nwName
|
2016-09-19 22:48:06 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 09:44:55 +00:00
|
|
|
func (n *Network) ResolveService(ctx context.Context, name string) ([]*net.SRV, []net.IP) {
|
2016-09-19 22:48:06 +00:00
|
|
|
c := n.getController()
|
|
|
|
|
|
|
|
srv := []*net.SRV{}
|
|
|
|
ip := []net.IP{}
|
|
|
|
|
2023-09-23 09:44:55 +00:00
|
|
|
log.G(ctx).Debugf("Service name To resolve: %v", name)
|
2016-09-19 22:48:06 +00:00
|
|
|
|
2017-05-22 02:25:52 +00:00
|
|
|
// There are DNS implementations that allow SRV queries for names not in
|
2016-09-19 22:48:06 +00:00
|
|
|
// the format defined by RFC 2782. Hence specific validations checks are
|
|
|
|
// not done
|
|
|
|
parts := strings.Split(name, ".")
|
|
|
|
if len(parts) < 3 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
portName := parts[0]
|
|
|
|
proto := parts[1]
|
|
|
|
svcName := strings.Join(parts[2:], ".")
|
|
|
|
|
2021-04-21 13:49:12 +00:00
|
|
|
networkID := n.ID()
|
2023-01-11 20:56:50 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2021-04-21 13:49:12 +00:00
|
|
|
sr, ok := c.svcRecords[networkID]
|
2016-09-19 22:48:06 +00:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
svcs, ok := sr.service[svcName]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, svc := range svcs {
|
|
|
|
if svc.portName != portName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if svc.proto != proto {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, t := range svc.target {
|
|
|
|
srv = append(srv,
|
|
|
|
&net.SRV{
|
|
|
|
Target: t.name,
|
|
|
|
Port: t.port,
|
|
|
|
})
|
|
|
|
|
|
|
|
ip = append(ip, t.ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return srv, ip
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) ExecFunc(f func()) error {
|
2016-09-19 22:48:06 +00:00
|
|
|
return types.NotImplementedErrorf("ExecFunc not supported by network")
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) NdotsSet() bool {
|
2016-09-19 22:48:06 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-04-07 17:51:39 +00:00
|
|
|
|
|
|
|
// config-only network is looked up by name
|
2023-07-21 22:38:57 +00:00
|
|
|
func (c *Controller) getConfigNetwork(name string) (*Network, error) {
|
|
|
|
var n *Network
|
|
|
|
c.WalkNetworks(func(current *Network) bool {
|
2023-07-25 15:37:19 +00:00
|
|
|
if current.ConfigOnly() && current.Name() == name {
|
2017-04-07 17:51:39 +00:00
|
|
|
n = current
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2023-07-21 22:38:57 +00:00
|
|
|
})
|
2017-04-07 17:51:39 +00:00
|
|
|
|
|
|
|
if n == nil {
|
|
|
|
return nil, types.NotFoundErrorf("configuration network %q not found", name)
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
return n, nil
|
2017-04-07 17:51:39 +00:00
|
|
|
}
|
2017-11-04 20:58:54 +00:00
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) lbSandboxName() string {
|
2018-04-10 16:34:41 +00:00
|
|
|
name := "lb-" + n.name
|
2018-04-09 21:06:08 +00:00
|
|
|
if n.ingress {
|
2018-04-10 16:34:41 +00:00
|
|
|
name = n.name + "-sbox"
|
2018-04-09 21:06:08 +00:00
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) lbEndpointName() string {
|
2018-04-09 21:06:08 +00:00
|
|
|
return n.name + "-endpoint"
|
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) createLoadBalancerSandbox() (retErr error) {
|
2018-04-09 21:06:08 +00:00
|
|
|
sandboxName := n.lbSandboxName()
|
2018-05-18 21:10:14 +00:00
|
|
|
// Mark the sandbox to be a load balancer
|
2018-07-23 21:18:16 +00:00
|
|
|
sbOptions := []SandboxOption{OptionLoadBalancer(n.id)}
|
2017-11-04 20:58:54 +00:00
|
|
|
if n.ingress {
|
|
|
|
sbOptions = append(sbOptions, OptionIngress())
|
|
|
|
}
|
|
|
|
sb, err := n.ctrlr.NewSandbox(sandboxName, sbOptions...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
2018-04-10 03:46:22 +00:00
|
|
|
if retErr != nil {
|
2017-11-04 20:58:54 +00:00
|
|
|
if e := n.ctrlr.SandboxDestroy(sandboxName); e != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("could not delete sandbox %s on failure on failure (%v): %v", sandboxName, retErr, e)
|
2017-11-04 20:58:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-04-09 21:06:08 +00:00
|
|
|
endpointName := n.lbEndpointName()
|
2017-11-04 20:58:54 +00:00
|
|
|
epOptions := []EndpointOption{
|
|
|
|
CreateOptionIpam(n.loadBalancerIP, nil, nil, nil),
|
|
|
|
CreateOptionLoadBalancer(),
|
|
|
|
}
|
2018-04-10 16:34:41 +00:00
|
|
|
if n.hasLoadBalancerEndpoint() && !n.ingress {
|
|
|
|
// Mark LB endpoints as anonymous so they don't show up in DNS
|
|
|
|
epOptions = append(epOptions, CreateOptionAnonymous())
|
|
|
|
}
|
2017-11-04 20:58:54 +00:00
|
|
|
ep, err := n.createEndpoint(endpointName, epOptions...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
2018-04-10 03:46:22 +00:00
|
|
|
if retErr != nil {
|
2017-11-04 20:58:54 +00:00
|
|
|
if e := ep.Delete(true); e != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("could not delete endpoint %s on failure on failure (%v): %v", endpointName, retErr, e)
|
2017-11-04 20:58:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := ep.Join(sb, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-10 03:46:22 +00:00
|
|
|
|
2017-11-07 08:00:31 +00:00
|
|
|
return sb.EnableService()
|
2017-11-04 20:58:54 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 22:38:57 +00:00
|
|
|
func (n *Network) deleteLoadBalancerSandbox() error {
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Lock()
|
2017-11-04 20:58:54 +00:00
|
|
|
c := n.ctrlr
|
|
|
|
name := n.name
|
2023-01-11 23:00:34 +00:00
|
|
|
n.mu.Unlock()
|
2017-11-04 20:58:54 +00:00
|
|
|
|
2018-04-09 21:06:08 +00:00
|
|
|
sandboxName := n.lbSandboxName()
|
|
|
|
endpointName := n.lbEndpointName()
|
2017-11-04 20:58:54 +00:00
|
|
|
|
|
|
|
endpoint, err := n.EndpointByName(endpointName)
|
|
|
|
if err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("Failed to find load balancer endpoint %s on network %s: %v", endpointName, name, err)
|
2017-11-04 20:58:54 +00:00
|
|
|
} else {
|
|
|
|
info := endpoint.Info()
|
|
|
|
if info != nil {
|
|
|
|
sb := info.Sandbox()
|
|
|
|
if sb != nil {
|
|
|
|
if err := sb.DisableService(); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("Failed to disable service on sandbox %s: %v", sandboxName, err)
|
2021-08-26 08:02:04 +00:00
|
|
|
// Ignore error and attempt to delete the load balancer endpoint
|
2017-11-04 20:58:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := endpoint.Delete(true); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("Failed to delete endpoint %s (%s) in %s: %v", endpoint.Name(), endpoint.ID(), sandboxName, err)
|
2021-08-26 08:02:04 +00:00
|
|
|
// Ignore error and attempt to delete the sandbox.
|
2017-11-04 20:58:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.SandboxDestroy(sandboxName); err != nil {
|
2018-04-09 22:08:39 +00:00
|
|
|
return fmt.Errorf("Failed to delete %s sandbox: %v", sandboxName, err)
|
2017-11-04 20:58:54 +00:00
|
|
|
}
|
2018-04-09 22:08:39 +00:00
|
|
|
return nil
|
2017-11-04 20:58:54 +00:00
|
|
|
}
|