Skip to content

Commit 42abccb

Browse files
Make daemon events listen for plugin lifecycle events.
Signed-off-by: Anusha Ragunathan <[email protected]>
1 parent 4e3d6e3 commit 42abccb

File tree

13 files changed

+166
-105
lines changed

13 files changed

+166
-105
lines changed

cmd/dockerd/daemon.go

-4
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,6 @@ func (cli *DaemonCli) start() (err error) {
262262
<-stopc // wait for daemonCli.start() to return
263263
})
264264

265-
if err := pluginInit(cli.Config, containerdRemote, registryService); err != nil {
266-
return err
267-
}
268-
269265
d, err := daemon.NewDaemon(cli.Config, registryService, containerdRemote)
270266
if err != nil {
271267
return fmt.Errorf("Error starting daemon: %v", err)

cmd/dockerd/daemon_no_plugin_support.go

-13
This file was deleted.

cmd/dockerd/daemon_plugin_support.go

-14
This file was deleted.

daemon/daemon.go

+4
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,10 @@ func NewDaemon(config *Config, registryService registry.Service, containerdRemot
604604
return nil, err
605605
}
606606

607+
if err := pluginInit(d, config, containerdRemote); err != nil {
608+
return nil, err
609+
}
610+
607611
return d, nil
608612
}
609613

daemon/daemon_experimental.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package daemon
44

55
import (
6+
"github.com/docker/docker/libcontainerd"
67
"github.com/docker/docker/plugin"
78
"github.com/docker/engine-api/types/container"
89
)
@@ -11,6 +12,15 @@ func (daemon *Daemon) verifyExperimentalContainerSettings(hostConfig *container.
1112
return nil, nil
1213
}
1314

15+
func pluginInit(d *Daemon, cfg *Config, remote libcontainerd.Remote) error {
16+
return plugin.Init(cfg.Root, remote, d.RegistryService, cfg.LiveRestore, d.LogPluginEvent)
17+
}
18+
1419
func pluginShutdown() {
15-
plugin.GetManager().Shutdown()
20+
manager := plugin.GetManager()
21+
// Check for a valid manager object. In error conditions, daemon init can fail
22+
// and shutdown called, before plugin manager is initialized.
23+
if manager != nil {
24+
manager.Shutdown()
25+
}
1626
}

daemon/daemon_stub.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
package daemon
44

5-
import "github.com/docker/engine-api/types/container"
5+
import (
6+
"github.com/docker/docker/libcontainerd"
7+
"github.com/docker/engine-api/types/container"
8+
)
69

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

14+
func pluginInit(d *Daemon, config *Config, remote libcontainerd.Remote) error {
15+
return nil
16+
}
17+
1118
func pluginShutdown() {
1219
}

daemon/events.go

+15
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ func (daemon *Daemon) LogImageEventWithAttributes(imageID, refName, action strin
5555
daemon.EventsService.Log(action, events.ImageEventType, actor)
5656
}
5757

