Files
cozystack/internal/gpu/model/class.go
Aleksei Sviridkin 46ff481549 feat(gpu): add the shared GPU placement and budget model
The binder re-checks what admission checked, and cozystack-api shows the
remaining budget a later order is checked against; computing these in
three places would let them disagree. The model is pure, so each caller
builds a snapshot from its own cache.

GPU budgets cap the whole subtree of the tenant that declares them,
walked over the namespace ancestry and independent of the tenant quota
pools. A raw GPU key blocks a class until the same tenant also declares
a GPU key, and then caps claims and raw-name pods together. Pod usage
follows the scheduler's arithmetic, sidecars included, so the binder
never books a unit a raw-name pod holds.

A node serves a class only when it is schedulable, Ready and carries no
NoSchedule or NoExecute taint the class does not tolerate. The
tolerations are the class's, because a claim is bound before the pod
that will use it exists. The unschedulable, not-ready and unreachable
taints are left to the first two checks, and a taint never counts as a
cordon. Gt and Lt tolerations do not count, since the scheduler ignores
them where their feature gate is off.

Template claim names are derived from the VMI and shortened with a hash
when the VMI name leaves no room, so they stay valid subdomains.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
2026-10-23 03:40:49 +03:00

85 lines
2.2 KiB
Go

/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package model
import (
"sort"
"strings"
gpuv1alpha1 "github.com/cozystack/cozystack/api/gpu/v1alpha1"
)
// EffectiveMaxCount returns how many GPUs of the class one VM may take,
// and false when nothing beyond one node's GPUs limits it.
func EffectiveMaxCount(spec *gpuv1alpha1.GPUClassSpec) (int32, bool) {
if spec.MaxCount != nil {
return *spec.MaxCount, true
}
if spec.Mode == gpuv1alpha1.GPUModeVGPU {
return 1, true
}
return 0, false
}
// ResourceNames is the effective GPU resource-name set, sorted: the
// classes' resource names, configured and the names node agents report.
// configured carries the hoster's list and the NVIDIA (10DE) and mdev
// names of the KubeVirt permittedHostDevices table; none replaces another.
func ResourceNames(classes []gpuv1alpha1.GPUClass, configured []string, nodes []gpuv1alpha1.GPUNode) []string {
set := map[string]struct{}{}
add := func(n string) {
if n != "" {
set[n] = struct{}{}
}
}
for i := range classes {
if dp := classes[i].Spec.Source.DevicePlugin; dp != nil {
add(dp.ResourceName)
}
}
for _, n := range configured {
add(n)
}
for i := range nodes {
for _, n := range nodes[i].Status.ResourceNames {
add(n)
}
}
out := make([]string, 0, len(set))
for n := range set {
out = append(out, n)
}
sort.Strings(out)
return out
}
// FormatResourceNames renders names for the settings ConfigMap.
func FormatResourceNames(names []string) string {
return strings.Join(names, ",")
}
// ParseResourceNames reads the settings ConfigMap value.
func ParseResourceNames(s string) []string {
var out []string
for _, n := range strings.Split(s, ",") {
if n = strings.TrimSpace(n); n != "" {
out = append(out, n)
}
}
return out
}