watch: use official develop section (#11026)

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De loof 2023-09-20 17:51:53 +02:00 committed by GitHub
parent d322ad91e8
commit 805541be9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 31 deletions

2
go.mod
View File

@ -7,7 +7,7 @@ require (
github.com/Microsoft/go-winio v0.6.1 github.com/Microsoft/go-winio v0.6.1
github.com/adrg/xdg v0.4.0 github.com/adrg/xdg v0.4.0
github.com/buger/goterm v1.0.4 github.com/buger/goterm v1.0.4
github.com/compose-spec/compose-go v1.18.4 github.com/compose-spec/compose-go v1.19.0
github.com/containerd/console v1.0.3 github.com/containerd/console v1.0.3
github.com/containerd/containerd v1.7.6 github.com/containerd/containerd v1.7.6
github.com/cucumber/godog v0.0.0-00010101000000-000000000000 // replaced; see replace for the actual version used github.com/cucumber/godog v0.0.0-00010101000000-000000000000 // replaced; see replace for the actual version used

4
go.sum
View File

@ -139,8 +139,8 @@ 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/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 h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE=
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4= 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.19.0 h1:t68gAcwStDg0hy2kFvqHJIksf6xkqRnlSKfL45/ETqo=
github.com/compose-spec/compose-go v1.18.4/go.mod h1:+MdqXV4RA7wdFsahh/Kb8U0pAJqkg7mr4PM9tFKU8RM= 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 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw= github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw= github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=

View File

@ -38,30 +38,12 @@ import (
"golang.org/x/sync/errgroup" "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 const quietPeriod = 500 * time.Millisecond
// fileEvent contains the Compose service and modified host system path. // fileEvent contains the Compose service and modified host system path.
type fileEvent struct { type fileEvent struct {
sync.PathMapping sync.PathMapping
Action WatchAction Action types.WatchAction
} }
// getSyncImplementation returns the the tar-based syncer unless it has been explicitly // 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 return err
} }
if service.Develop != nil {
config = service.Develop
}
if config == nil { if config == nil {
continue continue
} }
@ -169,7 +155,7 @@ func (s *composeService) Watch(ctx context.Context, project *types.Project, serv
return eg.Wait() 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) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
@ -223,7 +209,7 @@ func (s *composeService) watch(ctx context.Context, project *types.Project, name
// rules. // rules.
// //
// Any errors are logged as warnings and nil (no file event) is returned. // 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) { if !watch.IsChild(trigger.Path, hostPath) {
return nil return nil
} }
@ -250,7 +236,7 @@ func maybeFileEvent(trigger Trigger, hostPath string, ignore watch.PathMatcher)
} }
return &fileEvent{ return &fileEvent{
Action: WatchAction(trigger.Action), Action: trigger.Action,
PathMapping: sync.PathMapping{ PathMapping: sync.PathMapping{
HostPath: hostPath, HostPath: hostPath,
ContainerPath: containerPath, 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) { func loadDevelopmentConfig(service types.ServiceConfig, project *types.Project) (*types.DevelopConfig, error) {
var config DevelopmentConfig var config types.DevelopConfig
y, ok := service.Extensions["x-develop"] y, ok := service.Extensions["x-develop"]
if !ok { if !ok {
return nil, nil return nil, nil
} }
logrus.Warnf("x-develop is DEPRECATED, please use the official `develop` attribute")
err := mapstructure.Decode(y, &config) err := mapstructure.Decode(y, &config)
if err != nil { if err != nil {
return nil, err 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") 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) 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 { 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)) pathMappings := make([]sync.PathMapping, len(batch))
for i := range batch { for i := range batch {
if batch[i].Action == WatchActionRebuild { if batch[i].Action == types.WatchActionRebuild {
fmt.Fprintf( fmt.Fprintf(
s.stdinfo(), s.stdinfo(),
"Rebuilding %s after changes were detected:%s\n", "Rebuilding %s after changes were detected:%s\n",

View File

@ -40,7 +40,7 @@ func TestDebounceBatching(t *testing.T) {
eventBatchCh := batchDebounceEvents(ctx, clock, quietPeriod, ch) eventBatchCh := batchDebounceEvents(ctx, clock, quietPeriod, ch)
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
var action WatchAction = "a" var action types.WatchAction = "a"
if i%2 == 0 { if i%2 == 0 {
action = "b" action = "b"
} }
@ -124,7 +124,7 @@ func TestWatch_Sync(t *testing.T) {
dockerCli: cli, dockerCli: cli,
clock: clock, 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", Path: "/sync",
Action: "sync", Action: "sync",