Skip to content

Commit

Permalink
feat: regex container name filtering (#1241)
Browse files Browse the repository at this point in the history
* Allow container name regex filtering

* make regex names backwards compatible

Co-authored-by: Mateusz Drab <mateuszd@mpd.pw>
Co-authored-by: nils måsén <nils@piksel.se>
  • Loading branch information
3 people committed Aug 14, 2022
1 parent 36d3569 commit a429c37
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
20 changes: 17 additions & 3 deletions pkg/filters/filters.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package filters

import (
t "github.com/containrrr/watchtower/pkg/types"
"regexp"
"strings"

t "github.com/containrrr/watchtower/pkg/types"
)

// WatchtowerContainersFilter filters only watchtower containers
Expand All @@ -19,9 +21,21 @@ func FilterByNames(names []string, baseFilter t.Filter) t.Filter {

return func(c t.FilterableContainer) bool {
for _, name := range names {
if (name == c.Name()) || (name == c.Name()[1:]) {
if name == c.Name() || name == c.Name()[1:] {
return baseFilter(c)
}

if re, err := regexp.Compile(name); err == nil {
indices := re.FindStringIndex(c.Name())
if indices == nil {
continue
}
start := indices[0]
end := indices[1]
if start <= 1 && end >= len(c.Name())-1 {
return baseFilter(c)
}
}
}
return false
}
Expand Down Expand Up @@ -95,7 +109,7 @@ func BuildFilter(names []string, enableLabel bool, scope string) (t.Filter, stri
filter = FilterByNames(names, filter)

if len(names) > 0 {
sb.WriteString("with name \"")
sb.WriteString("which name matches \"")
for i, n := range names {
sb.WriteString(n)
if i < len(names)-1 {
Expand Down
30 changes: 26 additions & 4 deletions pkg/filters/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ func TestFilterByNames(t *testing.T) {
container.AssertExpectations(t)
}

func TestFilterByNamesRegex(t *testing.T) {
names := []string{`ba(b|ll)oon`}

filter := FilterByNames(names, NoFilter)
assert.NotNil(t, filter)

container := new(mocks.FilterableContainer)
container.On("Name").Return("balloon")
assert.True(t, filter(container))
container.AssertExpectations(t)

container = new(mocks.FilterableContainer)
container.On("Name").Return("spoon")
assert.False(t, filter(container))
container.AssertExpectations(t)

container = new(mocks.FilterableContainer)
container.On("Name").Return("baboonious")
assert.False(t, filter(container))
container.AssertExpectations(t)
}

func TestFilterByEnableLabel(t *testing.T) {
filter := FilterByEnableLabel(NoFilter)
assert.NotNil(t, filter)
Expand All @@ -68,8 +90,7 @@ func TestFilterByEnableLabel(t *testing.T) {
}

func TestFilterByScope(t *testing.T) {
var scope string
scope = "testscope"
scope := "testscope"

filter := FilterByScope(scope, NoFilter)
assert.NotNil(t, filter)
Expand Down Expand Up @@ -148,11 +169,12 @@ func TestFilterByImage(t *testing.T) {
}

func TestBuildFilter(t *testing.T) {
var names []string
names = append(names, "test")
names := []string{"test", "valid"}

filter, desc := BuildFilter(names, false, "")
assert.Contains(t, desc, "test")
assert.Contains(t, desc, "or")
assert.Contains(t, desc, "valid")

container := new(mocks.FilterableContainer)
container.On("Name").Return("Invalid")
Expand Down

0 comments on commit a429c37

Please sign in to comment.