Skip to content

Commit

Permalink
quotas: corrections to Resources.Add and quota apply parsing logic (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pkazmierczak authored Sep 9, 2024
1 parent cbc27e2 commit 2e6ccf8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/23894.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
quotas (Enterprise): Added the possibility to set device count limits
```
15 changes: 13 additions & 2 deletions command/quota_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,15 @@ func parseQuotaResource(result *api.Resources, list *ast.ObjectList) error {
}

func parseDeviceResource(result *[]*api.RequestedDevice, list *ast.ObjectList) error {
for _, o := range list.Elem().Items {
for idx, o := range list.Items {
if l := len(o.Keys); l == 0 {
return multierror.Prefix(fmt.Errorf("missing device name"), fmt.Sprintf("resources, device[%d]->", idx))
} else if l > 1 {
return multierror.Prefix(fmt.Errorf("only one name may be specified"), fmt.Sprintf("resources, device[%d]->", idx))
}

name := o.Keys[0].Token.Value().(string)

// Check for invalid keys
valid := []string{
"name",
Expand All @@ -310,12 +318,15 @@ func parseDeviceResource(result *[]*api.RequestedDevice, list *ast.ObjectList) e
return err
}

// Set the name
var device api.RequestedDevice
device.Name = name

var m map[string]interface{}
if err := hcl.DecodeObject(&m, o.Val); err != nil {
return err
}

var device api.RequestedDevice
if err := mapstructure.WeakDecode(m, &device); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion command/quota_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ limit {
memory = 1000
memory_max = 1000
device "nvidia/gpu/1080ti" {
count = 1,
count = 1
}
}
variables_limit = 1000
Expand Down
15 changes: 15 additions & 0 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2713,6 +2713,21 @@ func (r *Resources) Add(delta *Resources) {
r.Networks[idx].Add(n)
}
}

if r.Devices == nil && delta.Devices != nil {
r.Devices = make(ResourceDevices, 0)
}
for _, dd := range delta.Devices {
idx := slices.IndexFunc(r.Devices, func(d *RequestedDevice) bool { return d.Name == dd.Name })

// means it's not found
if idx < 0 {
r.Devices = append(r.Devices, dd)
continue
}

r.Devices[idx].Count += dd.Count
}
}

// GoString returns the string representation of the Resources struct.
Expand Down
12 changes: 12 additions & 0 deletions nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3821,6 +3821,12 @@ func TestResource_Add(t *testing.T) {
ReservedPorts: []Port{{"web", 80, 0, ""}},
},
},
Devices: []*RequestedDevice{
{
Name: "nvidia/gpu/Tesla M60",
Count: 1,
},
},
}

r1.Add(r2)
Expand All @@ -3838,6 +3844,12 @@ func TestResource_Add(t *testing.T) {
ReservedPorts: []Port{{"ssh", 22, 0, ""}, {"web", 80, 0, ""}},
},
},
Devices: []*RequestedDevice{
{
Name: "nvidia/gpu/Tesla M60",
Count: 1,
},
},
}

must.Eq(t, expect, r1)
Expand Down

0 comments on commit 2e6ccf8

Please sign in to comment.