-
Notifications
You must be signed in to change notification settings - Fork 2
fix(vdsnapshot): resolve fsfreeze race condition #1668
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
Open
hardcoretime
wants to merge
16
commits into
main
Choose a base branch
from
fix/vdsnapshot/freeze-request-race-condition
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
86e0af4
fix(vdsnapshot): resolve fsfreeze race condition
5ecaac3
fix(vdsnapshot): change IsFrozen func
77d43fb
fix(vdsnapshot): fix unit test
4dd30d7
fix(vdsnapshot): optimize getVirtualMachine func
1fd26e7
fix(vdsnapshot): fetch object once per reconciliation
7a368d7
fix(vdsnapshot): resolve review comments
0987034
fix: review comments
5f488b6
fix logic
880460e
fix: mark snapshot consistent even requiredConsistency is false
7734259
fix unit tests
24f8665
fix unit test
0d6444b
fix ready sync unfreeze request
4e3c331
fix kvvmi watcher and add new condition reason
dadad98
fix unit test
68feac4
fix vmsnapshot status sync and add kvvmi watcher
9065409
fix unit tests
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,23 +18,34 @@ package service | |
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| virtv1 "kubevirt.io/api/core/v1" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| "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/api/client/kubeclient" | ||
| "github.com/deckhouse/virtualization/api/core/v1alpha2" | ||
| "github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition" | ||
| subv1alpha2 "github.com/deckhouse/virtualization/api/subresources/v1alpha2" | ||
| ) | ||
|
|
||
| const ( | ||
| RequestFSFreeze = "freeze" | ||
| RequestFSUnfreeze = "unfreeze" | ||
| FSFrozen = "frozen" | ||
| ) | ||
|
|
||
| var ( | ||
| ErrUntrustedFilesystemFrozenCondition = errors.New("the filesystem status cannot be processed correctly") | ||
| ErrUnexpectedFilesystemFrozenRequest = errors.New("found unexpected filesystem frozen request in the virtual machine annotations") | ||
hardcoretime marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| type SnapshotService struct { | ||
| virtClient kubeclient.Client | ||
| client Client | ||
|
|
@@ -49,37 +60,72 @@ func NewSnapshotService(virtClient kubeclient.Client, client Client, protection | |
| } | ||
| } | ||
|
|
||
| func (s *SnapshotService) IsFrozen(vm *v1alpha2.VirtualMachine) bool { | ||
| if vm == nil { | ||
| return false | ||
| // IsFrozen checks if a freeze or unfreeze request has been performed | ||
| // and returns the "true" fsFreezeStatus if the internal virtual machine instance is "frozen", | ||
| // and "false" otherwise. | ||
| func (s *SnapshotService) IsFrozen(ctx context.Context, kvvmi *virtv1.VirtualMachineInstance) (bool, error) { | ||
hardcoretime marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if kvvmi == nil { | ||
| return false, nil | ||
| } | ||
|
|
||
| filesystemFrozen, _ := conditions.GetCondition(vmcondition.TypeFilesystemFrozen, vm.Status.Conditions) | ||
| if _, ok := kvvmi.Annotations[annotations.AnnVMFilesystemFrozenRequest]; ok { | ||
| return false, fmt.Errorf("failed to check %s/%s fsFreezeStatus: %w", kvvmi.Namespace, kvvmi.Name, ErrUntrustedFilesystemFrozenCondition) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let’s add to the error which exact value was found in the annotation |
||
| } | ||
|
|
||
| return filesystemFrozen.Status == metav1.ConditionTrue && filesystemFrozen.Reason == vmcondition.ReasonFilesystemFrozen.String() | ||
| return kvvmi.Status.FSFreezeStatus == FSFrozen, nil | ||
| } | ||
|
|
||
| func (s *SnapshotService) CanFreeze(vm *v1alpha2.VirtualMachine) bool { | ||
| if vm == nil || vm.Status.Phase != v1alpha2.MachineRunning || s.IsFrozen(vm) { | ||
| return false | ||
| func (s *SnapshotService) CanFreeze(ctx context.Context, kvvmi *virtv1.VirtualMachineInstance) (bool, error) { | ||
| if kvvmi == nil || kvvmi.Status.Phase != virtv1.Running { | ||
| return false, nil | ||
| } | ||
|
|
||
| agentReady, _ := conditions.GetCondition(vmcondition.TypeAgentReady, vm.Status.Conditions) | ||
| isFrozen, err := s.IsFrozen(ctx, kvvmi) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| if isFrozen { | ||
| return false, nil | ||
| } | ||
|
|
||
| return agentReady.Status == metav1.ConditionTrue | ||
| for _, c := range kvvmi.Status.Conditions { | ||
| if c.Type == virtv1.VirtualMachineInstanceAgentConnected { | ||
| return c.Status == corev1.ConditionTrue, nil | ||
| } | ||
| } | ||
|
|
||
| return false, nil | ||
| } | ||
|
|
||
| func (s *SnapshotService) Freeze(ctx context.Context, name, namespace string) error { | ||
| err := s.virtClient.VirtualMachines(namespace).Freeze(ctx, name, subv1alpha2.VirtualMachineFreeze{}) | ||
| func (s *SnapshotService) Freeze(ctx context.Context, kvvmi *virtv1.VirtualMachineInstance) error { | ||
| if request, ok := kvvmi.Annotations[annotations.AnnVMFilesystemFrozenRequest]; ok { | ||
| return fmt.Errorf("failed to freeze %s/%s virtual machine filesystem: %w: request type: %s", kvvmi.Namespace, kvvmi.Name, ErrUnexpectedFilesystemFrozenRequest, request) | ||
| } | ||
|
|
||
| err := s.annotateWithFSFreezeRequest(ctx, RequestFSFreeze, kvvmi) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to freeze virtual machine %s/%s: %w", namespace, name, err) | ||
| return fmt.Errorf("failed to annotate internal virtual machine instance with filesystem freeze request: %w", err) | ||
| } | ||
|
|
||
| err = s.virtClient.VirtualMachines(kvvmi.Namespace).Freeze(ctx, kvvmi.Name, subv1alpha2.VirtualMachineFreeze{}) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to freeze %s/%s virtual machine filesystem: %w", kvvmi.Namespace, kvvmi.Name, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (s *SnapshotService) CanUnfreezeWithVirtualDiskSnapshot(ctx context.Context, vdSnapshotName string, vm *v1alpha2.VirtualMachine) (bool, error) { | ||
| if vm == nil || !s.IsFrozen(vm) { | ||
| func (s *SnapshotService) CanUnfreezeWithVirtualDiskSnapshot(ctx context.Context, vdSnapshotName string, vm *v1alpha2.VirtualMachine, kvvmi *virtv1.VirtualMachineInstance) (bool, error) { | ||
| if vm == nil { | ||
| return false, nil | ||
| } | ||
|
|
||
| isFrozen, err := s.IsFrozen(ctx, kvvmi) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| if !isFrozen { | ||
| return false, nil | ||
| } | ||
|
|
||
|
|
@@ -91,7 +137,7 @@ func (s *SnapshotService) CanUnfreezeWithVirtualDiskSnapshot(ctx context.Context | |
| } | ||
|
|
||
| var vdSnapshots v1alpha2.VirtualDiskSnapshotList | ||
| err := s.client.List(ctx, &vdSnapshots, &client.ListOptions{ | ||
| err = s.client.List(ctx, &vdSnapshots, &client.ListOptions{ | ||
| Namespace: vm.Namespace, | ||
| }) | ||
| if err != nil { | ||
|
|
@@ -126,8 +172,16 @@ func (s *SnapshotService) CanUnfreezeWithVirtualDiskSnapshot(ctx context.Context | |
| return true, nil | ||
| } | ||
|
|
||
| func (s *SnapshotService) CanUnfreezeWithVirtualMachineSnapshot(ctx context.Context, vmSnapshotName string, vm *v1alpha2.VirtualMachine) (bool, error) { | ||
| if vm == nil || !s.IsFrozen(vm) { | ||
| func (s *SnapshotService) CanUnfreezeWithVirtualMachineSnapshot(ctx context.Context, vmSnapshotName string, vm *v1alpha2.VirtualMachine, kvvmi *virtv1.VirtualMachineInstance) (bool, error) { | ||
| if vm == nil { | ||
| return false, nil | ||
| } | ||
|
|
||
| isFrozen, err := s.IsFrozen(ctx, kvvmi) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| if !isFrozen { | ||
| return false, nil | ||
| } | ||
|
|
||
|
|
@@ -139,7 +193,7 @@ func (s *SnapshotService) CanUnfreezeWithVirtualMachineSnapshot(ctx context.Cont | |
| } | ||
|
|
||
| var vdSnapshots v1alpha2.VirtualDiskSnapshotList | ||
| err := s.client.List(ctx, &vdSnapshots, &client.ListOptions{ | ||
| err = s.client.List(ctx, &vdSnapshots, &client.ListOptions{ | ||
| Namespace: vm.Namespace, | ||
| }) | ||
| if err != nil { | ||
|
|
@@ -174,10 +228,19 @@ func (s *SnapshotService) CanUnfreezeWithVirtualMachineSnapshot(ctx context.Cont | |
| return true, nil | ||
| } | ||
|
|
||
| func (s *SnapshotService) Unfreeze(ctx context.Context, name, namespace string) error { | ||
| err := s.virtClient.VirtualMachines(namespace).Unfreeze(ctx, name) | ||
| func (s *SnapshotService) Unfreeze(ctx context.Context, kvvmi *virtv1.VirtualMachineInstance) error { | ||
| if request, ok := kvvmi.Annotations[annotations.AnnVMFilesystemFrozenRequest]; ok { | ||
| return fmt.Errorf("failed to unfreeze %s/%s virtual machine filesystem: %w: request type: %s", kvvmi.Namespace, kvvmi.Name, ErrUnexpectedFilesystemFrozenRequest, request) | ||
| } | ||
|
|
||
| err := s.annotateWithFSFreezeRequest(ctx, RequestFSUnfreeze, kvvmi) | ||
| if err != nil { | ||
| return fmt.Errorf("unfreeze virtual machine %s/%s: %w", namespace, name, err) | ||
| return fmt.Errorf("failed to annotate internal virtual machine instance with filesystem unfreeze request: %w", err) | ||
| } | ||
|
|
||
| err = s.virtClient.VirtualMachines(kvvmi.Namespace).Unfreeze(ctx, kvvmi.Name) | ||
| if err != nil { | ||
| return fmt.Errorf("unfreeze virtual machine %s/%s: %w", kvvmi.Namespace, kvvmi.Name, err) | ||
| } | ||
|
|
||
| return nil | ||
|
|
@@ -243,3 +306,70 @@ func (s *SnapshotService) CreateVirtualDiskSnapshot(ctx context.Context, vdSnaps | |
|
|
||
| return vdSnapshot, nil | ||
| } | ||
|
|
||
| func (s *SnapshotService) GetKubeVirtVirtualMachineInstance(ctx context.Context, vm *v1alpha2.VirtualMachine) (*virtv1.VirtualMachineInstance, error) { | ||
| if vm == nil { | ||
| return nil, nil | ||
| } | ||
| return object.FetchObject(ctx, client.ObjectKeyFromObject(vm), s.client, &virtv1.VirtualMachineInstance{}) | ||
| } | ||
|
|
||
| func (s *SnapshotService) annotateWithFSFreezeRequest(ctx context.Context, requestType string, kvvmi *virtv1.VirtualMachineInstance) error { | ||
| if kvvmi == nil { | ||
hardcoretime marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return nil | ||
| } | ||
|
|
||
| if kvvmi.Annotations == nil { | ||
| kvvmi.Annotations = make(map[string]string) | ||
| } | ||
| kvvmi.Annotations[annotations.AnnVMFilesystemFrozenRequest] = requestType | ||
|
|
||
| err := s.client.Update(ctx, kvvmi) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (s *SnapshotService) removeAnnFSFreezeRequest(ctx context.Context, kvvmi *virtv1.VirtualMachineInstance) error { | ||
| if kvvmi == nil || kvvmi.Annotations == nil { | ||
hardcoretime marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return nil | ||
| } | ||
|
|
||
| delete(kvvmi.Annotations, annotations.AnnVMFilesystemFrozenRequest) | ||
|
|
||
| err := s.client.Update(ctx, kvvmi) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (s *SnapshotService) SyncFSFreezeRequest(ctx context.Context, kvvmi *virtv1.VirtualMachineInstance) error { | ||
| if kvvmi == nil { | ||
| return nil | ||
| } | ||
|
|
||
| if request, ok := kvvmi.Annotations[annotations.AnnVMFilesystemFrozenRequest]; ok { | ||
hardcoretime marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| switch { | ||
| case request == RequestFSFreeze && kvvmi.Status.FSFreezeStatus == FSFrozen: | ||
| err := s.removeAnnFSFreezeRequest(ctx, kvvmi) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to sync kvvmi %s/%s fsFreezeStatus: %w", kvvmi.Namespace, kvvmi.Name, err) | ||
hardcoretime marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return nil | ||
| case request == RequestFSUnfreeze && kvvmi.Status.FSFreezeStatus != FSFrozen: | ||
| err := s.removeAnnFSFreezeRequest(ctx, kvvmi) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to sync kvvmi %s/%s fsFreezeStatus: %w", kvvmi.Namespace, kvvmi.Name, err) | ||
| } | ||
| return nil | ||
| default: | ||
| return fmt.Errorf("failed to sync kvvmi %s/%s fsFreezeStatus: %w", kvvmi.Namespace, kvvmi.Name, ErrUntrustedFilesystemFrozenCondition) | ||
hardcoretime marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| return 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
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.
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.