Skip to content

Commit

Permalink
Better naming constructs for container disk path functions
Browse files Browse the repository at this point in the history
Signed-off-by: David Vossel <[email protected]>
  • Loading branch information
davidvossel committed Apr 13, 2020
1 parent 3bf604b commit ab3f1ff
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
18 changes: 9 additions & 9 deletions pkg/container-disk/container-disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ var podsBaseDir = "/var/lib/kubelet/pods"

var mountBaseDir = filepath.Join(util.VirtShareDir, "/container-disks")

func GenerateLegacyVolumeMountDirOnHost(vmi *v1.VirtualMachineInstance) string {
func GetLegacyVolumeMountDirOnHost(vmi *v1.VirtualMachineInstance) string {
return filepath.Join(mountBaseDir, string(vmi.UID))
}

func GenerateVolumeMountDirOnGuest(vmi *v1.VirtualMachineInstance) string {
func GetVolumeMountDirOnGuest(vmi *v1.VirtualMachineInstance) string {
return filepath.Join(mountBaseDir, string(vmi.UID))
}

func GenerateVolumeMountDirOnHost(vmi *v1.VirtualMachineInstance) (string, bool, error) {
func GetVolumeMountDirOnHost(vmi *v1.VirtualMachineInstance) (string, bool, error) {
for podUID, _ := range vmi.Status.ActivePods {
basepath := fmt.Sprintf("%s/%s/volumes/kubernetes.io~empty-dir/container-disks", podsBaseDir, string(podUID))
exists, err := diskutils.FileExists(basepath)
Expand All @@ -63,8 +63,8 @@ func GenerateVolumeMountDirOnHost(vmi *v1.VirtualMachineInstance) (string, bool,
return "", false, nil
}

func GenerateDiskTargetPathFromHostView(vmi *v1.VirtualMachineInstance, volumeIndex int) (string, error) {
basepath, found, err := GenerateVolumeMountDirOnHost(vmi)
func GetDiskTargetPathFromHostView(vmi *v1.VirtualMachineInstance, volumeIndex int) (string, error) {
basepath, found, err := GetVolumeMountDirOnHost(vmi)
if err != nil {
return "", err
} else if !found {
Expand All @@ -74,7 +74,7 @@ func GenerateDiskTargetPathFromHostView(vmi *v1.VirtualMachineInstance, volumeIn
return fmt.Sprintf("%s/disk_%d.img", basepath, volumeIndex), nil
}

func GenerateDiskTargetPathFromLauncherView(volumeIndex int) string {
func GetDiskTargetPathFromLauncherView(volumeIndex int) string {
return filepath.Join(mountBaseDir, fmt.Sprintf("disk_%d.img", volumeIndex))
}

Expand All @@ -96,7 +96,7 @@ func setLocalDataOwner(user string) {
// GetDiskTargetPartFromLauncherView returns (path to disk image, image type, and error)
func GetDiskTargetPartFromLauncherView(volumeIndex int) (string, error) {

path := GenerateDiskTargetPathFromLauncherView(volumeIndex)
path := GetDiskTargetPathFromLauncherView(volumeIndex)
exists, err := diskutils.FileExists(path)
if err != nil {
return "", err
Expand All @@ -107,7 +107,7 @@ func GetDiskTargetPartFromLauncherView(volumeIndex int) (string, error) {
return "", fmt.Errorf("no supported file disk found for volume with index %d", volumeIndex)
}

func GenerateSocketPathFromHostView(vmi *v1.VirtualMachineInstance, volumeIndex int) (string, error) {
func GetSocketPathFromHostView(vmi *v1.VirtualMachineInstance, volumeIndex int) (string, error) {
for podUID, _ := range vmi.Status.ActivePods {
basepath := fmt.Sprintf("/pods/%s/volumes/kubernetes.io~empty-dir/container-disks", string(podUID))
exists, _ := diskutils.FileExists(basepath)
Expand Down Expand Up @@ -157,7 +157,7 @@ func GenerateContainers(vmi *v1.VirtualMachineInstance, podVolumeName string, bi
for index, volume := range vmi.Spec.Volumes {
if volume.ContainerDisk != nil {

volumeMountDir := GenerateVolumeMountDirOnGuest(vmi)
volumeMountDir := GetVolumeMountDirOnGuest(vmi)
diskContainerName := fmt.Sprintf("volume%s", volume.Name)
diskContainerImage := volume.ContainerDisk.Image
resources := kubev1.ResourceRequirements{}
Expand Down
10 changes: 5 additions & 5 deletions pkg/container-disk/container-disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var _ = Describe("ContainerDisk", func() {
expectedVolumeMountDir := fmt.Sprintf("%s/%s", tmpDir, string(vmi.UID))

// create a fake disk file
volumeMountDir := GenerateVolumeMountDirOnGuest(vmi)
volumeMountDir := GetVolumeMountDirOnGuest(vmi)
err = os.MkdirAll(volumeMountDir, 0750)
Expect(err).ToNot(HaveOccurred())
Expect(expectedVolumeMountDir).To(Equal(volumeMountDir))
Expand Down Expand Up @@ -97,24 +97,24 @@ var _ = Describe("ContainerDisk", func() {
}

// should not be found if dir doesn't exist
path, found, err := GenerateVolumeMountDirOnHost(vmi)
path, found, err := GetVolumeMountDirOnHost(vmi)
Expect(err).ToNot(HaveOccurred())
Expect(found).To(BeFalse())

// should be found if dir does exist
expectedPath := fmt.Sprintf("%s/1234/volumes/kubernetes.io~empty-dir/container-disks", tmpDir)
os.MkdirAll(expectedPath, 0755)
path, found, err = GenerateVolumeMountDirOnHost(vmi)
path, found, err = GetVolumeMountDirOnHost(vmi)
Expect(err).ToNot(HaveOccurred())
Expect(found).To(BeTrue())
Expect(path).To(Equal(expectedPath))

// should be able to generate legacy socket path dir
legacySocket := GenerateLegacyVolumeMountDirOnHost(vmi)
legacySocket := GetLegacyVolumeMountDirOnHost(vmi)
Expect(legacySocket).To(Equal(filepath.Join(tmpDir, "6789")))

// should return error if disk target doesn't exist
targetPath, err := GenerateDiskTargetPathFromHostView(vmi, 1)
targetPath, err := GetDiskTargetPathFromHostView(vmi, 1)
expectedPath = fmt.Sprintf("%s/1234/volumes/kubernetes.io~empty-dir/container-disks/disk_1.img", tmpDir)
Expect(err).ToNot(HaveOccurred())
Expect(targetPath).To(Equal(expectedPath))
Expand Down
8 changes: 4 additions & 4 deletions pkg/virt-handler/container-disk/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (m *Mounter) setMountTargetRecordEntry(vmi *v1.VirtualMachineInstance, targ
func (m *Mounter) Mount(vmi *v1.VirtualMachineInstance, verify bool) error {
for i, volume := range vmi.Spec.Volumes {
if volume.ContainerDisk != nil {
targetFile, err := containerdisk.GenerateDiskTargetPathFromHostView(vmi, i)
targetFile, err := containerdisk.GetDiskTargetPathFromHostView(vmi, i)
if err != nil {
return err
}
Expand All @@ -155,7 +155,7 @@ func (m *Mounter) Mount(vmi *v1.VirtualMachineInstance, verify bool) error {
if isMounted, err := nodeRes.IsMounted(targetFile); err != nil {
return fmt.Errorf("failed to determine if %s is already mounted: %v", targetFile, err)
} else if !isMounted {
sock, err := containerdisk.GenerateSocketPathFromHostView(vmi, i)
sock, err := containerdisk.GetSocketPathFromHostView(vmi, i)
if err != nil {
return err
}
Expand Down Expand Up @@ -196,7 +196,7 @@ func (m *Mounter) Mount(vmi *v1.VirtualMachineInstance, verify bool) error {
if err != nil {
return fmt.Errorf("failed to detect VMI pod: %v", err)
}
imageInfo, err := isolation.GetImageInfo(containerdisk.GenerateDiskTargetPathFromLauncherView(i), res)
imageInfo, err := isolation.GetImageInfo(containerdisk.GetDiskTargetPathFromLauncherView(i), res)
if err != nil {
return fmt.Errorf("failed to get image info: %v", err)
}
Expand All @@ -213,7 +213,7 @@ func (m *Mounter) Mount(vmi *v1.VirtualMachineInstance, verify bool) error {
// Legacy Unmount unmounts all container disks of a given VMI when the hold HostPath method was in use.
// This exists for backwards compatibility for VMIs running before a KubeVirt update occurs.
func (m *Mounter) legacyUnmount(vmi *v1.VirtualMachineInstance) error {
mountDir := containerdisk.GenerateLegacyVolumeMountDirOnHost(vmi)
mountDir := containerdisk.GetLegacyVolumeMountDirOnHost(vmi)

files, err := ioutil.ReadDir(mountDir)
if err != nil && !os.IsNotExist(err) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/virt-launcher/virtwrap/api/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ func Convert_v1_ContainerDiskSource_To_api_Disk(volumeName string, _ *v1.Contain
Source: &DiskSource{},
}

source := containerdisk.GenerateDiskTargetPathFromLauncherView(diskIndex)
source := containerdisk.GetDiskTargetPathFromLauncherView(diskIndex)

disk.BackingStore.Format.Type = c.DiskType[volumeName].Format
disk.BackingStore.Source.File = source
Expand Down

0 comments on commit ab3f1ff

Please sign in to comment.