Skip to content

Commit

Permalink
clear mac address on annotation
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Esteban <[email protected]>
  • Loading branch information
Eduartdo Esteban authored and esteban-ee committed Apr 25, 2024
1 parent e4fcd98 commit 3861c10
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/plugin/vm_restore_item_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!)
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"
}
52 changes: 52 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit 3861c10

Please sign in to comment.