Skip to content

Commit

Permalink
feat: add function to return named slots
Browse files Browse the repository at this point in the history
Signed-off-by: vsoch <[email protected]>
  • Loading branch information
vsoch committed May 6, 2024
1 parent 3e57ee8 commit c98d1d5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
22 changes: 20 additions & 2 deletions examples/nextgen/v1/example1/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,33 @@ func main() {
fmt.Println(string(out))

// Test getting slots
slots := js.GetSlots()
slots, err := js.GetSlots()
if err != nil {
fmt.Printf("error getting slots:%s\n", err)
os.Exit(1)
}
fmt.Printf("Found %d slots\n", len(slots))
for _, slot := range slots {
fmt.Println(slot)
}

slots = js.GetScheduledSlots()
slots, err = js.GetScheduledSlots()
if err != nil {
fmt.Printf("error getting scheduled slots:%s\n", err)
os.Exit(1)
}
fmt.Printf("Found %d scheduled slots\n", len(slots))
for _, slot := range slots {
fmt.Println(slot)
}

named, err := js.GetScheduledNamedSlots()
if err != nil {
fmt.Printf("error getting scheduled named slots:%s\n", err)
os.Exit(1)
}
fmt.Printf("Found %d scheduled named slots\n", len(slots))
for name, _ := range named {
fmt.Println(name)
}
}
59 changes: 46 additions & 13 deletions pkg/nextgen/v1/jobspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1

import (
"encoding/json"
"fmt"
"os"
"reflect"

Expand Down Expand Up @@ -35,7 +36,7 @@ func (js *Jobspec) JobspecToYaml() (string, error) {
}

// GetSlots returns all slots in resources
func (js *Jobspec) GetSlots() []Resource {
func (js *Jobspec) GetSlots() ([]Resource, error) {

emptyResource := Resource{}
slots := []Resource{}
Expand All @@ -50,10 +51,18 @@ func (js *Jobspec) GetSlots() []Resource {

// Slot at the top level already!
if resource.Type == "slot" {

// If the slot doesn't have a label, no go
if resource.Label == "" {
return slots, fmt.Errorf("slots are required to have a label")
}
slots = append(slots, resource)
}
for _, with := range resource.With {
slot := getSlots(with)
slot, err := getSlots(with)
if err != nil {
return slots, err
}
if !reflect.DeepEqual(emptyResource, slot) {
slots = append(slots, slot)
}
Expand All @@ -62,16 +71,19 @@ func (js *Jobspec) GetSlots() []Resource {

// If we found no slots, assume all top level resources are slots
if len(slots) == 0 {
return fauxSlots
return fauxSlots, nil
}
return slots
return slots, nil
}

// GetScheduledSlots returns all slots marked for scheduling
// If none are marked, we assume they all are
func (js *Jobspec) GetScheduledSlots() []Resource {
func (js *Jobspec) GetScheduledSlots() ([]Resource, error) {

slots := js.GetSlots()
slots, err := js.GetSlots()
if err != nil {
return slots, err
}
scheduled := []Resource{}

allTrue := true
Expand All @@ -83,9 +95,24 @@ func (js *Jobspec) GetScheduledSlots() []Resource {
}
// If none marked for scheduling, they all should be
if allTrue {
return slots
return slots, nil
}
return scheduled
return scheduled, nil
}

// GetScheduledNamedSlots returns slots as a lookup by name
func (js *Jobspec) GetScheduledNamedSlots() (map[string]Resource, error) {

slots, err := js.GetScheduledSlots()
named := map[string]Resource{}
if err != nil {
return named, err
}

for _, slot := range slots {
named[slot.Label] = slot
}
return named, nil
}

// A fauxSlot will only be use if we don't have any actual slots
Expand All @@ -100,23 +127,29 @@ func generateFauxSlot(name string, resource Resource) Resource {
}

// getSlots is a recursive helper function that takes resources explicitly
func getSlots(resource Resource) Resource {
func getSlots(resource Resource) (Resource, error) {

emptyResource := Resource{}

// If we find a slot, stop here
if resource.Type == "slot" {
return resource
if resource.Label == "" {
return resource, fmt.Errorf("slots are required to have a label")
}
return resource, nil
}
for _, with := range resource.With {
slot := getSlots(with)
slot, err := getSlots(with)
if err != nil {
return slot, err
}

// If we find a slot
if !reflect.DeepEqual(emptyResource, slot) {
return slot
return slot, nil
}
}
return emptyResource
return emptyResource, nil
}

// JobspectoJson convets back to json string
Expand Down

0 comments on commit c98d1d5

Please sign in to comment.