Skip to content

Commit

Permalink
Make daemon events listen for plugin lifecycle events.
Browse files Browse the repository at this point in the history
Signed-off-by: Anusha Ragunathan <anusha@docker.com>
  • Loading branch information
anusha-ragunathan committed Jul 26, 2016
1 parent 4e3d6e3 commit 42abccb
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 105 deletions.
4 changes: 0 additions & 4 deletions cmd/dockerd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,6 @@ func (cli *DaemonCli) start() (err error) {
<-stopc // wait for daemonCli.start() to return
})

if err := pluginInit(cli.Config, containerdRemote, registryService); err != nil {
return err
}

d, err := daemon.NewDaemon(cli.Config, registryService, containerdRemote)
if err != nil {
return fmt.Errorf("Error starting daemon: %v", err)
Expand Down
13 changes: 0 additions & 13 deletions cmd/dockerd/daemon_no_plugin_support.go

This file was deleted.

14 changes: 0 additions & 14 deletions cmd/dockerd/daemon_plugin_support.go

This file was deleted.

4 changes: 4 additions & 0 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,10 @@ func NewDaemon(config *Config, registryService registry.Service, containerdRemot
return nil, err
}

if err := pluginInit(d, config, containerdRemote); err != nil {
return nil, err
}

return d, nil
}

Expand Down
12 changes: 11 additions & 1 deletion daemon/daemon_experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package daemon

import (
"github.com/docker/docker/libcontainerd"
"github.com/docker/docker/plugin"
"github.com/docker/engine-api/types/container"
)
Expand All @@ -11,6 +12,15 @@ func (daemon *Daemon) verifyExperimentalContainerSettings(hostConfig *container.
return nil, nil
}

func pluginInit(d *Daemon, cfg *Config, remote libcontainerd.Remote) error {
return plugin.Init(cfg.Root, remote, d.RegistryService, cfg.LiveRestore, d.LogPluginEvent)
}

func pluginShutdown() {
plugin.GetManager().Shutdown()
manager := plugin.GetManager()
// Check for a valid manager object. In error conditions, daemon init can fail
// and shutdown called, before plugin manager is initialized.
if manager != nil {
manager.Shutdown()
}
}
9 changes: 8 additions & 1 deletion daemon/daemon_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

package daemon

import "github.com/docker/engine-api/types/container"
import (
"github.com/docker/docker/libcontainerd"
"github.com/docker/engine-api/types/container"
)

func (daemon *Daemon) verifyExperimentalContainerSettings(hostConfig *container.HostConfig, config *container.Config) ([]string, error) {
return nil, nil
}

func pluginInit(d *Daemon, config *Config, remote libcontainerd.Remote) error {
return nil
}

func pluginShutdown() {
}
15 changes: 15 additions & 0 deletions daemon/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ func (daemon *Daemon) LogImageEventWithAttributes(imageID, refName, action strin
daemon.EventsService.Log(action, events.ImageEventType, actor)
}

// LogPluginEvent generates an event related to a plugin with only the default attributes.
func (daemon *Daemon) LogPluginEvent(pluginID, refName, action string) {
daemon.LogPluginEventWithAttributes(pluginID, refName, action, map[string]string{})
}

// LogPluginEventWithAttributes generates an event related to a plugin with specific given attributes.
func (daemon *Daemon) LogPluginEventWithAttributes(pluginID, refName, action string, attributes map[string]string) {
attributes["name"] = refName
actor := events.Actor{
ID: pluginID,
Attributes: attributes,
}
daemon.EventsService.Log(action, events.PluginEventType, actor)
}

