Refactor ReplacePVCByHostDisk
Extract common functions
@@ -72,41 +72,54 @@ func ReplacePVCByHostDisk(vmi *v1.VirtualMachineInstance) error {
for i := range vmi.Spec.Volumes {
volume := vmi.Spec.Volumes[i]
- if volumeSource := &vmi.Spec.Volumes[i].VolumeSource; volumeSource.PersistentVolumeClaim != nil {
- // If a PVC is used in a Filesystem (passthough), it should not be mapped as a HostDisk and a image file should
- // not be created.
- if _, isPassthoughFSVolume := passthoughFSVolumes[volume.Name]; isPassthoughFSVolume {
- log.Log.V(4).Infof("this volume %s is mapped as a filesystem passthrough, will not be replaced by HostDisk", volume.Name)
+ volumeSource := &vmi.Spec.Volumes[i].VolumeSource
+ if volumeSource.PersistentVolumeClaim != nil {
+ if shouldSkipVolumeSource(passthoughFSVolumes, hotplugVolumes, pvcVolume, volume.Name) {
continue
}
- if hotplugVolumes[volume.Name] {
- log.Log.V(4).Infof("this volume %s is hotplugged, will not be replaced by HostDisk", volume.Name)
- continue
+ replaceForHostDisk(volumeSource, volume.Name, pvcVolume)
+ // PersistenVolumeClaim is replaced by HostDisk
+ volumeSource.PersistentVolumeClaim = nil
}
-
- volumeStatus, ok := pvcVolume[volume.Name]
- if !ok ||
- volumeStatus.PersistentVolumeClaimInfo.VolumeMode == nil ||
- *volumeStatus.PersistentVolumeClaimInfo.VolumeMode == k8sv1.PersistentVolumeBlock {
-
- // This is not a disk on a file system, so skip it.
- continue
+ }
+ return nil
}
+func replaceForHostDisk(volumeSource *v1.VolumeSource, volumeName string, pvcVolume map[string]v1.VolumeStatus) {
+ volumeStatus := pvcVolume[volumeName]
isShared := types.HasSharedAccessMode(volumeStatus.PersistentVolumeClaimInfo.AccessModes)
- file := getPVCDiskImgPath(vmi.Spec.Volumes[i].Name, "disk.img")
+ file := getPVCDiskImgPath(volumeName, "disk.img")
volumeSource.HostDisk = &v1.HostDisk{
Path: file,
Type: v1.HostDiskExistsOrCreate,
Capacity: volumeStatus.PersistentVolumeClaimInfo.Capacity[k8sv1.ResourceStorage],
Shared: &isShared,
}
- // PersistenVolumeClaim is replaced by HostDisk
- volumeSource.PersistentVolumeClaim = nil
}
+
+func shouldSkipVolumeSource(passthoughFSVolumes map[string]struct{}, hotplugVolumes map[string]bool, pvcVolume map[string]v1.VolumeStatus, volumeName string) bool {
+ // If a PVC is used in a Filesystem (passthough), it should not be mapped as a HostDisk and a image file should
+ // not be created.
+ if _, isPassthoughFSVolume := passthoughFSVolumes[volumeName]; isPassthoughFSVolume {
+ log.Log.V(4).Infof("this volume %s is mapped as a filesystem passthrough, will not be replaced by HostDisk", volumeName)
+ return true
}
- return nil
+
+ if hotplugVolumes[volumeName] {
+ log.Log.V(4).Infof("this volume %s is hotplugged, will not be replaced by HostDisk", volumeName)
+ return true
+ }
+
+ volumeStatus, ok := pvcVolume[volumeName]
+ if !ok ||
+ volumeStatus.PersistentVolumeClaimInfo.VolumeMode == nil ||
+ *volumeStatus.PersistentVolumeClaimInfo.VolumeMode == k8sv1.PersistentVolumeBlock {
+
+ // This is not a disk on a file system, so skip it.
+ return true
+ }
+ return false
}
func dirBytesAvailable(path string, reserve uint64) (uint64, error) {
