forked from docker-archive/classicswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
78 lines (65 loc) · 1.55 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package filter
import (
"errors"
log "github.com/Sirupsen/logrus"
"github.com/docker/swarm/cluster"
"github.com/samalba/dockerclient"
)
// Filter is exported
type Filter interface {
Name() string
// Return a subset of nodes that were accepted by the filtering policy.
Filter(*dockerclient.ContainerConfig, []cluster.Node) ([]cluster.Node, error)
}
var (
filters []Filter
// ErrNotSupported is exported
ErrNotSupported = errors.New("filter not supported")
)
func init() {
filters = []Filter{
&AffinityFilter{},
&HealthFilter{},
&ConstraintFilter{},
&PortFilter{},
&DependencyFilter{},
}
}
// New is exported
func New(names []string) ([]Filter, error) {
var selectedFilters []Filter
for _, name := range names {
found := false
for _, filter := range filters {
if filter.Name() == name {
log.WithField("name", name).Debug("Initializing filter")
selectedFilters = append(selectedFilters, filter)
found = true
break
}
}
if !found {
return nil, ErrNotSupported
}
}
return selectedFilters, nil
}
// ApplyFilters applies a set of filters in batch.
func ApplyFilters(filters []Filter, config *dockerclient.ContainerConfig, nodes []cluster.Node) ([]cluster.Node, error) {
var err error
for _, filter := range filters {
nodes, err = filter.Filter(config, nodes)
if err != nil {
return nil, err
}
}
return nodes, nil
}
// List returns the names of all the available filters
func List() []string {
names := []string{}
for _, filter := range filters {
names = append(names, filter.Name())
}
return names
}