From 3861c105c15d0e9e46cb7f985deb3deeed70a7b7 Mon Sep 17 00:00:00 2001 From: Eduartdo Esteban Date: Thu, 18 Apr 2024 08:00:52 -0700 Subject: [PATCH] clear mac address on annotation Signed-off-by: Eduardo Esteban --- pkg/plugin/vm_restore_item_action.go | 8 +++-- pkg/util/util.go | 10 ++++++ pkg/util/util_test.go | 52 ++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/pkg/plugin/vm_restore_item_action.go b/pkg/plugin/vm_restore_item_action.go index 81ce9f91..b87571be 100644 --- a/pkg/plugin/vm_restore_item_action.go +++ b/pkg/plugin/vm_restore_item_action.go @@ -29,6 +29,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" kvcore "kubevirt.io/api/core/v1" + "kubevirt.io/kubevirt-velero-plugin/pkg/util" ) // VIMRestorePlugin is a VMI restore item action plugin for Velero (duh!) @@ -63,8 +64,11 @@ func (p *VMRestorePlugin) Execute(input *velero.RestoreItemActionExecuteInput) ( return nil, errors.WithStack(err) } - for i := 0; i < len(vm.Spec.Template.Spec.Domain.Devices.Interfaces); i++ { - vm.Spec.Template.Spec.Domain.Devices.Interfaces[i].MacAddress = "" + if util.IsMacAdressClearedByAnnotation(vm) { + p.log.Info("Clear virtual machine MAC addresses") + for i := 0; i < len(vm.Spec.Template.Spec.Domain.Devices.Interfaces); i++ { + vm.Spec.Template.Spec.Domain.Devices.Interfaces[i].MacAddress = "" + } } item, err := runtime.DefaultUnstructuredConverter.ToUnstructured(vm) diff --git a/pkg/util/util.go b/pkg/util/util.go index debd924a..8cbe2ebf 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -28,6 +28,7 @@ import ( ) const VELERO_EXCLUDE_LABEL = "velero.io/exclude-from-backup" +const CLEAR_MAC_ADDRESS_ANNOTATION = "restore.kubevirt.io/clear-mac-address" func GetK8sClient() (*kubernetes.Clientset, error) { loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() @@ -365,3 +366,12 @@ func AddVMIObjectGraph(spec v1.VirtualMachineInstanceSpec, namespace string, ext return extra } + +func IsMacAdressClearedByAnnotation(vm *v1.VirtualMachine) (bool) { + annotations := vm.GetAnnotations() + if annotations == nil { + return false + } + annotation, ok := annotations[CLEAR_MAC_ADDRESS_ANNOTATION] + return ok && annotation == "true" +} \ No newline at end of file diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index ea0331d6..0837728a 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -581,3 +581,55 @@ func TestAddVMIObjectGraph(t *testing.T) { }) } } + +func TestIsMacAddressCleared(t *testing.T) { + testCases := []struct { + name string + resource string + vm kvcore.VirtualMachine + expected bool + }{ + {"Clear mac addres should return false", + "VirtualMachine", + kvcore.VirtualMachine{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + }, + }, + }, + false, + }, + {"Clear mac addres should return false", + "VirtualMachine", + kvcore.VirtualMachine{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + CLEAR_MAC_ADDRESS_ANNOTATION : "false", + }, + }, + }, + false, + }, + {"Clear mac addres should return true", + "VirtualMachine", + kvcore.VirtualMachine{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + CLEAR_MAC_ADDRESS_ANNOTATION : "true", + }, + }, + }, + true, + }, + + } + + logrus.SetLevel(logrus.ErrorLevel) + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := IsMacAdressClearedByAnnotation(&tc.vm) + + assert.Equal(t, tc.expected, result) + }) + } +}