From fc7a2652588ed30e3b3a10ab72329e82f0440236 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Mon, 7 Nov 2016 17:15:31 -0800 Subject: [PATCH] support mounts, devices and args for docker plugin set Signed-off-by: Victor Vieux --- plugin/v2/plugin.go | 52 ++++++++++++++++++++++++++++++++++++-- plugin/v2/settable.go | 2 +- plugin/v2/settable_test.go | 4 +-- 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/plugin/v2/plugin.go b/plugin/v2/plugin.go index 33c53fb105..b48e56b8ac 100644 --- a/plugin/v2/plugin.go +++ b/plugin/v2/plugin.go @@ -137,6 +137,8 @@ func (p *Plugin) Set(args []string) error { return err } + // TODO(vieux): lots of code duplication here, needs to be refactored. + next: for _, s := range sets { // range over all the envs in the config @@ -150,12 +152,58 @@ next: return fmt.Errorf("%q is not settable", s.prettyName()) } // is it, so lets update the settings in memory - updateConfigEnv(&p.PluginObj.Settings.Env, &s) + updateSettingsEnv(&p.PluginObj.Settings.Env, &s) continue next } } - //TODO: check devices, mount and args + // range over all the mounts in the config + for _, mount := range p.PluginObj.Config.Mounts { + // found the mount in the config + if mount.Name == s.name { + // is it settable ? + if ok, err := s.isSettable(allowedSettableFieldsMounts, mount.Settable); err != nil { + return err + } else if !ok { + return fmt.Errorf("%q is not settable", s.prettyName()) + } + + // it is, so lets update the settings in memory + *mount.Source = s.value + continue next + } + } + + // range over all the devices in the config + for _, device := range p.PluginObj.Config.Devices { + // found the device in the config + if device.Name == s.name { + // is it settable ? + if ok, err := s.isSettable(allowedSettableFieldsDevices, device.Settable); err != nil { + return err + } else if !ok { + return fmt.Errorf("%q is not settable", s.prettyName()) + } + + // it is, so lets update the settings in memory + *device.Path = s.value + continue next + } + } + + // found the name in the config + if p.PluginObj.Config.Args.Name == s.name { + // is it settable ? + if ok, err := s.isSettable(allowedSettableFieldsArgs, p.PluginObj.Config.Args.Settable); err != nil { + return err + } else if !ok { + return fmt.Errorf("%q is not settable", s.prettyName()) + } + + // it is, so lets update the settings in memory + p.PluginObj.Settings.Args = strings.Split(s.value, " ") + continue next + } return fmt.Errorf("setting %q not found in the plugin configuration", s.name) } diff --git a/plugin/v2/settable.go b/plugin/v2/settable.go index dc0f56a844..79c6befc24 100644 --- a/plugin/v2/settable.go +++ b/plugin/v2/settable.go @@ -90,7 +90,7 @@ func (set *settable) isSettable(allowedSettableFields []string, settable []strin return false, nil } -func updateConfigEnv(env *[]string, set *settable) { +func updateSettingsEnv(env *[]string, set *settable) { for i, e := range *env { if parts := strings.SplitN(e, "=", 2); parts[0] == set.name { (*env)[i] = fmt.Sprintf("%s=%s", set.name, set.value) diff --git a/plugin/v2/settable_test.go b/plugin/v2/settable_test.go index 57260bf7a3..7183f3a679 100644 --- a/plugin/v2/settable_test.go +++ b/plugin/v2/settable_test.go @@ -68,7 +68,7 @@ func TestIsSettable(t *testing.T) { } } -func TestUpdateConfigEnv(t *testing.T) { +func TestUpdateSettinsEnv(t *testing.T) { contexts := []struct { env []string set settable @@ -82,7 +82,7 @@ func TestUpdateConfigEnv(t *testing.T) { } for _, c := range contexts { - updateConfigEnv(&c.env, &c.set) + updateSettingsEnv(&c.env, &c.set) if !reflect.DeepEqual(c.env, c.newEnv) { t.Fatalf("expected env to be %q, got %q", c.newEnv, c.env)