Skip to content

Commit

Permalink
hooks: rebase on latest master
Browse files Browse the repository at this point in the history
Most of the changes are VM -> VMI
  • Loading branch information
phoracek committed Jul 2, 2018
1 parent 02076e6 commit e82fb35
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apiVersion: kubevirt.io/v1alpha1
kind: VirtualMachine
apiVersion: kubevirt.io/v1alpha2
kind: VirtualMachineInstance
metadata:
annotations:
hooks.kubevirt.io/hookSidecars: '[{"image": "registry:5000/kubevirt/example-hook-sidecar:devel"}]'
smbios.vm.kubevirt.io/baseBoardManufacturer: Radical Edward
creationTimestamp: null
labels:
special: vm-with-sidecar-hook
name: vm-with-sidecar-hook
special: vmi-with-sidecar-hook
name: vmi-with-sidecar-hook
spec:
domain:
devices:
Expand Down
10 changes: 5 additions & 5 deletions cmd/example-hook-sidecar/smbios.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ type v1alpha1Server struct{}
func (s v1alpha1Server) OnDefineDomain(ctx context.Context, params *hooksV1alpha1.OnDefineDomainParams) (*hooksV1alpha1.OnDefineDomainResult, error) {
log.Log.Info("Hook's OnDefineDomain callback method has been called")

vmJSON := params.GetVm()
vmSpec := vmSchema.VirtualMachine{}
err := json.Unmarshal(vmJSON, &vmSpec)
vmiJSON := params.GetVmi()
vmiSpec := vmSchema.VirtualMachineInstance{}
err := json.Unmarshal(vmiJSON, &vmiSpec)
if err != nil {
log.Log.Reason(err).Errorf("Failed to unmarshal given VM spec: %s", vmJSON)
log.Log.Reason(err).Errorf("Failed to unmarshal given VMI spec: %s", vmiJSON)
panic(err)
}

annotations := vmSpec.GetAnnotations()
annotations := vmiSpec.GetAnnotations()

if _, found := annotations[baseBoardManufacturerAnnotation]; !found {
log.Log.Info("SM BIOS hook sidecar was requested, but no attributes provided. Returning original domain spec")
Expand Down
8 changes: 4 additions & 4 deletions pkg/hooks/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,17 @@ func sortCallbacksPerHookPoint(callbacksPerHookPoint map[string][]*callackClient
}
}

func (m *Manager) OnDefineDomain(domainSpec *virtwrapApi.DomainSpec, vm *v1.VirtualMachine) (*virtwrapApi.DomainSpec, error) {
func (m *Manager) OnDefineDomain(domainSpec *virtwrapApi.DomainSpec, vmi *v1.VirtualMachineInstance) (*virtwrapApi.DomainSpec, error) {
if callbacks, found := m.callbacksPerHookPoint[hooksInfo.OnDefineDomainHookPointName]; found {
for _, callback := range callbacks {
if callback.Version == hooksV1alpha1.Version {
domainSpecXML, err := xml.Marshal(domainSpec)
if err != nil {
return nil, fmt.Errorf("Failed to marshal domain spec: %v", domainSpec)
}
vmJSON, err := json.Marshal(vm)
vmiJSON, err := json.Marshal(vmi)
if err != nil {
return nil, fmt.Errorf("Failed to marshal VM spec: %v", vm)
return nil, fmt.Errorf("Failed to marshal VMI spec: %v", vmi)
}

conn, err := dialSocket(callback.SocketPath)
Expand All @@ -193,7 +193,7 @@ func (m *Manager) OnDefineDomain(domainSpec *virtwrapApi.DomainSpec, vm *v1.Virt
defer cancel()
result, err := client.OnDefineDomain(ctx, &hooksV1alpha1.OnDefineDomainParams{
DomainXML: domainSpecXML,
Vm: vmJSON,
Vmi: vmiJSON,
})
if err != nil {
return nil, err
Expand Down
22 changes: 11 additions & 11 deletions pkg/hooks/v1alpha1/api.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/hooks/v1alpha1/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ service Callbacks {
message OnDefineDomainParams {
// domainXML is original libvirt domain specification
bytes domainXML = 1;
// vm is VirtualMachine is object of virtual machine currently processed by virt-launcher, it is encoded as JSON
bytes vm = 2;
// vmi is VirtualMachineInstance is object of virtual machine currently processed by virt-launcher, it is encoded as JSON
bytes vmi = 2;
}

message OnDefineDomainResult {
Expand Down
2 changes: 1 addition & 1 deletion pkg/virt-controller/services/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (t *templateService) RenderLaunchManifest(vmi *v1.VirtualMachineInstance) (
// Read requested hookSidecars from VM annotation
var err error
requestedHookSidecarList := make(hooks.HookSidecarList, 0)
if rawRequestedHookSidecarList, requestedHookSidecarListDefined := vm.GetObjectMeta().GetAnnotations()[hooks.HookSidecarListAnnotationName]; requestedHookSidecarListDefined {
if rawRequestedHookSidecarList, requestedHookSidecarListDefined := vmi.GetObjectMeta().GetAnnotations()[hooks.HookSidecarListAnnotationName]; requestedHookSidecarListDefined {
requestedHookSidecarList, err = hooks.UnmarshalHookSidecarList(rawRequestedHookSidecarList)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/virt-launcher/virtwrap/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (l *LibvirtDomainManager) preStartHook(vmi *v1.VirtualMachineInstance, doma
}

hooksManager := hooks.GetManager()
domainSpec, err := hooksManager.OnDefineDomain(&domain.Spec, vm)
domainSpec, err := hooksManager.OnDefineDomain(&domain.Spec, vmi)
if err != nil {
return domain, err
}
Expand Down
52 changes: 26 additions & 26 deletions tests/vm_hook_sidecar_test.go → tests/vmi_hook_sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,32 @@ var _ = Describe("HookSidecars", func() {
virtClient, err := kubecli.GetKubevirtClient()
tests.PanicOnError(err)

var vm *v1.VirtualMachine
var vmi *v1.VirtualMachineInstance

BeforeEach(func() {
tests.BeforeTestCleanup()
vm = tests.NewRandomVMWithEphemeralDisk(tests.RegistryDiskFor(tests.RegistryDiskAlpine))
vm.ObjectMeta.Annotations = map[string]string{
vmi = tests.NewRandomVMIWithEphemeralDisk(tests.RegistryDiskFor(tests.RegistryDiskAlpine))
vmi.ObjectMeta.Annotations = map[string]string{
"hooks.kubevirt.io/hookSidecars": `[{"image": "registry:5000/kubevirt/example-hook-sidecar:devel"}]`,
"smbios.vm.kubevirt.io/baseBoardManufacturer": "Radical Edward",
}
})

Describe("VM definition", func() {
Describe("VMI definition", func() {
Context("with SM BIOS hook sidecar", func() {
It("should successfully start with hook sidecar annotation", func() {
By("Starting a VM")
vm, err = virtClient.VM(tests.NamespaceTestDefault).Create(vm)
By("Starting a VMI")
vmi, err = virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).Create(vmi)
Expect(err).ToNot(HaveOccurred())
tests.WaitForSuccessfulVMStart(vm)
tests.WaitForSuccessfulVMIStart(vmi)
}, 300)

It("should call Collect on the hook sidecar", func() {
By("Getting hook-sidecar logs")
vm, err = virtClient.VM(tests.NamespaceTestDefault).Create(vm)
vmi, err = virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).Create(vmi)
Expect(err).ToNot(HaveOccurred())
logs := func() string { return getHookSidecarLogs(virtClient, vm) }
tests.WaitForSuccessfulVMStart(vm)
logs := func() string { return getHookSidecarLogs(virtClient, vmi) }
tests.WaitForSuccessfulVMIStart(vmi)
Eventually(logs,
11*time.Second,
500*time.Millisecond).
Expand All @@ -76,10 +76,10 @@ var _ = Describe("HookSidecars", func() {

It("should call OnDefineDomain on the hook sidecar", func() {
By("Getting hook-sidecar logs")
vm, err = virtClient.VM(tests.NamespaceTestDefault).Create(vm)
vmi, err = virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).Create(vmi)
Expect(err).ToNot(HaveOccurred())
logs := func() string { return getHookSidecarLogs(virtClient, vm) }
tests.WaitForSuccessfulVMStart(vm)
logs := func() string { return getHookSidecarLogs(virtClient, vmi) }
tests.WaitForSuccessfulVMIStart(vmi)
Eventually(logs,
11*time.Second,
500*time.Millisecond).
Expand All @@ -89,9 +89,9 @@ var _ = Describe("HookSidecars", func() {
It("should update domain XML with SM BIOS properties", func() {
By("Reading domain XML using virsh")
tests.SkipIfNoKubectl()
vm, err = virtClient.VM(tests.NamespaceTestDefault).Create(vm)
tests.WaitForSuccessfulVMStart(vm)
domainXml := getVmDomainXml(virtClient, vm)
vmi, err = virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).Create(vmi)
tests.WaitForSuccessfulVMIStart(vmi)
domainXml := getVmDomainXml(virtClient, vmi)
Expect(domainXml).Should(ContainSubstring("<sysinfo type='smbios'>"))
Expect(domainXml).Should(ContainSubstring("<smbios mode='sysinfo'/>"))
Expect(domainXml).Should(ContainSubstring("<entry name='manufacturer'>Radical Edward</entry>"))
Expand All @@ -101,9 +101,9 @@ var _ = Describe("HookSidecars", func() {

})

func getHookSidecarLogs(virtCli kubecli.KubevirtClient, vm *v1.VirtualMachine) string {
namespace := vm.GetObjectMeta().GetNamespace()
podName := getVmPodName(virtCli, vm)
func getHookSidecarLogs(virtCli kubecli.KubevirtClient, vmi *v1.VirtualMachineInstance) string {
namespace := vmi.GetObjectMeta().GetNamespace()
podName := getVmPodName(virtCli, vmi)

var tailLines int64 = 100
logsRaw, err := virtCli.CoreV1().
Expand All @@ -118,22 +118,22 @@ func getHookSidecarLogs(virtCli kubecli.KubevirtClient, vm *v1.VirtualMachine) s
return string(logsRaw)
}

func getVmDomainXml(virtCli kubecli.KubevirtClient, vm *v1.VirtualMachine) string {
podName := getVmPodName(virtCli, vm)
func getVmDomainXml(virtCli kubecli.KubevirtClient, vmi *v1.VirtualMachineInstance) string {
podName := getVmPodName(virtCli, vmi)

vmNameListRaw, err := tests.RunKubectlCommand("exec", "-ti", "--namespace", vm.GetObjectMeta().GetNamespace(), podName, "--container", "compute", "--", "virsh", "list", "--name")
vmNameListRaw, err := tests.RunKubectlCommand("exec", "-ti", "--namespace", vmi.GetObjectMeta().GetNamespace(), podName, "--container", "compute", "--", "virsh", "list", "--name")
Expect(err).ToNot(HaveOccurred())

vmName := strings.Split(vmNameListRaw, "\n")[0]
vmDomainXML, err := tests.RunKubectlCommand("exec", "-ti", "--namespace", vm.GetObjectMeta().GetNamespace(), podName, "--container", "compute", "--", "virsh", "dumpxml", vmName)
vmDomainXML, err := tests.RunKubectlCommand("exec", "-ti", "--namespace", vmi.GetObjectMeta().GetNamespace(), podName, "--container", "compute", "--", "virsh", "dumpxml", vmName)
Expect(err).ToNot(HaveOccurred())

return vmDomainXML
}

func getVmPodName(virtCli kubecli.KubevirtClient, vm *v1.VirtualMachine) string {
namespace := vm.GetObjectMeta().GetNamespace()
domain := vm.GetObjectMeta().GetName()
func getVmPodName(virtCli kubecli.KubevirtClient, vmi *v1.VirtualMachineInstance) string {
namespace := vmi.GetObjectMeta().GetNamespace()
domain := vmi.GetObjectMeta().GetName()
labelSelector := fmt.Sprintf("kubevirt.io/domain in (%s)", domain)

pods, err := virtCli.CoreV1().Pods(namespace).List(metav1.ListOptions{LabelSelector: labelSelector})
Expand Down
14 changes: 7 additions & 7 deletions tools/vms-generator/vms-generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,18 +578,18 @@ func getVMIPresetSmall() *v1.VirtualMachineInstancePreset {
return vmPreset
}

func getVmWithHookSidecar() *v1.VirtualMachine {
vm := getBaseVMI(vmiWithHookSidecar)
vm.Spec.Domain.Resources.Requests[k8sv1.ResourceMemory] = resource.MustParse("1024M")
func getVMIWithHookSidecar() *v1.VirtualMachineInstance {
vmi := getBaseVMI(vmiWithHookSidecar)
vmi.Spec.Domain.Resources.Requests[k8sv1.ResourceMemory] = resource.MustParse("1024M")

addRegistryDisk(&vm.Spec, fmt.Sprintf("%s/%s:%s", dockerPrefix, imageFedora, dockerTag), busVirtio)
addNoCloudDiskWitUserData(&vm.Spec, "#cloud-config\npassword: fedora\nchpasswd: { expire: False }")
addRegistryDisk(&vmi.Spec, fmt.Sprintf("%s/%s:%s", dockerPrefix, imageFedora, dockerTag), busVirtio)
addNoCloudDiskWitUserData(&vmi.Spec, "#cloud-config\npassword: fedora\nchpasswd: { expire: False }")

vm.ObjectMeta.Annotations = map[string]string{
vmi.ObjectMeta.Annotations = map[string]string{
"hooks.kubevirt.io/hookSidecars": `[{"image": "registry:5000/kubevirt/example-hook-sidecar:devel"}]`,
"smbios.vm.kubevirt.io/baseBoardManufacturer": "Radical Edward",
}
return vm
return vmi
}

func main() {
Expand Down

0 comments on commit e82fb35

Please sign in to comment.