forked from docker-archive/classicswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaffinity.go
57 lines (51 loc) · 1.44 KB
/
affinity.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
package filter
import (
"fmt"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/docker/swarm/cluster"
"github.com/samalba/dockerclient"
)
// AffinityFilter selects only nodes based on other containers on the node.
type AffinityFilter struct {
}
func (f *AffinityFilter) Filter(config *dockerclient.ContainerConfig, nodes []*cluster.Node) ([]*cluster.Node, error) {
affinities, err := parseExprs("affinity", config.Env)
if err != nil {
return nil, err
}
for _, affinity := range affinities {
log.Debugf("matching affinity: %s%s%s", affinity.key, OPERATORS[affinity.operator], affinity.value)
candidates := []*cluster.Node{}
for _, node := range nodes {
switch affinity.key {
case "container":
for _, container := range node.Containers() {
if affinity.Match(container.Id, container.Names[0]) {
candidates = append(candidates, node)
break
}
}
case "image":
done:
for _, image := range node.Images() {
if affinity.Match(image.Id) {
candidates = append(candidates, node)
break
}
for _, tag := range image.RepoTags {
if affinity.Match(tag, strings.Split(tag, ":")[0]) {
candidates = append(candidates, node)
break done
}
}
}
}
}
if len(candidates) == 0 {
return nil, fmt.Errorf("unable to find a node that satisfies %s%s%s", affinity.key, OPERATORS[affinity.operator], affinity.value)
}
nodes = candidates
}
return nodes, nil
}