-
Notifications
You must be signed in to change notification settings - Fork 618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only create global service tasks on nodes that satisfy the constraints #1570
Merged
aaronlehmann
merged 3 commits into
moby:master
from
aaronlehmann:global-service-orchestration
Oct 6, 2016
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package constraint | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/docker/swarmkit/api" | ||
) | ||
|
||
const ( | ||
eq = iota | ||
noteq | ||
|
||
nodeLabelPrefix = "node.labels." | ||
engineLabelPrefix = "engine.labels." | ||
) | ||
|
||
var ( | ||
alphaNumeric = regexp.MustCompile(`^(?i)[a-z_][a-z0-9\-_.]+$`) | ||
// value can be alphanumeric and some special characters. it shouldn't container | ||
// current or future operators like '>, <, ~', etc. | ||
valuePattern = regexp.MustCompile(`^(?i)[a-z0-9:\-_\s\.\*\(\)\?\+\[\]\\\^\$\|\/]+$`) | ||
|
||
// operators defines list of accepted operators | ||
operators = []string{"==", "!="} | ||
) | ||
|
||
// Constraint defines a constraint. | ||
type Constraint struct { | ||
key string | ||
operator int | ||
exp string | ||
} | ||
|
||
// Parse parses list of constraints. | ||
func Parse(env []string) ([]Constraint, error) { | ||
exprs := []Constraint{} | ||
for _, e := range env { | ||
found := false | ||
// each expr is in the form of "key op value" | ||
for i, op := range operators { | ||
if !strings.Contains(e, op) { | ||
continue | ||
} | ||
// split with the op | ||
parts := strings.SplitN(e, op, 2) | ||
|
||
if len(parts) < 2 { | ||
return nil, fmt.Errorf("invalid expr: %s", e) | ||
} | ||
|
||
part0 := strings.TrimSpace(parts[0]) | ||
// validate key | ||
matched := alphaNumeric.MatchString(part0) | ||
if matched == false { | ||
return nil, fmt.Errorf("key '%s' is invalid", part0) | ||
} | ||
|
||
part1 := strings.TrimSpace(parts[1]) | ||
|
||
// validate Value | ||
matched = valuePattern.MatchString(part1) | ||
if matched == false { | ||
return nil, fmt.Errorf("value '%s' is invalid", part1) | ||
} | ||
// TODO(dongluochen): revisit requirements to see if globing or regex are useful | ||
exprs = append(exprs, Constraint{key: part0, operator: i, exp: part1}) | ||
|
||
found = true | ||
break // found an op, move to next entry | ||
} | ||
if !found { | ||
return nil, fmt.Errorf("constraint expected one operator from %s", strings.Join(operators, ", ")) | ||
} | ||
} | ||
return exprs, nil | ||
} | ||
|
||
// Match checks if the Constraint matches the target strings. | ||
func (c *Constraint) Match(whats ...string) bool { | ||
var match bool | ||
|
||
// full string match | ||
for _, what := range whats { | ||
// case insensitive compare | ||
if strings.EqualFold(c.exp, what) { | ||
match = true | ||
break | ||
} | ||
} | ||
|
||
switch c.operator { | ||
case eq: | ||
return match | ||
case noteq: | ||
return !match | ||
} | ||
|
||
return false | ||
} | ||
|
||
// NodeMatches returns true if the node satisfies the given constraints. | ||
func NodeMatches(constraints []Constraint, n *api.Node) bool { | ||
for _, constraint := range constraints { | ||
switch { | ||
case strings.EqualFold(constraint.key, "node.id"): | ||
if !constraint.Match(n.ID) { | ||
return false | ||
} | ||
case strings.EqualFold(constraint.key, "node.hostname"): | ||
// if this node doesn't have hostname | ||
// it's equivalent to match an empty hostname | ||
// where '==' would fail, '!=' matches | ||
if n.Description == nil { | ||
if !constraint.Match("") { | ||
return false | ||
} | ||
continue | ||
} | ||
if !constraint.Match(n.Description.Hostname) { | ||
return false | ||
} | ||
case strings.EqualFold(constraint.key, "node.role"): | ||
if !constraint.Match(n.Spec.Role.String()) { | ||
return false | ||
} | ||
|
||
// node labels constraint in form like 'node.labels.key==value' | ||
case len(constraint.key) > len(nodeLabelPrefix) && strings.EqualFold(constraint.key[:len(nodeLabelPrefix)], nodeLabelPrefix): | ||
if n.Spec.Annotations.Labels == nil { | ||
if !constraint.Match("") { | ||
return false | ||
} | ||
continue | ||
} | ||
label := constraint.key[len(nodeLabelPrefix):] | ||
// label itself is case sensitive | ||
val := n.Spec.Annotations.Labels[label] | ||
if !constraint.Match(val) { | ||
return false | ||
} | ||
|
||
// engine labels constraint in form like 'engine.labels.key!=value' | ||
case len(constraint.key) > len(engineLabelPrefix) && strings.EqualFold(constraint.key[:len(engineLabelPrefix)], engineLabelPrefix): | ||
if n.Description == nil || n.Description.Engine == nil || n.Description.Engine.Labels == nil { | ||
if !constraint.Match("") { | ||
return false | ||
} | ||
continue | ||
} | ||
label := constraint.key[len(engineLabelPrefix):] | ||
val := n.Description.Engine.Labels[label] | ||
if !constraint.Match(val) { | ||
return false | ||
} | ||
default: | ||
// key doesn't match predefined syntax | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Shouldn't this be a subpackage of
scheduler
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would that make sense, since now
orchestrator
uses it as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's a moot point. To me it felt like it was part of the scheduler (which at the end of the day is the component responsible to place tasks onto nodes), and it turns out another component (global orchestrator) wants to use a piece of the scheduler. I don't think the scheduler and orchestrator are sharing a common piece, more like the orchestrator wants to use the scheduler for some stuff.
But it's a minor personal preference, no objective arguments