Add isSoft bool in type expr

Signed-off-by: sivaram mothiki <sivaram@opdemand.com>
This commit is contained in:
smothiki 2015-03-18 11:43:31 -06:00
parent dd78f41c32
commit f9e82918b7
3 changed files with 17 additions and 17 deletions

View File

@ -48,7 +48,7 @@ func (f *AffinityFilter) Filter(config *dockerclient.ContainerConfig, nodes []cl
}
}
if len(candidates) == 0 {
if affinity.IsSoft() {
if affinity.isSoft {
return nodes, nil
}
return nil, fmt.Errorf("unable to find a node that satisfies %s%s%s", affinity.key, OPERATORS[affinity.operator], affinity.value)

View File

@ -200,6 +200,12 @@ func TestAffinityFilter(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, result, 1)
result, err = f.Filter(&dockerclient.ContainerConfig{
Env: []string{"affinity:image==~ima~ge-0:tag3"},
}, nodes)
assert.Error(t, err)
assert.Len(t, result, 0)
result, err = f.Filter(&dockerclient.ContainerConfig{
Env: []string{"affinity:image==~image-1:tag3"},
}, nodes)

View File

@ -19,6 +19,7 @@ type expr struct {
key string
operator int
value string
isSoft bool
}
func parseExprs(key string, env []string) ([]expr, error) {
@ -55,7 +56,7 @@ func parseExprs(key string, env []string) ([]expr, error) {
if matched == false {
return nil, fmt.Errorf("Value '%s' is invalid", parts[1])
}
exprs = append(exprs, expr{key: strings.ToLower(parts[0]), operator: i, value: parts[1]})
exprs = append(exprs, expr{key: strings.ToLower(parts[0]), operator: i, value: strings.TrimLeft(parts[1], "~"), isSoft: isSoft(parts[1])})
} else {
exprs = append(exprs, expr{key: strings.ToLower(parts[0]), operator: i})
}
@ -74,24 +75,17 @@ func parseExprs(key string, env []string) ([]expr, error) {
func (e *expr) Match(whats ...string) bool {
var (
pattern string
match bool
err error
startIndex int
pattern string
match bool
err error
)
if e.IsSoft() {
startIndex = 1
} else {
startIndex = 0
}
if e.value[startIndex] == '/' && e.value[len(e.value)-1] == '/' {
if e.value[0] == '/' && e.value[len(e.value)-1] == '/' {
// regexp
pattern = e.value[startIndex+1 : len(e.value)-1]
pattern = e.value[1 : len(e.value)-1]
} else {
// simple match, create the regex for globbing (ex: ub*t* -> ^ub.*t.*$) and match.
pattern = "^" + strings.Replace(strings.Trim(e.value, "~"), "*", ".*", -1) + "$"
pattern = "^" + strings.Replace(e.value, "*", ".*", -1) + "$"
}
for _, what := range whats {
@ -112,8 +106,8 @@ func (e *expr) Match(whats ...string) bool {
return false
}
func (e *expr) IsSoft() bool {
if e.value[0] == '~' {
func isSoft(value string) bool {
if value[0] == '~' {
return true
} else {
return false