Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit af6c3ff

Browse files
wuqixuanDongsu Park
wuqixuan
authored and
Dongsu Park
committed
fleetd: support operators in metadata
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 #1143
1 parent d520dbb commit af6c3ff

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

job/job.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,17 @@ func (j *Job) RequiredTargetMetadata() map[string]pkg.Set {
284284
fleetMachineMetadata,
285285
} {
286286
for _, valuePair := range j.requirements()[key] {
287-
s := strings.Split(valuePair, "=")
287+
var s []string
288+
for _, sep := range []string{"<=", ">=", "!=", "<", ">"} {
289+
index := strings.Index(valuePair, sep)
290+
if index != -1 {
291+
s = []string{valuePair[0:index], valuePair[index:]}
292+
break
293+
}
294+
}
295+
if s == nil {
296+
s = strings.Split(valuePair, "=")
297+
}
288298

289299
if len(s) != 2 {
290300
continue

machine/machine.go

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
package machine
1616

1717
import (
18+
"strconv"
19+
"strings"
20+
1821
"github.com/coreos/fleet/log"
1922
"github.com/coreos/fleet/pkg"
2023
)
@@ -38,8 +41,81 @@ func HasMetadata(state *MachineState, metadata map[string]pkg.Set) bool {
3841
if values.Contains(local) {
3942
log.Debugf("Local Metadata(%s) meets requirement", key)
4043
} else {
41-
log.Debugf("Local Metadata(%s) does not match requirement", key)
42-
return false
44+
vs := values.Values()
45+
for _, v := range vs {
46+
if index := strings.Index(v, "<="); strings.Contains(v, "<=") && (index == 0) {
47+
need, err1 := strconv.Atoi(v[2:])
48+
have, err2 := strconv.Atoi(local)
49+
if err1 == nil && err2 == nil {
50+
if have <= need {
51+
log.Debugf("Local Metadata(%s) meets requirement", key)
52+
continue
53+
} else {
54+
log.Debugf("Local Metadata(%s) does not match requirement", key)
55+
return false
56+
}
57+
} else {
58+
log.Debugf("Local Metadata(%s) does not match requirement", key)
59+
return false
60+
}
61+
} else if index := strings.Index(v, ">="); strings.Contains(v, ">=") && (index == 0) {
62+
need, err1 := strconv.Atoi(v[2:])
63+
have, err2 := strconv.Atoi(local)
64+
if err1 == nil && err2 == nil {
65+
if have >= need {
66+
log.Debugf("Local Metadata(%s) meets requirement", key)
67+
continue
68+
} else {
69+
log.Debugf("Local Metadata(%s) does not match requirement", key)
70+
return false
71+
}
72+
} else {
73+
log.Debugf("Local Metadata(%s) does not match requirement", key)
74+
return false
75+
}
76+
} else if index := strings.Index(v, ">"); strings.Contains(v, ">") && (index == 0) {
77+
need, err1 := strconv.Atoi(v[1:])
78+
have, err2 := strconv.Atoi(local)
79+
if err1 == nil && err2 == nil {
80+
if have > need {
81+
log.Debugf("Local Metadata(%s) meets requirement", key)
82+
continue
83+
} else {
84+
log.Debugf("Local Metadata(%s) does not match requirement", key)
85+
return false
86+
}
87+
} else {
88+
log.Debugf("Local Metadata(%s) does not match requirement", key)
89+
return false
90+
}
91+
} else if index := strings.Index(v, "<"); strings.Contains(v, "<") && (index == 0) {
92+
need, err1 := strconv.Atoi(v[1:])
93+
have, err2 := strconv.Atoi(local)
94+
if err1 == nil && err2 == nil {
95+
if have < need {
96+
log.Debugf("Local Metadata(%s) meets requirement", key)
97+
continue
98+
} else {
99+
log.Debugf("Local Metadata(%s) does not match requirement", key)
100+
return false
101+
}
102+
} else {
103+
log.Debugf("Local Metadata(%s) does not match requirement", key)
104+
return false
105+
}
106+
} else if index := strings.Index(v, "!="); strings.Contains(v, "!=") && (index == 0) {
107+
if v[2:] != local {
108+
log.Debugf("Local Metadata(%s) meets requirement", key)
109+
continue
110+
} else {
111+
log.Debugf("Local Metadata(%s) does not match requirement", key)
112+
return false
113+
}
114+
} else {
115+
log.Debugf("Local Metadata(%s) does not match requirement", key)
116+
return false
117+
}
118+
}
43119
}
44120
}
45121

0 commit comments

Comments
 (0)