Skip to content

Commit

Permalink
feat(sync): add tag reject_regex filter
Browse files Browse the repository at this point in the history
Fix #2902

Signed-off-by: Vladimir Ermakov <[email protected]>
  • Loading branch information
vooon committed Jan 23, 2025
1 parent ececc9c commit f618593
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/extensions/config/sync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Content struct {
}

type Tags struct {
Regex *string
Semver *bool
Regex *string
RejectRegex *string
Semver *bool
}
33 changes: 33 additions & 0 deletions pkg/extensions/sync/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ func (cm ContentManager) FilterTags(repo string, tags []string) ([]string, error
}
}

if content.Tags.RejectRegex != nil {
tags, err = rejectTagsByRegex(tags, *content.Tags.RejectRegex, cm.log)
if err != nil {
return []string{}, err
}
}

if content.Tags.Semver != nil && *content.Tags.Semver {
tags = filterTagsBySemver(tags, cm.log)
}
Expand Down Expand Up @@ -240,6 +247,32 @@ func filterTagsByRegex(tags []string, regex string, log log.Logger) ([]string, e
return filteredTags, nil
}

// rejectTagsByRegex filter-out images by tag regex given in the config.
func rejectTagsByRegex(tags []string, regex string, log log.Logger) ([]string, error) {
if len(tags) == 0 || regex == "" {
return tags, nil
}

filteredTags := make([]string, 0, len(tags))

log.Info().Str("reject_regex", regex).Msg("filtering out tags using regex")

tagReg, err := regexp.Compile(regex)
if err != nil {
log.Error().Err(err).Str("reject_regex", regex).Msg("failed to compile regex")

return filteredTags, err
}

for _, tag := range tags {
if !tagReg.MatchString(tag) {
filteredTags = append(filteredTags, tag)
}
}

return filteredTags, nil
}

// filterTagsBySemver filters tags by checking if they are semver compliant.
func filterTagsBySemver(tags []string, log log.Logger) []string {
filteredTags := []string{}
Expand Down
19 changes: 19 additions & 0 deletions pkg/extensions/sync/content_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func TestGetContentByLocalRepo(t *testing.T) {
func TestFilterTags(t *testing.T) {
allTagsRegex := ".*"
badRegex := "[*"
rejectArchRegex := ".*(x86_64|aarch64|amd64|arm64)$"
semverFalse := false
semverTrue := true
testCases := []struct {
Expand Down Expand Up @@ -234,6 +235,24 @@ func TestFilterTags(t *testing.T) {
filteredTags: []string{},
err: false,
},
{
repo: "alpine",
content: []syncconf.Content{
{Prefix: "**", Tags: &syncconf.Tags{RejectRegex: &allTagsRegex}},
},
tags: []string{"v1", "v2", "v3"},
filteredTags: []string{},
err: false,
},
{
repo: "alpine",
content: []syncconf.Content{
{Prefix: "**", Tags: &syncconf.Tags{RejectRegex: &rejectArchRegex}},
},
tags: []string{"v1", "v2-x86_64", "v3-aarch64"},
filteredTags: []string{"v1"},
err: false,
},
}

Convey("Test FilterTags()", t, func() {
Expand Down

0 comments on commit f618593

Please sign in to comment.