mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-13 21:56:38 +01:00
feat: add support for "none" scope (#1800)
This commit is contained in:
parent
40b8c77100
commit
dd54055143
4 changed files with 92 additions and 19 deletions
|
|
@ -86,13 +86,14 @@ func FilterByDisabledLabel(baseFilter t.Filter) t.Filter {
|
|||
|
||||
// FilterByScope returns all containers that belongs to a specific scope
|
||||
func FilterByScope(scope string, baseFilter t.Filter) t.Filter {
|
||||
if scope == "" {
|
||||
return baseFilter
|
||||
}
|
||||
|
||||
return func(c t.FilterableContainer) bool {
|
||||
containerScope, ok := c.Scope()
|
||||
if ok && containerScope == scope {
|
||||
containerScope, containerHasScope := c.Scope()
|
||||
|
||||
if !containerHasScope || containerScope == "" {
|
||||
containerScope = "none"
|
||||
}
|
||||
|
||||
if containerScope == scope {
|
||||
return baseFilter(c)
|
||||
}
|
||||
|
||||
|
|
@ -152,7 +153,13 @@ func BuildFilter(names []string, disableNames []string, enableLabel bool, scope
|
|||
filter = FilterByEnableLabel(filter)
|
||||
sb.WriteString("using enable label, ")
|
||||
}
|
||||
if scope != "" {
|
||||
|
||||
if scope == "none" {
|
||||
// If a scope has explicitly defined as "none", containers should only be considered
|
||||
// if they do not have a scope defined, or if it's explicitly set to "none".
|
||||
filter = FilterByScope(scope, filter)
|
||||
sb.WriteString(`without a scope, "`)
|
||||
} else if scope != "" {
|
||||
// If a scope has been defined, containers should only be considered
|
||||
// if the scope is specifically set.
|
||||
filter = FilterByScope(scope, filter)
|
||||
|
|
|
|||
|
|
@ -111,6 +111,53 @@ func TestFilterByScope(t *testing.T) {
|
|||
container.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestFilterByNoneScope(t *testing.T) {
|
||||
scope := "none"
|
||||
|
||||
filter := FilterByScope(scope, NoFilter)
|
||||
assert.NotNil(t, filter)
|
||||
|
||||
container := new(mocks.FilterableContainer)
|
||||
container.On("Scope").Return("anyscope", true)
|
||||
assert.False(t, filter(container))
|
||||
container.AssertExpectations(t)
|
||||
|
||||
container = new(mocks.FilterableContainer)
|
||||
container.On("Scope").Return("", false)
|
||||
assert.True(t, filter(container))
|
||||
container.AssertExpectations(t)
|
||||
|
||||
container = new(mocks.FilterableContainer)
|
||||
container.On("Scope").Return("", true)
|
||||
assert.True(t, filter(container))
|
||||
container.AssertExpectations(t)
|
||||
|
||||
container = new(mocks.FilterableContainer)
|
||||
container.On("Scope").Return("none", true)
|
||||
assert.True(t, filter(container))
|
||||
container.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestBuildFilterNoneScope(t *testing.T) {
|
||||
filter, desc := BuildFilter(nil, nil, false, "none")
|
||||
|
||||
assert.Contains(t, desc, "without a scope")
|
||||
|
||||
scoped := new(mocks.FilterableContainer)
|
||||
scoped.On("Enabled").Return(false, false)
|
||||
scoped.On("Scope").Return("anyscope", true)
|
||||
|
||||
unscoped := new(mocks.FilterableContainer)
|
||||
unscoped.On("Enabled").Return(false, false)
|
||||
unscoped.On("Scope").Return("", false)
|
||||
|
||||
assert.False(t, filter(scoped))
|
||||
assert.True(t, filter(unscoped))
|
||||
|
||||
scoped.AssertExpectations(t)
|
||||
unscoped.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestFilterByDisabledLabel(t *testing.T) {
|
||||
filter := FilterByDisabledLabel(NoFilter)
|
||||
assert.NotNil(t, filter)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue