Skip to content

Commit efe0427

Browse files
committed
agent,manager,node: define our own plugin ifaces
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 <[email protected]>
1 parent 4b261ae commit efe0427

19 files changed

+147
-147
lines changed

agent/csi/plugin/manager.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import (
55
"fmt"
66
"sync"
77

8-
"github.com/docker/docker/pkg/plugingetter"
9-
108
"github.com/moby/swarmkit/v2/api"
9+
"github.com/moby/swarmkit/v2/node/plugin"
1110
)
1211

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

4039
// secrets is a SecretGetter for use by node plugins.
4140
secrets SecretGetter
4241

43-
pg plugingetter.PluginGetter
42+
pg plugin.Getter
4443
}
4544

46-
func NewManager(pg plugingetter.PluginGetter, secrets SecretGetter) Manager {
45+
func NewManager(pg plugin.Getter, secrets SecretGetter) Manager {
4746
return &pluginManager{
4847
plugins: map[string]NodePlugin{},
4948
newNodePluginFunc: NewNodePlugin,
@@ -104,17 +103,17 @@ func (pm *pluginManager) getPlugin(name string) (NodePlugin, error) {
104103
return p, nil
105104
}
106105

107-
pc, err := pm.pg.Get(name, DockerCSIPluginCap, plugingetter.Lookup)
106+
pc, err := pm.pg.Get(name, DockerCSIPluginCap)
108107
if err != nil {
109108
return nil, err
110109
}
111110

112-
pa, ok := pc.(plugingetter.PluginAddr)
111+
pa, ok := pc.(plugin.AddrPlugin)
113112
if !ok {
114113
return nil, fmt.Errorf("plugin does not implement PluginAddr interface")
115114
}
116115

117-
p := pm.newNodePluginFunc(name, pc, pa, pm.secrets)
116+
p := pm.newNodePluginFunc(name, pa, pm.secrets)
118117
pm.plugins[name] = p
119118
return p, nil
120119
}

agent/csi/plugin/manager_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var _ = Describe("Manager", func() {
1818

1919
BeforeEach(func() {
2020
pg = &testutils.FakePluginGetter{
21-
Plugins: map[string]*testutils.FakeCompatPlugin{},
21+
Plugins: map[string]*testutils.FakePlugin{},
2222
}
2323

2424
pm = &pluginManager{
@@ -27,21 +27,21 @@ var _ = Describe("Manager", func() {
2727
pg: pg,
2828
}
2929

30-
pg.Plugins["plug1"] = &testutils.FakeCompatPlugin{
30+
pg.Plugins["plug1"] = &testutils.FakePlugin{
3131
PluginName: "plug1",
3232
PluginAddr: &net.UnixAddr{
3333
Net: "unix",
3434
Name: "",
3535
},
3636
}
37-
pg.Plugins["plug2"] = &testutils.FakeCompatPlugin{
37+
pg.Plugins["plug2"] = &testutils.FakePlugin{
3838
PluginName: "plug2",
3939
PluginAddr: &net.UnixAddr{
4040
Net: "unix",
4141
Name: "fail",
4242
},
4343
}
44-
pg.Plugins["plug3"] = &testutils.FakeCompatPlugin{
44+
pg.Plugins["plug3"] = &testutils.FakePlugin{
4545
PluginName: "plug3",
4646
PluginAddr: &net.UnixAddr{
4747
Net: "unix",

agent/csi/plugin/plugin.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111
"google.golang.org/grpc/status"
1212

1313
"github.com/container-storage-interface/spec/lib/go/csi"
14-
"github.com/docker/docker/pkg/plugingetter"
1514
"github.com/moby/swarmkit/v2/api"
1615
"github.com/moby/swarmkit/v2/internal/csi/capability"
1716
"github.com/moby/swarmkit/v2/log"
17+
"github.com/moby/swarmkit/v2/node/plugin"
1818
)
1919

2020
// SecretGetter is a reimplementation of the exec.SecretGetter interface in the
@@ -88,17 +88,17 @@ const (
8888
TargetPublishPath string = "/data/published"
8989
)
9090

91-
func NewNodePlugin(name string, pc plugingetter.CompatPlugin, pa plugingetter.PluginAddr, secrets SecretGetter) NodePlugin {
92-
return newNodePlugin(name, pc, pa, secrets)
91+
func NewNodePlugin(name string, p plugin.AddrPlugin, secrets SecretGetter) NodePlugin {
92+
return newNodePlugin(name, p, secrets)
9393
}
9494

9595
// newNodePlugin returns a raw nodePlugin object, not behind an interface. this
9696
// is useful for testing.
97-
func newNodePlugin(name string, pc plugingetter.CompatPlugin, pa plugingetter.PluginAddr, secrets SecretGetter) *nodePlugin {
97+
func newNodePlugin(name string, p plugin.AddrPlugin, secrets SecretGetter) *nodePlugin {
9898
return &nodePlugin{
9999
name: name,
100-
socket: fmt.Sprintf("%s://%s", pa.Addr().Network(), pa.Addr().String()),
101-
scopePath: pc.ScopedPath,
100+
socket: fmt.Sprintf("%s://%s", p.Addr().Network(), p.Addr().String()),
101+
scopePath: p.ScopedPath,
102102
secrets: secrets,
103103
volumeMap: map[string]*volumePublishStatus{},
104104
}

agent/csi/plugin/plugin_fake_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/docker/docker/pkg/plugingetter"
8-
97
"github.com/moby/swarmkit/v2/api"
8+
mobyplugin "github.com/moby/swarmkit/v2/node/plugin"
109
)
1110

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

2120
// newFakeNodePlugin has the same signature as NewNodePlugin, allowing it to be
2221
// substituted in testing.
23-
func newFakeNodePlugin(name string, pc plugingetter.CompatPlugin, pa plugingetter.PluginAddr, secrets SecretGetter) NodePlugin {
22+
func newFakeNodePlugin(name string, pa mobyplugin.AddrPlugin, secrets SecretGetter) NodePlugin {
2423
return &fakeNodePlugin{
2524
name: name,
2625
socket: pa.Addr().String(),

agent/csi/plugin/plugin_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import (
1515
)
1616

1717
func newVolumeClient(name string, nodeID string) *nodePlugin {
18-
p := &testutils.FakeCompatPlugin{
18+
p := &testutils.FakePlugin{
1919
PluginName: name,
2020
PluginAddr: &net.UnixAddr{},
2121
}
22-
n := newNodePlugin(name, p, p, nil)
22+
n := newNodePlugin(name, p, nil)
2323
n.staging = true
2424

2525
fakeNodeClient := newFakeNodeClient(true, nodeID)

agent/csi/volumes.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import (
66
"sync"
77
"time"
88

9-
"github.com/docker/docker/pkg/plugingetter"
10-
119
"github.com/moby/swarmkit/v2/agent/csi/plugin"
1210
"github.com/moby/swarmkit/v2/agent/exec"
1311
"github.com/moby/swarmkit/v2/api"
1412
"github.com/moby/swarmkit/v2/log"
13+
mobyplugin "github.com/moby/swarmkit/v2/node/plugin"
1514
"github.com/moby/swarmkit/v2/volumequeue"
1615
)
1716

@@ -46,7 +45,7 @@ type volumes struct {
4645
}
4746

4847
// NewManager returns a place to store volumes.
49-
func NewManager(pg plugingetter.PluginGetter, secrets exec.SecretGetter) exec.VolumesManager {
48+
func NewManager(pg mobyplugin.Getter, secrets exec.SecretGetter) exec.VolumesManager {
5049
r := &volumes{
5150
volumes: map[string]volumeState{},
5251
plugins: plugin.NewManager(pg, secrets),

agent/dependency.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package agent
22

33
import (
4-
"github.com/docker/docker/pkg/plugingetter"
5-
64
"github.com/moby/swarmkit/v2/agent/configs"
75
"github.com/moby/swarmkit/v2/agent/csi"
86
"github.com/moby/swarmkit/v2/agent/exec"
97
"github.com/moby/swarmkit/v2/agent/secrets"
108
"github.com/moby/swarmkit/v2/api"
9+
"github.com/moby/swarmkit/v2/node/plugin"
1110
)
1211

1312
type dependencyManager struct {
@@ -18,7 +17,7 @@ type dependencyManager struct {
1817

1918
// NewDependencyManager creates a dependency manager object that wraps
2019
// objects which provide access to various dependency types.
21-
func NewDependencyManager(pg plugingetter.PluginGetter) exec.DependencyManager {
20+
func NewDependencyManager(pg plugin.Getter) exec.DependencyManager {
2221
d := &dependencyManager{
2322
secrets: secrets.NewManager(),
2423
configs: configs.NewManager(),

agent/worker_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestWorkerAssign(t *testing.T) {
4242
defer cleanup()
4343

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

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

418418
pg := &testutils.FakePluginGetter{
419-
Plugins: map[string]*testutils.FakeCompatPlugin{
419+
Plugins: map[string]*testutils.FakePlugin{
420420
"plugin-1": {
421421
PluginName: "plugin-1",
422422
PluginAddr: &net.UnixAddr{},

manager/csi/fakes_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"github.com/golang/protobuf/ptypes/wrappers"
1111
"google.golang.org/grpc"
1212

13-
"github.com/docker/docker/pkg/plugingetter"
1413
"github.com/moby/swarmkit/v2/api"
14+
mobyplugin "github.com/moby/swarmkit/v2/node/plugin"
1515
)
1616

1717
const (
@@ -203,11 +203,11 @@ type fakePluginMaker struct {
203203
plugins map[string]*fakePlugin
204204
}
205205

206-
func (fpm *fakePluginMaker) newFakePlugin(pc plugingetter.CompatPlugin, pa plugingetter.PluginAddr, provider SecretProvider) Plugin {
206+
func (fpm *fakePluginMaker) newFakePlugin(pa mobyplugin.AddrPlugin, provider SecretProvider) Plugin {
207207
fpm.Lock()
208208
defer fpm.Unlock()
209209
p := &fakePlugin{
210-
name: pc.Name(),
210+
name: pa.Name(),
211211
socket: pa.Addr().String(),
212212
swarmToCSI: map[string]string{},
213213
volumesCreated: map[string]*api.Volume{},
@@ -216,7 +216,7 @@ func (fpm *fakePluginMaker) newFakePlugin(pc plugingetter.CompatPlugin, pa plugi
216216
volumesUnpublished: map[string][]string{},
217217
removedIDs: map[string]struct{}{},
218218
}
219-
fpm.plugins[pc.Name()] = p
219+
fpm.plugins[pa.Name()] = p
220220
return p
221221
}
222222

manager/csi/manager.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
"sync"
88
"time"
99

10-
"github.com/docker/docker/pkg/plugingetter"
1110
"github.com/docker/go-events"
1211

1312
"github.com/moby/swarmkit/v2/api"
1413
"github.com/moby/swarmkit/v2/log"
1514
"github.com/moby/swarmkit/v2/manager/state/store"
15+
mobyplugin "github.com/moby/swarmkit/v2/node/plugin"
1616
"github.com/moby/swarmkit/v2/volumequeue"
1717
)
1818

@@ -36,12 +36,12 @@ type Manager struct {
3636

3737
// pg is the plugingetter, which allows us to access the Docker Engine's
3838
// plugin store.
39-
pg plugingetter.PluginGetter
39+
pg mobyplugin.Getter
4040

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

4646
// synchronization for starting and stopping the Manager
4747
startOnce sync.Once
@@ -55,7 +55,7 @@ type Manager struct {
5555
pendingVolumes *volumequeue.VolumeQueue
5656
}
5757

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

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

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

486-
p := vm.newPlugin(pc, pa, vm.provider)
486+
p := vm.newPlugin(pa, vm.provider)
487487
vm.plugins[name] = p
488488

489489
return p, nil

0 commit comments

Comments
 (0)