Skip to content
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

feat(sync): add tag rejectRegex filter #2906

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/cli/server/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,16 @@
}
}

if content.Tags != nil && content.Tags.RejectRegex != nil {
_, err := regexp.Compile(*content.Tags.RejectRegex)
if err != nil {
msg := "sync content reject_regex could not be compiled"
log.Error().Err(glob.ErrBadPattern).Str("reject_regex", *content.Tags.RejectRegex).Msg(msg)

return fmt.Errorf("%w: %s: %s", zerr.ErrBadConfig, msg, *content.Tags.RejectRegex)
}

Check warning on line 1158 in pkg/cli/server/root.go

View check run for this annotation

Codecov / codecov/patch

pkg/cli/server/root.go#L1152-L1158

Added lines #L1152 - L1158 were not covered by tests
}

if content.StripPrefix && !strings.Contains(content.Prefix, "/*") && content.Destination == "/" {
msg := "can not use stripPrefix true and destination '/' without using glob patterns in prefix"
log.Error().Err(zerr.ErrBadConfig).
Expand Down
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would call this "ExcludeRegex"

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 @@
}
}

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

Check warning on line 70 in pkg/extensions/sync/content.go

View check run for this annotation

Codecov / codecov/patch

pkg/extensions/sync/content.go#L69-L70

Added lines #L69 - L70 were not covered by tests
}

if content.Tags.Semver != nil && *content.Tags.Semver {
tags = filterTagsBySemver(tags, cm.log)
}
Expand Down Expand Up @@ -240,6 +247,32 @@
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
}

Check warning on line 254 in pkg/extensions/sync/content.go

View check run for this annotation

Codecov / codecov/patch

pkg/extensions/sync/content.go#L253-L254

Added lines #L253 - L254 were not covered by tests

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
}

Check warning on line 265 in pkg/extensions/sync/content.go

View check run for this annotation

Codecov / codecov/patch

pkg/extensions/sync/content.go#L262-L265

Added lines #L262 - L265 were not covered by tests

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
Loading