Skip to content

Commit

Permalink
Add load/save image event support
Browse files Browse the repository at this point in the history
For every docker load and save operations, it would log related
image events.

Signed-off-by: Kai Qiang Wu(Kennan) <wkqwu@cn.ibm.com>
  • Loading branch information
Kai Qiang Wu(Kennan) committed Apr 27, 2016
1 parent 0147164 commit 0656105
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 14 deletions.
4 changes: 2 additions & 2 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ func isBrokenPipe(e error) bool {
// the same tag are exported. names is the set of tags to export, and
// outStream is the writer which the images are written to.
func (daemon *Daemon) ExportImage(names []string, outStream io.Writer) error {
imageExporter := tarexport.NewTarExporter(daemon.imageStore, daemon.layerStore, daemon.referenceStore)
imageExporter := tarexport.NewTarExporter(daemon.imageStore, daemon.layerStore, daemon.referenceStore, daemon)
return imageExporter.Save(names, outStream)
}

Expand Down Expand Up @@ -1059,7 +1059,7 @@ func (daemon *Daemon) LookupImage(name string) (*types.ImageInspect, error) {
// complement of ImageExport. The input stream is an uncompressed tar
// ball containing images and metadata.
func (daemon *Daemon) LoadImage(inTar io.ReadCloser, outStream io.Writer, quiet bool) error {
imageExporter := tarexport.NewTarExporter(daemon.imageStore, daemon.layerStore, daemon.referenceStore)
imageExporter := tarexport.NewTarExporter(daemon.imageStore, daemon.layerStore, daemon.referenceStore, daemon)
return imageExporter.Load(inTar, outStream, quiet)
}

Expand Down
2 changes: 1 addition & 1 deletion docs/reference/api/docker_remote_api_v1.24.md
Original file line number Diff line number Diff line change
Expand Up @@ -2404,7 +2404,7 @@ Docker containers report the following events:

Docker images report the following events:

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

Docker volumes report the following events:

Expand Down
2 changes: 1 addition & 1 deletion docs/reference/commandline/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Docker containers report the following events:

Docker images report the following events:

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

Docker volumes report the following events:

Expand Down
1 change: 1 addition & 0 deletions image/tarexport/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func (l *tarexporter) Load(inTar io.ReadCloser, outStream io.Writer, quiet bool)
}

parentLinks = append(parentLinks, parentLink{imgID, m.Parent})
l.loggerImgEvent.LogImageEvent(imgID.String(), imgID.String(), "load")
}

for _, p := range validatedParentLinks(parentLinks) {
Expand Down
1 change: 1 addition & 0 deletions image/tarexport/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func (s *saveSession) save(outStream io.Writer) error {

parentID, _ := s.is.GetParent(id)
parentLinks = append(parentLinks, parentLink{id, parentID})
s.tarexporter.loggerImgEvent.LogImageEvent(id.String(), id.String(), "save")
}

for i, p := range validatedParentLinks(parentLinks) {
Expand Down
22 changes: 15 additions & 7 deletions image/tarexport/tarexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,24 @@ type manifestItem struct {
}

type tarexporter struct {
is image.Store
ls layer.Store
rs reference.Store
is image.Store
ls layer.Store
rs reference.Store
loggerImgEvent LogImageEvent
}

// LogImageEvent defines interface for event generation related to image tar(load and save) operations
type LogImageEvent interface {
//LogImageEvent generates an event related to an image operation
LogImageEvent(imageID, refName, action string)
}

// NewTarExporter returns new ImageExporter for tar packages
func NewTarExporter(is image.Store, ls layer.Store, rs reference.Store) image.Exporter {
func NewTarExporter(is image.Store, ls layer.Store, rs reference.Store, loggerImgEvent LogImageEvent) image.Exporter {
return &tarexporter{
is: is,
ls: ls,
rs: rs,
is: is,
ls: ls,
rs: rs,
loggerImgEvent: loggerImgEvent,
}
}
39 changes: 39 additions & 0 deletions integration-cli/docker_cli_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,45 @@ func (s *DockerSuite) TestEventsImageImport(c *check.C) {
c.Assert(matches["action"], checker.Equals, "import", check.Commentf("matches: %v\nout:\n%s\n", matches, out))
}

func (s *DockerSuite) TestEventsImageLoad(c *check.C) {
testRequires(c, DaemonIsLinux)
myImageName := "footest:v1"
dockerCmd(c, "tag", "busybox", myImageName)
since := daemonUnixTime(c)

out, _ := dockerCmd(c, "images", "-q", "--no-trunc", myImageName)
longImageID := strings.TrimSpace(out)
c.Assert(longImageID, checker.Not(check.Equals), "", check.Commentf("Id should not be empty"))

dockerCmd(c, "save", "-o", "saveimg.tar", myImageName)
dockerCmd(c, "rmi", myImageName)
out, _ = dockerCmd(c, "images", "-q", myImageName)
noImageID := strings.TrimSpace(out)
c.Assert(noImageID, checker.Equals, "", check.Commentf("Should not have any image"))
dockerCmd(c, "load", "-i", "saveimg.tar")

cmd := exec.Command("rm", "-rf", "saveimg.tar")
runCommand(cmd)

out, _ = dockerCmd(c, "images", "-q", "--no-trunc", myImageName)
imageID := strings.TrimSpace(out)
c.Assert(imageID, checker.Equals, longImageID, check.Commentf("Should have same image id as before"))

out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", "event=load")
events := strings.Split(strings.TrimSpace(out), "\n")
c.Assert(events, checker.HasLen, 1)
matches := eventstestutils.ScanMap(events[0])
c.Assert(matches["id"], checker.Equals, imageID, check.Commentf("matches: %v\nout:\n%s\n", matches, out))
c.Assert(matches["action"], checker.Equals, "load", check.Commentf("matches: %v\nout:\n%s\n", matches, out))

out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", "event=save")
events = strings.Split(strings.TrimSpace(out), "\n")
c.Assert(events, checker.HasLen, 1)
matches = eventstestutils.ScanMap(events[0])
c.Assert(matches["id"], checker.Equals, imageID, check.Commentf("matches: %v\nout:\n%s\n", matches, out))
c.Assert(matches["action"], checker.Equals, "save", check.Commentf("matches: %v\nout:\n%s\n", matches, out))
}

func (s *DockerSuite) TestEventsFilters(c *check.C) {
since := daemonUnixTime(c)
dockerCmd(c, "run", "--rm", "busybox", "true")
Expand Down
14 changes: 11 additions & 3 deletions man/docker-events.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ information and real-time information.

Docker containers will report the following events:

attach, commit, copy, create, destroy, die, exec_create, exec_start, export, kill, oom, pause, rename, resize, restart, start, stop, top, unpause
attach, commit, copy, create, destroy, die, exec_create, exec_start, export, kill, oom, pause, rename, resize, restart, start, stop, top, unpause, update

and Docker images will report:
Docker images report the following events:

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

Docker volumes report the following events:

create, mount, unmount, destroy

Docker networks report the following events:

create, connect, disconnect, destroy

# OPTIONS
**--help**
Expand Down

0 comments on commit 0656105

Please sign in to comment.