Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

additional vgpu validations #77

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pkg/webhook/vgpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func (v *vgpuValidator) Update(_ *types.Request, oldObj runtime.Object, newObj r
return checkVGPUUsage(v.kubevirtCache, newVGPUObj.Name)
}

// vGPU was enabled, run some basic sanity checks on request
if !oldVGPUObj.Spec.Enabled && newVGPUObj.Spec.Enabled {
return validateVGPUProfiles(oldVGPUObj, newVGPUObj)
}
return nil
}

Expand All @@ -73,3 +77,14 @@ func checkVGPUUsage(kc kubevirtctl.VirtualMachineCache, deviceName string) error

return nil
}

func validateVGPUProfiles(oldVGPUObj, newVGPUObj *devicesv1beta1.VGPUDevice) error {
if newVGPUObj.Spec.VGPUTypeName == "" {
return fmt.Errorf("VGPUTypeName cannot be empty for vGPU device %s", newVGPUObj.Name)
}

if _, ok := oldVGPUObj.Status.AvailableTypes[newVGPUObj.Spec.VGPUTypeName]; !ok {
return fmt.Errorf("VGPUTypeName %s is not a valid profile for vGPU device %s", newVGPUObj.Spec.VGPUTypeName, newVGPUObj.Name)
}
return nil
}
65 changes: 64 additions & 1 deletion pkg/webhook/vgpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,30 @@ var (
Enabled: false,
},
}

oldDisabledVGPU = &devicesv1beta1.VGPUDevice{
ObjectMeta: metav1.ObjectMeta{
Name: "vgpu1",
},
Spec: devicesv1beta1.VGPUDeviceSpec{
Enabled: false,
},
Status: devicesv1beta1.VGPUDeviceStatus{
AvailableTypes: map[string]string{
"GRID A100-8C": "nvidia-470",
"GRID A100-10C": "nvidia-471",
},
},
}
newEnabledVGPU = &devicesv1beta1.VGPUDevice{
ObjectMeta: metav1.ObjectMeta{
Name: "vgpu1",
},
Spec: devicesv1beta1.VGPUDeviceSpec{
Enabled: true,
},
}

vm1 = &kubevirtv1.VirtualMachine{
ObjectMeta: metav1.ObjectMeta{
Name: "vgpu-vm",
Expand Down Expand Up @@ -121,7 +145,7 @@ func Test_VGPUUpdates(t *testing.T) {
if v.expectError {
assert.Error(err, fmt.Sprintf("expected to find error for test case %s", v.name))
} else {
assert.NoError(err, fmt.Sprintf("expected to find no errorerror for test case %s", v.name))
assert.NoError(err, fmt.Sprintf("expected to find no error for test case %s", v.name))
}
}
}
Expand Down Expand Up @@ -156,3 +180,42 @@ func Test_VGPUDeletion(t *testing.T) {
}
}
}

func Test_validateVGPUProfile(t *testing.T) {
var testCases = []struct {
name string
gpu *devicesv1beta1.VGPUDevice
vGPUProfile string
expectError bool
}{
{
name: "no vGPU Profile",
gpu: oldDisabledVGPU,
expectError: true,
},
{
name: "invalid vGPU Profile",
gpu: newFreeVGPU,
vGPUProfile: "GRID A100-40C",
expectError: true,
},
{
name: "valid vGPU Profile",
gpu: newFreeVGPU,
vGPUProfile: "GRID A100-10C",
expectError: false,
},
}

assert := require.New(t)
for _, v := range testCases {
newEnabledVGPUCopy := newEnabledVGPU.DeepCopy()
newEnabledVGPUCopy.Spec.VGPUTypeName = v.vGPUProfile
err := validateVGPUProfiles(oldDisabledVGPU, newEnabledVGPUCopy)
if v.expectError {
assert.Error(err, fmt.Sprintf("expected to find error for test case: %s", v.name))
} else {
assert.NoError(err, fmt.Sprintf("expected to find no error for test case: %s", v.name))
}
}
}
Loading