Skip to content

Commit 818bc3c

Browse files
gloursndeloof
authored andcommitted
add sync+restart action to watch attribute
Signed-off-by: Guillaume Lours <[email protected]>
1 parent cf3e686 commit 818bc3c

File tree

6 files changed

+43
-5
lines changed

6 files changed

+43
-5
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/Microsoft/go-winio v0.6.1
88
github.com/adrg/xdg v0.4.0
99
github.com/buger/goterm v1.0.4
10-
github.com/compose-spec/compose-go v1.19.0
10+
github.com/compose-spec/compose-go v1.20.0
1111
github.com/containerd/console v1.0.3
1212
github.com/containerd/containerd v1.7.7
1313
github.com/davecgh/go-spew v1.1.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+g
139139
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
140140
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE=
141141
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4=
142-
github.com/compose-spec/compose-go v1.19.0 h1:t68gAcwStDg0hy2kFvqHJIksf6xkqRnlSKfL45/ETqo=
143-
github.com/compose-spec/compose-go v1.19.0/go.mod h1:+MdqXV4RA7wdFsahh/Kb8U0pAJqkg7mr4PM9tFKU8RM=
142+
github.com/compose-spec/compose-go v1.20.0 h1:h4ZKOst1EF/DwZp7dWkb+wbTVE4nEyT9Lc89to84Ol4=
143+
github.com/compose-spec/compose-go v1.20.0/go.mod h1:+MdqXV4RA7wdFsahh/Kb8U0pAJqkg7mr4PM9tFKU8RM=
144144
github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
145145
github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw=
146146
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=

pkg/compose/watch.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ func (t tarDockerClient) Exec(ctx context.Context, containerID string, cmd []str
415415

416416
func (s *composeService) handleWatchBatch(ctx context.Context, project *types.Project, serviceName string, build api.BuildOptions, batch []fileEvent, syncer sync.Syncer) error {
417417
pathMappings := make([]sync.PathMapping, len(batch))
418+
restartService := false
418419
for i := range batch {
419420
if batch[i].Action == types.WatchActionRebuild {
420421
fmt.Fprintf(
@@ -441,6 +442,9 @@ func (s *composeService) handleWatchBatch(ctx context.Context, project *types.Pr
441442
}
442443
return nil
443444
}
445+
if batch[i].Action == types.WatchActionSyncRestart {
446+
restartService = true
447+
}
444448
pathMappings[i] = batch[i].PathMapping
445449
}
446450

@@ -453,6 +457,13 @@ func (s *composeService) handleWatchBatch(ctx context.Context, project *types.Pr
453457
if err := syncer.Sync(ctx, service, pathMappings); err != nil {
454458
return err
455459
}
460+
if restartService {
461+
return s.Restart(ctx, project.Name, api.RestartOptions{
462+
Services: []string{serviceName},
463+
Project: project,
464+
NoDeps: false,
465+
})
466+
}
456467
return nil
457468
}
458469

pkg/e2e/fixtures/watch/compose.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ x-dev: &x-dev
66
ignore:
77
- '*.foo'
88
- ./ignored
9+
- action: sync+restart
10+
path: ./config
11+
target: /app/config
912

1013
services:
1114
alpine:
1215
build:
1316
dockerfile_inline: |-
1417
FROM alpine
1518
RUN mkdir -p /app/data
19+
RUN mkdir -p /app/config
1620
init: true
1721
command: sleep infinity
1822
develop: *x-dev
@@ -21,6 +25,7 @@ services:
2125
dockerfile_inline: |-
2226
FROM busybox
2327
RUN mkdir -p /app/data
28+
RUN mkdir -p /app/config
2429
init: true
2530
command: sleep infinity
2631
develop: *x-dev
@@ -29,6 +34,7 @@ services:
2934
dockerfile_inline: |-
3035
FROM debian
3136
RUN mkdir -p /app/data
37+
RUN mkdir -p /app/config
3238
init: true
3339
command: sleep infinity
3440
develop: *x-dev
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a config file

pkg/e2e/watch_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,18 @@ func TestWatch(t *testing.T) {
5959
func doTest(t *testing.T, svcName string, tarSync bool) {
6060
tmpdir := t.TempDir()
6161
dataDir := filepath.Join(tmpdir, "data")
62-
writeDataFile := func(name string, contents string) {
62+
configDir := filepath.Join(tmpdir, "config")
63+
64+
writeTestFile := func(name, contents, sourceDir string) {
6365
t.Helper()
64-
dest := filepath.Join(dataDir, name)
66+
dest := filepath.Join(sourceDir, name)
6567
require.NoError(t, os.MkdirAll(filepath.Dir(dest), 0o700))
6668
t.Logf("writing %q to %q", contents, dest)
6769
require.NoError(t, os.WriteFile(dest, []byte(contents+"\n"), 0o600))
6870
}
71+
writeDataFile := func(name, contents string) {
72+
writeTestFile(name, contents, dataDir)
73+
}
6974

7075
composeFilePath := filepath.Join(tmpdir, "compose.yaml")
7176
CopyFile(t, filepath.Join("fixtures", "watch", "compose.yaml"), composeFilePath)
@@ -195,5 +200,20 @@ func doTest(t *testing.T, svcName string, tarSync bool) {
195200
Err: "No such file or directory",
196201
})
197202

203+
t.Logf("Sync and restart use case")
204+
require.NoError(t, os.Mkdir(configDir, 0o700))
205+
writeTestFile("file.config", "This is an updated config file", configDir)
206+
checkRestart := func(state string) poll.Check {
207+
return func(pollLog poll.LogT) poll.Result {
208+
if strings.Contains(r.Combined(), state) {
209+
return poll.Success()
210+
}
211+
return poll.Continue(r.Combined())
212+
}
213+
}
214+
poll.WaitOn(t, checkRestart(fmt.Sprintf("%s-1 Restarting", svcName)))
215+
poll.WaitOn(t, checkRestart(fmt.Sprintf("%s-1 Started", svcName)))
216+
poll.WaitOn(t, checkFileContents("/app/config/file.config", "This is an updated config file"))
217+
198218
testComplete.Store(true)
199219
}

0 commit comments

Comments
 (0)