You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
5 years ago
|
package filters
|
||
7 years ago
|
|
||
5 years ago
|
import t "github.com/containrrr/watchtower/pkg/types"
|
||
7 years ago
|
|
||
|
// WatchtowerContainersFilter filters only watchtower containers
|
||
5 years ago
|
func WatchtowerContainersFilter(c t.FilterableContainer) bool { return c.IsWatchtower() }
|
||
7 years ago
|
|
||
5 years ago
|
// NoFilter will not filter out any containers
|
||
|
func NoFilter(t.FilterableContainer) bool { return true }
|
||
7 years ago
|
|
||
5 years ago
|
// FilterByNames returns all containers that match the specified name
|
||
|
func FilterByNames(names []string, baseFilter t.Filter) t.Filter {
|
||
7 years ago
|
if len(names) == 0 {
|
||
|
return baseFilter
|
||
|
}
|
||
|
|
||
5 years ago
|
return func(c t.FilterableContainer) bool {
|
||
7 years ago
|
for _, name := range names {
|
||
|
if (name == c.Name()) || (name == c.Name()[1:]) {
|
||
|
return baseFilter(c)
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// FilterByEnableLabel returns all containers that have the enabled label set
|
||
|
func FilterByEnableLabel(baseFilter t.Filter) t.Filter {
|
||
5 years ago
|
return func(c t.FilterableContainer) bool {
|
||
7 years ago
|
// If label filtering is enabled, containers should only be considered
|
||
|
// if the label is specifically set.
|
||
|
_, ok := c.Enabled()
|
||
|
if !ok {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
return baseFilter(c)
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// FilterByDisabledLabel returns all containers that have the enabled label set to disable
|
||
|
func FilterByDisabledLabel(baseFilter t.Filter) t.Filter {
|
||
5 years ago
|
return func(c t.FilterableContainer) bool {
|
||
7 years ago
|
enabledLabel, ok := c.Enabled()
|
||
7 years ago
|
if ok && !enabledLabel {
|
||
|
// If the label has been set and it demands a disable
|
||
7 years ago
|
return false
|
||
|
}
|
||
|
|
||
|
return baseFilter(c)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// BuildFilter creates the needed filter of containers
|
||
5 years ago
|
func BuildFilter(names []string, enableLabel bool) t.Filter {
|
||
5 years ago
|
filter := NoFilter
|
||
|
filter = FilterByNames(names, filter)
|
||
7 years ago
|
if enableLabel {
|
||
|
// If label filtering is enabled, containers should only be considered
|
||
|
// if the label is specifically set.
|
||
5 years ago
|
filter = FilterByEnableLabel(filter)
|
||
7 years ago
|
}
|
||
5 years ago
|
filter = FilterByDisabledLabel(filter)
|
||
7 years ago
|
return filter
|
||
|
}
|