@@ -8,8 +8,11 @@ import (
8
8
9
9
. "github.com/onsi/ginkgo/v2"
10
10
. "github.com/onsi/gomega"
11
+ "k8s.io/apimachinery/pkg/api/errors"
11
12
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
13
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
14
+ "k8s.io/apimachinery/pkg/runtime"
15
+ "k8s.io/client-go/dynamic"
13
16
appsv1 "open-cluster-management.io/multicloud-operators-subscription/pkg/apis/apps/placementrule/v1"
14
17
15
18
policiesv1 "open-cluster-management.io/governance-policy-propagator/api/v1"
@@ -536,4 +539,210 @@ var _ = Describe("Test policy propagation", func() {
536
539
utils .ListWithTimeout (clientHubDynamic , gvrPolicy , opt , 0 , false , 10 )
537
540
})
538
541
})
542
+
543
+ Describe ("Test spec.copyPolicyMetadata" , Ordered , func () {
544
+ const policyName = "case1-metadata"
545
+ policyClient := func () dynamic.ResourceInterface {
546
+ return clientHubDynamic .Resource (gvrPolicy ).Namespace (testNamespace )
547
+ }
548
+
549
+ getPolicy := func () * policiesv1.Policy {
550
+ return & policiesv1.Policy {
551
+ TypeMeta : metav1.TypeMeta {
552
+ Kind : policiesv1 .Kind ,
553
+ APIVersion : policiesv1 .GroupVersion .String (),
554
+ },
555
+ ObjectMeta : metav1.ObjectMeta {
556
+ Name : policyName ,
557
+ Namespace : testNamespace ,
558
+ },
559
+ Spec : policiesv1.PolicySpec {
560
+ Disabled : false ,
561
+ PolicyTemplates : []* policiesv1.PolicyTemplate {},
562
+ },
563
+ }
564
+ }
565
+
566
+ BeforeAll (func () {
567
+ By ("Creating a PlacementRule" )
568
+ plr := & unstructured.Unstructured {
569
+ Object : map [string ]interface {}{
570
+ "apiVersion" : "apps.open-cluster-management.io/v1" ,
571
+ "kind" : "PlacementRule" ,
572
+ "metadata" : map [string ]interface {}{
573
+ "name" : policyName ,
574
+ "namespace" : testNamespace ,
575
+ },
576
+ "spec" : map [string ]interface {}{},
577
+ },
578
+ }
579
+ var err error
580
+ plr , err = clientHubDynamic .Resource (gvrPlacementRule ).Namespace (testNamespace ).Create (
581
+ context .TODO (), plr , metav1.CreateOptions {},
582
+ )
583
+ Expect (err ).To (BeNil ())
584
+
585
+ plr .Object ["status" ] = utils .GeneratePlrStatus ("managed1" )
586
+ _ , err = clientHubDynamic .Resource (gvrPlacementRule ).Namespace (testNamespace ).UpdateStatus (
587
+ context .TODO (), plr , metav1.UpdateOptions {},
588
+ )
589
+ Expect (err ).To (BeNil ())
590
+
591
+ By ("Creating a PlacementBinding" )
592
+ plb := & unstructured.Unstructured {
593
+ Object : map [string ]interface {}{
594
+ "apiVersion" : policiesv1 .GroupVersion .String (),
595
+ "kind" : "PlacementBinding" ,
596
+ "metadata" : map [string ]interface {}{
597
+ "name" : policyName ,
598
+ "namespace" : testNamespace ,
599
+ },
600
+ "placementRef" : map [string ]interface {}{
601
+ "apiGroup" : gvrPlacementRule .Group ,
602
+ "kind" : "PlacementRule" ,
603
+ "name" : policyName ,
604
+ },
605
+ "subjects" : []interface {}{
606
+ map [string ]interface {}{
607
+ "apiGroup" : policiesv1 .GroupVersion .Group ,
608
+ "kind" : policiesv1 .Kind ,
609
+ "name" : policyName ,
610
+ },
611
+ },
612
+ },
613
+ }
614
+
615
+ _ , err = clientHubDynamic .Resource (gvrPlacementBinding ).Namespace (testNamespace ).Create (
616
+ context .TODO (), plb , metav1.CreateOptions {},
617
+ )
618
+ Expect (err ).To (BeNil ())
619
+ })
620
+
621
+ AfterEach (func () {
622
+ By ("Removing the policy" )
623
+ err := policyClient ().Delete (context .TODO (), policyName , metav1.DeleteOptions {})
624
+ if ! errors .IsNotFound (err ) {
625
+ Expect (err ).To (BeNil ())
626
+ }
627
+
628
+ replicatedPolicy := utils .GetWithTimeout (
629
+ clientHubDynamic ,
630
+ gvrPolicy ,
631
+ testNamespace + "." + policyName ,
632
+ "managed1" ,
633
+ false ,
634
+ defaultTimeoutSeconds ,
635
+ )
636
+ Expect (replicatedPolicy ).To (BeNil ())
637
+ })
638
+
639
+ AfterAll (func () {
640
+ By ("Removing the PlacementRule" )
641
+ err := clientHubDynamic .Resource (gvrPlacementRule ).Namespace (testNamespace ).Delete (
642
+ context .TODO (), policyName , metav1.DeleteOptions {},
643
+ )
644
+ if ! errors .IsNotFound (err ) {
645
+ Expect (err ).To (BeNil ())
646
+ }
647
+
648
+ By ("Removing the PlacementBinding" )
649
+ err = clientHubDynamic .Resource (gvrPlacementBinding ).Namespace (testNamespace ).Delete (
650
+ context .TODO (), policyName , metav1.DeleteOptions {},
651
+ )
652
+ if ! errors .IsNotFound (err ) {
653
+ Expect (err ).To (BeNil ())
654
+ }
655
+ })
656
+
657
+ It ("Verifies that spec.copyPolicyMetadata defaults to unset" , func () {
658
+ By ("Creating an empty policy" )
659
+ policy := getPolicy ()
660
+ policyMap , err := runtime .DefaultUnstructuredConverter .ToUnstructured (policy )
661
+ Expect (err ).To (BeNil ())
662
+
663
+ policyRV , err := policyClient ().Create (
664
+ context .TODO (), & unstructured.Unstructured {Object : policyMap }, metav1.CreateOptions {},
665
+ )
666
+ Expect (err ).To (BeNil ())
667
+
668
+ _ , found , _ := unstructured .NestedBool (policyRV .Object , "spec" , "copyPolicyMetadata" )
669
+ Expect (found ).To (BeFalse ())
670
+ })
671
+
672
+ It ("verifies that the labels and annotations are copied with spec.copyPolicyMetadata=true" , func () {
673
+ By ("Creating a policy with labels and annotations" )
674
+ policy := getPolicy ()
675
+ copyPolicyMetadata := true
676
+ policy .Spec .CopyPolicyMetadata = & copyPolicyMetadata
677
+ policy .SetAnnotations (map [string ]string {"do" : "copy" , "please" : "do-copy" })
678
+ policy .SetLabels (map [string ]string {"do" : "copy" , "please" : "do-copy" })
679
+
680
+ policyMap , err := runtime .DefaultUnstructuredConverter .ToUnstructured (policy )
681
+ Expect (err ).To (BeNil ())
682
+
683
+ _ , err = policyClient ().Create (
684
+ context .TODO (), & unstructured.Unstructured {Object : policyMap }, metav1.CreateOptions {},
685
+ )
686
+ Expect (err ).To (BeNil ())
687
+
688
+ Eventually (func (g Gomega ) {
689
+ replicatedPlc := utils .GetWithTimeout (
690
+ clientHubDynamic ,
691
+ gvrPolicy ,
692
+ testNamespace + "." + policyName ,
693
+ "managed1" ,
694
+ true ,
695
+ defaultTimeoutSeconds ,
696
+ )
697
+
698
+ annotations := replicatedPlc .GetAnnotations ()
699
+ g .Expect (annotations ["do" ]).To (Equal ("copy" ))
700
+ g .Expect (annotations ["please" ]).To (Equal ("do-copy" ))
701
+ // This annotation is always set.
702
+ g .Expect (annotations ["argocd.argoproj.io/compare-options" ]).To (Equal ("IgnoreExtraneous" ))
703
+
704
+ labels := replicatedPlc .GetLabels ()
705
+ g .Expect (labels ["do" ]).To (Equal ("copy" ))
706
+ g .Expect (labels ["please" ]).To (Equal ("do-copy" ))
707
+ }, defaultTimeoutSeconds , 1 ).Should (Succeed ())
708
+ })
709
+
710
+ It ("verifies that the labels and annotations are not copied with spec.copyPolicyMetadata=false" , func () {
711
+ By ("Creating a policy with labels and annotations" )
712
+ policy := getPolicy ()
713
+ copyPolicyMetadata := false
714
+ policy .Spec .CopyPolicyMetadata = & copyPolicyMetadata
715
+ policy .SetAnnotations (map [string ]string {"do-not" : "copy" , "please" : "do-not-copy" })
716
+ policy .SetLabels (map [string ]string {"do-not" : "copy" , "please" : "do-not-copy" })
717
+
718
+ policyMap , err := runtime .DefaultUnstructuredConverter .ToUnstructured (policy )
719
+ Expect (err ).To (BeNil ())
720
+
721
+ _ , err = policyClient ().Create (
722
+ context .TODO (), & unstructured.Unstructured {Object : policyMap }, metav1.CreateOptions {},
723
+ )
724
+ Expect (err ).To (BeNil ())
725
+
726
+ Eventually (func (g Gomega ) {
727
+ replicatedPlc := utils .GetWithTimeout (
728
+ clientHubDynamic ,
729
+ gvrPolicy ,
730
+ testNamespace + "." + policyName ,
731
+ "managed1" ,
732
+ true ,
733
+ defaultTimeoutSeconds ,
734
+ )
735
+
736
+ annotations := replicatedPlc .GetAnnotations ()
737
+ g .Expect (annotations ["do-not" ]).To (Equal ("" ))
738
+ g .Expect (annotations ["please" ]).To (Equal ("" ))
739
+ // This annotation is always set.
740
+ g .Expect (annotations ["argocd.argoproj.io/compare-options" ]).To (Equal ("IgnoreExtraneous" ))
741
+
742
+ labels := replicatedPlc .GetLabels ()
743
+ g .Expect (labels ["do-not" ]).To (Equal ("" ))
744
+ g .Expect (labels ["please" ]).To (Equal ("" ))
745
+ }, defaultTimeoutSeconds , 1 ).Should (Succeed ())
746
+ })
747
+ })
539
748
})
0 commit comments