58+
// LogPluginEvent generates an event related to a plugin with only the default attributes.
59+
func (daemon *Daemon) LogPluginEvent(pluginID, refName, action string) {
60+
daemon.LogPluginEventWithAttributes(pluginID, refName, action, map[string]string{})
61+
}
62+
63+
// LogPluginEventWithAttributes generates an event related to a plugin with specific given attributes.
64+
func (daemon *Daemon) LogPluginEventWithAttributes(pluginID, refName, action string, attributes map[string]string) {
65+
attributes["name"] = refName
66+
actor := events.Actor{
67+
ID: pluginID,
68+
Attributes: attributes,
69+
}
70+
daemon.EventsService.Log(action, events.PluginEventType, actor)
71+
}
72+
5873
// LogVolumeEvent generates an event related to a volume.
5974
func (daemon *Daemon) LogVolumeEvent(volumeID, action string, attributes map[string]string) {
6075
actor := events.Actor{

daemon/events/filter.go

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func (ef *Filter) Include(ev events.Message) bool {
2222
ef.filter.ExactMatch("type", ev.Type) &&
2323
ef.matchDaemon(ev) &&
2424
ef.matchContainer(ev) &&
25+
ef.matchPlugin(ev) &&
2526
ef.matchVolume(ev) &&
2627
ef.matchNetwork(ev) &&
2728
ef.matchImage(ev) &&
@@ -43,6 +44,10 @@ func (ef *Filter) matchContainer(ev events.Message) bool {
4344
return ef.fuzzyMatchName(ev, events.ContainerEventType)
4445
}
4546

47+
func (ef *Filter) matchPlugin(ev events.Message) bool {
48+
return ef.fuzzyMatchName(ev, events.PluginEventType)
49+
}
50+
4651
func (ef *Filter) matchVolume(ev events.Message) bool {
4752
return ef.fuzzyMatchName(ev, events.VolumeEventType)
4853
}

docs/reference/commandline/events.md

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Docker images report the following events:
3030

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

33+
Docker plugins(experimental) report the following events:
34+
35+
install, enable, disable, remove
36+
3337
Docker volumes report the following events:
3438

3539
create, mount, unmount, destroy
@@ -74,6 +78,7 @@ The currently supported filters are:
7478
* container (`container=<name or id>`)
7579
* event (`event=<event action>`)
7680
* image (`image=<tag or id>`)
81+
* plugin (experimental) (`plugin=<name or id>`)
7782
* label (`label=<key>` or `label=<key>=<value>`)
7883
* type (`type=<container or image or volume or network or daemon>`)
7984
* volume (`volume=<name or id>`)
@@ -171,3 +176,7 @@ relative to the current time on the client machine:
171176
$ docker events --filter 'type=network'
172177
2015-12-23T21:38:24.705709133Z network create 8b111217944ba0ba844a65b13efcd57dc494932ee2527577758f939315ba2c5b (name=test-event-network-local, type=bridge)
173178
2015-12-23T21:38:25.119625123Z network connect 8b111217944ba0ba844a65b13efcd57dc494932ee2527577758f939315ba2c5b (name=test-event-network-local, container=b4be644031a3d90b400f88ab3d4bdf4dc23adb250e696b6328b85441abe2c54e, type=bridge)
179+
180+
$ docker events --filter 'type=plugin' (experimental)
181+
2016-07-25T17:30:14.825557616Z plugin pull ec7b87f2ce84330fe076e666f17dfc049d2d7ae0b8190763de94e1f2d105993f (name=tiborvass/no-remove:latest)
182+
2016-07-25T17:30:14.888127370Z plugin enable ec7b87f2ce84330fe076e666f17dfc049d2d7ae0b8190763de94e1f2d105993f (name=tiborvass/no-remove:latest)

integration-cli/docker_cli_events_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,32 @@ func (s *DockerSuite) TestEventsImageLoad(c *check.C) {
297297
c.Assert(matches["action"], checker.Equals, "save", check.Commentf("matches: %v\nout:\n%s\n", matches, out))
298298
}
299299

300+
func (s *DockerSuite) TestEventsPluginOps(c *check.C) {
301+
testRequires(c, DaemonIsLinux, ExperimentalDaemon)
302+
303+
pluginName := "tiborvass/no-remove:latest"
304+
since := daemonUnixTime(c)
305+
306+
dockerCmd(c, "plugin", "install", pluginName, "--grant-all-permissions")
307+
dockerCmd(c, "plugin", "disable", pluginName)
308+
dockerCmd(c, "plugin", "remove", pluginName)
309+
310+
out, _ := dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c))
311+
events := strings.Split(out, "\n")
312+
events = events[:len(events)-1]
313+
314+
nEvents := len(events)
315+
c.Assert(nEvents, checker.GreaterOrEqualThan, 4)
316+
317+
pluginEvents := eventActionsByIDAndType(c, events, pluginName, "plugin")
318+
c.Assert(pluginEvents, checker.HasLen, 4, check.Commentf("events: %v", events))
319+
320+
c.Assert(pluginEvents[0], checker.Equals, "pull", check.Commentf(out))
321+
c.Assert(pluginEvents[1], checker.Equals, "enable", check.Commentf(out))
322+
c.Assert(pluginEvents[2], checker.Equals, "disable", check.Commentf(out))
323+
c.Assert(pluginEvents[3], checker.Equals, "remove", check.Commentf(out))
324+
}
325+
300326
func (s *DockerSuite) TestEventsFilters(c *check.C) {
301327
since := daemonUnixTime(c)
302328
dockerCmd(c, "run", "--rm", "busybox", "true")

plugin/backend.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ func (pm *Manager) Disable(name string) error {
2222
if err != nil {
2323
return err
2424
}
25-
return pm.disable(p)
25+
if err := pm.disable(p); err != nil {
26+
return err
27+
}
28+
pm.pluginEventLogger(p.PluginObj.ID, name, "disable")
29+
return nil
2630
}
2731

2832
// Enable activates a plugin, which implies that they are ready to be used by containers.
@@ -31,7 +35,11 @@ func (pm *Manager) Enable(name string) error {
3135
if err != nil {
3236
return err
3337
}
34-
return pm.enable(p)
38+
if err := pm.enable(p); err != nil {
39+
return err
40+
}
41+
pm.pluginEventLogger(p.PluginObj.ID, name, "enable")
42+
return nil
3543
}
3644

3745
// Inspect examines a plugin manifest
@@ -40,10 +48,10 @@ func (pm *Manager) Inspect(name string) (tp types.Plugin, err error) {
4048
if err != nil {
4149
return tp, err
4250
}
43-
return p.P, nil
51+
return p.PluginObj, nil
4452
}
4553

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

89-
return computePrivileges(&p.P.Manifest), nil
97+
pm.pluginEventLogger(pluginID, name, "pull")
98+
return computePrivileges(&p.PluginObj.Manifest), nil
9099
}
91100

92101
// List displays the list of plugins and associated metadata.
93102
func (pm *Manager) List() ([]types.Plugin, error) {
94103
out := make([]types.Plugin, 0, len(pm.plugins))
95104
for _, p := range pm.plugins {
96-
out = append(out, p.P)
105+
out = append(out, p.PluginObj)
97106
}
98107
return out, nil
99108
}
@@ -104,7 +113,7 @@ func (pm *Manager) Push(name string, metaHeader http.Header, authConfig *types.A
104113
if err != nil {
105114
return err
106115
}
107-
dest := filepath.Join(pm.libRoot, p.P.ID)
116+
dest := filepath.Join(pm.libRoot, p.PluginObj.ID)
108117
config, err := os.Open(filepath.Join(dest, "manifest.json"))
109118
if err != nil {
110119
return err
@@ -127,7 +136,11 @@ func (pm *Manager) Remove(name string) error {
127136
if err != nil {
128137
return err
129138
}
130-
return pm.remove(p)
139+
if err := pm.remove(p); err != nil {
140+
return err
141+
}
142+
pm.pluginEventLogger(p.PluginObj.ID, name, "remove")
143+
return nil
131144
}
132145

133146
// Set sets plugin args

0 commit comments

Comments
 (0)