Skip to content

Commit 805541b

Browse files
authored
watch: use official develop section (docker#11026)
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent d322ad9 commit 805541b

File tree

4 files changed

+18
-31
lines changed

4 files changed

+18
-31
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.18.4
10+
github.com/compose-spec/compose-go v1.19.0
1111
github.com/containerd/console v1.0.3
1212
github.com/containerd/containerd v1.7.6
1313
github.com/cucumber/godog v0.0.0-00010101000000-000000000000 // replaced; see replace for the actual version used

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.18.4 h1:yLYfsc3ATAYZVAJcXyx/V847/JVBmf3pfKfR13mXU4s=
143-
github.com/compose-spec/compose-go v1.18.4/go.mod h1:+MdqXV4RA7wdFsahh/Kb8U0pAJqkg7mr4PM9tFKU8RM=
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=
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: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,12 @@ import (
3838
"golang.org/x/sync/errgroup"
3939
)
4040

41-
type DevelopmentConfig struct {
42-
Watch []Trigger `json:"watch,omitempty"`
43-
}
44-
45-
type WatchAction string
46-
47-
const (
48-
WatchActionSync WatchAction = "sync"
49-
WatchActionRebuild WatchAction = "rebuild"
50-
)
51-
52-
type Trigger struct {
53-
Path string `json:"path,omitempty"`
54-
Action string `json:"action,omitempty"`
55-
Target string `json:"target,omitempty"`
56-
Ignore []string `json:"ignore,omitempty"`
57-
}
58-
5941
const quietPeriod = 500 * time.Millisecond
6042

6143
// fileEvent contains the Compose service and modified host system path.
6244
type fileEvent struct {
6345
sync.PathMapping
64-
Action WatchAction
46+
Action types.WatchAction
6547
}
6648

6749
// getSyncImplementation returns the the tar-based syncer unless it has been explicitly
@@ -95,6 +77,10 @@ func (s *composeService) Watch(ctx context.Context, project *types.Project, serv
9577
return err
9678
}
9779

80+
if service.Develop != nil {
81+
config = service.Develop
82+
}
83+
9884
if config == nil {
9985
continue
10086
}
@@ -169,7 +155,7 @@ func (s *composeService) Watch(ctx context.Context, project *types.Project, serv
169155
return eg.Wait()
170156
}
171157

172-
func (s *composeService) watch(ctx context.Context, project *types.Project, name string, options api.WatchOptions, watcher watch.Notify, syncer sync.Syncer, triggers []Trigger) error {
158+
func (s *composeService) watch(ctx context.Context, project *types.Project, name string, options api.WatchOptions, watcher watch.Notify, syncer sync.Syncer, triggers []types.Trigger) error {
173159
ctx, cancel := context.WithCancel(ctx)
174160
defer cancel()
175161

@@ -223,7 +209,7 @@ func (s *composeService) watch(ctx context.Context, project *types.Project, name
223209
// rules.
224210
//
225211
// Any errors are logged as warnings and nil (no file event) is returned.
226-
func maybeFileEvent(trigger Trigger, hostPath string, ignore watch.PathMatcher) *fileEvent {
212+
func maybeFileEvent(trigger types.Trigger, hostPath string, ignore watch.PathMatcher) *fileEvent {
227213
if !watch.IsChild(trigger.Path, hostPath) {
228214
return nil
229215
}
@@ -250,20 +236,21 @@ func maybeFileEvent(trigger Trigger, hostPath string, ignore watch.PathMatcher)
250236
}
251237

252238
return &fileEvent{
253-
Action: WatchAction(trigger.Action),
239+
Action: trigger.Action,
254240
PathMapping: sync.PathMapping{
255241
HostPath: hostPath,
256242
ContainerPath: containerPath,
257243
},
258244
}
259245
}
260246

261-
func loadDevelopmentConfig(service types.ServiceConfig, project *types.Project) (*DevelopmentConfig, error) {
262-
var config DevelopmentConfig
247+
func loadDevelopmentConfig(service types.ServiceConfig, project *types.Project) (*types.DevelopConfig, error) {
248+
var config types.DevelopConfig
263249
y, ok := service.Extensions["x-develop"]
264250
if !ok {
265251
return nil, nil
266252
}
253+
logrus.Warnf("x-develop is DEPRECATED, please use the official `develop` attribute")
267254
err := mapstructure.Decode(y, &config)
268255
if err != nil {
269256
return nil, err
@@ -286,7 +273,7 @@ func loadDevelopmentConfig(service types.ServiceConfig, project *types.Project)
286273
return nil, errors.New("watch rules MUST define a path")
287274
}
288275

289-
if trigger.Action == string(WatchActionRebuild) && service.Build == nil {
276+
if trigger.Action == types.WatchActionRebuild && service.Build == nil {
290277
return nil, fmt.Errorf("service %s doesn't have a build section, can't apply 'rebuild' on watch", service.Name)
291278
}
292279

@@ -429,7 +416,7 @@ func (t tarDockerClient) Exec(ctx context.Context, containerID string, cmd []str
429416
func (s *composeService) handleWatchBatch(ctx context.Context, project *types.Project, serviceName string, build api.BuildOptions, batch []fileEvent, syncer sync.Syncer) error {
430417
pathMappings := make([]sync.PathMapping, len(batch))
431418
for i := range batch {
432-
if batch[i].Action == WatchActionRebuild {
419+
if batch[i].Action == types.WatchActionRebuild {
433420
fmt.Fprintf(
434421
s.stdinfo(),
435422
"Rebuilding %s after changes were detected:%s\n",

pkg/compose/watch_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestDebounceBatching(t *testing.T) {
4040

4141
eventBatchCh := batchDebounceEvents(ctx, clock, quietPeriod, ch)
4242
for i := 0; i < 100; i++ {
43-
var action WatchAction = "a"
43+
var action types.WatchAction = "a"
4444
if i%2 == 0 {
4545
action = "b"
4646
}
@@ -124,7 +124,7 @@ func TestWatch_Sync(t *testing.T) {
124124
dockerCli: cli,
125125
clock: clock,
126126
}
127-
err := service.watch(ctx, &proj, "test", api.WatchOptions{}, watcher, syncer, []Trigger{
127+
err := service.watch(ctx, &proj, "test", api.WatchOptions{}, watcher, syncer, []types.Trigger{
128128
{
129129
Path: "/sync",
130130
Action: "sync",

0 commit comments

Comments
 (0)