diff --git a/pkg/hooks/api.go b/pkg/hooks/api.go index 4350414760e6..062faa8cbdf8 100644 --- a/pkg/hooks/api.go +++ b/pkg/hooks/api.go @@ -7,6 +7,7 @@ import ( ) const HookSidecarListAnnotationName = "hookSidecars" +const HookSocketsSharedDirectory = "/var/run/kubevirt-hooks" type HookSidecarList []HookSidecar diff --git a/pkg/virt-controller/services/template.go b/pkg/virt-controller/services/template.go index 4f535208ed9e..38de13c607be 100644 --- a/pkg/virt-controller/services/template.go +++ b/pkg/virt-controller/services/template.go @@ -216,6 +216,19 @@ func (t *templateService) RenderLaunchManifest(vmi *v1.VirtualMachineInstance) ( } } + if len(requestedHookSidecarList) != 0 { + volumes = append(volumes, k8sv1.Volume{ + Name: "hooks", + VolumeSource: k8sv1.VolumeSource{ + EmptyDir: &k8sv1.EmptyDirVolumeSource{}, + }, + }) + volumesMounts = append(volumesMounts, k8sv1.VolumeMount{ + Name: "hooks", + MountPath: hooks.HookSocketsSharedDirectory, + }) + } + command := []string{"/usr/share/kubevirt/virt-launcher/entrypoint.sh", "--qemu-timeout", "5m", "--name", domain, @@ -321,6 +334,12 @@ func (t *templateService) RenderLaunchManifest(vmi *v1.VirtualMachineInstance) ( Name: "hook-sidecar-" + requestedHookSidecar.Image, Image: requestedHookSidecar.Image, ImagePullPolicy: requestedHookSidecar.ImagePullPolicy, + VolumeMounts: []k8sv1.VolumeMount{ + k8sv1.VolumeMount{ + Name: "hooks", + MountPath: hooks.HookSocketsSharedDirectory, + }, + }, }) } diff --git a/pkg/virt-controller/services/template_test.go b/pkg/virt-controller/services/template_test.go index a7643655f278..da4cc978393d 100644 --- a/pkg/virt-controller/services/template_test.go +++ b/pkg/virt-controller/services/template_test.go @@ -55,6 +55,7 @@ var _ = Describe("Template", func() { pod, err := svc.RenderLaunchManifest(&v1.VirtualMachineInstance{ObjectMeta: metav1.ObjectMeta{Name: "testvmi", Namespace: "testns", UID: "1234", Annotations: annotations}, Spec: v1.VirtualMachineInstanceSpec{Domain: v1.DomainSpec{}}}) Expect(err).ToNot(HaveOccurred()) + Expect(len(pod.Spec.Containers)).To(Equal(2)) Expect(pod.Spec.Containers[0].Image).To(Equal("kubevirt/virt-launcher")) Expect(pod.ObjectMeta.Labels).To(Equal(map[string]string{ v1.AppLabel: "virt-launcher", @@ -100,6 +101,7 @@ var _ = Describe("Template", func() { pod, err := svc.RenderLaunchManifest(&vmi) Expect(err).ToNot(HaveOccurred()) + Expect(len(pod.Spec.Containers)).To(Equal(2)) Expect(pod.Spec.Containers[0].Image).To(Equal("kubevirt/virt-launcher")) Expect(pod.ObjectMeta.Labels).To(Equal(map[string]string{ v1.AppLabel: "virt-launcher", @@ -121,7 +123,9 @@ var _ = Describe("Template", func() { Expect(pod.Spec.Containers[1].Name).To(Equal("hook-sidecar-some-image:v1")) Expect(pod.Spec.Containers[1].Image).To(Equal("some-image:v1")) Expect(pod.Spec.Containers[1].ImagePullPolicy).To(Equal(kubev1.PullPolicy("IfNotPresent"))) - Expect(pod.Spec.Volumes[0].HostPath.Path).To(Equal("/var/run/kubevirt")) + Expect(pod.Spec.Containers[1].VolumeMounts[0].MountPath).To(Equal(hooks.HookSocketsSharedDirectory)) + Expect(pod.Spec.Volumes[0].EmptyDir.Medium).To(Equal(kubev1.StorageMedium(""))) + Expect(pod.Spec.Volumes[1].HostPath.Path).To(Equal("/var/run/kubevirt")) Expect(pod.Spec.Containers[0].VolumeMounts[0].MountPath).To(Equal("/var/run/kubevirt")) Expect(*pod.Spec.TerminationGracePeriodSeconds).To(Equal(int64(60))) })