Skip to content

Commit b5771e2

Browse files
authored
Merge pull request #21 from LarsxGitHub/custom-plugin-support
Add support for custom volume driver
2 parents cbb95e0 + 870c93c commit b5771e2

File tree

13 files changed

+109
-28
lines changed

13 files changed

+109
-28
lines changed

client/create_volume.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (c *Client) CreateVolume(ctx context.Context, name, driver string, labels,
2727
}
2828

2929
// requestForDriver returns a CreateVolumeRequest given the string representation of the driver name
30-
// and an arbitrary list of key-value entries representing options of the driver.
30+
// and an arbitrary list of key-value entries representing options of the driver.
3131
func requestForDriver(driver string, options map[string]string) (*cpb.CreateVolumeRequest, error) {
3232
req := &cpb.CreateVolumeRequest{}
3333
switch strings.ToLower(driver) {
@@ -55,7 +55,13 @@ func requestForDriver(driver string, options map[string]string) (*cpb.CreateVolu
5555
LocalMountOptions: localOpts,
5656
}
5757
default:
58-
return nil, fmt.Errorf("unknown driver: %q", driver)
58+
// This is a custom driver, so we need to pass the options through.
59+
req.Driver = cpb.Driver_DS_CUSTOM
60+
req.Options = &cpb.CreateVolumeRequest_CustomOptions{
61+
CustomOptions: &cpb.CustomOptions{
62+
Options: options,
63+
},
64+
}
5965
}
6066
return req, nil
6167
}

client/create_volume_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/google/go-cmp/cmp"
2323
"google.golang.org/protobuf/testing/protocmp"
24+
2425
cpb "github.com/openconfig/gnoi/containerz"
2526
)
2627

