-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
68 lines (58 loc) · 1.58 KB
/
convert.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
62
63
64
65
66
67
68
package v1
import (
"fmt"
"strings"
)
// NewSimpleJobSpec generates a simple jobspec for nodes, command, tasks, and (optionally) a name
func NewSimpleJobspec(name, command string, nodes, tasks int32) (*Jobspec, error) {
// If no name provided for the slot, use the first
// work of the command
if name == "" {
parts := strings.Split(command, " ")
name = strings.ToLower(parts[0])
}
if nodes < 1 {
return nil, fmt.Errorf("nodes for the job must be >= 1")
}
if command == "" {
return nil, fmt.Errorf("a command must be provided")
}
// The node resource is what we are asking for
nodeResource := Resource{
Type: "node",
Count: nodes,
}
// The slot is where we are doing an assessment for scheduling
slot := Resource{
Type: "slot",
Count: int32(1),
Label: name,
}
// If tasks are defined, this is total tasks across the nodes
// We add to the slot
if tasks != 0 {
taskResource := Resource{
Type: "core",
Count: tasks,
}
slot.With = []Resource{taskResource}
}
// And then the entire resource spec is added to the top level node resource
nodeResource.With = []Resource{slot}
// Resource name matches resources to named set
resourceName := "task-resources"
// Tasks reference the slot and command
// Note: if we need better split can use "github.com/google/shlex"
cmd := strings.Split(command, " ")
taskResource := Task{
Command: cmd,
Replicas: 1,
Resources: resourceName,
}
tasklist := []Task{taskResource}
return &Jobspec{
Version: jobspecVersion,
Tasks: tasklist,
Resources: Resources{resourceName: nodeResource},
}, nil
}