mirror of
https://github.com/containrrr/watchtower.git
synced 2025-09-21 21:30:48 +02:00
feat(filters): Add a flag/env to explicitly exclude containers by name (#1784)
This commit is contained in:
parent
9180e9558e
commit
623f4e67fb
5 changed files with 118 additions and 19 deletions
|
@ -13,7 +13,7 @@ func WatchtowerContainersFilter(c t.FilterableContainer) bool { return c.IsWatch
|
|||
// NoFilter will not filter out any containers
|
||||
func NoFilter(t.FilterableContainer) bool { return true }
|
||||
|
||||
// FilterByNames returns all containers that match the specified name
|
||||
// FilterByNames returns all containers that match one of the specified names
|
||||
func FilterByNames(names []string, baseFilter t.Filter) t.Filter {
|
||||
if len(names) == 0 {
|
||||
return baseFilter
|
||||
|
@ -41,6 +41,22 @@ func FilterByNames(names []string, baseFilter t.Filter) t.Filter {
|
|||
}
|
||||
}
|
||||
|
||||
// FilterByDisableNames returns all containers that don't match any of the specified names
|
||||
func FilterByDisableNames(disableNames []string, baseFilter t.Filter) t.Filter {
|
||||
if len(disableNames) == 0 {
|
||||
return baseFilter
|
||||
}
|
||||
|
||||
return func(c t.FilterableContainer) bool {
|
||||
for _, name := range disableNames {
|
||||
if name == c.Name() || name == c.Name()[1:] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return baseFilter(c)
|
||||
}
|
||||
}
|
||||
|
||||
// FilterByEnableLabel returns all containers that have the enabled label set
|
||||
func FilterByEnableLabel(baseFilter t.Filter) t.Filter {
|
||||
return func(c t.FilterableContainer) bool {
|
||||
|
@ -103,10 +119,11 @@ func FilterByImage(images []string, baseFilter t.Filter) t.Filter {
|
|||
}
|
||||
|
||||
// BuildFilter creates the needed filter of containers
|
||||
func BuildFilter(names []string, enableLabel bool, scope string) (t.Filter, string) {
|
||||
func BuildFilter(names []string, disableNames []string, enableLabel bool, scope string) (t.Filter, string) {
|
||||
sb := strings.Builder{}
|
||||
filter := NoFilter
|
||||
filter = FilterByNames(names, filter)
|
||||
filter = FilterByDisableNames(disableNames, filter)
|
||||
|
||||
if len(names) > 0 {
|
||||
sb.WriteString("which name matches \"")
|
||||
|
@ -118,6 +135,16 @@ func BuildFilter(names []string, enableLabel bool, scope string) (t.Filter, stri
|
|||
}
|
||||
sb.WriteString(`", `)
|
||||
}
|
||||
if len(disableNames) > 0 {
|
||||
sb.WriteString("not named one of \"")
|
||||
for i, n := range disableNames {
|
||||
sb.WriteString(n)
|
||||
if i < len(disableNames)-1 {
|
||||
sb.WriteString(`" or "`)
|
||||
}
|
||||
}
|
||||
sb.WriteString(`", `)
|
||||
}
|
||||
|
||||
if enableLabel {
|
||||
// If label filtering is enabled, containers should only be considered
|
||||
|
|
|
@ -171,7 +171,7 @@ func TestFilterByImage(t *testing.T) {
|
|||
func TestBuildFilter(t *testing.T) {
|
||||
names := []string{"test", "valid"}
|
||||
|
||||
filter, desc := BuildFilter(names, false, "")
|
||||
filter, desc := BuildFilter(names, []string{}, false, "")
|
||||
assert.Contains(t, desc, "test")
|
||||
assert.Contains(t, desc, "or")
|
||||
assert.Contains(t, desc, "valid")
|
||||
|
@ -210,7 +210,7 @@ func TestBuildFilterEnableLabel(t *testing.T) {
|
|||
var names []string
|
||||
names = append(names, "test")
|
||||
|
||||
filter, desc := BuildFilter(names, true, "")
|
||||
filter, desc := BuildFilter(names, []string{}, true, "")
|
||||
assert.Contains(t, desc, "using enable label")
|
||||
|
||||
container := new(mocks.FilterableContainer)
|
||||
|
@ -235,3 +235,52 @@ func TestBuildFilterEnableLabel(t *testing.T) {
|
|||
assert.False(t, filter(container))
|
||||
container.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestBuildFilterDisableContainer(t *testing.T) {
|
||||
filter, desc := BuildFilter([]string{}, []string{"excluded", "notfound"}, false, "")
|
||||
assert.Contains(t, desc, "not named")
|
||||
assert.Contains(t, desc, "excluded")
|
||||
assert.Contains(t, desc, "or")
|
||||
assert.Contains(t, desc, "notfound")
|
||||
|
||||
container := new(mocks.FilterableContainer)
|
||||
container.On("Name").Return("Another")
|
||||
container.On("Enabled").Return(false, false)
|
||||
assert.True(t, filter(container))
|
||||
container.AssertExpectations(t)
|
||||
|
||||
container = new(mocks.FilterableContainer)
|
||||
container.On("Name").Return("AnotherOne")
|
||||
container.On("Enabled").Return(true, true)
|
||||
assert.True(t, filter(container))
|
||||
container.AssertExpectations(t)
|
||||
|
||||
container = new(mocks.FilterableContainer)
|
||||
container.On("Name").Return("test")
|
||||
container.On("Enabled").Return(false, false)
|
||||
assert.True(t, filter(container))
|
||||
container.AssertExpectations(t)
|
||||
|
||||
container = new(mocks.FilterableContainer)
|
||||
container.On("Name").Return("excluded")
|
||||
container.On("Enabled").Return(true, true)
|
||||
assert.False(t, filter(container))
|
||||
container.AssertExpectations(t)
|
||||
|
||||
container = new(mocks.FilterableContainer)
|
||||
container.On("Name").Return("excludedAsSubstring")
|
||||
container.On("Enabled").Return(true, true)
|
||||
assert.True(t, filter(container))
|
||||
container.AssertExpectations(t)
|
||||
|
||||
container = new(mocks.FilterableContainer)
|
||||
container.On("Name").Return("notfound")
|
||||
container.On("Enabled").Return(true, true)
|
||||
assert.False(t, filter(container))
|
||||
container.AssertExpectations(t)
|
||||
|
||||
container = new(mocks.FilterableContainer)
|
||||
container.On("Enabled").Return(false, true)
|
||||
assert.False(t, filter(container))
|
||||
container.AssertExpectations(t)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue