mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-09 05:25:14 +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>
531 lines
20 KiB
Go
531 lines
20 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"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
|
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
|
|
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
|
|
|
gpuv1alpha1 "github.com/cozystack/cozystack/api/gpu/v1alpha1"
|
|
"github.com/cozystack/cozystack/internal/gpu"
|
|
)
|
|
|
|
func TestClaimIsAttachedWhileItsVMIRunsAndBoundAfterItEnds(t *testing.T) {
|
|
v := vmiObj("vm1", "Scheduled", "n1", "")
|
|
e := newEnv(t,
|
|
class("l40s", rnL40S),
|
|
node("n1", map[string]int64{rnL40S: 2}),
|
|
claim("a", "l40s", 1, bound("n1"), recorded("vm1")),
|
|
v,
|
|
)
|
|
e.pass()
|
|
expectPlacement(t, e.claim("a"), gpuv1alpha1.GPUClaimAttached, "n1")
|
|
|
|
_ = unstructured.SetNestedField(v.Object, "Succeeded", "status", "phase")
|
|
e.update(v)
|
|
e.pass()
|
|
d := e.claim("a")
|
|
expectPlacement(t, d, gpuv1alpha1.GPUClaimBound, "n1")
|
|
if d.Status.AttachedTo != nil {
|
|
t.Fatalf("attachment kept after the VMI finished: %+v", d.Status)
|
|
}
|
|
}
|
|
|
|
func TestClaimLeavesAttachedWhenItsVMIIsRemoved(t *testing.T) {
|
|
e := newEnv(t,
|
|
class("l40s", rnL40S),
|
|
node("n1", map[string]int64{rnL40S: 2}),
|
|
claim("a", "l40s", 1, bound("n1"), attached("vm1")),
|
|
)
|
|
e.pass()
|
|
d := e.claim("a")
|
|
expectPlacement(t, d, gpuv1alpha1.GPUClaimBound, "n1")
|
|
if d.Status.AttachedTo != nil {
|
|
t.Fatal("attachment kept after the VMI was removed")
|
|
}
|
|
}
|
|
|
|
func TestAttachmentWhoseVMINeverAppearsIsClearedAfterTheGrace(t *testing.T) {
|
|
e := newEnv(t,
|
|
class("l40s", rnL40S),
|
|
node("n1", map[string]int64{rnL40S: 2}),
|
|
claim("a", "l40s", 1, bound("n1"), recorded("vm1")),
|
|
)
|
|
e.pass()
|
|
if e.claim("a").Status.AttachedTo == nil {
|
|
t.Fatal("attachment cleared inside the grace period")
|
|
}
|
|
e.now = t0.Add(3 * time.Minute)
|
|
e.pass()
|
|
if d := e.claim("a"); d.Status.AttachedTo != nil {
|
|
t.Fatalf("stale attachment kept: %+v", d.Status)
|
|
}
|
|
}
|
|
|
|
func TestTemplateClaimIsDeletedWhenItsVMIEnds(t *testing.T) {
|
|
v := vmiObj("vm1", "Running", "n1", "")
|
|
e := newEnv(t,
|
|
class("l40s", rnL40S),
|
|
node("n1", map[string]int64{rnL40S: 2}),
|
|
claim("vm1-gpu-0", "l40s", 1, template("vm1"), bound("n1"), recorded("vm1")),
|
|
v,
|
|
)
|
|
e.pass()
|
|
d := e.claim("vm1-gpu-0")
|
|
expectPlacement(t, d, gpuv1alpha1.GPUClaimAttached, "n1")
|
|
|
|
_ = unstructured.SetNestedField(v.Object, "Failed", "status", "phase")
|
|
e.update(v)
|
|
e.pass()
|
|
if !e.gone(&gpuv1alpha1.GPUClaim{ObjectMeta: d.ObjectMeta}) {
|
|
t.Fatal("template claim kept after its VMI finished")
|
|
}
|
|
}
|
|
|
|
// The controller creates a template claim only for a VMI it has seen, so a
|
|
// template claim whose VMI is gone, attached or not yet, is deleted at once.
|
|
func TestTemplateClaimWhoseVMIIsGoneIsDeleted(t *testing.T) {
|
|
e := newEnv(t,
|
|
class("l40s", rnL40S),
|
|
node("n1", map[string]int64{rnL40S: 2}),
|
|
claim("vm1-gpu-0", "l40s", 1, template("vm1"), bound("n1"), recorded("vm1")),
|
|
claim("vm2-gpu-0", "l40s", 1, template("vm2")),
|
|
)
|
|
e.pass()
|
|
for _, name := range []string{"vm1-gpu-0", "vm2-gpu-0"} {
|
|
if !e.gone(&gpuv1alpha1.GPUClaim{ObjectMeta: claim(name, "l40s", 1).ObjectMeta}) {
|
|
t.Fatalf("template claim %s kept although its VMI is gone", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A template claim created but not attached yet stays while its VMI lives.
|
|
func TestAnUnattachedTemplateClaimOfALiveVMIStays(t *testing.T) {
|
|
e := newEnv(t,
|
|
class("l40s", rnL40S),
|
|
node("n1", map[string]int64{rnL40S: 2}),
|
|
claim("vm1-gpu-0", "l40s", 1, template("vm1")),
|
|
vmiObj("vm1", "Scheduling", "", ""),
|
|
)
|
|
e.pass()
|
|
if e.gone(&gpuv1alpha1.GPUClaim{ObjectMeta: claim("vm1-gpu-0", "l40s", 1).ObjectMeta}) {
|
|
t.Fatal("the template claim of a live VMI was deleted before it was attached")
|
|
}
|
|
}
|
|
|
|
func TestTemplateClaimOfAnEarlierVMIIsNotTakenByItsSuccessor(t *testing.T) {
|
|
d := claim("vm1-gpu-0", "l40s", 1, template("vm1"), bound("n1"), attached("vm1"))
|
|
d.OwnerReferences = []metav1.OwnerReference{{APIVersion: "kubevirt.io/v1", Kind: "VirtualMachineInstance", Name: "vm1", UID: "old-uid"}}
|
|
e := newEnv(t,
|
|
class("l40s", rnL40S),
|
|
node("n1", map[string]int64{rnL40S: 2}),
|
|
d,
|
|
vmiObj("vm1", "Running", "n1", ""),
|
|
)
|
|
e.pass()
|
|
if !e.gone(d) {
|
|
t.Fatal("a template claim owned by the previous VMI was kept")
|
|
}
|
|
}
|
|
|
|
func TestDeletedClaimStaysWhileAVMIUsesIt(t *testing.T) {
|
|
v := vmiObj("vm1", "Running", "n1", "")
|
|
d := claim("a", "l40s", 1, bound("n1"), attached("vm1"))
|
|
e := newEnv(t, class("l40s", rnL40S), node("n1", map[string]int64{rnL40S: 2}), d, v)
|
|
e.pass()
|
|
e.delete(d)
|
|
e.pass()
|
|
got := e.claim("a")
|
|
expectPlacement(t, got, gpuv1alpha1.GPUClaimAttached, "n1")
|
|
if !e.recordExists("a") {
|
|
t.Fatal("usage record dropped while the VMI still uses the deleted claim")
|
|
}
|
|
|
|
_ = unstructured.SetNestedField(v.Object, "Succeeded", "status", "phase")
|
|
e.update(v)
|
|
e.pass()
|
|
if !e.gone(d) {
|
|
t.Fatal("deleted claim kept after its VMI finished")
|
|
}
|
|
if e.recordExists("a") {
|
|
t.Fatal("usage record outlived the claim")
|
|
}
|
|
}
|
|
|
|
func TestClassFinalizerHoldsWhileAClaimHoldsOrOwesGPUs(t *testing.T) {
|
|
c := class("l40s", rnL40S)
|
|
d := claim("a", "l40s", 1, lost("dead"))
|
|
e := newEnv(t, c, node("n1", nil), d)
|
|
e.delete(c)
|
|
e.pass()
|
|
if !controllerutil.ContainsFinalizer(e.class("l40s"), gpu.FinalizerClass) {
|
|
t.Fatal("class finalizer removed while a Lost claim owes GPUs")
|
|
}
|
|
e.delete(d)
|
|
e.pass()
|
|
if !e.gone(c) {
|
|
t.Fatal("class kept after its last claim went")
|
|
}
|
|
}
|
|
|
|
// With the feature disabled no KubeVirt kind is known, and finalizers and
|
|
// usage records still run, so a deletion never hangs.
|
|
func TestDisabledFeatureStillReleasesDeletedClaimsAndClasses(t *testing.T) {
|
|
c := class("l40s", rnL40S)
|
|
d := claim("a", "l40s", 1, bound("n1"), attached("vm1"))
|
|
o := claim("order", "l40s", 1)
|
|
e := newEnvWith(t, false, c, d, o, node("n1", map[string]int64{rnL40S: 2}))
|
|
e.pass()
|
|
if !e.recordExists("a") {
|
|
t.Fatal("usage record missing with the feature disabled")
|
|
}
|
|
if got := e.claim("order"); got.Status.Phase != "" {
|
|
t.Fatalf("an order was placed with the feature disabled: %+v", got.Status)
|
|
}
|
|
e.delete(d)
|
|
e.delete(o)
|
|
e.delete(c)
|
|
e.pass()
|
|
if !e.gone(d) || !e.gone(c) {
|
|
t.Fatal("a disabled feature hung a deletion")
|
|
}
|
|
if e.recordExists("a") {
|
|
t.Fatal("usage record kept")
|
|
}
|
|
}
|
|
|
|
func TestFinalizersAreAddedToNewObjects(t *testing.T) {
|
|
c := class("l40s", rnL40S)
|
|
c.Finalizers = nil
|
|
d := claim("a", "l40s", 1)
|
|
d.Finalizers = nil
|
|
e := newEnvWith(t, false, c, d)
|
|
e.pass()
|
|
if !controllerutil.ContainsFinalizer(e.class("l40s"), gpu.FinalizerClass) ||
|
|
!controllerutil.ContainsFinalizer(e.claim("a"), gpu.FinalizerClaim) {
|
|
t.Fatal("finalizers not added")
|
|
}
|
|
}
|
|
|
|
// With the feature off nothing watches VMIs, so the pass itself must come
|
|
// back for a deleted claim a VMI still uses.
|
|
func TestDisabledFeatureComesBackForAClaimItsVMIStillUses(t *testing.T) {
|
|
v := vmiObj("vm1", "Running", "n1", "")
|
|
d := claim("a", "l40s", 1, bound("n1"), attached("vm1"))
|
|
e := newEnv(t, class("l40s", rnL40S), d, v)
|
|
e.r.Config.Enabled = false
|
|
e.delete(d)
|
|
res, err := e.r.Reconcile(t.Context(), ctrl.Request{NamespacedName: passKey})
|
|
if err != nil || res.RequeueAfter == 0 {
|
|
t.Fatalf("no requeue while a deleted claim waits for its VMI: %v, %+v", err, res)
|
|
}
|
|
if e.gone(d) {
|
|
t.Fatal("released while the VMI runs")
|
|
}
|
|
_ = unstructured.SetNestedField(v.Object, "Succeeded", "status", "phase")
|
|
e.update(v)
|
|
e.pass()
|
|
if !e.gone(d) {
|
|
t.Fatal("not released after the VMI finished")
|
|
}
|
|
}
|
|
|
|
// The manager's client reads unstructured objects from the API server, so
|
|
// the KubeVirt view must come from the cache the watches fill.
|
|
func TestKubeVirtObjectsAreReadThroughTheCache(t *testing.T) {
|
|
e := newEnv(t, class("l40s", rnL40S), node("n1", map[string]int64{rnL40S: 2}),
|
|
claim("a", "l40s", 1, bound("n1"), recorded("vm1")))
|
|
cached := fake.NewClientBuilder().WithScheme(testScheme(true)).WithObjects(vmiObj("vm1", "Running", "n1", "")).Build()
|
|
e.r.Cache = cached
|
|
e.pass()
|
|
expectPlacement(t, e.claim("a"), gpuv1alpha1.GPUClaimAttached, "n1")
|
|
}
|
|
|
|
// The webhook recorded the attachment and the cache has not shown the VMI
|
|
// yet: a deletion in that window must not release the GPU it is starting
|
|
// with.
|
|
func TestDeletedClaimWithAFreshAttachmentWaitsForTheGrace(t *testing.T) {
|
|
for name, placed := range map[string]claimOpt{"Bound": bound("n1"), "Lost": lost("gone")} {
|
|
t.Run(name, func(t *testing.T) {
|
|
d := claim("a", "l40s", 1, placed, recorded("vm1"))
|
|
e := newEnv(t, class("l40s", rnL40S), node("n1", map[string]int64{rnL40S: 2}), d)
|
|
e.delete(d)
|
|
e.pass()
|
|
if e.gone(d) {
|
|
t.Fatal("released before its VMI could appear")
|
|
}
|
|
e.now = t0.Add(3 * time.Minute)
|
|
e.pass()
|
|
if !e.gone(d) {
|
|
t.Fatal("kept after the attachment grace")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The webhook may record a Lost claim for a new VMI, which the gate then
|
|
// rebinds; until the VMI appears the attachment is as fresh as a held one.
|
|
func TestALostClaimWithAFreshAttachmentWaitsForItsVMI(t *testing.T) {
|
|
e := newEnv(t, class("l40s", rnL40S), node("n1", map[string]int64{rnL40S: 2}),
|
|
claim("a", "l40s", 1, lost("gone"), recorded("vm1")))
|
|
e.pass()
|
|
if got := e.claim("a").Status.AttachedTo; got == nil || got.VMI != "vm1" {
|
|
t.Fatalf("attachment %+v cleared before its VMI could appear", got)
|
|
}
|
|
e.create(scheduling("vm1", "a"))
|
|
e.create(gatedPod("vm1"))
|
|
e.pass()
|
|
expectPlacement(t, e.claim("a"), gpuv1alpha1.GPUClaimAttached, "n1")
|
|
expectReleased(t, e.pod("virt-launcher-vm1"), "n1")
|
|
}
|
|
|
|
// A VMI whose pod still waits at the gate holds no GPU, so a claim deleted
|
|
// under it goes, and the VMI reports why it cannot start.
|
|
func TestADeletedClaimOfAGatedVMIIsReleasedAndReported(t *testing.T) {
|
|
d := claim("a", "l40s", 1, bound("n1"), attached("vm1"))
|
|
e := newEnv(t, class("l40s", rnL40S), node("n1", map[string]int64{rnL40S: 2}), d,
|
|
scheduling("vm1", "a"), gatedPod("vm1"))
|
|
rec := withRecorder(e)
|
|
e.delete(d)
|
|
e.pass()
|
|
if !e.gone(d) {
|
|
t.Fatal("a deleted claim was kept for a VMI whose pod never left the gate")
|
|
}
|
|
expectGated(t, e.pod("virt-launcher-vm1"))
|
|
got := drain(rec)
|
|
if len(got) != 1 || !strings.HasPrefix(got[0], "Warning "+ReasonClaimMissing+" ") || !strings.Contains(got[0], "GPUClaim a") {
|
|
t.Fatalf("events %q, want one Warning %s naming the claim", got, ReasonClaimMissing)
|
|
}
|
|
}
|
|
|
|
// A pinned pod may already be on its way to the GPU, so its claim stays.
|
|
func TestADeletedClaimOfAPinnedVMIStays(t *testing.T) {
|
|
d := claim("a", "l40s", 1, bound("n1"), attached("vm1"))
|
|
e := newEnv(t, class("l40s", rnL40S), node("n1", map[string]int64{rnL40S: 2}), d,
|
|
scheduling("vm1", "a"), pinnedPod("vm1", "n1"))
|
|
e.delete(d)
|
|
e.pass()
|
|
if e.gone(d) {
|
|
t.Fatal("a deleted claim was released under a pinned pod")
|
|
}
|
|
}
|
|
|
|
// The reason a gated set waited does not outlive its VMI.
|
|
func TestTheWaitingConditionGoesWithTheVMI(t *testing.T) {
|
|
v := scheduling("vm1", "a", "l40s:1")
|
|
p := gatedPod("vm1")
|
|
e := newEnv(t, class("l40s", rnL40S), node("n1", map[string]int64{rnL40S: 1}),
|
|
claim("a", "l40s", 1, bound("n1"), attached("vm1")), v, p)
|
|
e.pass()
|
|
expectWaiting(t, e.claim("a"), ReasonCapacity)
|
|
e.delete(p)
|
|
e.delete(v)
|
|
e.pass()
|
|
got := e.claim("a")
|
|
expectPlacement(t, got, gpuv1alpha1.GPUClaimBound, "n1")
|
|
if c := waiting(got); c != nil {
|
|
t.Fatalf("condition %+v kept after the VMI went", c)
|
|
}
|
|
}
|
|
|
|
func TestAFailedClaimReleaseKeepsItsClassFinalizer(t *testing.T) {
|
|
c := class("l40s", rnL40S)
|
|
d := claim("a", "l40s", 1, bound("n1"))
|
|
failed := false
|
|
cl := fake.NewClientBuilder().WithScheme(testScheme(true)).WithObjects(c, d, node("n1", map[string]int64{rnL40S: 1})).
|
|
WithStatusSubresource(&gpuv1alpha1.GPUClass{}, &gpuv1alpha1.GPUClaim{}).
|
|
WithInterceptorFuncs(interceptor.Funcs{Update: func(ctx context.Context, w client.WithWatch, obj client.Object, opts ...client.UpdateOption) error {
|
|
if _, ok := obj.(*gpuv1alpha1.GPUClaim); ok && !failed && !obj.GetDeletionTimestamp().IsZero() {
|
|
failed = true
|
|
return apierrors.NewInternalError(errors.New("etcd unavailable"))
|
|
}
|
|
return w.Update(ctx, obj, opts...)
|
|
}}).Build()
|
|
e := &env{t: t, c: cl, now: t0}
|
|
e.r = &Reconciler{Client: cl, Reader: cl, Now: func() time.Time { return e.now },
|
|
Config: gpu.Config{Enabled: true, LostCapacityGrace: time.Minute, AttachmentGrace: time.Minute}}
|
|
e.delete(d)
|
|
e.delete(c)
|
|
_, _ = e.r.Reconcile(t.Context(), ctrl.Request{NamespacedName: passKey})
|
|
if e.gone(&gpuv1alpha1.GPUClass{ObjectMeta: c.ObjectMeta}) {
|
|
t.Fatal("the class went although its claim's release failed")
|
|
}
|
|
}
|
|
|
|
// With the feature enabled before KubeVirt serves its kinds, as on a fresh
|
|
// install, there is no VMI to read: passes still run, so usage records,
|
|
// finalizers and placement do not wait for KubeVirt.
|
|
func TestEnabledFeatureRunsBeforeKubeVirtServesItsKinds(t *testing.T) {
|
|
c := class("l40s", rnL40S)
|
|
d := claim("a", "l40s", 1, bound("n1"))
|
|
o := claim("order", "l40s", 1)
|
|
e := newEnvWith(t, false, c, d, o, node("n1", map[string]int64{rnL40S: 2}))
|
|
e.r.Config.Enabled = true
|
|
// The manager's cache answers a kind no CRD serves with NoKindMatch.
|
|
e.r.Cache = interceptor.NewClient(e.c.(client.WithWatch), interceptor.Funcs{
|
|
List: func(ctx context.Context, c client.WithWatch, list client.ObjectList, opts ...client.ListOption) error {
|
|
if u, ok := list.(*unstructured.UnstructuredList); ok {
|
|
gvk := u.GroupVersionKind()
|
|
return &meta.NoKindMatchError{GroupKind: gvk.GroupKind(), SearchedVersions: []string{gvk.Version}}
|
|
}
|
|
return c.List(ctx, list, opts...)
|
|
},
|
|
})
|
|
e.pass()
|
|
if !e.recordExists("a") {
|
|
t.Fatal("usage record missing before KubeVirt serves its kinds")
|
|
}
|
|
if got := e.claim("order"); got.Status.Phase != gpuv1alpha1.GPUClaimBound {
|
|
t.Fatalf("an order was not placed before KubeVirt serves its kinds: %+v", got.Status)
|
|
}
|
|
e.delete(d)
|
|
e.delete(c)
|
|
e.pass()
|
|
if !e.gone(d) {
|
|
t.Fatal("a deletion hung until KubeVirt serves its kinds")
|
|
}
|
|
}
|
|
|
|
// A kind KubeVirt served a moment ago can read as not served, such as
|
|
// while an API server's discovery refills after its restart. A claim
|
|
// attached to a running VMI must not read that as a VMI that is gone.
|
|
func TestUnservedKubeVirtKindsDoNotDetachAttachedClaims(t *testing.T) {
|
|
d := claim("a", "l40s", 1, bound("n1"), attached("vm1"))
|
|
d.Status.Phase = gpuv1alpha1.GPUClaimAttached
|
|
e := newEnvWith(t, false, class("l40s", rnL40S), d, node("n1", map[string]int64{rnL40S: 2}))
|
|
e.r.Config.Enabled = true
|
|
e.r.Cache = interceptor.NewClient(e.c.(client.WithWatch), interceptor.Funcs{
|
|
List: func(ctx context.Context, c client.WithWatch, list client.ObjectList, opts ...client.ListOption) error {
|
|
if u, ok := list.(*unstructured.UnstructuredList); ok {
|
|
gvk := u.GroupVersionKind()
|
|
return &meta.NoKindMatchError{GroupKind: gvk.GroupKind(), SearchedVersions: []string{gvk.Version}}
|
|
}
|
|
return c.List(ctx, list, opts...)
|
|
},
|
|
})
|
|
_, err := e.r.Reconcile(context.Background(), ctrl.Request{NamespacedName: passKey})
|
|
if err == nil || !strings.Contains(err.Error(), "GPUClaim "+ns+"/a is attached") {
|
|
t.Fatalf("the pass must fail naming the attached claim, got %v", err)
|
|
}
|
|
if got := e.claim("a"); got.Status.AttachedTo == nil || got.Status.Phase != gpuv1alpha1.GPUClaimAttached {
|
|
t.Fatalf("an unserved KubeVirt kind detached a claim: %+v", got.Status)
|
|
}
|
|
}
|
|
|
|
// A template claim exists only for a VMI the controller saw, so while the
|
|
// KubeVirt kinds read as not served it must not be taken for one whose VMI
|
|
// is gone.
|
|
func TestUnservedKubeVirtKindsDoNotDeleteTemplateClaims(t *testing.T) {
|
|
d := claim("vm1-gpu-0", "l40s", 1, template("vm1"))
|
|
e := newEnvWith(t, false, class("l40s", rnL40S), d, node("n1", map[string]int64{rnL40S: 2}))
|
|
e.r.Config.Enabled = true
|
|
e.r.Cache = interceptor.NewClient(e.c.(client.WithWatch), interceptor.Funcs{
|
|
List: func(ctx context.Context, c client.WithWatch, list client.ObjectList, opts ...client.ListOption) error {
|
|
if u, ok := list.(*unstructured.UnstructuredList); ok {
|
|
gvk := u.GroupVersionKind()
|
|
return &meta.NoKindMatchError{GroupKind: gvk.GroupKind(), SearchedVersions: []string{gvk.Version}}
|
|
}
|
|
return c.List(ctx, list, opts...)
|
|
},
|
|
})
|
|
if _, err := e.r.Reconcile(context.Background(), ctrl.Request{NamespacedName: passKey}); err == nil {
|
|
t.Fatal("the pass must fail while a template claim exists and KubeVirt kinds read as not served")
|
|
}
|
|
if e.gone(d) {
|
|
t.Fatal("an unserved KubeVirt kind deleted a template claim")
|
|
}
|
|
}
|
|
|
|
func TestALostClaimKeepsWhyItLeftWhenItsVMIEnds(t *testing.T) {
|
|
why := "node gone is gone and no node has room for the claim's set"
|
|
d := claim("a", "l40s", 1, lost("gone"), attached("vm1"))
|
|
meta.SetStatusCondition(&d.Status.Conditions, metav1.Condition{Type: ConditionWaiting, Status: metav1.ConditionTrue,
|
|
Reason: ReasonCapacity, Message: why})
|
|
e := newEnv(t, class("l40s", rnL40S), node("n1", nil), d, vmiObj("vm1", "Failed", "gone", ""))
|
|
e.pass()
|
|
got := e.claim("a")
|
|
if got.Status.AttachedTo != nil {
|
|
t.Fatalf("attachment %+v kept after the VMI failed", got.Status.AttachedTo)
|
|
}
|
|
if c := waiting(got); c == nil || c.Message != why {
|
|
t.Fatalf("condition %+v, want the reason the claim left its node", c)
|
|
}
|
|
}
|
|
|
|
// The pod cache can lag the live claim read: a pod the last pass released
|
|
// must not look gated to the release of its claim.
|
|
func TestADeletedClaimStaysWhenItsPodLeftTheGateBeyondTheCache(t *testing.T) {
|
|
d := claim("a", "l40s", 1, bound("n1"), attached("vm1"))
|
|
e := newEnv(t, class("l40s", rnL40S), node("n1", map[string]int64{rnL40S: 2}), d,
|
|
scheduling("vm1", "a"), gatedPod("vm1"))
|
|
e.delete(d)
|
|
e.r.Reader = fake.NewClientBuilder().WithScheme(testScheme(true)).WithObjects(e.claim("a"), pinnedPod("vm1", "n1")).Build()
|
|
e.pass()
|
|
if e.gone(d) {
|
|
t.Fatal("claim released under a pod the API shows pinned")
|
|
}
|
|
}
|
|
|
|
// A pod the API cannot be asked about may have left the gate: the claim
|
|
// stays and the pass fails, so it runs again.
|
|
func TestADeletedClaimStaysWhenItsPodCannotBeRead(t *testing.T) {
|
|
d := claim("a", "l40s", 1, bound("n1"), attached("vm1"))
|
|
e := newEnv(t, class("l40s", rnL40S), node("n1", map[string]int64{rnL40S: 2}), d,
|
|
scheduling("vm1", "a"), gatedPod("vm1"))
|
|
e.delete(d)
|
|
e.r.Reader = fake.NewClientBuilder().WithScheme(testScheme(true)).WithObjects(e.claim("a"), gatedPod("vm1")).
|
|
WithInterceptorFuncs(interceptor.Funcs{Get: func(ctx context.Context, c client.WithWatch, k client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
|
|
if _, ok := obj.(*corev1.Pod); ok {
|
|
return apierrors.NewInternalError(errors.New("etcd unavailable"))
|
|
}
|
|
return c.Get(ctx, k, obj, opts...)
|
|
}}).Build()
|
|
if _, err := e.r.Reconcile(t.Context(), ctrl.Request{NamespacedName: passKey}); err == nil {
|
|
t.Fatal("a failed pod read was not reported")
|
|
}
|
|
if e.gone(d) {
|
|
t.Fatal("claim released under a pod that could not be read")
|
|
}
|
|
}
|
|
|
|
// A pod gone from the API holds no GPU.
|
|
func TestADeletedClaimGoesWhenItsGatedPodIsGoneFromTheAPI(t *testing.T) {
|
|
d := claim("a", "l40s", 1, bound("n1"), attached("vm1"))
|
|
e := newEnv(t, class("l40s", rnL40S), node("n1", map[string]int64{rnL40S: 2}), d,
|
|
scheduling("vm1", "a"), gatedPod("vm1"))
|
|
e.delete(d)
|
|
e.r.Reader = fake.NewClientBuilder().WithScheme(testScheme(true)).WithObjects(e.claim("a")).Build()
|
|
e.pass()
|
|
if !e.gone(d) {
|
|
t.Fatal("claim kept for a pod the API no longer has")
|
|
}
|
|
}
|