@@ -10,13 +10,15 @@ import (
1010 "k8s.io/utils/strings/slices"
1111 "kubevirt.io/client-go/kubecli"
1212 "kubevirt.io/kubevirt-velero-plugin/tests/framework"
13+ "kubevirt.io/kubevirt-velero-plugin/tests/patch"
1314
1415 . "github.com/onsi/ginkgo/v2"
1516 . "github.com/onsi/gomega"
1617 velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
1718 v1 "k8s.io/api/core/v1"
1819 apierrs "k8s.io/apimachinery/pkg/api/errors"
1920 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+ "k8s.io/apimachinery/pkg/types"
2022 kvv1 "kubevirt.io/api/core/v1"
2123 cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
2224 . "kubevirt.io/kubevirt-velero-plugin/tests/framework/matcher"
@@ -2606,55 +2608,67 @@ var _ = Describe("Resource excludes", func() {
26062608 })
26072609
26082610 Context ("Exclude label" , func () {
2609- addExcludeLabel := func (labels map [string ]string ) map [string ]string {
2610- if labels == nil {
2611- labels = make (map [string ]string )
2612- }
2613- labels ["velero.io/exclude-from-backup" ] = "true"
2614- return labels
2615- }
2616-
26172611 addExcludeLabelToDV := func (name string ) {
2618- updateFunc := func (dataVolume * cdiv1.DataVolume ) * cdiv1.DataVolume {
2619- dataVolume .SetLabels (addExcludeLabel (dataVolume .GetLabels ()))
2620- return dataVolume
2621- }
2612+ dv , err := framework .FindDataVolume (f .KvClient , f .Namespace .Name , name )
2613+ Expect (err ).ToNot (HaveOccurred ())
2614+
2615+ patchData , err := patch .New (
2616+ patch .WithAddLabel (velerov1api .ExcludeFromBackupLabel , "true" , dv .Labels ),
2617+ ).GeneratePayload ()
2618+ Expect (err ).ToNot (HaveOccurred ())
26222619
2623- retryOnceOnErr (updateDataVolume (f .KvClient , f .Namespace .Name , name , updateFunc )).Should (BeNil ())
2620+ _ , err = f .KvClient .CdiClient ().CdiV1beta1 ().DataVolumes (f .Namespace .Name ).Patch (context .TODO (), name , types .JSONPatchType , patchData , metav1.PatchOptions {})
2621+ Expect (err ).ToNot (HaveOccurred ())
26242622 }
26252623
26262624 addExcludeLabelToPVC := func (name string ) {
2627- update := func (pvc * v1.PersistentVolumeClaim ) * v1.PersistentVolumeClaim {
2628- pvc .SetLabels (addExcludeLabel (pvc .GetLabels ()))
2629- return pvc
2630- }
2631- retryOnceOnErr (updatePvc (f .K8sClient , f .Namespace .Name , name , update )).Should (BeNil ())
2625+ pvc , err := framework .FindPVC (f .K8sClient , f .Namespace .Name , name )
2626+ Expect (err ).ToNot (HaveOccurred ())
2627+
2628+ patchData , err := patch .New (
2629+ patch .WithAddLabel (velerov1api .ExcludeFromBackupLabel , "true" , pvc .Labels ),
2630+ ).GeneratePayload ()
2631+ Expect (err ).ToNot (HaveOccurred ())
2632+
2633+ _ , err = f .K8sClient .CoreV1 ().PersistentVolumeClaims (f .Namespace .Name ).Patch (context .TODO (), name , types .JSONPatchType , patchData , metav1.PatchOptions {})
2634+ Expect (err ).ToNot (HaveOccurred ())
26322635 }
26332636
26342637 addExcludeLabelToVMI := func (name string ) {
2635- update := func (vmi * kvv1.VirtualMachineInstance ) * kvv1.VirtualMachineInstance {
2636- vmi .SetLabels (addExcludeLabel (vmi .GetLabels ()))
2637- return vmi
2638- }
2639- retryOnceOnErr (updateVmi (f .KvClient , f .Namespace .Name , name , update )).Should (BeNil ())
2638+ vmi , err := f .KvClient .VirtualMachineInstance (f .Namespace .Name ).Get (context .Background (), name , metav1.GetOptions {})
2639+ Expect (err ).ToNot (HaveOccurred ())
2640+
2641+ patchData , err := patch .New (
2642+ patch .WithAddLabel (velerov1api .ExcludeFromBackupLabel , "true" , vmi .Labels ),
2643+ ).GeneratePayload ()
2644+ Expect (err ).ToNot (HaveOccurred ())
2645+
2646+ _ , err = f .KvClient .VirtualMachineInstance (f .Namespace .Name ).Patch (context .Background (), name , types .JSONPatchType , patchData , metav1.PatchOptions {})
2647+ Expect (err ).ToNot (HaveOccurred ())
26402648 }
26412649
26422650 addExcludeLabelToVM := func (name string ) {
2643- update := func (vm * kvv1.VirtualMachine ) * kvv1.VirtualMachine {
2644- vm .SetLabels (addExcludeLabel (vm .GetLabels ()))
2645- return vm
2646- }
2647- retryOnceOnErr (updateVm (f .KvClient , f .Namespace .Name , name , update )).Should (BeNil ())
2651+ vm , err := f .KvClient .VirtualMachine (f .Namespace .Name ).Get (context .Background (), name , metav1.GetOptions {})
2652+ Expect (err ).ToNot (HaveOccurred ())
2653+
2654+ patchData , err := patch .New (
2655+ patch .WithAddLabel (velerov1api .ExcludeFromBackupLabel , "true" , vm .Labels ),
2656+ ).GeneratePayload ()
2657+ Expect (err ).ToNot (HaveOccurred ())
2658+
2659+ _ , err = f .KvClient .VirtualMachine (f .Namespace .Name ).Patch (context .Background (), name , types .JSONPatchType , patchData , metav1.PatchOptions {})
2660+ Expect (err ).ToNot (HaveOccurred ())
26482661 }
26492662
26502663 addExcludeLabelToLauncherPodForVM := func (vmName string ) {
2651- retryOnceOnErr (
2652- func () error {
2653- pod := framework .FindLauncherPod (f .K8sClient , f .Namespace .Name , vmName )
2654- pod .SetLabels (addExcludeLabel (pod .GetLabels ()))
2655- _ , err := f .K8sClient .CoreV1 ().Pods (f .Namespace .Name ).Update (context .TODO (), & pod , metav1.UpdateOptions {})
2656- return err
2657- }).Should (BeNil ())
2664+ pod := framework .FindLauncherPod (f .K8sClient , f .Namespace .Name , vmName )
2665+ patchData , err := patch .New (
2666+ patch .WithAddLabel (velerov1api .ExcludeFromBackupLabel , "true" , pod .Labels ),
2667+ ).GeneratePayload ()
2668+ Expect (err ).ToNot (HaveOccurred ())
2669+
2670+ _ , err = f .K8sClient .CoreV1 ().Pods (f .Namespace .Name ).Patch (context .TODO (), pod .Name , types .JSONPatchType , patchData , metav1.PatchOptions {})
2671+ Expect (err ).ToNot (HaveOccurred ())
26582672 }
26592673
26602674 Context ("Standalone DV" , func () {
@@ -3456,70 +3470,6 @@ func addExpectedPVs(client *kubernetes.Clientset, namespace string, resources ma
34563470 resources ["PersistentVolume" ] = pvs
34573471}
34583472
3459- func updateVm (kvClient kubecli.KubevirtClient , namespace string , name string ,
3460- update func (* kvv1.VirtualMachine ) * kvv1.VirtualMachine ) func () error {
3461- return func () error {
3462- vm , err := kvClient .VirtualMachine (namespace ).Get (context .Background (), name , metav1.GetOptions {})
3463- if err != nil {
3464- return err
3465- }
3466- vm = update (vm )
3467-
3468- _ , err = kvClient .VirtualMachine (namespace ).Update (context .Background (), vm , metav1.UpdateOptions {})
3469- return err
3470- }
3471- }
3472-
3473- func updateVmi (kvClient kubecli.KubevirtClient , namespace string , name string ,
3474- update func (* kvv1.VirtualMachineInstance ) * kvv1.VirtualMachineInstance ) func () error {
3475- return func () error {
3476- vmi , err := kvClient .VirtualMachineInstance (namespace ).Get (context .Background (), name , metav1.GetOptions {})
3477- if err != nil {
3478- return err
3479- }
3480- vmi = update (vmi )
3481-
3482- _ , err = kvClient .VirtualMachineInstance (namespace ).Update (context .Background (), vmi , metav1.UpdateOptions {})
3483- return err
3484- }
3485- }
3486-
3487- func updatePvc (client * kubernetes.Clientset , namespace string , name string ,
3488- update func (* v1.PersistentVolumeClaim ) * v1.PersistentVolumeClaim ) func () error {
3489- return func () error {
3490- pvc , err := framework .FindPVC (client , namespace , name )
3491- if err != nil {
3492- return err
3493- }
3494- pvc = update (pvc )
3495-
3496- _ , err = client .CoreV1 ().PersistentVolumeClaims (namespace ).Update (context .TODO (), pvc , metav1.UpdateOptions {})
3497- return err
3498- }
3499- }
3500- func updateDataVolume (kvClient kubecli.KubevirtClient , namespace string , name string ,
3501- update func (dataVolume * cdiv1.DataVolume ) * cdiv1.DataVolume ) func () error {
3502- return func () error {
3503- dv , err := framework .FindDataVolume (kvClient , namespace , name )
3504- if err != nil {
3505- return err
3506- }
3507- dv = update (dv )
3508-
3509- _ , err = kvClient .CdiClient ().CdiV1beta1 ().DataVolumes (namespace ).Update (context .TODO (), dv , metav1.UpdateOptions {})
3510- return err
3511- }
3512- }
3513-
3514- func retryOnceOnErr (f func () error ) Assertion {
3515- err := f ()
3516- if err != nil {
3517- err = f ()
3518- }
3519-
3520- return Expect (err )
3521- }
3522-
35233473func runPodAndWaitSucceeded (kvClient kubecli.KubevirtClient , namespace string , podSpec * v1.Pod ) * v1.Pod {
35243474 return framework .RunPodAndWaitPhase (kvClient , namespace , podSpec , v1 .PodSucceeded )
35253475}
0 commit comments