@@ -61,11 +62,23 @@ func TestCreateVolume(t *testing.T) {
6162
wantName: "simple",
6263
},
6364
{
64-
name: "invalid-driver",
65-
inName: "simple",
66-
inDriver: "garbage",
67-
inMsg: &cpb.CreateVolumeResponse{Name: "simple"},
68-
wantErr: fmt.Errorf("unknown driver: %q", "garbage"),
65+
name: "custom-driver",
66+
inName: "simple",
67+
inDriver: "custom-driver",
68+
inOptions: map[string]string{"foo": "bar"},
69+
inLabels: map[string]string{"label1": "value1", "label2": "value2"},
70+
inMsg: &cpb.CreateVolumeResponse{Name: "simple"},
71+
wantRequest: &cpb.CreateVolumeRequest{
72+
Name: "simple",
73+
Driver: cpb.Driver_DS_CUSTOM,
74+
Labels: map[string]string{"label1": "value1", "label2": "value2"},
75+
Options: &cpb.CreateVolumeRequest_CustomOptions{
76+
CustomOptions: &cpb.CustomOptions{
77+
Options: map[string]string{"foo": "bar"},
78+
},
79+
},
80+
},
81+
wantName: "simple",
6982
},
7083
{
7184
name: "good-driver-wrong-option",

cmd/container.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
package cmd
1616

1717
import (
18-
"github.com/spf13/cobra"
1918
"github.com/openconfig/containerz/client"
19+
"github.com/spf13/cobra"
2020
)
2121

2222
var containerCmd = &cobra.Command{
2323
Use: "container",
2424
Short: "General container operations",
2525
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
26-
RootCmd.PersistentPreRun(cmd, args)
2726
var err error
2827
containerzClient, err = client.NewClient(cmd.Context(), addr)
2928
return err

cmd/image.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
package cmd
1616

1717
import (
18-
"github.com/spf13/cobra"
1918
"github.com/openconfig/containerz/client"
19+
"github.com/spf13/cobra"
20+
2021
cpb "github.com/openconfig/gnoi/containerz"
2122
)
2223

@@ -30,7 +31,6 @@ var imageCmd = &cobra.Command{
3031
Use: "image",
3132
Short: "General image operations",
3233
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
33-
RootCmd.PersistentPreRun(cmd, args)
3434
var err error
3535
containerzClient, err = client.NewClient(cmd.Context(), addr)
3636
return err

cmd/plugin.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
package cmd
1616

1717
import (
18-
"github.com/spf13/cobra"
1918
"github.com/openconfig/containerz/client"
19+
"github.com/spf13/cobra"
2020
)
2121

2222
var pluginCmd = &cobra.Command{
2323
Use: "plugin",
2424
Short: "General plugin operations",
2525
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
26-
RootCmd.PersistentPreRun(cmd, args)
2726
var err error
2827
containerzClient, err = client.NewClient(cmd.Context(), addr)
2928
return err

cmd/root.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package cmd
1717

1818
import (
19-
2019
"github.com/spf13/cobra"
2120
)
2221

@@ -28,8 +27,6 @@ var (
2827
var RootCmd = &cobra.Command{
2928
Use: "containerz",
3029
Short: "Containerz suite of CLI tools",
31-
PersistentPreRun: func(cmd *cobra.Command, args []string) {
32-
},
3330
Run: func(command *cobra.Command, args []string) {
3431
command.HelpFunc()(command, args)
3532
},

cmd/volume.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
package cmd
1616

1717
import (
18-
"github.com/spf13/cobra"
1918
"github.com/openconfig/containerz/client"
19+
"github.com/spf13/cobra"
2020
)
2121

2222
var volumesCmd = &cobra.Command{
2323
Use: "volume",
2424
Short: "General volume operations",
2525
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
26-
RootCmd.PersistentPreRun(cmd, args)
2726
var err error
2827
containerzClient, err = client.NewClient(cmd.Context(), addr)
2928
return err

containers/docker/volume_create.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"strings"
66

77
"github.com/docker/docker/api/types/volume"
8+
"github.com/openconfig/containerz/containers"
89
"google.golang.org/grpc/codes"
910
"google.golang.org/grpc/status"
10-
"github.com/openconfig/containerz/containers"
1111

1212
cpb "github.com/openconfig/gnoi/containerz"
1313
)
@@ -35,6 +35,15 @@ func (m *Manager) VolumeCreate(ctx context.Context, name string, driver cpb.Driv
3535
volOpts["o"] = strings.Join(vopts.GetOptions(), ",")
3636
volOpts["device"] = vopts.GetMountpoint()
3737
}
38+
case cpb.Driver_DS_CUSTOM:
39+
kind = "custom:latest"
40+
if optionz.VolumeDriverOptions != nil {
41+
vopts, ok := optionz.VolumeDriverOptions.(*cpb.CustomOptions)
42+
if !ok {
43+
return "", status.Error(codes.InvalidArgument, "driver is marked as custom but options are not CustomOptions")
44+
}
45+
volOpts = vopts.GetOptions()
46+
}
3847
}
3948

4049
create := volume.CreateOptions{

containers/docker/volume_create_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"context"
55
"testing"
66

7+
"github.com/docker/docker/api/types/volume"
78
"github.com/google/go-cmp/cmp"
89
"github.com/google/go-cmp/cmp/cmpopts"
9-
"github.com/docker/docker/api/types/volume"
1010
"github.com/openconfig/containerz/containers"
1111
cpb "github.com/openconfig/gnoi/containerz"
1212
)
@@ -89,6 +89,28 @@ func TestVolumeCreate(t *testing.T) {
8989
},
9090
},
9191
},
92+
{
93+
name: "named-volume-with-opts-and-custom-driver",
94+
inName: "some-volume",
95+
wantResp: "some-volume",
96+
inDriver: cpb.Driver_DS_CUSTOM,
97+
inOpts: []options.Option{
98+
options.WithVolumeDriverOpts(&cpb.CustomOptions{
99+
Options: map[string]string{"some-option": "some-value"},
100+
}),
101+
options.WithVolumeLabels(map[string]string{"some-label": "some-label"}),
102+
},
103+
wantState: &fakeVolumeCreatingDocker{
104+
V: volume.Volume{
105+
Name: "some-volume",
106+
Driver: "custom:latest",
107+
Options: map[string]string{
108+
"some-option": "some-value",
109+
},
110+
Labels: map[string]string{"some-label": "some-label"},
111+
},
112+
},
113+
},
92114
}
93115

94116
for _, tc := range tests {

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ require (
1010
github.com/docker/go-connections v0.5.0
1111
github.com/google/go-cmp v0.6.0
1212
github.com/moby/moby v27.5.0+incompatible
13-
github.com/openconfig/gnoi v0.6.0
13+
github.com/openconfig/gnoi v0.6.1-0.20250206212518-26d177339690
1414
github.com/opencontainers/image-spec v1.1.0
1515
github.com/spf13/cobra v1.8.1
1616
golang.org/x/sys v0.29.0
17-
google.golang.org/grpc v1.69.4
17+
google.golang.org/grpc v1.70.0
1818
google.golang.org/protobuf v1.36.3
1919
k8s.io/klog/v2 v2.130.1
2020
)

0 commit comments

Comments
 (0)