From ba1688ccef63f087f492800fa846b097bf81100a Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Thu, 18 Dec 2014 01:56:13 +0000 Subject: [PATCH] add matching to constraints Signed-off-by: Victor Vieux --- scheduler/filter/label.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/scheduler/filter/label.go b/scheduler/filter/label.go index dbd3f80cb5..041e0a7fe4 100644 --- a/scheduler/filter/label.go +++ b/scheduler/filter/label.go @@ -2,8 +2,10 @@ package filter import ( "fmt" + "regexp" "strings" + log "github.com/Sirupsen/logrus" "github.com/docker/swarm/cluster" "github.com/samalba/dockerclient" ) @@ -27,18 +29,32 @@ func (f *LabelFilter) extractConstraints(env []string) map[string]string { func (f *LabelFilter) Filter(config *dockerclient.ContainerConfig, nodes []*cluster.Node) ([]*cluster.Node, error) { constraints := f.extractConstraints(config.Env) for k, v := range constraints { + regex := "^" + strings.Replace(v, "*", ".*", -1) + "$" + log.Debugf("matching constraint: %s=%s", k, regex) candidates := []*cluster.Node{} for _, node := range nodes { switch k { case "node": // "node" label is a special case pinning a container to a specific node. - if strings.ToLower(node.ID) == v || strings.ToLower(node.Name) == v { + matchedID, err := regexp.MatchString(regex, strings.ToLower(node.ID)) + if err != nil { + log.Error(err) + } + matchedName, err := regexp.MatchString(regex, strings.ToLower(node.Name)) + if err != nil { + log.Error(err) + } + if matchedID || matchedName { candidates = append(candidates, node) } default: // By default match the node labels. if label, ok := node.Labels[k]; ok { - if strings.Contains(strings.ToLower(label), v) { + matched, err := regexp.MatchString(regex, strings.ToLower(label)) + if err != nil { + log.Error(err) + } + if matched { candidates = append(candidates, node) } }