Check for plugin state before enable and disable.
This prevents unnecessary API call to containerd. Signed-off-by: Anusha Ragunathan <anusha@docker.com>
This commit is contained in:
parent
8f11896a45
commit
b867f6c6e1
|
@ -68,3 +68,24 @@ func (s *DockerSuite) TestPluginInstallImage(c *check.C) {
|
||||||
c.Assert(err, checker.NotNil)
|
c.Assert(err, checker.NotNil)
|
||||||
c.Assert(out, checker.Contains, "content is not a plugin")
|
c.Assert(out, checker.Contains, "content is not a plugin")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *DockerSuite) TestPluginEnableDisableNegative(c *check.C) {
|
||||||
|
testRequires(c, DaemonIsLinux, ExperimentalDaemon)
|
||||||
|
out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pName)
|
||||||
|
c.Assert(err, checker.IsNil)
|
||||||
|
c.Assert(strings.TrimSpace(out), checker.Contains, pName)
|
||||||
|
|
||||||
|
out, _, err = dockerCmdWithError("plugin", "enable", pName)
|
||||||
|
c.Assert(err, checker.NotNil)
|
||||||
|
c.Assert(strings.TrimSpace(out), checker.Contains, "already enabled")
|
||||||
|
|
||||||
|
_, _, err = dockerCmdWithError("plugin", "disable", pName)
|
||||||
|
c.Assert(err, checker.IsNil)
|
||||||
|
|
||||||
|
out, _, err = dockerCmdWithError("plugin", "disable", pName)
|
||||||
|
c.Assert(err, checker.NotNil)
|
||||||
|
c.Assert(strings.TrimSpace(out), checker.Contains, "already disabled")
|
||||||
|
|
||||||
|
_, _, err = dockerCmdWithError("plugin", "remove", pName)
|
||||||
|
c.Assert(err, checker.IsNil)
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
package plugin
|
package plugin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
@ -20,6 +21,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (pm *Manager) enable(p *plugin) error {
|
func (pm *Manager) enable(p *plugin) error {
|
||||||
|
if p.P.Active {
|
||||||
|
return fmt.Errorf("plugin %s is already enabled", p.Name())
|
||||||
|
}
|
||||||
spec, err := pm.initSpec(p)
|
spec, err := pm.initSpec(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -27,12 +31,18 @@ func (pm *Manager) enable(p *plugin) error {
|
||||||
|
|
||||||
p.restartManager = restartmanager.New(container.RestartPolicy{Name: "always"}, 0)
|
p.restartManager = restartmanager.New(container.RestartPolicy{Name: "always"}, 0)
|
||||||
if err := pm.containerdClient.Create(p.P.ID, libcontainerd.Spec(*spec), libcontainerd.WithRestartManager(p.restartManager)); err != nil { // POC-only
|
if err := pm.containerdClient.Create(p.P.ID, libcontainerd.Spec(*spec), libcontainerd.WithRestartManager(p.restartManager)); err != nil { // POC-only
|
||||||
|
if err := p.restartManager.Cancel(); err != nil {
|
||||||
|
logrus.Errorf("enable: restartManager.Cancel failed due to %v", err)
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
socket := p.P.Manifest.Interface.Socket
|
socket := p.P.Manifest.Interface.Socket
|
||||||
p.client, err = plugins.NewClient("unix://"+filepath.Join(p.runtimeSourcePath, socket), nil)
|
p.client, err = plugins.NewClient("unix://"+filepath.Join(p.runtimeSourcePath, socket), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if err := p.restartManager.Cancel(); err != nil {
|
||||||
|
logrus.Errorf("enable: restartManager.Cancel failed due to %v", err)
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,6 +124,9 @@ func (pm *Manager) initSpec(p *plugin) (*specs.Spec, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pm *Manager) disable(p *plugin) error {
|
func (pm *Manager) disable(p *plugin) error {
|
||||||
|
if !p.P.Active {
|
||||||
|
return fmt.Errorf("plugin %s is already disabled", p.Name())
|
||||||
|
}
|
||||||
if err := p.restartManager.Cancel(); err != nil {
|
if err := p.restartManager.Cancel(); err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue