Skip to content

Commit

Permalink
fleetd: support operators in metadata
Browse files Browse the repository at this point in the history
If define metadata in fleet conf, such as "ram=1024", we can
define the operator in [X-Fleet] unit as below:
[X-Fleet]
MachineMetadata=ram>=2048
The operators have been supported: "<=", ">=", "!=", "<", ">", "="
If the operatior are "<=", ">=", "!=", "<", ">", the value should
be integer, otherwise, the unit will never be launched.

Fixes coreos#1143
  • Loading branch information
wuqixuan authored and Dongsu Park committed Jun 3, 2016
1 parent d738969 commit a2267f3
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
12 changes: 11 additions & 1 deletion job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,17 @@ func (j *Job) RequiredTargetMetadata() map[string]pkg.Set {
fleetMachineMetadata,
} {
for _, valuePair := range j.requirements()[key] {
s := strings.Split(valuePair, "=")
var s []string
for _, sep := range []string{"<=", ">=", "!=", "<", ">"} {
index := strings.Index(valuePair, sep)
if index != -1 {
s = []string{valuePair[0:index], valuePair[index:]}
break
}
}
if s == nil {
s = strings.Split(valuePair, "=")
}

if len(s) != 2 {
continue
Expand Down
80 changes: 78 additions & 2 deletions machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package machine

import (
"strconv"
"strings"

"github.com/coreos/fleet/log"
"github.com/coreos/fleet/pkg"
)
Expand All @@ -38,8 +41,81 @@ func HasMetadata(state *MachineState, metadata map[string]pkg.Set) bool {
if values.Contains(local) {
log.Debugf("Local Metadata(%s) meets requirement", key)
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
vs := values.Values()
for _, v := range vs {
if index := strings.Index(v, "<="); strings.Contains(v, "<=") && (index == 0) {
need, err1 := strconv.Atoi(v[2:])
have, err2 := strconv.Atoi(local)
if err1 == nil && err2 == nil {
if have <= need {
log.Debugf("Local Metadata(%s) meets requirement", key)
continue
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else if index := strings.Index(v, ">="); strings.Contains(v, ">=") && (index == 0) {
need, err1 := strconv.Atoi(v[2:])
have, err2 := strconv.Atoi(local)
if err1 == nil && err2 == nil {
if have >= need {
log.Debugf("Local Metadata(%s) meets requirement", key)
continue
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else if index := strings.Index(v, ">"); strings.Contains(v, ">") && (index == 0) {
need, err1 := strconv.Atoi(v[1:])
have, err2 := strconv.Atoi(local)
if err1 == nil && err2 == nil {
if have > need {
log.Debugf("Local Metadata(%s) meets requirement", key)
continue
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else if index := strings.Index(v, "<"); strings.Contains(v, "<") && (index == 0) {
need, err1 := strconv.Atoi(v[1:])
have, err2 := strconv.Atoi(local)
if err1 == nil && err2 == nil {
if have < need {
log.Debugf("Local Metadata(%s) meets requirement", key)
continue
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else if index := strings.Index(v, "!="); strings.Contains(v, "!=") && (index == 0) {
if v[2:] != local {
log.Debugf("Local Metadata(%s) meets requirement", key)
continue
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
} else {
log.Debugf("Local Metadata(%s) does not match requirement", key)
return false
}
}
}
}

Expand Down

0 comments on commit a2267f3

Please sign in to comment.