mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-14 06:06:38 +01: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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue