Skip to content

Commit

Permalink
virt-controller, template: use a builder pattern to handle launcher p…
Browse files Browse the repository at this point in the history
…ods volumes

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
  • Loading branch information
maiqueb committed Jun 6, 2022
1 parent bbed79a commit 05059f8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 53 deletions.
1 change: 1 addition & 0 deletions pkg/virt-controller/services/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go_library(
srcs = [
"multus_annotations.go",
"rendercontainer.go",
"rendervolumes.go",
"template.go",
],
importpath = "kubevirt.io/kubevirt/pkg/virt-controller/services",
Expand Down
67 changes: 67 additions & 0 deletions pkg/virt-controller/services/rendervolumes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package services

import (
k8sv1 "k8s.io/api/core/v1"
"kubevirt.io/kubevirt/pkg/util"
"path/filepath"
)

type VolumeRendererOption func(renderer *VolumeRenderer)

type VolumeRenderer struct {
containerDiskDir string
ephemeralDiskDir string
virtShareDir string
}

func NewVolumeRenderer(ephemeralDisk string, containerDiskDir string, virtShareDir string) *VolumeRenderer {
return &VolumeRenderer{
containerDiskDir: containerDiskDir,
ephemeralDiskDir: ephemeralDisk,
virtShareDir: virtShareDir,
}
}

func (vr *VolumeRenderer) Mounts() []k8sv1.VolumeMount {
volumeMounts := []k8sv1.VolumeMount{
mountPath("private", util.VirtPrivateDir),
mountPath("public", util.VirtShareDir),
mountPath("ephemeral-disks", vr.ephemeralDiskDir),
mountPathWithPropagation(containerDisks, vr.containerDiskDir, k8sv1.MountPropagationHostToContainer),
mountPath("libvirt-runtime", "/var/run/libvirt"),
mountPath("sockets", filepath.Join(vr.virtShareDir, "sockets")),
}
return volumeMounts
}

func (vr *VolumeRenderer) Volumes() []k8sv1.Volume {
volumes := []k8sv1.Volume{
emptyDirVolume("private"),
emptyDirVolume("public"),
emptyDirVolume("sockets"),
}
return volumes
}

func mountPath(name string, path string) k8sv1.VolumeMount {
return k8sv1.VolumeMount{
Name: name,
MountPath: path,
}
}

func mountPathWithPropagation(name string, path string, propagation k8sv1.MountPropagationMode) k8sv1.VolumeMount {
return k8sv1.VolumeMount{
Name: name,
MountPath: path,
MountPropagation: &propagation,
}
}

func emptyDirVolume(name string) k8sv1.Volume {
return k8sv1.Volume{
Name: name,
VolumeSource: k8sv1.VolumeSource{
EmptyDir: &k8sv1.EmptyDirVolumeSource{}},
}
}
54 changes: 3 additions & 51 deletions pkg/virt-controller/services/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,10 @@ func (t *templateService) renderLaunchManifest(vmi *v1.VirtualMachineInstance, i
namespace := precond.MustNotBeEmpty(vmi.GetObjectMeta().GetNamespace())
nodeSelector := map[string]string{}

var volumes []k8sv1.Volume
volumeRenderer := NewVolumeRenderer(t.ephemeralDiskDir, t.containerDiskDir, t.virtShareDir)
volumes := volumeRenderer.Volumes()
var volumeDevices []k8sv1.VolumeDevice
var volumeMounts []k8sv1.VolumeMount
volumeMounts := volumeRenderer.Mounts()
var imagePullSecrets []k8sv1.LocalObjectReference

var userId int64 = util.RootUser
Expand All @@ -485,28 +486,6 @@ func (t *templateService) renderLaunchManifest(vmi *v1.VirtualMachineInstance, i
userId = util.NonRootUID
}

volumeMounts = append(volumeMounts, k8sv1.VolumeMount{
Name: "private",
MountPath: util.VirtPrivateDir,
})
volumes = append(volumes, k8sv1.Volume{
Name: "private",
VolumeSource: k8sv1.VolumeSource{
EmptyDir: &k8sv1.EmptyDirVolumeSource{},
},
})

volumeMounts = append(volumeMounts, k8sv1.VolumeMount{
Name: "public",
MountPath: util.VirtShareDir,
})
volumes = append(volumes, k8sv1.Volume{
Name: "public",
VolumeSource: k8sv1.VolumeSource{
EmptyDir: &k8sv1.EmptyDirVolumeSource{},
},
})

hotplugVolumes := make(map[string]bool)
for _, volumeStatus := range vmi.Status.VolumeStatus {
if volumeStatus.HotplugVolume != nil {
Expand All @@ -527,17 +506,7 @@ func (t *templateService) renderLaunchManifest(vmi *v1.VirtualMachineInstance, i

gracePeriodSeconds := gracePeriodInSeconds(vmi)

volumeMounts = append(volumeMounts, k8sv1.VolumeMount{
Name: "ephemeral-disks",
MountPath: t.ephemeralDiskDir,
})

prop := k8sv1.MountPropagationHostToContainer
volumeMounts = append(volumeMounts, k8sv1.VolumeMount{
Name: containerDisks,
MountPath: t.containerDiskDir,
MountPropagation: &prop,
})
if !vmi.Spec.Domain.Devices.DisableHotplug {
volumeMounts = append(volumeMounts, k8sv1.VolumeMount{
Name: hotplugDisks,
Expand All @@ -546,23 +515,6 @@ func (t *templateService) renderLaunchManifest(vmi *v1.VirtualMachineInstance, i
})
}

volumeMounts = append(volumeMounts, k8sv1.VolumeMount{
Name: "libvirt-runtime",
MountPath: "/var/run/libvirt",
})

// virt-launcher cmd socket dir
volumeMounts = append(volumeMounts, k8sv1.VolumeMount{
Name: "sockets",
MountPath: filepath.Join(t.virtShareDir, "sockets"),
})
volumes = append(volumes, k8sv1.Volume{
Name: "sockets",
VolumeSource: k8sv1.VolumeSource{
EmptyDir: &k8sv1.EmptyDirVolumeSource{},
},
})

serviceAccountName := ""

for _, volume := range vmi.Spec.Volumes {
Expand Down
14 changes: 12 additions & 2 deletions pkg/virt-controller/services/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,12 @@ var _ = Describe("Template", func() {

Expect(pod.Spec.Volumes[0].EmptyDir).ToNot(BeNil())

Expect(pod.Spec.Containers[0].VolumeMounts[5].MountPath).To(Equal("/var/run/kubevirt/sockets"))
Expect(pod.Spec.Containers[0].VolumeMounts).To(
ContainElement(
kubev1.VolumeMount{
Name: "sockets",
MountPath: "/var/run/kubevirt/sockets"},
))

Expect(pod.Spec.Volumes[1].EmptyDir.Medium).To(Equal(kubev1.StorageMedium("")))

Expand Down Expand Up @@ -1948,7 +1953,12 @@ var _ = Describe("Template", func() {
Expect(pod.Spec.Volumes[3].EmptyDir.Medium).To(Equal(kubev1.StorageMediumHugePages))

Expect(pod.Spec.Containers[0].VolumeMounts).To(HaveLen(7))
Expect(pod.Spec.Containers[0].VolumeMounts[6].MountPath).To(Equal("/dev/hugepages"))
Expect(pod.Spec.Containers[0].VolumeMounts).To(
ContainElement(
kubev1.VolumeMount{
Name: "hugepages",
MountPath: "/dev/hugepages"},
))
},
Entry("hugepages-2Mi on amd64", "amd64", "2Mi", 223),
Entry("hugepages-1Gi on amd64", "amd64", "1Gi", 223),
Expand Down

0 comments on commit 05059f8

Please sign in to comment.