Skip to content

Commit bb5b77a

Browse files
fix(sync): apply tag filters before destination mapping (#4003)
* fix(sync): apply tag filters before destination mapping Signed-off-by: Akash Kumar <meakash7902@gmail.com> * fix(sync): return stable pointer from getContentByUpstreamRepo Iterate by index and return &cm.contents[i] so callers get the slice element rather than a copy of the loop variable, matching the existing GetContentByLocalRepo helper. Signed-off-by: Akash Kumar <meakash7902@gmail.com> --------- Signed-off-by: Akash Kumar <meakash7902@gmail.com>
1 parent 9e13be8 commit bb5b77a

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

pkg/extensions/sync/content.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (cm ContentManager) MatchesContent(repo string) bool {
5050

5151
// FilterTags filters a repo tags based on content config rules (semver, regex).
5252
func (cm ContentManager) FilterTags(repo string, tags []string) ([]string, error) {
53-
content := cm.GetContentByLocalRepo(repo)
53+
content := cm.getContentByUpstreamRepo(repo)
5454

5555
var err error
5656
// filter based on tags rules
@@ -106,7 +106,9 @@ func (cm ContentManager) GetRepoSource(repo string) string {
106106

107107
// utilies functions.
108108
func (cm ContentManager) getContentByUpstreamRepo(repo string) *syncconf.Content {
109-
for _, content := range cm.contents {
109+
for i := range cm.contents {
110+
content := &cm.contents[i]
111+
110112
var prefix string
111113
// handle prefixes starting with '/'
112114
if strings.HasPrefix(content.Prefix, "/") {
@@ -125,7 +127,7 @@ func (cm ContentManager) getContentByUpstreamRepo(repo string) *syncconf.Content
125127
}
126128

127129
if matched {
128-
return &content
130+
return content
129131
}
130132
}
131133

pkg/extensions/sync/content_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ func TestContentManager(t *testing.T) {
8282
}
8383
})
8484

85+
Convey("Test GetRepoSource() no match", t, func() {
86+
content := syncconf.Content{Prefix: "repo", Destination: "/mirror"}
87+
cm := sync.NewContentManager([]syncconf.Content{content}, log.NewTestLogger())
88+
So(cm.GetRepoSource("other"), ShouldEqual, "")
89+
})
90+
8591
Convey("Test MatchesContent() error", t, func() {
8692
content := syncconf.Content{Prefix: "[repo%^&"}
8793
cm := sync.NewContentManager([]syncconf.Content{content}, log.NewTestLogger())
@@ -169,6 +175,8 @@ func TestGetContentByLocalRepo(t *testing.T) {
169175

170176
func TestFilterTags(t *testing.T) {
171177
allTagsRegex := ".*"
178+
emptyRegex := ""
179+
kubeRouterRegex := "1.5.0"
172180
badRegex := "[*"
173181
excludeArchRegex := ".*(x86_64|aarch64|amd64|arm64)$"
174182
semverFalse := false
@@ -216,6 +224,33 @@ func TestFilterTags(t *testing.T) {
216224
filteredTags: []string{"v1.0.1"},
217225
err: false,
218226
},
227+
{
228+
repo: "kubernetes/kube-router",
229+
content: []syncconf.Content{
230+
{
231+
Prefix: "kubernetes/kube-router",
232+
Destination: "/mirror",
233+
Tags: &syncconf.Tags{Regex: &kubeRouterRegex, Semver: &semverTrue},
234+
},
235+
},
236+
tags: []string{"1.5.0", "v2.5.0"},
237+
filteredTags: []string{"1.5.0"},
238+
err: false,
239+
},
240+
{
241+
repo: "kubernetes/kube-router",
242+
content: []syncconf.Content{
243+
{
244+
Prefix: "kubernetes/kube-router",
245+
Destination: "/mirror",
246+
StripPrefix: true,
247+
Tags: &syncconf.Tags{Regex: &kubeRouterRegex, Semver: &semverTrue},
248+
},
249+
},
250+
tags: []string{"1.5.0", "v2.5.0"},
251+
filteredTags: []string{"1.5.0"},
252+
err: false,
253+
},
219254
{
220255
repo: "repo",
221256
content: []syncconf.Content{
@@ -253,6 +288,15 @@ func TestFilterTags(t *testing.T) {
253288
filteredTags: []string{"v1"},
254289
err: false,
255290
},
291+
{
292+
repo: "alpine",
293+
content: []syncconf.Content{
294+
{Prefix: "**", Tags: &syncconf.Tags{ExcludeRegex: &emptyRegex}},
295+
},
296+
tags: []string{"v1", "v2", "v3"},
297+
filteredTags: []string{"v1", "v2", "v3"},
298+
err: false,
299+
},
256300
{
257301
repo: "repo",
258302
content: []syncconf.Content{

0 commit comments

Comments
 (0)