-
Notifications
You must be signed in to change notification settings - Fork 82
MTV-2419 | VM spec should be preserved if possible #4290
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
gwencasey96
wants to merge
2
commits into
kubev2v:main
Choose a base branch
from
gwencasey96:vm-spec-should-be-preserved-if-possibler
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.
+136
−1
Open
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
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 |
|---|---|---|
|
|
@@ -279,11 +279,141 @@ func (r *Builder) VirtualMachine(vmRef ref.Ref, object *cnv.VirtualMachineSpec, | |
|
|
||
| targetVmSpec := sourceVm.Spec.DeepCopy() | ||
| object.Template = targetVmSpec.Template | ||
| // Preserve DataVolumeTemplates from source VM to maintain user workflows | ||
| // that may expect the VM's DataVolume to be present | ||
| object.DataVolumeTemplates = targetVmSpec.DataVolumeTemplates | ||
|
|
||
| // Sanitize DataVolumeTemplates to prevent conflicts with Forklift-created DataVolumes: | ||
| // 1. Match template names to the PVC names (Forklift creates DataVolumes with Name = pvc.Name) | ||
| // 2. Clear spec.source to prevent KubeVirt from trying to create new DataVolumes with invalid sources | ||
| // 3. Ensure namespace is set correctly (will be set by KubeVirt to match VM namespace) | ||
| // 4. Update volume references to match renamed DataVolumeTemplates | ||
| r.sanitizeDataVolumeTemplates(vmRef, object.DataVolumeTemplates, object) | ||
|
Member
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. don't think we need this we are trying to persist the VM spec as much as possible to the source, my main question was about the disks themselves and the data on them |
||
|
|
||
| r.mapNetworks(sourceVm, targetVmSpec) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // sanitizeDataVolumeTemplates ensures DataVolumeTemplates are compatible with Forklift-created DataVolumes. | ||
| // Forklift creates DataVolumes with Name = pvc.Name, so templates must match these names. | ||
| // We also clear spec.source to prevent KubeVirt from trying to create new DataVolumes with invalid source URLs. | ||
| // Additionally, we update volume references to match any renamed DataVolumeTemplates. | ||
| func (r *Builder) sanitizeDataVolumeTemplates(vmRef ref.Ref, templates []cnv.DataVolumeTemplateSpec, object *cnv.VirtualMachineSpec) { | ||
| // Build a map of volume name -> PVC name from VM volumes | ||
| // This allows us to match DataVolumeTemplates to the PVCs that Forklift will create DataVolumes for | ||
| volumeToPVCName := make(map[string]string) | ||
| sourceVm, err := r.getSourceVmFromDefinition(vmRef) | ||
| if err != nil { | ||
| r.Log.Error(err, "Failed to get source VM for DataVolumeTemplate sanitization") | ||
| return | ||
| } | ||
|
|
||
| for _, vol := range sourceVm.Spec.Template.Spec.Volumes { | ||
| var pvcName string | ||
| switch { | ||
| case vol.PersistentVolumeClaim != nil: | ||
| // Volume references PVC directly | ||
| pvcName = vol.PersistentVolumeClaim.ClaimName | ||
| case vol.DataVolume != nil: | ||
| // Volume references DataVolume - need to find the PVC it creates | ||
| // In OCP, DataVolumes typically create PVCs with the same name as the DataVolume | ||
| // But we need to check the actual PVC name from the source | ||
| dv := &cdi.DataVolume{} | ||
| err := r.sourceClient.Get(context.TODO(), client.ObjectKey{ | ||
| Namespace: vmRef.Namespace, | ||
| Name: vol.DataVolume.Name, | ||
| }, dv) | ||
| if err != nil { | ||
| r.Log.V(1).Info("Could not find source DataVolume, using DataVolume name as PVC name", | ||
| "dataVolume", vol.DataVolume.Name, "error", err) | ||
| pvcName = vol.DataVolume.Name | ||
| } else { | ||
| // Use the actual PVC name that the DataVolume created | ||
| if dv.Status.ClaimName != "" { | ||
| pvcName = dv.Status.ClaimName | ||
| } else { | ||
| // Fallback: DataVolume name typically matches PVC name | ||
| pvcName = vol.DataVolume.Name | ||
| } | ||
| } | ||
| default: | ||
| continue | ||
| } | ||
| if pvcName != "" { | ||
| volumeToPVCName[vol.Name] = pvcName | ||
| } | ||
| } | ||
|
|
||
| // Track template name changes so we can update volume references | ||
| templateNameMap := make(map[string]string) // old name -> new name | ||
|
|
||
| // Sanitize each template | ||
| for i := range templates { | ||
| template := &templates[i] | ||
| oldName := template.Name | ||
|
|
||
| // Find the corresponding PVC name by matching template to VM volumes | ||
| // DataVolumeTemplates are referenced by volumes via vol.DataVolume.Name matching template name | ||
| var targetPVCName string | ||
| for volName, pvcName := range volumeToPVCName { | ||
| // Check if any volume references this template by name | ||
| for _, vol := range sourceVm.Spec.Template.Spec.Volumes { | ||
| if vol.Name == volName && vol.DataVolume != nil && vol.DataVolume.Name == template.Name { | ||
| targetPVCName = pvcName | ||
| break | ||
| } | ||
| } | ||
| if targetPVCName != "" { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| // If we found a matching PVC name, update the template name to match | ||
| // Forklift creates DataVolumes with Name = pvc.Name, so template must match | ||
| if targetPVCName != "" && template.Name != targetPVCName { | ||
| template.Name = targetPVCName | ||
| template.GenerateName = "" | ||
| templateNameMap[oldName] = targetPVCName | ||
| r.Log.V(1).Info("Updated DataVolumeTemplate name to match Forklift-created DataVolume", | ||
| "oldName", oldName, "newName", template.Name, "pvcName", targetPVCName) | ||
| } else { | ||
| // If we can't find a match, clear GenerateName to use explicit Name | ||
| // and log a warning | ||
| if template.GenerateName != "" { | ||
| r.Log.Info("DataVolumeTemplate has GenerateName but no matching volume found, "+ | ||
| "template may not match Forklift-created DataVolume", | ||
| "generateName", template.GenerateName, "templateName", template.Name) | ||
| template.GenerateName = "" | ||
| } | ||
| } | ||
|
|
||
| // Clear spec.source to prevent KubeVirt from trying to create new DataVolumes | ||
| // with source URLs/namespaces that don't exist on the destination cluster. | ||
| // Forklift has already created the DataVolumes, so templates should not have source specs. | ||
| template.Spec.Source = nil | ||
|
|
||
| // Ensure namespace is empty - KubeVirt will set it to match the VM namespace | ||
| template.Namespace = "" | ||
| } | ||
|
|
||
| // Update volume references to match renamed DataVolumeTemplates | ||
| // Volumes that reference DataVolumes by name need to be updated if the template name changed | ||
| if len(templateNameMap) > 0 && object.Template != nil { | ||
| for i := range object.Template.Spec.Volumes { | ||
| vol := &object.Template.Spec.Volumes[i] | ||
| if vol.DataVolume != nil { | ||
| if newName, renamed := templateNameMap[vol.DataVolume.Name]; renamed { | ||
| oldDVName := vol.DataVolume.Name | ||
| vol.DataVolume.Name = newName | ||
| r.Log.V(1).Info("Updated volume DataVolume reference to match renamed template", | ||
| "volumeName", vol.Name, "oldDVName", oldDVName, "newDVName", newName) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // ConfigMaps builds CRs for each of the ConfigMaps that the source VM depends upon. | ||
| // Migration labels are set to track when they were first created, but since these may be | ||
| // used by more than one VM they are not labeled with the VM id. | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This preserves the spec, but what about the disks themselves referenced in the spec?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you-there were a few things in my initial PR I needed to think through more. Now the DataVolumeTemplate names are mapped to the PVC names that Forklift uses when creating DataVolumes and sanitized. The volume references will also be updated when template names change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The disk references are preserved in line above so we don't need anything else
but still question is: Are we migrating the datavolume referenced in the VM?
If you look at the export api which is used for the cold migraiton it support datavolumes. Are we using it already in a way that the datavolumes data are copied?