|
| 1 | +/* |
| 2 | +Copyright 2026 Flant JSC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package validators |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + resourcev1 "k8s.io/api/resource/v1" |
| 24 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 25 | + "k8s.io/component-base/featuregate" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 28 | + |
| 29 | + "github.com/deckhouse/virtualization-controller/pkg/controller/kvbuilder" |
| 30 | + "github.com/deckhouse/virtualization-controller/pkg/featuregates" |
| 31 | + "github.com/deckhouse/virtualization/api/core/v1alpha2" |
| 32 | +) |
| 33 | + |
| 34 | +type GPUDevicesValidator struct { |
| 35 | + client client.Client |
| 36 | + featureGate featuregate.FeatureGate |
| 37 | +} |
| 38 | + |
| 39 | +func NewGPUDevicesValidator(client client.Client, featureGate featuregate.FeatureGate) *GPUDevicesValidator { |
| 40 | + return &GPUDevicesValidator{client: client, featureGate: featureGate} |
| 41 | +} |
| 42 | + |
| 43 | +func (v *GPUDevicesValidator) ValidateCreate(ctx context.Context, vm *v1alpha2.VirtualMachine) (admission.Warnings, error) { |
| 44 | + return nil, v.validateGPUDevices(ctx, vm) |
| 45 | +} |
| 46 | + |
| 47 | +func (v *GPUDevicesValidator) ValidateUpdate(ctx context.Context, _, newVM *v1alpha2.VirtualMachine) (admission.Warnings, error) { |
| 48 | + return nil, v.validateGPUDevices(ctx, newVM) |
| 49 | +} |
| 50 | + |
| 51 | +func (v *GPUDevicesValidator) validateGPUDevices(ctx context.Context, vm *v1alpha2.VirtualMachine) error { |
| 52 | + if len(vm.Spec.GPUDevices) == 0 { |
| 53 | + return nil |
| 54 | + } |
| 55 | + |
| 56 | + if !v.featureGate.Enabled(featuregates.GPU) { |
| 57 | + return fmt.Errorf("GPU device attachment requires the GPU feature gate") |
| 58 | + } |
| 59 | + |
| 60 | + deviceClass := &resourcev1.DeviceClass{} |
| 61 | + err := v.client.Get(ctx, client.ObjectKey{Name: kvbuilder.GPUDeviceClassName}, deviceClass) |
| 62 | + if err == nil { |
| 63 | + return nil |
| 64 | + } |
| 65 | + if apierrors.IsNotFound(err) { |
| 66 | + return fmt.Errorf("GPU device attachment requires DeviceClass %q", kvbuilder.GPUDeviceClassName) |
| 67 | + } |
| 68 | + return fmt.Errorf("failed to get GPU DeviceClass %q: %w", kvbuilder.GPUDeviceClassName, err) |
| 69 | +} |
0 commit comments