Files
cozystack/internal/gpu/controller/status.go
Aleksei Sviridkin 5ff3cdaf8c feat(gpu): bind, move and release GPU devices in the leader
The binder is the only writer of a device's placement, so budget and
capacity checks that admission already ran cannot be raced past by two
replicas or two webhook calls. Every event collapses into one pass over
all GPU objects, which serializes the decisions without per-object
locks, and devices are read past the cache so a unit booked in the last
pass is never booked again from a stale view.

A device that carries an unanswered bind request also queues a pass
that only answers requests, so the webhook, which waits seconds, is not
held behind moves, usage records and status of the whole cluster. The
full pass moves held sets off nodes that cannot keep them, rebinds Lost
sets before new orders, and binds new orders onto the fullest node with
room. Finalizers and usage records run with the feature disabled too,
so turning GPU off never hangs a namespace or class deletion.

The devices one VM names move, go Lost and are rebound as a set. Every
pass restores that invariant before it places anything, gathering a set
left partly Lost, by a write that failed half way or by a VMI that ended
next to held members, onto one node or sending all of it to Lost, so
each later step only ever changes whole sets.

A Lost device already counts against its tenant's budgets, so the pod of
its still running VMI is not counted again under a raw GPU key; for node
capacity it is, since the device itself holds no units.

Signed-off-by: Aleksei Sviridkin <f@lex.la>
Assisted-by: LLM
2026-10-21 05:24:04 +03:00

185 lines
6.5 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 controller
import (
"context"
"fmt"
"sort"
"strings"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/cozystack/cozystack/internal/gpu"
"github.com/cozystack/cozystack/internal/gpu/model"
)
// ConditionResourceNameNotPermitted is set on a class whose resource name
// the KubeVirt permittedHostDevices table leaves out while the table is
// set: KubeVirt then never renders the virt-launcher pod and the VMI hangs
// Pending, so the VMI webhook refuses such devices up front.
const ConditionResourceNameNotPermitted = "ResourceNameNotPermitted"
func condition(t string, on bool, reason, message string, w *world, generation int64) metav1.Condition {
c := metav1.Condition{Type: t, Status: metav1.ConditionFalse, Reason: "None", ObservedGeneration: generation,
LastTransitionTime: metav1.NewTime(w.now)}
if on {
c.Status, c.Reason, c.Message = metav1.ConditionTrue, reason, message
}
return c
}
// classStatus publishes free capacity and the class conditions. The status
// is readable by every authenticated user, so conditions name no nodes and
// carry no counts.
func (r *Reconciler) classStatus(ctx context.Context, w *world) []error {
var errs []error
for name, c := range w.classes {
before := c.Status.DeepCopy()
free, maxFree := w.snap.ClassCapacity(name)
c.Status.Free = ptr.To(int32(free))
c.Status.MaxFreePerNode = ptr.To(int32(maxFree))
rn := w.resourceName(name)
lost := false
conflicting := map[string]bool{}
for _, n := range w.snap.Nodes {
if !selects(w.snap.Classes[name], n.Labels) {
continue
}
lost = lost || w.lost[n.Name].covers(rn)
for _, other := range w.snap.Conflicts(n.Name) {
conflicting[other] = true
}
}
inConflict := conflicting[name]
delete(conflicting, name)
others := sortedKeys(conflicting)
for _, cond := range []metav1.Condition{
condition(gpu.ConditionLostCapacity, lost, "NodeLostCapacity",
"a node serving this class has lost capacity; its devices move or wait", w, c.Generation),
condition(gpu.ConditionResourceNameConflict, inConflict, "SharedResourceName",
fmt.Sprintf("a node is selected by this class and by %s with the same resource name and serves none of them", strings.Join(others, ", ")), w, c.Generation),
condition(ConditionResourceNameNotPermitted, w.permitted != nil && !w.permitted.Has(string(rn)), "NotInPermittedHostDevices",
fmt.Sprintf("resource name %s is not in the KubeVirt permittedHostDevices table, so VMs cannot start with devices of this class", rn), w, c.Generation),
} {
meta.SetStatusCondition(&c.Status.Conditions, cond)
}
if equality.Semantic.DeepEqual(before, &c.Status) {
continue
}
errs = append(errs, client.IgnoreNotFound(r.Client.Status().Update(ctx, c)))
}
return errs
}
func selects(c model.Class, labels map[string]string) bool {
for k, v := range c.NodeSelector {
if labels[k] != v {
return false
}
}
return true
}
func sortedKeys(m map[string]bool) []string {
out := make([]string, 0, len(m))
for k := range m {
out = append(out, k)
}
sort.Strings(out)
return out
}
// nodeStatus sets the controller's conditions on GPUNode. It patches only
// conditions, so the agent's inventory in the same status is left alone.
func (r *Reconciler) nodeStatus(ctx context.Context, w *world) []error {
var errs []error
for name, gn := range w.gpuNodes {
// The agent writes the same status; a conditions list replaced from
// a stale copy would drop what it wrote in between.
patch := client.MergeFromWithOptions(gn.DeepCopy(), client.MergeFromWithOptimisticLock{})
before := gn.Status.DeepCopy()
conflicts := w.snap.Conflicts(name)
var lostNames []string
if l := w.lost[name]; l != nil && !l.notReady {
for rn := range l.healthy {
lostNames = append(lostNames, string(rn))
}
sort.Strings(lostNames)
}
lostMsg := "the node has been NotReady for longer than the grace period"
if len(lostNames) > 0 {
lostMsg = "devices of " + strings.Join(lostNames, ", ") + " have been missing or unhealthy for longer than the grace period"
}
meta.SetStatusCondition(&gn.Status.Conditions, condition(gpu.ConditionLostCapacity, w.lost[name] != nil,
"NodeLostCapacity", lostMsg, w, gn.Generation))
meta.SetStatusCondition(&gn.Status.Conditions, condition(gpu.ConditionResourceNameConflict, len(conflicts) > 0,
"SharedResourceName", "GPUClasses "+strings.Join(conflicts, ", ")+" share a resource name on this node, which serves none of them",
w, gn.Generation))
if equality.Semantic.DeepEqual(before, &gn.Status) {
continue
}
errs = append(errs, client.IgnoreNotFound(r.Client.Status().Patch(ctx, gn, patch)))
}
return errs
}
// settings writes the effective GPU resource-name set and the raw-name
// policy for the admission policies and cozystack-api.
func (r *Reconciler) settings(ctx context.Context, w *world) error {
data := map[string]string{
gpu.SettingsKeyResourceNames: model.FormatResourceNames(w.resourceNames),
gpu.SettingsKeyRawResourceNames: string(r.rawPolicy()),
}
cm := &corev1.ConfigMap{}
err := r.Client.Get(ctx, key(gpu.SettingsNamespace, gpu.SettingsConfigMap), cm)
if apierrors.IsNotFound(err) {
cm = &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: gpu.SettingsConfigMap, Namespace: gpu.SettingsNamespace},
Data: data,
}
// A stale cache right after the first create reads NotFound.
if err := r.Client.Create(ctx, cm); !apierrors.IsAlreadyExists(err) {
return err
}
return nil
}
if err != nil {
return err
}
if equality.Semantic.DeepEqual(cm.Data, data) {
return nil
}
cm.Data = data
return r.Client.Update(ctx, cm)
}
func (r *Reconciler) rawPolicy() gpu.RawNamesPolicy {
if r.Config.RawResourceNames == "" {
return gpu.RawNamesAllowed
}
return r.Config.RawResourceNames
}