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

A VM starts through its launcher pod, which the pod webhook holds at a
scheduling gate. The pass creates the VMI's template claims, gathers
the whole set onto one node that every class of the set tolerates and
has room on, then pins the pod there and removes the gate in one
update, since the API server lets a pod's node affinity change only
while the pod is gated. A set that cannot be placed keeps its pod
gated with a condition on its claims and a Warning event on the VMI,
once per change of reason, so the VM waits for capacity instead of
being refused into KubeVirt's backoff. The set goes only to a node the
pod's own node selector and required affinity accept and, when the VMI
has a volume bound to a node's PersistentVolume, that volume reaches.
Gated pods go before moves and new orders.

A pin cannot be undone, so a pinned pod its node refuses for lack of
resources is deleted and the node is skipped for that set for ten
minutes, or until its allocatable, taints or readiness change. The
template claims of the set go with its failed VMI, so the controller
also keeps the rejection in memory for the VM's next VMI. Only a VMI
whose pod is already pinned is deleted to free a claim whose node
cannot keep it; a VMI whose pod is gated has its set gathered instead.

The full pass also 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. Each usage record is owned by its claim and sits under
no WorkloadMonitor, so no tenant label can add or drop one, and
garbage collection removes it with the claim. Without a monitor label
the Workload controller would take such a record for an orphan, so it
now leaves a record owned by a GPUClaim to that owner.

The claims 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 claim 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 claim itself holds no units. A pod is a
VMI's launcher only when the VMI lists it in status.activePods,
because whoever creates a pod sets its owner reference. The KubeVirt
watches start once KubeVirt serves its kinds, whose CRDs a fresh
install adds after this controller starts.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
2026-10-26 11:22:36 +03:00

114 lines
3.7 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"
"errors"
"slices"
gpuv1alpha1 "github.com/cozystack/cozystack/api/gpu/v1alpha1"
)
// unattachedSetMember selects the claims a VM's set moves, goes Lost and
// is rebound with: those no VMI uses. An attached claim follows its VMI.
func unattachedSetMember(d *gpuv1alpha1.GPUClaim) bool {
return (holds(d) || d.Status.Phase == gpuv1alpha1.GPUClaimLost) &&
d.Status.AttachedTo == nil && d.DeletionTimestamp.IsZero()
}
func allLost(set []*gpuv1alpha1.GPUClaim) bool {
for _, d := range set {
if d.Status.Phase != gpuv1alpha1.GPUClaimLost {
return false
}
}
return true
}
// mixesLost reports a set that breaks the set invariant.
func mixesLost(set []*gpuv1alpha1.GPUClaim) bool {
lost := 0
for _, d := range set {
if d.Status.Phase == gpuv1alpha1.GPUClaimLost {
lost++
}
}
return lost > 0 && lost < len(set)
}
// normalizeSets restores the set invariant, which holds after every pass
// in which no write failed:
//
// The unattached claims one VM names are either all Lost or all held.
// A set that had a Lost member is held on one node.
//
// Held sets may otherwise sit on several nodes, as separate orders bind
// apart; the cordon-only stay and a Stay pin keep a held set in place.
// Every placement step changes a set as a whole. A write that failed half
// way through a set breaks the invariant, and so does a VMI that ended
// and left its Lost claim unattached next to held members. This step runs
// right after attachments, before any placement, and gathers such a set
// onto one node with room for all of it or sends all of it to Lost.
func (r *Reconciler) normalizeSets(ctx context.Context, w *world) []error {
var errs []error
for _, set := range w.vmSets(unattachedSetMember) {
if w.aborted {
break
}
if !mixesLost(set) {
continue
}
if target := w.pick(set); target != "" {
before := map[*gpuv1alpha1.GPUClaim]string{}
wasLost := map[*gpuv1alpha1.GPUClaim]bool{}
for _, d := range set {
before[d] = string(d.Status.Phase) + "@" + d.Status.NodeName
wasLost[d] = d.Status.Phase == gpuv1alpha1.GPUClaimLost
}
w.placeOn(set, target)
// Held members first: until the last Lost member is written the
// set still mixes Lost and held, so a write that fails part way
// leaves it for the next pass to gather, never held but split.
slices.SortStableFunc(set, func(a, b *gpuv1alpha1.GPUClaim) int { return boolInt(wasLost[a]) - boolInt(wasLost[b]) })
errs = append(errs, r.writeSet(ctx, w, set, func(d *gpuv1alpha1.GPUClaim) bool {
return before[d] != string(d.Status.Phase)+"@"+d.Status.NodeName
}))
continue
}
var changed []*gpuv1alpha1.GPUClaim
for _, d := range set {
if d.Status.Phase == gpuv1alpha1.GPUClaimLost {
continue
}
d.Status.Phase = gpuv1alpha1.GPUClaimLost
w.setWaiting(d, ReasonCapacity, "another claim of its set is Lost and no node has room for the whole set")
w.setClaim(d)
changed = append(changed, d)
}
errs = append(errs, r.writeSet(ctx, w, changed, func(*gpuv1alpha1.GPUClaim) bool { return true }))
}
return []error{errors.Join(errs...)}
}
func boolInt(b bool) int {
if b {
return 1
}
return 0
}