Skip to content

Commit

Permalink
Vendor dependency cycle-free swarmkit
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
corhere committed Feb 28, 2024
1 parent 3ca1d75 commit 7b0ab10
Show file tree
Hide file tree
Showing 113 changed files with 29,815 additions and 298 deletions.
42 changes: 42 additions & 0 deletions daemon/cluster/convert/pluginadapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package convert

import (
"github.com/docker/docker/pkg/plugingetter"
"github.com/moby/swarmkit/v2/node/plugin"
)

// SwarmPluginGetter adapts a plugingetter.PluginGetter to a Swarmkit plugin.Getter.
func SwarmPluginGetter(pg plugingetter.PluginGetter) plugin.Getter {
return pluginGetter{pg}
}

type pluginGetter struct {
pg plugingetter.PluginGetter
}

var _ plugin.Getter = (*pluginGetter)(nil)

type swarmPlugin struct {
plugingetter.CompatPlugin
}

func (p swarmPlugin) Client() plugin.Client {
return p.CompatPlugin.Client()
}

func (g pluginGetter) Get(name string, capability string) (plugin.Plugin, error) {
p, err := g.pg.Get(name, capability, plugingetter.Lookup)
if err != nil {
return nil, err
}
return swarmPlugin{p}, nil
}

func (g pluginGetter) GetAllManagedPluginsByCap(capability string) []plugin.Plugin {
pp := g.pg.GetAllManagedPluginsByCap(capability)
ret := make([]plugin.Plugin, len(pp))
for i, p := range pp {
ret[i] = swarmPlugin{p}
}
return ret
}
2 changes: 1 addition & 1 deletion daemon/cluster/executor/container/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewExecutor(b executorpkg.Backend, p plugin.Backend, i executorpkg.ImageBac
pluginBackend: p,
imageBackend: i,
volumeBackend: v,
dependencies: agent.NewDependencyManager(b.PluginGetter()),
dependencies: agent.NewDependencyManager(convert.SwarmPluginGetter(b.PluginGetter())),
}
}

