-
Notifications
You must be signed in to change notification settings - Fork 3
feat(module): validate dvcr section in ModuleConfig #1628
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
images/virtualization-artifact/pkg/controller/moduleconfig/dvcr_validator.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| /* | ||
| Copyright 2025 Flant JSC | ||
|
|
||
| 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 moduleconfig | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/api/resource" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
|
||
| "github.com/deckhouse/virtualization-controller/pkg/common" | ||
| "github.com/deckhouse/virtualization-controller/pkg/common/object" | ||
| mcapi "github.com/deckhouse/virtualization-controller/pkg/controller/moduleconfig/api" | ||
| ) | ||
|
|
||
| const ( | ||
| dvcrPVCName = "dvcr" | ||
| dvcrNamespace = "d8-virtualization" | ||
| dvcrPVCStorageType = "PersistentVolumeClaim" | ||
|
|
||
| // Settings field names | ||
| dvcrField = "dvcr" | ||
| storageField = "storage" | ||
| typeField = "type" | ||
| persistentVolumeClaimField = "persistentVolumeClaim" | ||
| storageClassNameField = "storageClassName" | ||
| sizeField = "size" | ||
| ) | ||
|
|
||
| type dvcrValidator struct { | ||
| client client.Client | ||
| } | ||
|
|
||
| func newDvcrValidator(client client.Client) *dvcrValidator { | ||
| return &dvcrValidator{ | ||
| client: client, | ||
| } | ||
| } | ||
|
|
||
| func (v dvcrValidator) ValidateUpdate(ctx context.Context, oldMC, newMC *mcapi.ModuleConfig) (admission.Warnings, error) { | ||
| oldDvcr, err := parseDvcrSettings(oldMC.Spec.Settings) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| newDvcr, err := parseDvcrSettings(newMC.Spec.Settings) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Only validate PersistentVolumeClaim settings | ||
| if newDvcr.StorageType != dvcrPVCStorageType { | ||
| return nil, nil | ||
| } | ||
|
|
||
| // Check if DVCR PVC exists | ||
| pvcExists, err := v.checkPVCExists(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("internal error: unable to check DVCR PVC existence: %w", err) | ||
| } | ||
|
|
||
| // Only validate if PVC exists | ||
| if !pvcExists { | ||
| return nil, nil | ||
| } | ||
|
|
||
| // Validate storageClassName hasn't changed | ||
| if oldDvcr.StorageClassName != newDvcr.StorageClassName { | ||
| return nil, fmt.Errorf( | ||
| "changing storageClassName for DVCR is forbidden when PVC already exists: old=%q, new=%q", | ||
| oldDvcr.StorageClassName, | ||
| newDvcr.StorageClassName, | ||
| ) | ||
| } | ||
|
|
||
| // Validate size hasn't been reduced | ||
| if newDvcr.Size.Cmp(oldDvcr.Size) == common.CmpLesser { | ||
| return nil, fmt.Errorf( | ||
| "reducing DVCR size is forbidden when PVC already exists: old=%s, new=%s", | ||
| oldDvcr.Size.String(), | ||
| newDvcr.Size.String(), | ||
| ) | ||
| } | ||
|
|
||
| return nil, nil | ||
| } | ||
|
|
||
| func (v dvcrValidator) checkPVCExists(ctx context.Context) (bool, error) { | ||
| pvc, err := object.FetchObject(ctx, types.NamespacedName{ | ||
| Name: dvcrPVCName, | ||
| Namespace: dvcrNamespace, | ||
| }, v.client, &corev1.PersistentVolumeClaim{}) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| return pvc != nil, nil | ||
| } | ||
|
|
||
| type dvcrSettings struct { | ||
| StorageType string | ||
| StorageClassName string | ||
| Size resource.Quantity | ||
| } | ||
|
|
||
| func parseDvcrSettings(settings mcapi.SettingsValues) (*dvcrSettings, error) { | ||
| result := &dvcrSettings{} | ||
|
|
||
| dvcr, ok := settings[dvcrField].(map[string]interface{}) | ||
| if !ok { | ||
| return nil, fmt.Errorf("failed to parse %s settings", dvcrField) | ||
| } | ||
|
|
||
| storage, ok := dvcr[storageField].(map[string]interface{}) | ||
| if !ok { | ||
| return nil, fmt.Errorf("failed to parse %s.%s settings", dvcrField, storageField) | ||
| } | ||
|
|
||
| storageType, ok := storage[typeField].(string) | ||
| if !ok { | ||
| return nil, fmt.Errorf("failed to parse %s.%s.%s", dvcrField, storageField, typeField) | ||
| } | ||
| result.StorageType = storageType | ||
|
|
||
| // Only parse PVC fields if type is PersistentVolumeClaim | ||
| if storageType == dvcrPVCStorageType { | ||
| pvc, ok := storage[persistentVolumeClaimField].(map[string]interface{}) | ||
| if !ok { | ||
| return nil, fmt.Errorf("failed to parse %s.%s.%s", dvcrField, storageField, persistentVolumeClaimField) | ||
| } | ||
|
|
||
| // storageClassName is optional | ||
| if storageClassName, ok := pvc[storageClassNameField].(string); ok { | ||
| result.StorageClassName = storageClassName | ||
| } | ||
|
|
||
| // size is required by OpenAPI schema | ||
| sizeStr, ok := pvc[sizeField].(string) | ||
| if !ok { | ||
| return nil, fmt.Errorf("failed to parse %s.%s.%s.%s", dvcrField, storageField, persistentVolumeClaimField, sizeField) | ||
| } | ||
|
|
||
| size, err := resource.ParseQuantity(sizeStr) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to parse DVCR size %q: %w", sizeStr, err) | ||
| } | ||
| result.Size = size | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.