-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Watch initial sync #12047
Merged
Merged
Watch initial sync #12047
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
"errors" | ||
"fmt" | ||
"io" | ||
"io/fs" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
|
@@ -157,6 +158,16 @@ func (s *composeService) watch(ctx context.Context, syncChannel chan bool, proje | |
if checkIfPathAlreadyBindMounted(trigger.Path, service.Volumes) { | ||
logrus.Warnf("path '%s' also declared by a bind mount volume, this path won't be monitored!\n", trigger.Path) | ||
continue | ||
} else { | ||
var initialSync bool | ||
success, err := trigger.Extensions.Get("x-initialSync", &initialSync) | ||
if err == nil && success && initialSync && (trigger.Action == types.WatchActionSync || trigger.Action == types.WatchActionSyncRestart) { | ||
// Need to check initial files are in container that are meant to be synched from watch action | ||
err := s.initialSync(ctx, project, service, trigger, ignore, syncer) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
paths = append(paths, trigger.Path) | ||
pathLogs = append(pathLogs, fmt.Sprintf("Action %s for path %q", trigger.Action, trigger.Path)) | ||
|
@@ -574,3 +585,123 @@ func (s *composeService) pruneDanglingImagesOnRebuild(ctx context.Context, proje | |
} | ||
} | ||
} | ||
|
||
// Walks develop.watch.path and checks which files should be copied inside the container | ||
// ignores develop.watch.ignore, Dockerfile, compose files, bind mounted paths and .git | ||
func (s *composeService) initialSync(ctx context.Context, project *types.Project, service types.ServiceConfig, trigger types.Trigger, ignore watch.PathMatcher, syncer sync.Syncer) error { | ||
dockerFileIgnore, err := watch.NewDockerPatternMatcher("/", []string{"Dockerfile", "*compose*.y*ml"}) | ||
if err != nil { | ||
return err | ||
} | ||
triggerIgnore, err := watch.NewDockerPatternMatcher("/", trigger.Ignore) | ||
if err != nil { | ||
return err | ||
} | ||
ignoreInitialSync := watch.NewCompositeMatcher(ignore, dockerFileIgnore, triggerIgnore) | ||
|
||
pathsToCopy, err := s.initialSyncFiles(ctx, project, service, trigger, ignoreInitialSync) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return syncer.Sync(ctx, service, pathsToCopy) | ||
} | ||
|
||
// Syncs files from develop.watch.path if thy have been modified after the image has been created | ||
// | ||
//nolint:gocyclo | ||
func (s *composeService) initialSyncFiles(ctx context.Context, project *types.Project, service types.ServiceConfig, trigger types.Trigger, ignore watch.PathMatcher) ([]sync.PathMapping, error) { | ||
fi, err := os.Stat(trigger.Path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
timeImageCreated, err := s.imageCreatedTime(ctx, project, service.Name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var pathsToCopy []sync.PathMapping | ||
switch mode := fi.Mode(); { | ||
case mode.IsDir(): | ||
// process directory | ||
err = filepath.WalkDir(trigger.Path, func(path string, d fs.DirEntry, err error) error { | ||
if err != nil { | ||
// handle possible path err, just in case... | ||
return err | ||
} | ||
if trigger.Path == path { | ||
// walk starts at the root directory | ||
return nil | ||
} | ||
if shouldIgnore(filepath.Base(path), ignore) || checkIfPathAlreadyBindMounted(path, service.Volumes) { | ||
// By definition sync ignores bind mounted paths | ||
if d.IsDir() { | ||
// skip folder | ||
return fs.SkipDir | ||
} | ||
return nil // skip file | ||
} | ||
info, err := d.Info() | ||
if err != nil { | ||
return err | ||
} | ||
if !d.IsDir() { | ||
if info.ModTime().Before(timeImageCreated) { | ||
// skip file if it was modified before image creation | ||
return nil | ||
} | ||
rel, err := filepath.Rel(trigger.Path, path) | ||
if err != nil { | ||
return err | ||
} | ||
// only copy files (and not full directories) | ||
pathsToCopy = append(pathsToCopy, sync.PathMapping{ | ||
HostPath: path, | ||
ContainerPath: filepath.Join(trigger.Target, rel), | ||
}) | ||
} | ||
return nil | ||
}) | ||
case mode.IsRegular(): | ||
// process file | ||
if fi.ModTime().After(timeImageCreated) && !shouldIgnore(filepath.Base(trigger.Path), ignore) && !checkIfPathAlreadyBindMounted(trigger.Path, service.Volumes) { | ||
pathsToCopy = append(pathsToCopy, sync.PathMapping{ | ||
HostPath: trigger.Path, | ||
ContainerPath: trigger.Target, | ||
}) | ||
} | ||
} | ||
return pathsToCopy, err | ||
} | ||
|
||
func shouldIgnore(name string, ignore watch.PathMatcher) bool { | ||
shouldIgnore, _ := ignore.Matches(name) | ||
// ignore files that match any ignore pattern | ||
return shouldIgnore | ||
} | ||
|
||
// gets the image creation time for a service | ||
func (s *composeService) imageCreatedTime(ctx context.Context, project *types.Project, serviceName string) (time.Time, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. possibly can be moved somewhere else more generic |
||
containers, err := s.apiClient().ContainerList(ctx, container.ListOptions{ | ||
All: true, | ||
Filters: filters.NewArgs( | ||
filters.Arg("label", fmt.Sprintf("%s=%s", api.ProjectLabel, project.Name)), | ||
filters.Arg("label", fmt.Sprintf("%s=%s", api.ServiceLabel, serviceName))), | ||
}) | ||
if err != nil { | ||
return time.Now(), err | ||
} | ||
if len(containers) == 0 { | ||
return time.Now(), fmt.Errorf("Could not get created time for service's image") | ||
} | ||
|
||
img, _, err := s.apiClient().ImageInspectWithRaw(ctx, containers[0].ImageID) | ||
if err != nil { | ||
return time.Now(), err | ||
} | ||
// Need to get oldest one? | ||
timeCreated, err := time.Parse(time.RFC3339Nano, img.Created) | ||
if err != nil { | ||
return time.Now(), err | ||
} | ||
return timeCreated, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a better way to handle all the
compose.yaml
variations pattern? For instance, triedcompose.y[a]?ml
but it did not work :(