Expand Down
9 changes: 6 additions & 3 deletions daemon/cluster/noderunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (

"github.com/containerd/log"
types "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/daemon/cluster/convert"
"github.com/docker/docker/daemon/cluster/executor/container"
lncluster "github.com/docker/docker/libnetwork/cluster"
"github.com/docker/docker/libnetwork/cnmallocator"
swarmapi "github.com/moby/swarmkit/v2/api"
swarmallocator "github.com/moby/swarmkit/v2/manager/allocator/cnmallocator"
"github.com/moby/swarmkit/v2/manager/allocator/networkallocator"
swarmnode "github.com/moby/swarmkit/v2/node"
"github.com/pkg/errors"
"google.golang.org/grpc"
Expand Down Expand Up @@ -123,7 +125,7 @@ func (n *nodeRunner) start(conf nodeStartConfig) error {
ListenControlAPI: control,
ListenRemoteAPI: conf.ListenAddr,
AdvertiseRemoteAPI: conf.AdvertiseAddr,
NetworkConfig: &swarmallocator.NetworkConfig{
NetworkConfig: &networkallocator.Config{
DefaultAddrPool: conf.DefaultAddressPool,
SubnetSize: conf.SubnetSize,
VXLANUDPPort: conf.DataPathPort,
Expand All @@ -144,7 +146,8 @@ func (n *nodeRunner) start(conf nodeStartConfig) error {
ElectionTick: n.cluster.config.RaftElectionTick,
UnlockKey: conf.lockKey,
AutoLockManagers: conf.autolock,
PluginGetter: n.cluster.config.Backend.PluginGetter(),
PluginGetter: convert.SwarmPluginGetter(n.cluster.config.Backend.PluginGetter()),
NetworkProvider: cnmallocator.NewProvider(n.cluster.config.Backend.PluginGetter()),
}
if conf.availability != "" {
avail, ok := swarmapi.NodeSpec_Availability_value[strings.ToUpper(string(conf.availability))]
Expand Down
14 changes: 14 additions & 0 deletions libnetwork/cnmallocator/allocator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cnmallocator

import (
"runtime"
"testing"

"github.com/moby/swarmkit/v2/manager/allocator"
"gotest.tools/v3/skip"
)

func TestAllocator(t *testing.T) {
skip.If(t, runtime.GOOS == "windows", "Allocator tests are hardcoded to use Linux network driver names")
allocator.RunAllocatorTests(t, NewProvider(nil))
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ var initializers = map[string]func(driverapi.Registerer) error{
}

// PredefinedNetworks returns the list of predefined network structures
func PredefinedNetworks() []networkallocator.PredefinedNetworkData {
func (*Provider) PredefinedNetworks() []networkallocator.PredefinedNetworkData {
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"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/log"
"github.com/moby/swarmkit/v2/manager/allocator/networkallocator"
)

func initIPAMDrivers(r ipamapi.Registerer, netConfig *NetworkConfig) error {
func initIPAMDrivers(r ipamapi.Registerer, netConfig *networkallocator.Config) error {
var addressPool []*ipamutils.NetworkToSplit
var str strings.Builder
str.WriteString("Subnetlist - ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var initializers = map[string]func(driverapi.Registerer) error{
}

// PredefinedNetworks returns the list of predefined network structures
func PredefinedNetworks() []networkallocator.PredefinedNetworkData {
func (*Provider) PredefinedNetworks() []networkallocator.PredefinedNetworkData {
return []networkallocator.PredefinedNetworkData{
{Name: "bridge", Driver: "bridge"},
{Name: "host", Driver: "host"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var initializers = map[string]func(driverapi.Registerer) error{
}

// PredefinedNetworks returns the list of predefined network structures
func PredefinedNetworks() []networkallocator.PredefinedNetworkData {
func (*Provider) PredefinedNetworks() []networkallocator.PredefinedNetworkData {
return []networkallocator.PredefinedNetworkData{
{Name: "nat", Driver: "nat"},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import (
const initializers = nil

// PredefinedNetworks returns the list of predefined network structures
func PredefinedNetworks() []networkallocator.PredefinedNetworkData {
func (*Provider) PredefinedNetworks() []networkallocator.PredefinedNetworkData {
return nil
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"strings"

"github.com/containerd/log"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/drivers/remote"
"github.com/docker/docker/libnetwork/drvregistry"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/docker/docker/libnetwork/scope"
"github.com/docker/docker/pkg/plugingetter"
"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/log"
"github.com/moby/swarmkit/v2/manager/allocator/networkallocator"
"github.com/pkg/errors"
)
Expand All @@ -40,9 +40,6 @@ type cnmNetworkAllocator struct {
// The driver registry for all internal and external network drivers.
networkRegistry drvregistry.Networks

// The port allocator instance for allocating node ports
portAllocator *portAllocator

// Local network state used by cnmNetworkAllocator to do network management.
networks map[string]*network

Expand Down Expand Up @@ -87,51 +84,32 @@ type networkDriver struct {
capability *driverapi.Capability
}

// NetworkConfig is used to store network related cluster config in the Manager.
type NetworkConfig struct {
// DefaultAddrPool specifies default subnet pool for global scope networks
DefaultAddrPool []string

// SubnetSize specifies the subnet size of the networks created from
// the default subnet pool
SubnetSize uint32

// VXLANUDPPort specifies the UDP port number for VXLAN traffic
VXLANUDPPort uint32
}

// New returns a new NetworkAllocator handle
func New(pg plugingetter.PluginGetter, netConfig *NetworkConfig) (networkallocator.NetworkAllocator, error) {
// NewAllocator returns a new NetworkAllocator handle
func (p *Provider) NewAllocator(netConfig *networkallocator.Config) (networkallocator.NetworkAllocator, error) {
na := &cnmNetworkAllocator{
networks: make(map[string]*network),
services: make(map[string]struct{}),
tasks: make(map[string]struct{}),
nodes: make(map[string]map[string]struct{}),
pg: pg,
pg: p.pg,
}

for ntype, i := range initializers {
if err := i(&na.networkRegistry); err != nil {
return nil, fmt.Errorf("failed to register %q network driver: %w", ntype, err)
}
}
if err := remote.Register(&na.networkRegistry, pg); err != nil {
if err := remote.Register(&na.networkRegistry, p.pg); err != nil {
return nil, fmt.Errorf("failed to initialize network driver plugins: %w", err)
}

if err := initIPAMDrivers(&na.ipamRegistry, netConfig); err != nil {
return nil, err
}
if err := remoteipam.Register(&na.ipamRegistry, pg); err != nil {
if err := remoteipam.Register(&na.ipamRegistry, p.pg); err != nil {
return nil, fmt.Errorf("failed to initialize IPAM driver plugins: %w", err)
}

pa, err := newPortAllocator()
if err != nil {
return nil, err
}

na.portAllocator = pa
return na, nil
}

Expand Down Expand Up @@ -209,11 +187,8 @@ func (na *cnmNetworkAllocator) Deallocate(n *api.Network) error {
}

// AllocateService allocates all the network resources such as virtual
// IP and ports needed by the service.
// IP needed by the service.
func (na *cnmNetworkAllocator) AllocateService(s *api.Service) (err error) {
if err = na.portAllocator.serviceAllocatePorts(s); err != nil {
return err
}
defer func() {
if err != nil {
na.DeallocateService(s)
Expand Down Expand Up @@ -300,7 +275,7 @@ networkLoop:
}

// DeallocateService de-allocates all the network resources such as
// virtual IP and ports associated with the service.
// virtual IP associated with the service.
func (na *cnmNetworkAllocator) DeallocateService(s *api.Service) error {
if s.Endpoint == nil {
return nil
Expand All @@ -316,7 +291,6 @@ func (na *cnmNetworkAllocator) DeallocateService(s *api.Service) error {
}
s.Endpoint.VirtualIPs = nil

na.portAllocator.serviceDeallocatePorts(s)
delete(na.services, s.ID)

return nil
Expand Down Expand Up @@ -373,19 +347,8 @@ func (na *cnmNetworkAllocator) IsTaskAllocated(t *api.Task) bool {
return true
}

// HostPublishPortsNeedUpdate returns true if the passed service needs
// allocations for its published ports in host (non ingress) mode
func (na *cnmNetworkAllocator) HostPublishPortsNeedUpdate(s *api.Service) bool {
return na.portAllocator.hostPublishPortsNeedUpdate(s)
}

// IsServiceAllocated returns false if the passed service needs to have network resources allocated/updated.
func (na *cnmNetworkAllocator) IsServiceAllocated(s *api.Service, flags ...func(*networkallocator.ServiceAllocationOpts)) bool {
var options networkallocator.ServiceAllocationOpts
for _, flag := range flags {
flag(&options)
}

specNetworks := serviceNetworks(s)

// If endpoint mode is VIP and allocator does not have the
Expand Down Expand Up @@ -447,10 +410,6 @@ func (na *cnmNetworkAllocator) IsServiceAllocated(s *api.Service, flags ...func(
}
}

if (s.Spec.Endpoint != nil && len(s.Spec.Endpoint.Ports) != 0) ||
(s.Endpoint != nil && len(s.Endpoint.Ports) != 0) {
return na.portAllocator.isPortsAllocatedOnInit(s, options.OnInit)
}
return true
}

Expand Down

0 comments on commit 7b0ab10

Please sign in to comment.