mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-14 06:06:38 +01:00
feat: include additional info in startup (#809)
This commit is contained in:
parent
5e17ef6014
commit
9fa2fd82a6
7 changed files with 185 additions and 55 deletions
|
|
@ -1,6 +1,9 @@
|
|||
package filters
|
||||
|
||||
import t "github.com/containrrr/watchtower/pkg/types"
|
||||
import (
|
||||
t "github.com/containrrr/watchtower/pkg/types"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// WatchtowerContainersFilter filters only watchtower containers
|
||||
func WatchtowerContainersFilter(c t.FilterableContainer) bool { return c.IsWatchtower() }
|
||||
|
|
@ -68,19 +71,45 @@ func FilterByScope(scope string, baseFilter t.Filter) t.Filter {
|
|||
}
|
||||
|
||||
// BuildFilter creates the needed filter of containers
|
||||
func BuildFilter(names []string, enableLabel bool, scope string) t.Filter {
|
||||
func BuildFilter(names []string, enableLabel bool, scope string) (t.Filter, string) {
|
||||
sb := strings.Builder{}
|
||||
filter := NoFilter
|
||||
filter = FilterByNames(names, filter)
|
||||
|
||||
if len(names) > 0 {
|
||||
sb.WriteString("with name \"")
|
||||
for i, n := range names {
|
||||
sb.WriteString(n)
|
||||
if i < len(names)-1 {
|
||||
sb.WriteString(`" or "`)
|
||||
}
|
||||
}
|
||||
sb.WriteString(`", `)
|
||||
}
|
||||
|
||||
if enableLabel {
|
||||
// If label filtering is enabled, containers should only be considered
|
||||
// if the label is specifically set.
|
||||
filter = FilterByEnableLabel(filter)
|
||||
sb.WriteString("using enable label, ")
|
||||
}
|
||||
if scope != "" {
|
||||
// If a scope has been defined, containers should only be considered
|
||||
// if the scope is specifically set.
|
||||
filter = FilterByScope(scope, filter)
|
||||
sb.WriteString(`in scope "`)
|
||||
sb.WriteString(scope)
|
||||
sb.WriteString(`", `)
|
||||
}
|
||||
filter = FilterByDisabledLabel(filter)
|
||||
return filter
|
||||
|
||||
filterDesc := "Checking all containers (except explicitly disabled with label)"
|
||||
if sb.Len() > 0 {
|
||||
filterDesc = "Only checking containers " + sb.String()
|
||||
|
||||
// Remove the last ", "
|
||||
filterDesc = filterDesc[:len(filterDesc)-2]
|
||||
}
|
||||
|
||||
return filter, filterDesc
|
||||
}
|
||||
|
|
|
|||
|
|
@ -114,7 +114,8 @@ func TestBuildFilter(t *testing.T) {
|
|||
var names []string
|
||||
names = append(names, "test")
|
||||
|
||||
filter := BuildFilter(names, false, "")
|
||||
filter, desc := BuildFilter(names, false, "")
|
||||
assert.Contains(t, desc, "test")
|
||||
|
||||
container := new(mocks.FilterableContainer)
|
||||
container.On("Name").Return("Invalid")
|
||||
|
|
@ -150,7 +151,8 @@ func TestBuildFilterEnableLabel(t *testing.T) {
|
|||
var names []string
|
||||
names = append(names, "test")
|
||||
|
||||
filter := BuildFilter(names, true, "")
|
||||
filter, desc := BuildFilter(names, true, "")
|
||||
assert.Contains(t, desc, "using enable label")
|
||||
|
||||
container := new(mocks.FilterableContainer)
|
||||
container.On("Enabled").Return(false, false)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue