Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions pkg/controller/plan/adapter/ocp/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

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

	object.Template = targetVmSpec.Template

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?


// 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)
Copy link
Member

Choose a reason for hiding this comment

The 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.
Expand Down
7 changes: 6 additions & 1 deletion pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -1942,7 +1942,12 @@ func (r *KubeVirt) vmTemplate(vm *plan.VMStatus) (virtualMachine *cnv.VirtualMac
virtualMachine.Namespace = r.Plan.Spec.TargetNamespace
virtualMachine.Spec.Template.Spec.Volumes = []cnv.Volume{}
virtualMachine.Spec.Template.Spec.Networks = []cnv.Network{}
virtualMachine.Spec.DataVolumeTemplates = []cnv.DataVolumeTemplateSpec{}
// Preserve DataVolumeTemplates for OCP source VMs to maintain user workflows
// that may expect the VM's DataVolume to be present. The OCP builder will
// set them from the source VM spec if they exist.
if !r.Plan.IsSourceProviderOCP() {
virtualMachine.Spec.DataVolumeTemplates = []cnv.DataVolumeTemplateSpec{}
}
delete(virtualMachine.Annotations, AnnKubevirtValidations)

ok = true
Expand Down
Loading