From 0cd5d23a8efda8131cc22c8ec0de1d8db4558274 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 20 Sep 2023 09:53:33 +0200 Subject: [PATCH] use official `develop` section to configure watch command Signed-off-by: Nicolas De Loof --- go.mod | 2 ++ go.sum | 4 ++-- pkg/compose/watch.go | 39 +++++++++++++-------------------------- pkg/compose/watch_test.go | 4 ++-- 4 files changed, 19 insertions(+), 30 deletions(-) diff --git a/go.mod b/go.mod index 04f2cdb7dca..64a0c5b1059 100644 --- a/go.mod +++ b/go.mod @@ -187,3 +187,5 @@ require ( // Override for e2e tests replace github.com/cucumber/godog => github.com/laurazard/godog v0.0.0-20220922095256-4c4b17abdae7 + +replace github.com/compose-spec/compose-go => github.com/ndeloof/compose-go v1.2.4-0.20230920074557-86cf001d948d diff --git a/go.sum b/go.sum index e9296368956..f9f329b92ce 100644 --- a/go.sum +++ b/go.sum @@ -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.18.4 h1:yLYfsc3ATAYZVAJcXyx/V847/JVBmf3pfKfR13mXU4s= -github.com/compose-spec/compose-go v1.18.4/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= @@ -501,6 +499,8 @@ github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7P github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/ndeloof/compose-go v1.2.4-0.20230920074557-86cf001d948d h1:mdtoWSiBQsGYfSj231gbpZvFWXddG98xX2bCRfRiCpY= +github.com/ndeloof/compose-go v1.2.4-0.20230920074557-86cf001d948d/go.mod h1:+MdqXV4RA7wdFsahh/Kb8U0pAJqkg7mr4PM9tFKU8RM= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.0 h1:Iw5WCbBcaAAd0fpRb1c9r5YCylv4XDoCSigm1zLevwU= diff --git a/pkg/compose/watch.go b/pkg/compose/watch.go index 39f762d2c0e..29b0fe33e30 100644 --- a/pkg/compose/watch.go +++ b/pkg/compose/watch.go @@ -38,30 +38,12 @@ import ( "golang.org/x/sync/errgroup" ) -type DevelopmentConfig struct { - Watch []Trigger `json:"watch,omitempty"` -} - -type WatchAction string - -const ( - WatchActionSync WatchAction = "sync" - WatchActionRebuild WatchAction = "rebuild" -) - -type Trigger struct { - Path string `json:"path,omitempty"` - Action string `json:"action,omitempty"` - Target string `json:"target,omitempty"` - Ignore []string `json:"ignore,omitempty"` -} - const quietPeriod = 500 * time.Millisecond // fileEvent contains the Compose service and modified host system path. type fileEvent struct { sync.PathMapping - Action WatchAction + Action types.WatchAction } // 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 return err } + if service.Develop != nil { + config = service.Develop + } + if config == nil { continue } @@ -169,7 +155,7 @@ func (s *composeService) Watch(ctx context.Context, project *types.Project, serv return eg.Wait() } -func (s *composeService) watch(ctx context.Context, project *types.Project, name string, options api.WatchOptions, watcher watch.Notify, syncer sync.Syncer, triggers []Trigger) error { +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 { ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -223,7 +209,7 @@ func (s *composeService) watch(ctx context.Context, project *types.Project, name // rules. // // Any errors are logged as warnings and nil (no file event) is returned. -func maybeFileEvent(trigger Trigger, hostPath string, ignore watch.PathMatcher) *fileEvent { +func maybeFileEvent(trigger types.Trigger, hostPath string, ignore watch.PathMatcher) *fileEvent { if !watch.IsChild(trigger.Path, hostPath) { return nil } @@ -250,7 +236,7 @@ func maybeFileEvent(trigger Trigger, hostPath string, ignore watch.PathMatcher) } return &fileEvent{ - Action: WatchAction(trigger.Action), + Action: trigger.Action, PathMapping: sync.PathMapping{ HostPath: hostPath, ContainerPath: containerPath, @@ -258,12 +244,13 @@ func maybeFileEvent(trigger Trigger, hostPath string, ignore watch.PathMatcher) } } -func loadDevelopmentConfig(service types.ServiceConfig, project *types.Project) (*DevelopmentConfig, error) { - var config DevelopmentConfig +func loadDevelopmentConfig(service types.ServiceConfig, project *types.Project) (*types.DevelopConfig, error) { + var config types.DevelopConfig y, ok := service.Extensions["x-develop"] if !ok { return nil, nil } + logrus.Warnf("x-develop is DEPRECATED, please use the official `develop` attribute") err := mapstructure.Decode(y, &config) if err != nil { return nil, err @@ -286,7 +273,7 @@ func loadDevelopmentConfig(service types.ServiceConfig, project *types.Project) return nil, errors.New("watch rules MUST define a path") } - if trigger.Action == string(WatchActionRebuild) && service.Build == nil { + if trigger.Action == types.WatchActionRebuild && service.Build == nil { return nil, fmt.Errorf("service %s doesn't have a build section, can't apply 'rebuild' on watch", service.Name) } @@ -429,7 +416,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)) for i := range batch { - if batch[i].Action == WatchActionRebuild { + if batch[i].Action == types.WatchActionRebuild { fmt.Fprintf( s.stdinfo(), "Rebuilding %s after changes were detected:%s\n", diff --git a/pkg/compose/watch_test.go b/pkg/compose/watch_test.go index 61785a505a1..678a2ef8a4b 100644 --- a/pkg/compose/watch_test.go +++ b/pkg/compose/watch_test.go @@ -40,7 +40,7 @@ func TestDebounceBatching(t *testing.T) { eventBatchCh := batchDebounceEvents(ctx, clock, quietPeriod, ch) for i := 0; i < 100; i++ { - var action WatchAction = "a" + var action types.WatchAction = "a" if i%2 == 0 { action = "b" } @@ -124,7 +124,7 @@ func TestWatch_Sync(t *testing.T) { dockerCli: cli, clock: clock, } - err := service.watch(ctx, &proj, "test", api.WatchOptions{}, watcher, syncer, []Trigger{ + err := service.watch(ctx, &proj, "test", api.WatchOptions{}, watcher, syncer, []types.Trigger{ { Path: "/sync", Action: "sync",