|
| 1 | +package v1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "sigs.k8s.io/yaml" |
| 9 | + |
| 10 | + "github.com/compspec/jobspec-go/pkg/schema" |
| 11 | +) |
| 12 | + |
| 13 | +// LoadJobspecYaml loads a jobspec from a yaml file path |
| 14 | +func LoadJobspecYaml(yamlFile string) (*Jobspec, error) { |
| 15 | + js := Jobspec{} |
| 16 | + file, err := os.ReadFile(yamlFile) |
| 17 | + if err != nil { |
| 18 | + return &js, err |
| 19 | + } |
| 20 | + |
| 21 | + err = yaml.Unmarshal([]byte(file), &js) |
| 22 | + if err != nil { |
| 23 | + return &js, err |
| 24 | + } |
| 25 | + return &js, nil |
| 26 | +} |
| 27 | + |
| 28 | +// JobspectoYaml convets back to yaml (as string) |
| 29 | +func (js *Jobspec) JobspecToYaml() (string, error) { |
| 30 | + out, err := yaml.Marshal(js) |
| 31 | + if err != nil { |
| 32 | + return "", err |
| 33 | + } |
| 34 | + return string(out), nil |
| 35 | +} |
| 36 | + |
| 37 | +// GetResources to get resource groups across the jobspec |
| 38 | +// this is intended for graph scheduling |
| 39 | +func (js *Jobspec) GetResources(data []byte) ([]Resource, error) { |
| 40 | + |
| 41 | + // We assume every discovered resource is a unique satisfy |
| 42 | + resources := []Resource{} |
| 43 | + |
| 44 | + // Make sure all task and group resources are known |
| 45 | + for _, task := range js.Tasks { |
| 46 | + r, err := js.getResources(task) |
| 47 | + if err != nil { |
| 48 | + return resources, err |
| 49 | + } |
| 50 | + resources = append(resources, r) |
| 51 | + } |
| 52 | + for _, group := range js.Groups { |
| 53 | + r, err := js.getResources(group) |
| 54 | + if err != nil { |
| 55 | + return resources, err |
| 56 | + } |
| 57 | + resources = append(resources, r) |
| 58 | + } |
| 59 | + return resources, nil |
| 60 | +} |
| 61 | + |
| 62 | +// getResources unwraps resources. If there is a named string, we assume |
| 63 | +// in reference to a named resource group. We will need a strategy to combine |
| 64 | +// these intelligently when we ask for a match - right now just assuming |
| 65 | +// separate groups |
| 66 | +func (js *Jobspec) getResources(resources interface{}) (Resource, error) { |
| 67 | + resource := Resource{} |
| 68 | + switch v := resources.(type) { |
| 69 | + case string: |
| 70 | + resourceKey := resources.(string) |
| 71 | + spec, ok := js.Resources[resourceKey] |
| 72 | + if !ok { |
| 73 | + return resource, fmt.Errorf("task is missing resource") |
| 74 | + } |
| 75 | + return spec, nil |
| 76 | + case Resource: |
| 77 | + return resources.(Resource), nil |
| 78 | + default: |
| 79 | + return resource, fmt.Errorf("type %s is unknown", v) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// JobspectoJson convets back to json string |
| 84 | +func (js *Jobspec) JobspecToJson() (string, error) { |
| 85 | + out, err := json.MarshalIndent(js, "", " ") |
| 86 | + if err != nil { |
| 87 | + return "", err |
| 88 | + } |
| 89 | + return string(out), nil |
| 90 | +} |
| 91 | + |
| 92 | +// Validate converts to bytes and validate with jsonschema |
| 93 | +func (js *Jobspec) Validate() (bool, error) { |
| 94 | + |
| 95 | + // Get back into bytes form |
| 96 | + out, err := yaml.Marshal(js) |
| 97 | + if err != nil { |
| 98 | + return false, err |
| 99 | + } |
| 100 | + // Validate the jobspec |
| 101 | + return schema.Validate(out, schema.SchemaUrl, Schema) |
| 102 | + |
| 103 | +} |
| 104 | + |
| 105 | +// Helper function to get a job name, derived from the command |
| 106 | +func (js *Jobspec) GetJobName() string { |
| 107 | + |
| 108 | + // Generic name to fall back tp |
| 109 | + name := "app" |
| 110 | + if js.Name != "" { |
| 111 | + name = js.Name |
| 112 | + } |
| 113 | + return name |
| 114 | +} |
0 commit comments