mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-14 06:06:38 +01: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:
parent
36d3569c4a
commit
a429c373ff
2 changed files with 43 additions and 7 deletions
|
|
@ -1,8 +1,10 @@
|
||||||
package filters
|
package filters
|
||||||
|
|
||||||
import (
|
import (
|
||||||
t "github.com/containrrr/watchtower/pkg/types"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
t "github.com/containrrr/watchtower/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WatchtowerContainersFilter filters only watchtower containers
|
// WatchtowerContainersFilter filters only watchtower containers
|
||||||
|
|
@ -19,9 +21,21 @@ func FilterByNames(names []string, baseFilter t.Filter) t.Filter {
|
||||||
|
|
||||||
return func(c t.FilterableContainer) bool {
|
return func(c t.FilterableContainer) bool {
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
if (name == c.Name()) || (name == c.Name()[1:]) {
|
if name == c.Name() || name == c.Name()[1:] {
|
||||||
return baseFilter(c)
|
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
|
return false
|
||||||
}
|
}
|
||||||
|
|
@ -95,7 +109,7 @@ func BuildFilter(names []string, enableLabel bool, scope string) (t.Filter, stri
|
||||||
filter = FilterByNames(names, filter)
|
filter = FilterByNames(names, filter)
|
||||||
|
|
||||||
if len(names) > 0 {
|
if len(names) > 0 {
|
||||||
sb.WriteString("with name \"")
|
sb.WriteString("which name matches \"")
|
||||||
for i, n := range names {
|
for i, n := range names {
|
||||||
sb.WriteString(n)
|
sb.WriteString(n)
|
||||||
if i < len(names)-1 {
|
if i < len(names)-1 {
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,28 @@ func TestFilterByNames(t *testing.T) {
|
||||||
container.AssertExpectations(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) {
|
func TestFilterByEnableLabel(t *testing.T) {
|
||||||
filter := FilterByEnableLabel(NoFilter)
|
filter := FilterByEnableLabel(NoFilter)
|
||||||
assert.NotNil(t, filter)
|
assert.NotNil(t, filter)
|
||||||
|
|
@ -68,8 +90,7 @@ func TestFilterByEnableLabel(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFilterByScope(t *testing.T) {
|
func TestFilterByScope(t *testing.T) {
|
||||||
var scope string
|
scope := "testscope"
|
||||||
scope = "testscope"
|
|
||||||
|
|
||||||
filter := FilterByScope(scope, NoFilter)
|
filter := FilterByScope(scope, NoFilter)
|
||||||
assert.NotNil(t, filter)
|
assert.NotNil(t, filter)
|
||||||
|
|
@ -148,11 +169,12 @@ func TestFilterByImage(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildFilter(t *testing.T) {
|
func TestBuildFilter(t *testing.T) {
|
||||||
var names []string
|
names := []string{"test", "valid"}
|
||||||
names = append(names, "test")
|
|
||||||
|
|
||||||
filter, desc := BuildFilter(names, false, "")
|
filter, desc := BuildFilter(names, false, "")
|
||||||
assert.Contains(t, desc, "test")
|
assert.Contains(t, desc, "test")
|
||||||
|
assert.Contains(t, desc, "or")
|
||||||
|
assert.Contains(t, desc, "valid")
|
||||||
|
|
||||||
container := new(mocks.FilterableContainer)
|
container := new(mocks.FilterableContainer)
|
||||||
container.On("Name").Return("Invalid")
|
container.On("Name").Return("Invalid")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue