-
Notifications
You must be signed in to change notification settings - Fork 16
/
auto.go
61 lines (47 loc) · 1.17 KB
/
auto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package dynaml
import (
"github.com/mandelsoft/spiff/yaml"
)
var (
refJobs = NewReferenceExpr("", "jobs")
)
type AutoExpr struct {
Path []string
}
func (e AutoExpr) Evaluate(binding Binding, locally bool) (interface{}, EvaluationInfo, bool) {
info := DefaultInfo()
if len(e.Path) == 3 && e.Path[0] == "resource_pools" && e.Path[2] == "size" {
jobs, info, found := refJobs.Evaluate(binding, false)
if !found {
info.Issue = yaml.NewIssue("no jobs found")
return nil, info, false
}
if !isResolvedValue(jobs, binding) {
return e, info, true
}
jobsList, ok := jobs.([]yaml.Node)
if !ok {
return info.Error("jobs must be a list")
}
var size int64
for _, job := range jobsList {
poolName, ok := yaml.FindString(job, binding.GetFeatures(), "resource_pool")
if !ok {
continue
}
if poolName != yaml.PathComponent(e.Path[1]) {
continue
}
instances, ok := yaml.FindInt(job, binding.GetFeatures(), "instances")
if !ok {
return nil, info, false
}
size += instances
}
return size, info, true
}
return info.Error("auto only allowed for size entry in resource pools")
}
func (e AutoExpr) String() string {
return "auto"
}