-
Notifications
You must be signed in to change notification settings - Fork 3
fix(vd): move metadata sync to its own step #1776
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
6 commits
Select commit
Hold shift + click to select a range
48ab779
fix(vd): move metadata sync to its own step
575fd57
fix(vd): add original metadata unit test
c2ed5f5
test(vmrestore): enable annotations and labels checks
df3cb96
fix(vd): resolve review comments
adb3a22
fix(vd): remove unnecessary condition
99fb091
fix(vd): resolve review comments
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
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
72 changes: 0 additions & 72 deletions
72
images/virtualization-artifact/pkg/common/vdsnapshot/vdsnapshot.go
This file was deleted.
Oops, something went wrong.
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
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
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
193 changes: 193 additions & 0 deletions
193
...tualization-artifact/pkg/controller/vd/internal/source/step/add_original_metadata_step.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,193 @@ | ||
| /* | ||
| 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 step | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
|
||
| "github.com/deckhouse/virtualization-controller/pkg/common/annotations" | ||
| "github.com/deckhouse/virtualization-controller/pkg/common/object" | ||
| "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" | ||
| "github.com/deckhouse/virtualization-controller/pkg/controller/service" | ||
| "github.com/deckhouse/virtualization-controller/pkg/eventrecord" | ||
| "github.com/deckhouse/virtualization/api/core/v1alpha2" | ||
| "github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition" | ||
| ) | ||
|
|
||
| const lastAppliedConfigAnnotation = "kubectl.kubernetes.io/last-applied-configuration" | ||
|
|
||
| type AddOriginalMetadataStep struct { | ||
| recorder eventrecord.EventRecorderLogger | ||
| client client.Client | ||
| cb *conditions.ConditionBuilder | ||
| } | ||
|
|
||
| func NewAddOriginalMetadataStep( | ||
| recorder eventrecord.EventRecorderLogger, | ||
| client client.Client, | ||
| cb *conditions.ConditionBuilder, | ||
| ) *AddOriginalMetadataStep { | ||
| return &AddOriginalMetadataStep{ | ||
| recorder: recorder, | ||
| client: client, | ||
| cb: cb, | ||
| } | ||
| } | ||
|
|
||
| func (s AddOriginalMetadataStep) Take(ctx context.Context, vd *v1alpha2.VirtualDisk) (*reconcile.Result, error) { | ||
| vdSnapshot, err := object.FetchObject(ctx, types.NamespacedName{Name: vd.Spec.DataSource.ObjectRef.Name, Namespace: vd.Namespace}, s.client, &v1alpha2.VirtualDiskSnapshot{}) | ||
| if err != nil { | ||
| err = fmt.Errorf("failed to fetch the virtual disk snapshot: %w", err) | ||
| vd.Status.Phase = v1alpha2.DiskFailed | ||
| s.cb. | ||
| Status(metav1.ConditionFalse). | ||
| Reason(vdcondition.ProvisioningFailed). | ||
| Message(service.CapitalizeFirstLetter(err.Error() + ".")) | ||
| return nil, err | ||
| } | ||
|
|
||
| if vdSnapshot == nil { | ||
| vd.Status.Phase = v1alpha2.DiskPending | ||
| s.cb. | ||
| Status(metav1.ConditionFalse). | ||
| Reason(vdcondition.ProvisioningNotStarted). | ||
| Message(fmt.Sprintf("VirtualDiskSnapshot %q not found.", vd.Spec.DataSource.ObjectRef.Name)) | ||
| return &reconcile.Result{}, nil | ||
| } | ||
|
|
||
| vs, err := object.FetchObject(ctx, types.NamespacedName{Name: vdSnapshot.Status.VolumeSnapshotName, Namespace: vdSnapshot.Namespace}, s.client, &vsv1.VolumeSnapshot{}) | ||
| if err != nil { | ||
| err = fmt.Errorf("failed to fetch the volume snapshot: %w", err) | ||
| vd.Status.Phase = v1alpha2.DiskFailed | ||
| s.cb. | ||
| Status(metav1.ConditionFalse). | ||
| Reason(vdcondition.ProvisioningFailed). | ||
| Message(service.CapitalizeFirstLetter(err.Error() + ".")) | ||
| return nil, err | ||
| } | ||
|
|
||
| if vdSnapshot.Status.Phase != v1alpha2.VirtualDiskSnapshotPhaseReady || vs == nil || vs.Status == nil || vs.Status.ReadyToUse == nil || !*vs.Status.ReadyToUse { | ||
| vd.Status.Phase = v1alpha2.DiskPending | ||
| s.cb. | ||
| Status(metav1.ConditionFalse). | ||
| Reason(vdcondition.ProvisioningNotStarted). | ||
| Message(fmt.Sprintf("VirtualDiskSnapshot %q is not ready to use.", vdSnapshot.Name)) | ||
| return &reconcile.Result{}, nil | ||
| } | ||
|
|
||
| areAnnotationsAdded, err := setOriginalAnnotations(vd, vs) | ||
| if err != nil { | ||
| wrappedErr := fmt.Errorf("failed to set original annotations: %w", err) | ||
| vd.Status.Phase = v1alpha2.DiskFailed | ||
| s.cb. | ||
| Status(metav1.ConditionFalse). | ||
| Reason(vdcondition.ProvisioningFailed). | ||
| Message(service.CapitalizeFirstLetter(wrappedErr.Error() + ".")) | ||
| return nil, wrappedErr | ||
| } | ||
|
|
||
| areLabelsAdded, err := setOriginalLabels(vd, vs) | ||
| if err != nil { | ||
| wrappedErr := fmt.Errorf("failed to set original labels: %w", err) | ||
| vd.Status.Phase = v1alpha2.DiskFailed | ||
| s.cb. | ||
| Status(metav1.ConditionFalse). | ||
| Reason(vdcondition.ProvisioningFailed). | ||
| Message(service.CapitalizeFirstLetter(wrappedErr.Error() + ".")) | ||
| return nil, wrappedErr | ||
| } | ||
|
|
||
| // Ensure that new metadata is applied correctly because a conflict error can occur | ||
| // when updating the virtual disk resource. Therefore, this step should finish | ||
| // with reconciliation if the metadata has changed. | ||
| if areAnnotationsAdded || areLabelsAdded { | ||
| msg := "The original metadata sync has started" | ||
| s.recorder.Event( | ||
| vd, | ||
| corev1.EventTypeNormal, | ||
| v1alpha2.ReasonMetadataSyncStarted, | ||
| msg, | ||
| ) | ||
| s.cb. | ||
| Status(metav1.ConditionFalse). | ||
| Reason(vdcondition.Provisioning). | ||
| Message(msg) | ||
| return &reconcile.Result{}, nil | ||
| } | ||
|
|
||
| return nil, nil | ||
| } | ||
|
|
||
| func setOriginalAnnotations(vd *v1alpha2.VirtualDisk, vs *vsv1.VolumeSnapshot) (bool, error) { | ||
| var originalAnnotationsMap map[string]string | ||
| if vs.Annotations[annotations.AnnVirtualDiskOriginalAnnotations] != "" { | ||
| err := json.Unmarshal([]byte(vs.Annotations[annotations.AnnVirtualDiskOriginalAnnotations]), &originalAnnotationsMap) | ||
| if err != nil { | ||
| return false, fmt.Errorf("failed to unmarshal the original annotations: %w", err) | ||
| } | ||
| } | ||
|
|
||
| if vd.Annotations == nil { | ||
| vd.Annotations = make(map[string]string) | ||
| } | ||
|
|
||
| var areAnnotationsAdded bool | ||
| for key, originalvalue := range originalAnnotationsMap { | ||
| if key == lastAppliedConfigAnnotation { | ||
| continue | ||
| } | ||
| if currentValue, exists := vd.Annotations[key]; !exists || currentValue != originalvalue { | ||
| vd.Annotations[key] = originalvalue | ||
| areAnnotationsAdded = true | ||
| } | ||
| } | ||
|
|
||
| return areAnnotationsAdded, nil | ||
| } | ||
|
|
||
| func setOriginalLabels(vd *v1alpha2.VirtualDisk, vs *vsv1.VolumeSnapshot) (bool, error) { | ||
| var originalLabelsMap map[string]string | ||
| if vs.Annotations[annotations.AnnVirtualDiskOriginalLabels] != "" { | ||
| err := json.Unmarshal([]byte(vs.Annotations[annotations.AnnVirtualDiskOriginalLabels]), &originalLabelsMap) | ||
| if err != nil { | ||
| return false, fmt.Errorf("failed to unmarshal the original labels: %w", err) | ||
| } | ||
| } | ||
|
|
||
| if vd.Labels == nil { | ||
| vd.Labels = make(map[string]string) | ||
| } | ||
|
|
||
| var areLabelsAdded bool | ||
| for key, originalvalue := range originalLabelsMap { | ||
| if currentValue, exists := vd.Labels[key]; !exists || currentValue != originalvalue { | ||
| vd.Labels[key] = originalvalue | ||
| areLabelsAdded = true | ||
| } | ||
| } | ||
|
|
||
| return areLabelsAdded, nil | ||
| } | ||
Oops, something went wrong.
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.