Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

quotas: corrections to Resources.Add and quota apply parsing logic #23894

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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