mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-03 02:27:01 +09:00
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>
152 lines
5.5 KiB
Go
152 lines
5.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"
|
|
"testing"
|
|
"time"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/client-go/rest"
|
|
"k8s.io/utils/ptr"
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
"sigs.k8s.io/controller-runtime/pkg/config"
|
|
"sigs.k8s.io/controller-runtime/pkg/event"
|
|
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
|
"sigs.k8s.io/controller-runtime/pkg/source"
|
|
|
|
"github.com/cozystack/cozystack/internal/gpu"
|
|
)
|
|
|
|
// Setup wires the watches without reaching an API server, with the feature
|
|
// on and off.
|
|
func TestSetupBuildsTheControllerEnabledAndDisabled(t *testing.T) {
|
|
for _, enabled := range []bool{false, true} {
|
|
mgr, err := ctrl.NewManager(&rest.Config{Host: "https://127.0.0.1:1"}, ctrl.Options{
|
|
Scheme: testScheme(enabled),
|
|
Metrics: metricsserver.Options{BindAddress: "0"},
|
|
Controller: config.Controller{SkipNameValidation: ptr.To(true)},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("manager: %v", err)
|
|
}
|
|
if err := Setup(mgr, gpu.Config{Enabled: enabled}); err != nil {
|
|
t.Fatalf("Setup(enabled=%v): %v", enabled, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPodEventsAreFilteredToExtendedResources(t *testing.T) {
|
|
plain := launcherPod("web", "", "n1", map[string]int64{"cpu": 1})
|
|
if requestsExtendedResource(plain) {
|
|
t.Fatal("a pod with only cpu must not trigger a pass")
|
|
}
|
|
if !requestsExtendedResource(launcherPod("gpu", "", "n1", map[string]int64{rnL40S: 1})) {
|
|
t.Fatal("a pod requesting a GPU resource name must trigger a pass")
|
|
}
|
|
if requestsExtendedResource(&corev1.Node{}) {
|
|
t.Fatal("a non-pod object must not match")
|
|
}
|
|
}
|
|
|
|
// Node layouts arrive with the host layer; until then no node has a
|
|
// pending layout change, and this hook must be replaced, not extended.
|
|
func TestLayoutPendingIsAHookThatNeverFires(t *testing.T) {
|
|
if layoutPending("n1") {
|
|
t.Fatal("no node can have a pending layout before the host layer ships")
|
|
}
|
|
}
|
|
|
|
func TestNodeUpdatesThatCannotChangePlacementAreDropped(t *testing.T) {
|
|
base := node("n1", map[string]int64{rnL40S: 2})
|
|
heartbeat := base.DeepCopy()
|
|
heartbeat.Status.Conditions[0].LastHeartbeatTime = metav1.NewTime(t0)
|
|
if nodePlacementChanged.Update(event.UpdateEvent{ObjectOld: base, ObjectNew: heartbeat}) {
|
|
t.Fatal("a heartbeat must not trigger a pass")
|
|
}
|
|
for name, change := range map[string]func(*corev1.Node){
|
|
"cordon": cordoned,
|
|
"taint": tainted("dedicated", corev1.TaintEffectNoSchedule),
|
|
"label": labelled("pool", "a"),
|
|
"not ready": notReadySince(t0),
|
|
"allocatable": func(n *corev1.Node) { n.Status.Allocatable[rnL40S] = resource.MustParse("1") },
|
|
} {
|
|
changed := base.DeepCopy()
|
|
change(changed)
|
|
if !nodePlacementChanged.Update(event.UpdateEvent{ObjectOld: base, ObjectNew: changed}) {
|
|
t.Errorf("%s must trigger a pass", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLauncherPodsWithOnlyKubeVirtDevicesDoNotTriggerAPass(t *testing.T) {
|
|
if requestsExtendedResource(launcherPod("virt-launcher-x", "x", "n1", map[string]int64{"devices.kubevirt.io/kvm": 1})) {
|
|
t.Fatal("every VM's launcher pod would trigger a pass")
|
|
}
|
|
}
|
|
|
|
// A gated launcher pod requests its GPUs, but its status changes, such as
|
|
// the scheduler refusing it after the pin, matter whatever it requests.
|
|
func TestLauncherPodsWithClaimReferencesTriggerAPass(t *testing.T) {
|
|
p := launcherPod("virt-launcher-x", "x", "", map[string]int64{"devices.kubevirt.io/kvm": 1})
|
|
if podMatters(p) {
|
|
t.Fatal("a launcher pod without claim references or GPUs triggered a pass")
|
|
}
|
|
p.Labels = map[string]string{gpu.LabelClaimReference: "false"}
|
|
if podMatters(p) {
|
|
t.Fatal("only the value true marks claim references")
|
|
}
|
|
p.Labels = map[string]string{gpu.LabelClaimReference: gpu.LabelValueTrue}
|
|
if !podMatters(p) {
|
|
t.Fatal("a launcher pod with claim references does not trigger a pass")
|
|
}
|
|
}
|
|
|
|
type recordingWatcher struct {
|
|
srcs []source.TypedSource[reconcile.Request]
|
|
}
|
|
|
|
func (w *recordingWatcher) Watch(src source.TypedSource[reconcile.Request]) error {
|
|
w.srcs = append(w.srcs, src)
|
|
return nil
|
|
}
|
|
|
|
// A watch on a kind no CRD serves fails the controller's start, so the
|
|
// KubeVirt watches join it only once KubeVirt serves them.
|
|
func TestKubeVirtWatchesWaitForTheirKinds(t *testing.T) {
|
|
m := &laterMapper{served: 4}
|
|
w := &recordingWatcher{}
|
|
srcs := []source.TypedSource[reconcile.Request]{nil, nil, nil}
|
|
if err := watchWhenServed(context.Background(), m, time.Millisecond, w, srcs); err != nil {
|
|
t.Fatalf("watchWhenServed: %v", err)
|
|
}
|
|
if m.calls < 4 || len(w.srcs) != 3 {
|
|
t.Fatalf("looked up %d times, added %d watches; want every watch once the kinds are served", m.calls, len(w.srcs))
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
|
|
defer cancel()
|
|
w = &recordingWatcher{}
|
|
if err := watchWhenServed(ctx, &laterMapper{served: 1 << 30}, time.Millisecond, w, srcs); err != nil || len(w.srcs) != 0 {
|
|
t.Fatalf("a manager stopped before KubeVirt serves its kinds: err %v, %d watches", err, len(w.srcs))
|
|
}
|
|
}
|