moby/libnetwork/cnmallocator/drivers_ipam.go
Cory Snider 7b0ab1011c Vendor dependency cycle-free swarmkit
Moby imports Swarmkit; Swarmkit no longer imports Moby. In order to
accomplish this feat, Swarmkit has introduced a new plugin.Getter
interface so it could stop importing our pkg/plugingetter package. This
new interface is not entirely compatible with our
plugingetter.PluginGetter interface, necessitating a thin adapter.

Swarmkit had to jettison the CNM network allocator to stop having to
import libnetwork as the cnmallocator package is deeply tied to
libnetwork. Move the CNM network allocator into libnetwork, where it
belongs. The package had a short an uninteresting Git history in the
Swarmkit repository so no effort was made to retain history.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2024-02-28 09:46:45 -05:00

53 lines
1.5 KiB
Go

package cnmallocator
import (
"context"
"strconv"
"strings"
"github.com/containerd/log"
"github.com/docker/docker/libnetwork/ipamapi"
builtinIpam "github.com/docker/docker/libnetwork/ipams/builtin"
nullIpam "github.com/docker/docker/libnetwork/ipams/null"
"github.com/docker/docker/libnetwork/ipamutils"
"github.com/moby/swarmkit/v2/manager/allocator/networkallocator"
)
func initIPAMDrivers(r ipamapi.Registerer, netConfig *networkallocator.Config) error {
var addressPool []*ipamutils.NetworkToSplit
var str strings.Builder
str.WriteString("Subnetlist - ")
// Extract defaultAddrPool param info and construct ipamutils.NetworkToSplit
// from the info. We will be using it to call Libnetwork API
// We also need to log new address pool info whenever swarm init
// happens with default address pool option
if netConfig != nil {
for _, p := range netConfig.DefaultAddrPool {
addressPool = append(addressPool, &ipamutils.NetworkToSplit{
Base: p,
Size: int(netConfig.SubnetSize),
})
str.WriteString(p + ",")
}
str.WriteString(": Size ")
str.WriteString(strconv.Itoa(int(netConfig.SubnetSize)))
}
if err := ipamutils.ConfigGlobalScopeDefaultNetworks(addressPool); err != nil {
return err
}
if addressPool != nil {
log.G(context.TODO()).Infof("Swarm initialized global default address pool to: " + str.String())
}
for _, fn := range [](func(ipamapi.Registerer) error){
builtinIpam.Register,
nullIpam.Register,
} {
if err := fn(r); err != nil {
return err
}
}
return nil
}