Files
cozystack/internal/gpu/model/node_test.go
T
Aleksei Sviridkin 5d90af051b 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 devices and raw-name pods together.

A node takes binds only when it is schedulable, Ready and carries no
NoSchedule or NoExecute taint the pod does not tolerate; the
node.kubernetes.io taints are left to the first two checks, and a
taint never counts as a cordon.

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

105 lines
4.3 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 (
"testing"
corev1 "k8s.io/api/core/v1"
)
func readyNode(mutate func(*corev1.Node)) *corev1.Node {
n := &corev1.Node{Status: corev1.NodeStatus{Conditions: []corev1.NodeCondition{{Type: corev1.NodeReady, Status: corev1.ConditionTrue}}}}
n.Name = "gpu-node-3"
if mutate != nil {
mutate(n)
}
return n
}
func withTaint(key string, effect corev1.TaintEffect) func(*corev1.Node) {
return func(n *corev1.Node) {
n.Spec.Taints = append(n.Spec.Taints, corev1.Taint{Key: key, Effect: effect})
}
}
const farTaint = "medik8s.io/fence-agents-remediation"
func TestCordoned(t *testing.T) {
tests := []struct {
name string
node *corev1.Node
want bool
}{
{"unschedulable", readyNode(func(n *corev1.Node) { n.Spec.Unschedulable = true }), true},
{"schedulable", readyNode(nil), false},
{"Fence Agents Remediation taint alone", readyNode(withTaint(farTaint, corev1.TaintEffectNoSchedule)), false},
{"unschedulable taint without the field", readyNode(withTaint(corev1.TaintNodeUnschedulable, corev1.TaintEffectNoSchedule)), false},
}
for _, tc := range tests {
if got := Cordoned(tc.node); got != tc.want {
t.Errorf("%s: Cordoned = %v, want %v", tc.name, got, tc.want)
}
}
}
func TestReady(t *testing.T) {
tests := []struct {
name string
node *corev1.Node
want bool
}{
{"ready", readyNode(nil), true},
{"not ready", readyNode(func(n *corev1.Node) { n.Status.Conditions[0].Status = corev1.ConditionFalse }), false},
{"unknown", readyNode(func(n *corev1.Node) { n.Status.Conditions[0].Status = corev1.ConditionUnknown }), false},
{"no condition", readyNode(func(n *corev1.Node) { n.Status.Conditions = nil }), false},
}
for _, tc := range tests {
if got := Ready(tc.node); got != tc.want {
t.Errorf("%s: Ready = %v, want %v", tc.name, got, tc.want)
}
}
}
func TestEligible(t *testing.T) {
tolerateFAR := []corev1.Toleration{{Key: farTaint, Operator: corev1.TolerationOpExists}}
tests := []struct {
name string
node *corev1.Node
tolerations []corev1.Toleration
want bool
}{
{name: "ready, schedulable, untainted", node: readyNode(nil), want: true},
{name: "cordoned", node: readyNode(func(n *corev1.Node) { n.Spec.Unschedulable = true }), want: false},
{name: "not ready", node: readyNode(func(n *corev1.Node) { n.Status.Conditions[0].Status = corev1.ConditionFalse }), want: false},
{name: "untolerated NoSchedule", node: readyNode(withTaint(farTaint, corev1.TaintEffectNoSchedule)), want: false},
{name: "untolerated NoExecute", node: readyNode(withTaint("example.com/maintenance", corev1.TaintEffectNoExecute)), want: false},
{name: "tolerated NoSchedule", node: readyNode(withTaint(farTaint, corev1.TaintEffectNoSchedule)), tolerations: tolerateFAR, want: true},
{name: "PreferNoSchedule does not count", node: readyNode(withTaint("example.com/soft", corev1.TaintEffectPreferNoSchedule)), want: true},
// The node.kubernetes.io taints mirror cordon and readiness, which
// the field and the condition already decide.
{name: "node.kubernetes.io/unschedulable taint on a schedulable node", node: readyNode(withTaint(corev1.TaintNodeUnschedulable, corev1.TaintEffectNoSchedule)), want: true},
{name: "node.kubernetes.io/not-ready taint on a ready node", node: readyNode(withTaint(corev1.TaintNodeNotReady, corev1.TaintEffectNoExecute)), want: true},
{name: "toleration of another effect", node: readyNode(withTaint(farTaint, corev1.TaintEffectNoExecute)), tolerations: []corev1.Toleration{{Key: farTaint, Operator: corev1.TolerationOpExists, Effect: corev1.TaintEffectNoSchedule}}, want: false},
}
for _, tc := range tests {
if got := Eligible(tc.node, tc.tolerations); got != tc.want {
t.Errorf("%s: Eligible = %v, want %v", tc.name, got, tc.want)
}
}
}