|
| 1 | +/* |
| 2 | +Copyright 2025 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 moduleconfig |
| 18 | + |
| 19 | +// import ( |
| 20 | +// "context" |
| 21 | +// "fmt" |
| 22 | +// "slices" |
| 23 | + |
| 24 | +// corev1 "k8s.io/api/core/v1" |
| 25 | +// cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1" |
| 26 | +// "sigs.k8s.io/controller-runtime/pkg/client" |
| 27 | +// "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 28 | + |
| 29 | +// mcapi "github.com/deckhouse/virtualization-controller/pkg/controller/moduleconfig/api" |
| 30 | +// ) |
| 31 | + |
| 32 | +// type viStorageClassValidator struct { |
| 33 | +// client client.Client |
| 34 | +// } |
| 35 | + |
| 36 | +// func newViStorageClassValidator(client client.Client) *viStorageClassValidator { |
| 37 | +// return &viStorageClassValidator{ |
| 38 | +// client: client, |
| 39 | +// } |
| 40 | +// } |
| 41 | + |
| 42 | +// func (v viStorageClassValidator) ValidateUpdate(ctx context.Context, _, newMC *mcapi.ModuleConfig) (admission.Warnings, error) { |
| 43 | +// warnings := make([]string, 0) |
| 44 | + |
| 45 | +// viScSettings := parseViStorageClass(newMC.Spec.Settings) |
| 46 | +// if viScSettings.DefaultStorageClassName != "" { |
| 47 | +// scWarnings, err := v.validateStorageClass(ctx, viScSettings.DefaultStorageClassName) |
| 48 | +// if err != nil { |
| 49 | +// return warnings, err |
| 50 | +// } |
| 51 | +// if len(scWarnings) != 0 { |
| 52 | +// warnings = append(warnings, scWarnings...) |
| 53 | +// } |
| 54 | +// } |
| 55 | + |
| 56 | +// if len(viScSettings.AllowedStorageClassSelector.MatchNames) != 0 { |
| 57 | +// for _, sc := range viScSettings.AllowedStorageClassSelector.MatchNames { |
| 58 | +// scWarnings, err := v.validateStorageClass(ctx, sc) |
| 59 | +// if err != nil { |
| 60 | +// return warnings, err |
| 61 | +// } |
| 62 | +// if len(scWarnings) != 0 { |
| 63 | +// warnings = append(warnings, scWarnings...) |
| 64 | +// } |
| 65 | +// } |
| 66 | +// } |
| 67 | + |
| 68 | +// return admission.Warnings{}, nil |
| 69 | +// } |
| 70 | + |
| 71 | +// func (v viStorageClassValidator) validateStorageClass(ctx context.Context, scName string) (admission.Warnings, error) { |
| 72 | +// scProfile := &cdiv1.StorageProfile{} |
| 73 | +// err := v.client.Get(ctx, client.ObjectKey{Name: scName}, scProfile, &client.GetOptions{}) |
| 74 | +// if err != nil { |
| 75 | +// return admission.Warnings{}, fmt.Errorf("failed to obtain the `StorageProfile` %s: %w", scName, err) |
| 76 | +// } |
| 77 | +// if len(scProfile.Status.ClaimPropertySets) == 0 { |
| 78 | +// return admission.Warnings{}, fmt.Errorf("failed to validate the `PersistentVolumeMode` of the `StorageProfile`: %s", scName) |
| 79 | +// } |
| 80 | + |
| 81 | +// var valid bool |
| 82 | +// for _, cps := range scProfile.Spec.ClaimPropertySets { |
| 83 | +// if slices.Contains(cps.AccessModes, corev1.ReadWriteMany) && *cps.VolumeMode == corev1.PersistentVolumeBlock { |
| 84 | +// valid = true |
| 85 | +// } |
| 86 | +// } |
| 87 | + |
| 88 | +// if !valid { |
| 89 | +// return admission.Warnings{}, fmt.Errorf("the storage class %q is not supported in the current version due to known compatibility issues with virtual images; please choose a different storage class", scName) |
| 90 | +// } |
| 91 | + |
| 92 | +// return admission.Warnings{}, nil |
| 93 | +// } |
| 94 | + |
| 95 | +// type viStorageClassSettings struct { |
| 96 | +// DefaultStorageClassName string |
| 97 | +// AllowedStorageClassSelector AllowedStorageClassSelector |
| 98 | +// } |
| 99 | + |
| 100 | +// type AllowedStorageClassSelector struct { |
| 101 | +// MatchNames []string |
| 102 | +// } |
| 103 | + |
| 104 | +// func parseViStorageClass(settings mcapi.SettingsValues) *viStorageClassSettings { |
| 105 | +// viScSettings := &viStorageClassSettings{} |
| 106 | +// if virtualImages, ok := settings["virtualImages"].(map[string]interface{}); ok { |
| 107 | +// if defaultClass, ok := virtualImages["defaultStorageClassName"].(string); ok { |
| 108 | +// viScSettings.DefaultStorageClassName = defaultClass |
| 109 | +// } |
| 110 | + |
| 111 | +// if allowedSelector, ok := virtualImages["allowedStorageClassSelector"].(map[string]interface{}); ok { |
| 112 | +// if matchNames, ok := allowedSelector["matchNames"].([]interface{}); ok { |
| 113 | +// var matchNameStrings []string |
| 114 | +// for _, name := range matchNames { |
| 115 | +// if strName, ok := name.(string); ok { |
| 116 | +// matchNameStrings = append(matchNameStrings, strName) |
| 117 | +// } |
| 118 | +// } |
| 119 | +// viScSettings.AllowedStorageClassSelector.MatchNames = matchNameStrings |
| 120 | +// } |
| 121 | +// } |
| 122 | +// } |
| 123 | + |
| 124 | +// return viScSettings |
| 125 | +// } |
0 commit comments