mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
feat(api): refuse a vGPU class on a worker pool
Every KubernetesNodes pool runs Talos workers, which carry no vGPU guest driver, so a pool that names a vGPU class would start workers that never use their GPUs. The write is refused while it is made, before any machine exists. A class must exist before the pool is stored, so a later class cannot change the mode the pool was checked against. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <f@lex.la>
This commit is contained in:
3 files changed
+119
No files matched your search
@@ -29,12 +29,14 @@ import (
|
||||
|
||||
helmv2 "github.com/fluxcd/helm-controller/api/v2"
|
||||
structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
"k8s.io/klog/v2"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
|
||||
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
|
||||
"github.com/cozystack/cozystack/internal/gpu"
|
||||
"github.com/cozystack/cozystack/internal/gpu/model"
|
||||
appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1"
|
||||
@@ -158,6 +160,7 @@ func gpuDemand(kind string, raw []byte) (map[string]int64, error) {
|
||||
}
|
||||
}
|
||||
demand := map[string]int64{}
|
||||
var perWorker int64
|
||||
for _, e := range v.GPUs {
|
||||
if e.ClassName == "" {
|
||||
continue
|
||||
@@ -169,6 +172,10 @@ func gpuDemand(kind string, raw []byte) (map[string]int64, error) {
|
||||
if count < 1 {
|
||||
return nil, fmt.Errorf("decode %s gpus: class %s has count %d, below 1", kind, e.ClassName, count)
|
||||
}
|
||||
perWorker += count
|
||||
if perWorker > gpu.MaxVMIUnits {
|
||||
return nil, fmt.Errorf("a VMI can expand at most %d GPU units from class entries", gpu.MaxVMIUnits)
|
||||
}
|
||||
if replicas > 0 && count > math.MaxInt64/replicas {
|
||||
return nil, fmt.Errorf("decode %s gpus: class %s reserves more than %d units", kind, e.ClassName, int64(math.MaxInt64))
|
||||
}
|
||||
@@ -415,3 +422,40 @@ func specRaw(app *appsv1alpha1.Application) []byte {
|
||||
}
|
||||
return app.Spec.Raw
|
||||
}
|
||||
|
||||
// validateGPUClassModes refuses a KubernetesNodes write whose class entries
|
||||
// name a vGPU class: every worker pool runs Talos, which carries no vGPU
|
||||
// guest driver, so the workers would start and never use their GPUs. A
|
||||
// class must exist so its mode is checked before the pool is stored.
|
||||
func (r *REST) validateGPUClassModes(ctx context.Context, app *appsv1alpha1.Application) field.ErrorList {
|
||||
if r.kindName != kubernetesNodesKind {
|
||||
return nil
|
||||
}
|
||||
var v gpuValues
|
||||
if raw := specRaw(app); len(raw) > 0 {
|
||||
if err := json.Unmarshal(raw, &v); err != nil {
|
||||
return field.ErrorList{field.Invalid(field.NewPath("spec", "gpus"), "", err.Error())}
|
||||
}
|
||||
}
|
||||
var errs field.ErrorList
|
||||
for i, e := range v.GPUs {
|
||||
if e.ClassName == "" {
|
||||
continue
|
||||
}
|
||||
c := &cozyv1alpha1.GPUClass{}
|
||||
err := r.w.Get(ctx, client.ObjectKey{Name: e.ClassName}, c)
|
||||
if apierrors.IsNotFound(err) {
|
||||
errs = append(errs, field.Invalid(field.NewPath("spec", "gpus").Index(i).Child("className"), e.ClassName,
|
||||
fmt.Sprintf("GPUClass %s does not exist; its mode must be known before creating a worker pool", e.ClassName)))
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return append(errs, field.InternalError(field.NewPath("spec", "gpus").Index(i), err))
|
||||
}
|
||||
if c.Spec.Mode == cozyv1alpha1.GPUModeVGPU {
|
||||
errs = append(errs, field.Forbidden(field.NewPath("spec", "gpus").Index(i).Child("className"), fmt.Sprintf(
|
||||
"GPUClass %s is vGPU, and Talos workers carry no vGPU guest driver; a worker pool takes passthrough classes only", e.ClassName)))
|
||||
}
|
||||
}
|
||||
return errs
|
||||
}
|
||||
@@ -32,7 +32,9 @@ import (
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
"k8s.io/apiserver/pkg/endpoints/request"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
|
||||
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
|
||||
appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1"
|
||||
"github.com/cozystack/cozystack/pkg/config"
|
||||
)
|
||||
@@ -506,3 +508,70 @@ func TestGPURemainingBudget(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func newNodesRESTWithClasses(t *testing.T, classes ...*cozyv1alpha1.GPUClass) *REST {
|
||||
t.Helper()
|
||||
scheme := runtime.NewScheme()
|
||||
for _, add := range []func(*runtime.Scheme) error{helmv2.AddToScheme, cozyv1alpha1.AddToScheme} {
|
||||
if err := add(scheme); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
objs := make([]client.Object, 0, len(classes))
|
||||
for _, c := range classes {
|
||||
objs = append(objs, c)
|
||||
}
|
||||
c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()
|
||||
return &REST{c: c, w: c, kindName: kubernetesNodesKind, releaseConfig: config.ReleaseConfig{Prefix: "kubernetes-nodes-"}}
|
||||
}
|
||||
|
||||
func gpuClassOf(name string, mode cozyv1alpha1.GPUMode) *cozyv1alpha1.GPUClass {
|
||||
return &cozyv1alpha1.GPUClass{ObjectMeta: metav1.ObjectMeta{Name: name}, Spec: cozyv1alpha1.GPUClassSpec{Mode: mode}}
|
||||
}
|
||||
|
||||
// Talos workers carry no vGPU guest driver, so a worker pool takes
|
||||
// passthrough classes only, refused before any machine is created.
|
||||
func TestValidateGPUClassModes(t *testing.T) {
|
||||
r := newNodesRESTWithClasses(t, gpuClassOf("h100", cozyv1alpha1.GPUModePassthrough), gpuClassOf("l40s-24c", cozyv1alpha1.GPUModeVGPU))
|
||||
pool := func(values string) *appsv1alpha1.Application {
|
||||
return &appsv1alpha1.Application{ObjectMeta: metav1.ObjectMeta{Name: "c-p", Namespace: "tenant-root"}, Spec: &apiextv1.JSON{Raw: []byte(values)}}
|
||||
}
|
||||
expectGPUErrs(t, r.validateGPUClassModes(context.Background(), pool(`{"gpus":[{"className":"h100","count":2}]}`)), "")
|
||||
expectGPUErrs(t, r.validateGPUClassModes(context.Background(), pool(`{"gpus":[{"className":"l40s-24c"}]}`)), "GPUClass l40s-24c is vGPU")
|
||||
expectGPUErrs(t, r.validateGPUClassModes(context.Background(), pool(`{"gpus":[{"className":"missing"}]}`)), "GPUClass missing does not exist")
|
||||
expectGPUErrs(t, r.validateGPUClassModes(context.Background(), pool(`{"gpus":[{"name":"nvidia.com/GH200"}]}`)), "")
|
||||
|
||||
r.kindName = vmInstanceKind
|
||||
expectGPUErrs(t, r.validateGPUClassModes(context.Background(), pool(`{"gpus":[{"className":"l40s-24c"}]}`)), "")
|
||||
}
|
||||
|
||||
func TestCreateRefusesAVGPUClassOnAWorkerPool(t *testing.T) {
|
||||
r := newNodesRESTWithClasses(t, gpuClassOf("l40s-24c", cozyv1alpha1.GPUModeVGPU))
|
||||
r.gvk = schema.GroupVersionKind{Group: appsv1alpha1.GroupName, Version: "v1alpha1", Kind: kubernetesNodesKind}
|
||||
app := &appsv1alpha1.Application{
|
||||
TypeMeta: metav1.TypeMeta{APIVersion: "apps.cozystack.io/v1alpha1", Kind: kubernetesNodesKind},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "c-p", Namespace: "tenant-root"},
|
||||
Spec: &apiextv1.JSON{Raw: []byte(`{"maxReplicas":1,"gpus":[{"className":"l40s-24c"}]}`)},
|
||||
}
|
||||
sentinel := errors.New("past the GPU class check")
|
||||
_, err := r.Create(request.WithNamespace(context.Background(), "tenant-root"), app, func(context.Context, runtime.Object) error { return sentinel }, &metav1.CreateOptions{})
|
||||
if errors.Is(err, sentinel) || !strings.Contains(fmt.Sprint(err), "is vGPU") {
|
||||
t.Fatalf("Create = %v, want a vGPU refusal", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplicationGPUExpansionLimit(t *testing.T) {
|
||||
registerTestSchemas(t)
|
||||
for _, kind := range []string{vmInstanceKind, kubernetesNodesKind} {
|
||||
t.Run(kind, func(t *testing.T) {
|
||||
r := newAppREST(t, kind, "test-", "tests")
|
||||
for _, raw := range []string{
|
||||
`{"maxReplicas":1,"gpus":[{"className":"h100","count":1025}]}`,
|
||||
`{"maxReplicas":1,"gpus":[{"className":"h100","count":600},{"className":"h100","count":600}]}`,
|
||||
} {
|
||||
expectGPUErrs(t, r.validateGPUBudget(context.Background(), vmApplication("x", "tenant-root", raw), nil), "at most 1024 GPU units")
|
||||
}
|
||||
expectGPUErrs(t, r.validateGPUBudget(context.Background(), vmApplication("x", "tenant-root", `{"maxReplicas":10,"gpus":[{"className":"h100","count":1024}]}`), nil), "")
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -240,6 +240,9 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation
|
||||
r.warnRemovedKubernetesFields(ctx, app)
|
||||
r.warnRemovedUserPasswords(ctx, app)
|
||||
|
||||
if gErrs := r.validateGPUClassModes(ctx, app); len(gErrs) > 0 {
|
||||
return nil, apierrors.NewInvalid(r.gvk.GroupKind(), app.Name, gErrs)
|
||||
}
|
||||
if gErrs := r.validateGPUBudget(ctx, app, nil); len(gErrs) > 0 {
|
||||
return nil, apierrors.NewInvalid(r.gvk.GroupKind(), app.Name, gErrs)
|
||||
}
|
||||
@@ -607,6 +610,9 @@ func (r *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObje
|
||||
return nil, false, apierrors.NewInvalid(r.gvk.GroupKind(), app.Name, qErrs)
|
||||
}
|
||||
}
|
||||
if gErrs := r.validateGPUClassModes(ctx, app); len(gErrs) > 0 {
|
||||
return nil, false, apierrors.NewInvalid(r.gvk.GroupKind(), app.Name, gErrs)
|
||||
}
|
||||
if gErrs := r.validateGPUBudget(ctx, app, oldObj.(*appsv1alpha1.Application)); len(gErrs) > 0 {
|
||||
return nil, false, apierrors.NewInvalid(r.gvk.GroupKind(), app.Name, gErrs)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user