forked from docker-archive/classicswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
constraint.go
53 lines (46 loc) · 1.39 KB
/
constraint.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
package filter
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/docker/swarm/cluster"
"github.com/samalba/dockerclient"
)
// ConstraintFilter selects only nodes that match certain labels.
type ConstraintFilter struct {
}
// Name returns the name of the filter
func (f *ConstraintFilter) Name() string {
return "constraint"
}
// Filter is exported
func (f *ConstraintFilter) Filter(config *dockerclient.ContainerConfig, nodes []cluster.Node) ([]cluster.Node, error) {
constraints, err := parseExprs("constraint", config.Env)
if err != nil {
return nil, err
}
for _, constraint := range constraints {
log.Debugf("matching constraint: %s %s %s", constraint.key, OPERATORS[constraint.operator], constraint.value)
candidates := []cluster.Node{}
for _, node := range nodes {
switch constraint.key {
case "node":
// "node" label is a special case pinning a container to a specific node.
if constraint.Match(node.ID(), node.Name()) {
candidates = append(candidates, node)
}
default:
if constraint.Match(node.Labels()[constraint.key]) {
candidates = append(candidates, node)
}
}
}
if len(candidates) == 0 {
if constraint.isSoft {
return nodes, nil
}
return nil, fmt.Errorf("unable to find a node that satisfies %s%s%s", constraint.key, OPERATORS[constraint.operator], constraint.value)
}
nodes = candidates
}
return nodes, nil
}