Skip to content

Commit

Permalink
add sync+restart action to watch attribute
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
  • Loading branch information
glours committed Oct 16, 2023
1 parent 9b4d577 commit f1af9cc
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 6 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,5 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/compose-spec/compose-go v1.19.0 => github.com/glours/compose-go v0.0.0-20231012150443-3d07b9fd4c6d
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+g
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE=
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4=
github.com/compose-spec/compose-go v1.19.0 h1:t68gAcwStDg0hy2kFvqHJIksf6xkqRnlSKfL45/ETqo=
github.com/compose-spec/compose-go v1.19.0/go.mod h1:+MdqXV4RA7wdFsahh/Kb8U0pAJqkg7mr4PM9tFKU8RM=
github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
Expand Down Expand Up @@ -224,6 +222,8 @@ github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbS
github.com/fvbommel/sortorder v1.0.2 h1:mV4o8B2hKboCdkJm+a7uX/SIpZob4JzUpc5GGnM45eo=
github.com/fvbommel/sortorder v1.0.2/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/glours/compose-go v0.0.0-20231012150443-3d07b9fd4c6d h1:3arKd56giwUYHcJRc7MsUHzLSI9TBSoSSLh5ST+KINI=
github.com/glours/compose-go v0.0.0-20231012150443-3d07b9fd4c6d/go.mod h1:+MdqXV4RA7wdFsahh/Kb8U0pAJqkg7mr4PM9tFKU8RM=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down
11 changes: 11 additions & 0 deletions pkg/compose/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ func (t tarDockerClient) Exec(ctx context.Context, containerID string, cmd []str

func (s *composeService) handleWatchBatch(ctx context.Context, project *types.Project, serviceName string, build api.BuildOptions, batch []fileEvent, syncer sync.Syncer) error {
pathMappings := make([]sync.PathMapping, len(batch))
restartService := false
for i := range batch {
if batch[i].Action == types.WatchActionRebuild {
fmt.Fprintf(
Expand All @@ -441,6 +442,9 @@ func (s *composeService) handleWatchBatch(ctx context.Context, project *types.Pr
}
return nil
}
if batch[i].Action == types.WatchActionSyncRestart {
restartService = true
}
pathMappings[i] = batch[i].PathMapping
}

Expand All @@ -453,6 +457,13 @@ func (s *composeService) handleWatchBatch(ctx context.Context, project *types.Pr
if err := syncer.Sync(ctx, service, pathMappings); err != nil {
return err
}
if restartService {
return s.Restart(ctx, project.Name, api.RestartOptions{
Services: []string{serviceName},
Project: project,
NoDeps: false,
})
}
return nil
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/e2e/fixtures/watch/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ services:
init: true
command: sleep infinity
develop: *x-dev
watch-restart:
build:
dockerfile_inline: |-
FROM alpine
RUN mkdir -p /app/config
init: true
command: sleep infinity
develop:
watch:
- action: sync+restart
path: ./config/file.config
target: /app/config
1 change: 1 addition & 0 deletions pkg/e2e/fixtures/watch/config/file.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a config file
32 changes: 28 additions & 4 deletions pkg/e2e/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,18 @@ func TestWatch(t *testing.T) {
func doTest(t *testing.T, svcName string, tarSync bool) {
tmpdir := t.TempDir()
dataDir := filepath.Join(tmpdir, "data")
writeDataFile := func(name string, contents string) {
configDir := filepath.Join(tmpdir, "config")

writeTestFile := func(name, contents, sourceDir string) {
t.Helper()
dest := filepath.Join(dataDir, name)
dest := filepath.Join(sourceDir, name)
require.NoError(t, os.MkdirAll(filepath.Dir(dest), 0o700))
t.Logf("writing %q to %q", contents, dest)
require.NoError(t, os.WriteFile(dest, []byte(contents+"\n"), 0o600))
}
writeDataFile := func(name, contents string) {
writeTestFile(name, contents, dataDir)
}

composeFilePath := filepath.Join(tmpdir, "compose.yaml")
CopyFile(t, filepath.Join("fixtures", "watch", "compose.yaml"), composeFilePath)
Expand Down Expand Up @@ -112,19 +117,23 @@ func doTest(t *testing.T, svcName string, tarSync bool) {

require.NoError(t, os.Mkdir(dataDir, 0o700))

checkFileContents := func(path string, contents string) poll.Check {
checkFileContentsWithServiceName := func(serviceName, path, contents string) poll.Check {
return func(pollLog poll.LogT) poll.Result {
if r.Cmd.ProcessState != nil {
return poll.Error(fmt.Errorf("watch process exited early: %s", r.Cmd.ProcessState))
}
res := icmd.RunCmd(cli.NewDockerComposeCmd(t, "exec", svcName, "cat", path))
res := icmd.RunCmd(cli.NewDockerComposeCmd(t, "exec", serviceName, "cat", path))
if strings.Contains(res.Stdout(), contents) {
return poll.Success()
}
return poll.Continue(res.Combined())
}
}

checkFileContents := func(path string, contents string) poll.Check {
return checkFileContentsWithServiceName(svcName, path, contents)
}

waitForFlush := func() {
b := make([]byte, 32)
_, _ = rand.Read(b)
Expand Down Expand Up @@ -195,5 +204,20 @@ func doTest(t *testing.T, svcName string, tarSync bool) {
Err: "No such file or directory",
})

t.Logf("Sync and restart use case")
require.NoError(t, os.Mkdir(configDir, 0o700))
writeTestFile("file.config", "This is an updated config file", configDir)
checkServiceState := func(serviceName string, state string) poll.Check {
return func(pollLog poll.LogT) poll.Result {
res := icmd.RunCmd(cli.NewDockerComposeCmd(t, "ps", serviceName))
if strings.Contains(res.Stdout(), state) {
return poll.Success()
}
return poll.Continue(res.Combined())
}
}
poll.WaitOn(t, checkServiceState("watch-restart", "Up"))
poll.WaitOn(t, checkFileContentsWithServiceName("watch-restart", "/app/config/file.config", "This is an updated config file"))

testComplete.Store(true)
}

0 comments on commit f1af9cc

Please sign in to comment.