1
0
mirror of https://github.com/containrrr/watchtower.git synced 2024-12-03 08:45:43 +02:00

feat: regex container name filtering (#1241)

* 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>
This commit is contained in:
Mateusz Drab 2022-08-14 09:08:45 +01:00 committed by GitHub
parent 36d3569c4a
commit a429c373ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 7 deletions

View File

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

View File

@ -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)
@ -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)
@ -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")