Skip to content

Commit

Permalink
agent,manager,node: define our own plugin ifaces
Browse files Browse the repository at this point in the history
Allow Moby plugins to be used without Swarmkit having to import
github.com/docker/docker packages by defining our own interfaces which
cover the subset of functionality we use. Adapters will have to be
written on the Moby side as their API contains methods which return
concrete struct types, but they only need to be trivial wrappers as
their *plugins.Client satisfies our plugin.Client interface.

Signed-off-by: Cory Snider <csnider@mirantis.com>
  • Loading branch information
corhere committed Feb 23, 2024
1 parent 4b261ae commit efe0427
Show file tree
Hide file tree
Showing 19 changed files with 147 additions and 147 deletions.
15 changes: 7 additions & 8 deletions agent/csi/plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"fmt"
"sync"

"github.com/docker/docker/pkg/plugingetter"

"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/node/plugin"
)

const (
Expand Down Expand Up @@ -35,15 +34,15 @@ type pluginManager struct {
// newNodePluginFunc usually points to NewNodePlugin. However, for testing,
// NewNodePlugin can be swapped out with a function that creates fake node
// plugins
newNodePluginFunc func(string, plugingetter.CompatPlugin, plugingetter.PluginAddr, SecretGetter) NodePlugin
newNodePluginFunc func(string, plugin.AddrPlugin, SecretGetter) NodePlugin

// secrets is a SecretGetter for use by node plugins.
secrets SecretGetter

pg plugingetter.PluginGetter
pg plugin.Getter
}

func NewManager(pg plugingetter.PluginGetter, secrets SecretGetter) Manager {
func NewManager(pg plugin.Getter, secrets SecretGetter) Manager {
return &pluginManager{
plugins: map[string]NodePlugin{},
newNodePluginFunc: NewNodePlugin,
Expand Down Expand Up @@ -104,17 +103,17 @@ func (pm *pluginManager) getPlugin(name string) (NodePlugin, error) {
return p, nil
}

pc, err := pm.pg.Get(name, DockerCSIPluginCap, plugingetter.Lookup)
pc, err := pm.pg.Get(name, DockerCSIPluginCap)
if err != nil {
return nil, err
}

pa, ok := pc.(plugingetter.PluginAddr)
pa, ok := pc.(plugin.AddrPlugin)
if !ok {
return nil, fmt.Errorf("plugin does not implement PluginAddr interface")
}

p := pm.newNodePluginFunc(name, pc, pa, pm.secrets)
p := pm.newNodePluginFunc(name, pa, pm.secrets)
pm.plugins[name] = p
return p, nil
}
8 changes: 4 additions & 4 deletions agent/csi/plugin/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var _ = Describe("Manager", func() {

BeforeEach(func() {
pg = &testutils.FakePluginGetter{
Plugins: map[string]*testutils.FakeCompatPlugin{},
Plugins: map[string]*testutils.FakePlugin{},
}

pm = &pluginManager{
Expand All @@ -27,21 +27,21 @@ var _ = Describe("Manager", func() {
pg: pg,
}

pg.Plugins["plug1"] = &testutils.FakeCompatPlugin{
pg.Plugins["plug1"] = &testutils.FakePlugin{
PluginName: "plug1",
PluginAddr: &net.UnixAddr{
Net: "unix",
Name: "",
},
}
pg.Plugins["plug2"] = &testutils.FakeCompatPlugin{
pg.Plugins["plug2"] = &testutils.FakePlugin{
PluginName: "plug2",
PluginAddr: &net.UnixAddr{
Net: "unix",
Name: "fail",
},
}
pg.Plugins["plug3"] = &testutils.FakeCompatPlugin{
pg.Plugins["plug3"] = &testutils.FakePlugin{
PluginName: "plug3",
PluginAddr: &net.UnixAddr{
Net: "unix",
Expand Down
12 changes: 6 additions & 6 deletions agent/csi/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"google.golang.org/grpc/status"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/docker/docker/pkg/plugingetter"
"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/internal/csi/capability"
"github.com/moby/swarmkit/v2/log"
"github.com/moby/swarmkit/v2/node/plugin"
)

// SecretGetter is a reimplementation of the exec.SecretGetter interface in the
Expand Down Expand Up @@ -88,17 +88,17 @@ const (
TargetPublishPath string = "/data/published"
)

func NewNodePlugin(name string, pc plugingetter.CompatPlugin, pa plugingetter.PluginAddr, secrets SecretGetter) NodePlugin {
return newNodePlugin(name, pc, pa, secrets)
func NewNodePlugin(name string, p plugin.AddrPlugin, secrets SecretGetter) NodePlugin {
return newNodePlugin(name, p, secrets)
}

// newNodePlugin returns a raw nodePlugin object, not behind an interface. this
// is useful for testing.
func newNodePlugin(name string, pc plugingetter.CompatPlugin, pa plugingetter.PluginAddr, secrets SecretGetter) *nodePlugin {
func newNodePlugin(name string, p plugin.AddrPlugin, secrets SecretGetter) *nodePlugin {
return &nodePlugin{
name: name,
socket: fmt.Sprintf("%s://%s", pa.Addr().Network(), pa.Addr().String()),
scopePath: pc.ScopedPath,
socket: fmt.Sprintf("%s://%s", p.Addr().Network(), p.Addr().String()),
scopePath: p.ScopedPath,
secrets: secrets,
volumeMap: map[string]*volumePublishStatus{},
}
Expand Down
5 changes: 2 additions & 3 deletions agent/csi/plugin/plugin_fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"context"
"fmt"

"github.com/docker/docker/pkg/plugingetter"

"github.com/moby/swarmkit/v2/api"
mobyplugin "github.com/moby/swarmkit/v2/node/plugin"
)

// plugin_fake_test.go contains code for faking node plugins in the context of
Expand All @@ -20,7 +19,7 @@ type fakeNodePlugin struct {

// newFakeNodePlugin has the same signature as NewNodePlugin, allowing it to be
// substituted in testing.
func newFakeNodePlugin(name string, pc plugingetter.CompatPlugin, pa plugingetter.PluginAddr, secrets SecretGetter) NodePlugin {
func newFakeNodePlugin(name string, pa mobyplugin.AddrPlugin, secrets SecretGetter) NodePlugin {
return &fakeNodePlugin{
name: name,
socket: pa.Addr().String(),
Expand Down
4 changes: 2 additions & 2 deletions agent/csi/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
)

func newVolumeClient(name string, nodeID string) *nodePlugin {
p := &testutils.FakeCompatPlugin{
p := &testutils.FakePlugin{
PluginName: name,
PluginAddr: &net.UnixAddr{},
}
n := newNodePlugin(name, p, p, nil)
n := newNodePlugin(name, p, nil)
n.staging = true

fakeNodeClient := newFakeNodeClient(true, nodeID)
Expand Down
5 changes: 2 additions & 3 deletions agent/csi/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"sync"
"time"

"github.com/docker/docker/pkg/plugingetter"

"github.com/moby/swarmkit/v2/agent/csi/plugin"
"github.com/moby/swarmkit/v2/agent/exec"
"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/log"
mobyplugin "github.com/moby/swarmkit/v2/node/plugin"
"github.com/moby/swarmkit/v2/volumequeue"
)

Expand Down Expand Up @@ -46,7 +45,7 @@ type volumes struct {
}

// NewManager returns a place to store volumes.
func NewManager(pg plugingetter.PluginGetter, secrets exec.SecretGetter) exec.VolumesManager {
func NewManager(pg mobyplugin.Getter, secrets exec.SecretGetter) exec.VolumesManager {
r := &volumes{
volumes: map[string]volumeState{},
plugins: plugin.NewManager(pg, secrets),
Expand Down
5 changes: 2 additions & 3 deletions agent/dependency.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package agent

import (
"github.com/docker/docker/pkg/plugingetter"

"github.com/moby/swarmkit/v2/agent/configs"
"github.com/moby/swarmkit/v2/agent/csi"
"github.com/moby/swarmkit/v2/agent/exec"
"github.com/moby/swarmkit/v2/agent/secrets"
"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/node/plugin"
)

type dependencyManager struct {
Expand All @@ -18,7 +17,7 @@ type dependencyManager struct {

// NewDependencyManager creates a dependency manager object that wraps
// objects which provide access to various dependency types.
func NewDependencyManager(pg plugingetter.PluginGetter) exec.DependencyManager {
func NewDependencyManager(pg plugin.Getter) exec.DependencyManager {
d := &dependencyManager{
secrets: secrets.NewManager(),
configs: configs.NewManager(),
Expand Down
6 changes: 3 additions & 3 deletions agent/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestWorkerAssign(t *testing.T) {
defer cleanup()

pg := &testutils.FakePluginGetter{
Plugins: map[string]*testutils.FakeCompatPlugin{
Plugins: map[string]*testutils.FakePlugin{
"plugin-1": {
PluginName: "plugin-1",
PluginAddr: &net.UnixAddr{},
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestWorkerWait(t *testing.T) {
ctx := context.Background()

pg := &testutils.FakePluginGetter{
Plugins: map[string]*testutils.FakeCompatPlugin{
Plugins: map[string]*testutils.FakePlugin{
"plugin-1": {
PluginName: "plugin-1",
PluginAddr: &net.UnixAddr{},
Expand Down Expand Up @@ -416,7 +416,7 @@ func TestWorkerUpdate(t *testing.T) {
ctx := context.Background()

pg := &testutils.FakePluginGetter{
Plugins: map[string]*testutils.FakeCompatPlugin{
Plugins: map[string]*testutils.FakePlugin{
"plugin-1": {
PluginName: "plugin-1",
PluginAddr: &net.UnixAddr{},
Expand Down
8 changes: 4 additions & 4 deletions manager/csi/fakes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/golang/protobuf/ptypes/wrappers"
"google.golang.org/grpc"

"github.com/docker/docker/pkg/plugingetter"
"github.com/moby/swarmkit/v2/api"
mobyplugin "github.com/moby/swarmkit/v2/node/plugin"
)

const (
Expand Down Expand Up @@ -203,11 +203,11 @@ type fakePluginMaker struct {
plugins map[string]*fakePlugin
}

func (fpm *fakePluginMaker) newFakePlugin(pc plugingetter.CompatPlugin, pa plugingetter.PluginAddr, provider SecretProvider) Plugin {
func (fpm *fakePluginMaker) newFakePlugin(pa mobyplugin.AddrPlugin, provider SecretProvider) Plugin {
fpm.Lock()
defer fpm.Unlock()
p := &fakePlugin{
name: pc.Name(),
name: pa.Name(),
socket: pa.Addr().String(),
swarmToCSI: map[string]string{},
volumesCreated: map[string]*api.Volume{},
Expand All @@ -216,7 +216,7 @@ func (fpm *fakePluginMaker) newFakePlugin(pc plugingetter.CompatPlugin, pa plugi
volumesUnpublished: map[string][]string{},
removedIDs: map[string]struct{}{},
}
fpm.plugins[pc.Name()] = p
fpm.plugins[pa.Name()] = p
return p
}

Expand Down
14 changes: 7 additions & 7 deletions manager/csi/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"sync"
"time"

"github.com/docker/docker/pkg/plugingetter"
"github.com/docker/go-events"

"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/log"
"github.com/moby/swarmkit/v2/manager/state/store"
mobyplugin "github.com/moby/swarmkit/v2/node/plugin"
"github.com/moby/swarmkit/v2/volumequeue"
)

Expand All @@ -36,12 +36,12 @@ type Manager struct {

// pg is the plugingetter, which allows us to access the Docker Engine's
// plugin store.
pg plugingetter.PluginGetter
pg mobyplugin.Getter

// newPlugin is a function which returns an object implementing the Plugin
// interface. It allows us to swap out the implementation of plugins while
// unit-testing the Manager
newPlugin func(pc plugingetter.CompatPlugin, pa plugingetter.PluginAddr, provider SecretProvider) Plugin
newPlugin func(p mobyplugin.AddrPlugin, provider SecretProvider) Plugin

// synchronization for starting and stopping the Manager
startOnce sync.Once
Expand All @@ -55,7 +55,7 @@ type Manager struct {
pendingVolumes *volumequeue.VolumeQueue
}

func NewManager(s *store.MemoryStore, pg plugingetter.PluginGetter) *Manager {
func NewManager(s *store.MemoryStore, pg mobyplugin.Getter) *Manager {
return &Manager{
store: s,
stopChan: make(chan struct{}),
Expand Down Expand Up @@ -469,7 +469,7 @@ func (vm *Manager) getPlugin(name string) (Plugin, error) {
}

// otherwise, we need to load the plugin.
pc, err := vm.pg.Get(name, DockerCSIPluginCap, plugingetter.Lookup)
pc, err := vm.pg.Get(name, DockerCSIPluginCap)
if err != nil {
return nil, err
}
Expand All @@ -478,12 +478,12 @@ func (vm *Manager) getPlugin(name string) (Plugin, error) {
return nil, errors.New("driver \"" + name + "\" not found")
}

pa, ok := pc.(plugingetter.PluginAddr)
pa, ok := pc.(mobyplugin.AddrPlugin)
if !ok {
return nil, errors.New("plugin for driver \"" + name + "\" does not implement PluginAddr")
}

p := vm.newPlugin(pc, pa, vm.provider)
p := vm.newPlugin(pa, vm.provider)
vm.plugins[name] = p

return p, nil
Expand Down
Loading

0 comments on commit efe0427

Please sign in to comment.