Skip to content

Commit

Permalink
emit a "tag" event when building image with "-t" parameter
Browse files Browse the repository at this point in the history
This is useful for cluster systems such as swarm to sync the image
state when new images are successfully built.

Signed-off-by: Shijiang Wei <mountkin@gmail.com>
  • Loading branch information
mountkin committed Oct 19, 2015
1 parent d35a1f2 commit 2968fa4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
1 change: 0 additions & 1 deletion api/server/router/local/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,6 @@ func (s *router) postImagesTag(ctx context.Context, w http.ResponseWriter, r *ht
if err := s.daemon.TagImage(repo, tag, name, force); err != nil {
return err
}
s.daemon.EventsService.Log("tag", utils.ImageReference(repo, tag), "")
w.WriteHeader(http.StatusCreated)
return nil
}
Expand Down
7 changes: 6 additions & 1 deletion daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"github.com/docker/docker/pkg/truncindex"
"github.com/docker/docker/registry"
"github.com/docker/docker/runconfig"
"github.com/docker/docker/utils"
volumedrivers "github.com/docker/docker/volume/drivers"
"github.com/docker/docker/volume/local"
"github.com/docker/docker/volume/store"
Expand Down Expand Up @@ -1026,7 +1027,11 @@ func (daemon *Daemon) Graph() *graph.Graph {
// imageName. If force is true, an existing tag with the same name may be
// overwritten.
func (daemon *Daemon) TagImage(repoName, tag, imageName string, force bool) error {
return daemon.repositories.Tag(repoName, tag, imageName, force)
if err := daemon.repositories.Tag(repoName, tag, imageName, force); err != nil {
return err
}
daemon.EventsService.Log("tag", utils.ImageReference(repoName, tag), "")
return nil
}

// PullImage initiates a pull operation. image is the repository name to pull, and
Expand Down
35 changes: 35 additions & 0 deletions integration-cli/docker_cli_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -6223,3 +6224,37 @@ func (s *DockerSuite) TestBuildNoNamedVolume(c *check.C) {
_, err := buildImage("test", dockerFile, false)
c.Assert(err, check.NotNil, check.Commentf("image build should have failed"))
}

func (s *DockerSuite) TestBuildTagEvent(c *check.C) {
testRequires(c, DaemonIsLinux)
resp, rc, err := sockRequestRaw("GET", `/events?filters={"event":["tag"]}`, nil, "application/json")
c.Assert(err, check.IsNil)
defer rc.Close()
c.Assert(resp.StatusCode, check.Equals, http.StatusOK)

type event struct {
Status string `json:"status"`
ID string `json:"id"`
}
ch := make(chan event)
go func() {
ev := event{}
if err := json.NewDecoder(rc).Decode(&ev); err == nil {
ch <- ev
}
}()

dockerFile := `FROM busybox
RUN echo events
`
_, err = buildImage("test", dockerFile, false)
c.Assert(err, check.IsNil)

select {
case ev := <-ch:
c.Assert(ev.Status, check.Equals, "tag")
c.Assert(ev.ID, check.Equals, "test:")
case <-time.After(time.Second):
c.Fatal("The 'tag' event not heard from the server")
}
}
10 changes: 6 additions & 4 deletions integration-cli/docker_cli_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,13 +414,13 @@ func (s *DockerSuite) TestEventsFilterLabels(c *check.C) {
func (s *DockerSuite) TestEventsFilterImageLabels(c *check.C) {
testRequires(c, DaemonIsLinux)
since := daemonTime(c).Unix()
name := "labelfilterimage"
name := "labelfiltertest"
label := "io.docker.testing=image"

// Build a test image.
_, err := buildImage(name, `
_, err := buildImage(name, fmt.Sprintf(`
FROM busybox:latest
LABEL io.docker.testing=image`, true)
LABEL %s`, label), true)
if err != nil {
c.Fatalf("Couldn't create image: %q", err)
}
Expand All @@ -437,7 +437,9 @@ func (s *DockerSuite) TestEventsFilterImageLabels(c *check.C) {
"--filter", fmt.Sprintf("label=%s", label))

events := strings.Split(strings.TrimSpace(out), "\n")
c.Assert(len(events), checker.Equals, 2, check.Commentf("Events == %s", events))

// 2 events from the "docker tag" command, another one is from "docker build"
c.Assert(len(events), checker.Equals, 3, check.Commentf("Events == %s", events))
for _, e := range events {
c.Assert(e, checker.Contains, "labelfiltertest")
}
Expand Down

0 comments on commit 2968fa4

Please sign in to comment.