From eeafea6c1d9ece48a3ae17dc8eb1e2e2aa25a219 Mon Sep 17 00:00:00 2001 From: Simon Stratmann Date: Thu, 14 Mar 2024 08:53:15 +0100 Subject: [PATCH 1/2] fix: Allow partial match for manual sync See #9326 --- pkg/skaffold/sync/sync.go | 13 ++++++--- pkg/skaffold/sync/sync_test.go | 52 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/pkg/skaffold/sync/sync.go b/pkg/skaffold/sync/sync.go index c89196e53c5..b45c183d453 100644 --- a/pkg/skaffold/sync/sync.go +++ b/pkg/skaffold/sync/sync.go @@ -217,9 +217,9 @@ func autoSyncItem(ctx context.Context, a *latest.Artifact, tag string, e filemon } func latestTag(image string, builds []graph.Artifact) string { - for _, build := range builds { - if build.ImageName == image { - return build.Tag + for _, _build := range builds { + if _build.ImageName == image { + return _build.Tag } } return "" @@ -227,6 +227,7 @@ func latestTag(image string, builds []graph.Artifact) string { func intersect(ctx context.Context, contextWd, containerWd string, syncRules []*latest.SyncRule, files []string) (syncMap, error) { ret := make(syncMap) + hadMismatch := false for _, f := range files { relPath, err := filepath.Rel(contextWd, f) if err != nil { @@ -240,11 +241,15 @@ func intersect(ctx context.Context, contextWd, containerWd string, syncRules []* if len(dsts) == 0 { log.Entry(ctx).Infof("Changed file %s does not match any sync pattern. Skipping sync", relPath) - return nil, nil + hadMismatch = true + continue } ret[f] = dsts } + if len(ret) == 0 && hadMismatch { + return nil, nil + } return ret, nil } diff --git a/pkg/skaffold/sync/sync_test.go b/pkg/skaffold/sync/sync_test.go index 43078cc1346..30847f7ae65 100644 --- a/pkg/skaffold/sync/sync_test.go +++ b/pkg/skaffold/sync/sync_test.go @@ -81,6 +81,58 @@ func TestNewSyncItem(t *testing.T) { Delete: map[string][]string{}, }, }, + { + description: "manual: match copy partial match first", + artifact: &latest.Artifact{ + ImageName: "test", + Sync: &latest.Sync{ + Manual: []*latest.SyncRule{{Src: "*.html", Dest: "."}}, + }, + Workspace: ".", + }, + builds: []graph.Artifact{ + { + ImageName: "test", + Tag: "test:123", + }, + }, + evt: filemon.Events{ + Added: []string{"someOtherFile.txt", "index.html"}, + }, + expected: &Item{ + Image: "test:123", + Copy: map[string][]string{ + "index.html": {"index.html"}, + }, + Delete: map[string][]string{}, + }, + }, + { + description: "manual: match copy partial match last", + artifact: &latest.Artifact{ + ImageName: "test", + Sync: &latest.Sync{ + Manual: []*latest.SyncRule{{Src: "*.html", Dest: "."}}, + }, + Workspace: ".", + }, + builds: []graph.Artifact{ + { + ImageName: "test", + Tag: "test:123", + }, + }, + evt: filemon.Events{ + Added: []string{"index.html", "someOtherFile.txt"}, + }, + expected: &Item{ + Image: "test:123", + Copy: map[string][]string{ + "index.html": {"index.html"}, + }, + Delete: map[string][]string{}, + }, + }, { description: "manual: no tag for image", artifact: &latest.Artifact{ From b62745cc6cd227929c501bcdece3625af32fd233 Mon Sep 17 00:00:00 2001 From: Simon Stratmann Date: Mon, 18 Mar 2024 07:57:21 +0100 Subject: [PATCH 2/2] fix: Allow partial match for manual sync See #9326 --- pkg/skaffold/sync/sync.go | 7 +------ pkg/skaffold/sync/sync_test.go | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/pkg/skaffold/sync/sync.go b/pkg/skaffold/sync/sync.go index b45c183d453..0e906af215b 100644 --- a/pkg/skaffold/sync/sync.go +++ b/pkg/skaffold/sync/sync.go @@ -227,7 +227,6 @@ func latestTag(image string, builds []graph.Artifact) string { func intersect(ctx context.Context, contextWd, containerWd string, syncRules []*latest.SyncRule, files []string) (syncMap, error) { ret := make(syncMap) - hadMismatch := false for _, f := range files { relPath, err := filepath.Rel(contextWd, f) if err != nil { @@ -240,16 +239,12 @@ func intersect(ctx context.Context, contextWd, containerWd string, syncRules []* } if len(dsts) == 0 { - log.Entry(ctx).Infof("Changed file %s does not match any sync pattern. Skipping sync", relPath) - hadMismatch = true + log.Entry(ctx).Infof("Changed file %s does not match any sync pattern. Skipping sync for it", relPath) continue } ret[f] = dsts } - if len(ret) == 0 && hadMismatch { - return nil, nil - } return ret, nil } diff --git a/pkg/skaffold/sync/sync_test.go b/pkg/skaffold/sync/sync_test.go index 30847f7ae65..e94d0766c5b 100644 --- a/pkg/skaffold/sync/sync_test.go +++ b/pkg/skaffold/sync/sync_test.go @@ -269,6 +269,13 @@ func TestNewSyncItem(t *testing.T) { Tag: "placeholder", }, }, + expected: &Item{ + Image: "placeholder", + Copy: map[string][]string{}, + Delete: map[string][]string{ + "index.html": {"index.html"}, + }, + }, }, { description: "manual: not delete syncable", @@ -289,6 +296,13 @@ func TestNewSyncItem(t *testing.T) { Tag: "placeholder", }, }, + expected: &Item{ + Image: "placeholder", + Copy: map[string][]string{ + "index.html": {"/static/index.html"}, + }, + Delete: map[string][]string{}, + }, }, { description: "manual: err bad pattern", @@ -767,7 +781,11 @@ func TestNewSyncItem(t *testing.T) { }] }`, }, - expected: nil, + expected: &Item{ + Image: "test:123", + Copy: map[string][]string{}, + Delete: map[string][]string{}, + }, }, // Auto with Jib