// LogVolumeEvent generates an event related to a volume.
func (daemon *Daemon) LogVolumeEvent(volumeID, action string, attributes map[string]string) {
actor := events.Actor{
Expand Down
5 changes: 5 additions & 0 deletions daemon/events/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (ef *Filter) Include(ev events.Message) bool {
ef.filter.ExactMatch("type", ev.Type) &&
ef.matchDaemon(ev) &&
ef.matchContainer(ev) &&
ef.matchPlugin(ev) &&
ef.matchVolume(ev) &&
ef.matchNetwork(ev) &&
ef.matchImage(ev) &&
Expand All @@ -43,6 +44,10 @@ func (ef *Filter) matchContainer(ev events.Message) bool {
return ef.fuzzyMatchName(ev, events.ContainerEventType)
}

func (ef *Filter) matchPlugin(ev events.Message) bool {
return ef.fuzzyMatchName(ev, events.PluginEventType)
}

func (ef *Filter) matchVolume(ev events.Message) bool {
return ef.fuzzyMatchName(ev, events.VolumeEventType)
}
Expand Down
9 changes: 9 additions & 0 deletions docs/reference/commandline/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Docker images report the following events:

delete, import, load, pull, push, save, tag, untag

Docker plugins(experimental) report the following events:

install, enable, disable, remove

Docker volumes report the following events:

create, mount, unmount, destroy
Expand Down Expand Up @@ -74,6 +78,7 @@ The currently supported filters are:
* container (`container=<name or id>`)
* event (`event=<event action>`)
* image (`image=<tag or id>`)
* plugin (experimental) (`plugin=<name or id>`)
* label (`label=<key>` or `label=<key>=<value>`)
* type (`type=<container or image or volume or network or daemon>`)
* volume (`volume=<name or id>`)
Expand Down Expand Up @@ -171,3 +176,7 @@ relative to the current time on the client machine:
$ docker events --filter 'type=network'
2015-12-23T21:38:24.705709133Z network create 8b111217944ba0ba844a65b13efcd57dc494932ee2527577758f939315ba2c5b (name=test-event-network-local, type=bridge)
2015-12-23T21:38:25.119625123Z network connect 8b111217944ba0ba844a65b13efcd57dc494932ee2527577758f939315ba2c5b (name=test-event-network-local, container=b4be644031a3d90b400f88ab3d4bdf4dc23adb250e696b6328b85441abe2c54e, type=bridge)

$ docker events --filter 'type=plugin' (experimental)
2016-07-25T17:30:14.825557616Z plugin pull ec7b87f2ce84330fe076e666f17dfc049d2d7ae0b8190763de94e1f2d105993f (name=tiborvass/no-remove:latest)
2016-07-25T17:30:14.888127370Z plugin enable ec7b87f2ce84330fe076e666f17dfc049d2d7ae0b8190763de94e1f2d105993f (name=tiborvass/no-remove:latest)
26 changes: 26 additions & 0 deletions integration-cli/docker_cli_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,32 @@ func (s *DockerSuite) TestEventsImageLoad(c *check.C) {
c.Assert(matches["action"], checker.Equals, "save", check.Commentf("matches: %v\nout:\n%s\n", matches, out))
}

func (s *DockerSuite) TestEventsPluginOps(c *check.C) {
testRequires(c, DaemonIsLinux, ExperimentalDaemon)

pluginName := "tiborvass/no-remove:latest"
since := daemonUnixTime(c)

dockerCmd(c, "plugin", "install", pluginName, "--grant-all-permissions")
dockerCmd(c, "plugin", "disable", pluginName)
dockerCmd(c, "plugin", "remove", pluginName)

out, _ := dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c))
events := strings.Split(out, "\n")
events = events[:len(events)-1]

nEvents := len(events)
c.Assert(nEvents, checker.GreaterOrEqualThan, 4)

pluginEvents := eventActionsByIDAndType(c, events, pluginName, "plugin")
c.Assert(pluginEvents, checker.HasLen, 4, check.Commentf("events: %v", events))

c.Assert(pluginEvents[0], checker.Equals, "pull", check.Commentf(out))
c.Assert(pluginEvents[1], checker.Equals, "enable", check.Commentf(out))
c.Assert(pluginEvents[2], checker.Equals, "disable", check.Commentf(out))
c.Assert(pluginEvents[3], checker.Equals, "remove", check.Commentf(out))
}

func (s *DockerSuite) TestEventsFilters(c *check.C) {
since := daemonUnixTime(c)
dockerCmd(c, "run", "--rm", "busybox", "true")
Expand Down
29 changes: 21 additions & 8 deletions plugin/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ func (pm *Manager) Disable(name string) error {
if err != nil {
return err
}
return pm.disable(p)
if err := pm.disable(p); err != nil {
return err
}
pm.pluginEventLogger(p.PluginObj.ID, name, "disable")
return nil
}

// Enable activates a plugin, which implies that they are ready to be used by containers.
Expand All @@ -31,7 +35,11 @@ func (pm *Manager) Enable(name string) error {
if err != nil {
return err
}
return pm.enable(p)
if err := pm.enable(p); err != nil {
return err
}
pm.pluginEventLogger(p.PluginObj.ID, name, "enable")
return nil
}

// Inspect examines a plugin manifest
Expand All @@ -40,10 +48,10 @@ func (pm *Manager) Inspect(name string) (tp types.Plugin, err error) {
if err != nil {
return tp, err
}
return p.P, nil
return p.PluginObj, nil
}

// Pull pulls a plugin and enables it.
// Pull pulls a plugin and computes the privileges required to install it.
func (pm *Manager) Pull(name string, metaHeader http.Header, authConfig *types.AuthConfig) (types.PluginPrivileges, error) {
ref, err := reference.ParseNamed(name)
if err != nil {
Expand Down Expand Up @@ -86,14 +94,15 @@ func (pm *Manager) Pull(name string, metaHeader http.Header, authConfig *types.A
pm.save()
pm.Unlock()

return computePrivileges(&p.P.Manifest), nil
pm.pluginEventLogger(pluginID, name, "pull")
return computePrivileges(&p.PluginObj.Manifest), nil
}

// List displays the list of plugins and associated metadata.
func (pm *Manager) List() ([]types.Plugin, error) {
out := make([]types.Plugin, 0, len(pm.plugins))
for _, p := range pm.plugins {
out = append(out, p.P)
out = append(out, p.PluginObj)
}
return out, nil
}
Expand All @@ -104,7 +113,7 @@ func (pm *Manager) Push(name string, metaHeader http.Header, authConfig *types.A
if err != nil {
return err
}
dest := filepath.Join(pm.libRoot, p.P.ID)
dest := filepath.Join(pm.libRoot, p.PluginObj.ID)
config, err := os.Open(filepath.Join(dest, "manifest.json"))
if err != nil {
return err
Expand All @@ -127,7 +136,11 @@ func (pm *Manager) Remove(name string) error {
if err != nil {
return err
}
return pm.remove(p)
if err := pm.remove(p); err != nil {
return err
}
pm.pluginEventLogger(p.PluginObj.ID, name, "remove")
return nil
}

// Set sets plugin args
Expand Down
Loading

0 comments on commit 42abccb

Please sign in to comment.