@@ -90,7 +90,8 @@ func TestSecretReplicator(t *testing.T) {
9090 Name : prefix + "test" ,
9191 },
9292 }
93- _ , err = client .CoreV1 ().Namespaces ().Create (context .TODO (), & ns , metav1.CreateOptions {})
93+
94+ nsData , err := client .CoreV1 ().Namespaces ().Create (context .TODO (), & ns , metav1.CreateOptions {})
9495 require .NoError (t , err )
9596
9697 ns2 := corev1.Namespace {
@@ -533,6 +534,164 @@ func TestSecretReplicator(t *testing.T) {
533534 require .Equal (t , []byte ("Hello Bar" ), updTarget .Data ["bar" ])
534535 })
535536
537+ t .Run ("replication is pushed to other namespaces without ownerReferences" , func (t * testing.T ) {
538+ sourceLabels := map [string ]string {
539+ "foo" : "bar" ,
540+ "hello" : "world" ,
541+ }
542+ source := corev1.Secret {
543+ ObjectMeta : metav1.ObjectMeta {
544+ Name : "source-pushed-to-other-without-owner-references" ,
545+ Namespace : ns .Name ,
546+ Annotations : map [string ]string {
547+ common .ReplicateTo : prefix + "test2" ,
548+ },
549+ Labels : sourceLabels ,
550+ OwnerReferences : []metav1.OwnerReference {{
551+ APIVersion : "v1" ,
552+ Kind : "Namespace" ,
553+ Name : nsData .Name ,
554+ UID : nsData .UID ,
555+ }},
556+ },
557+ Type : corev1 .SecretTypeOpaque ,
558+ Data : map [string ][]byte {
559+ "foo" : []byte ("Hello Foo" ),
560+ "bar" : []byte ("Hello Bar" ),
561+ },
562+ }
563+
564+ wg , stop := waitForSecrets (client , 2 , EventHandlerFuncs {
565+ AddFunc : func (wg * sync.WaitGroup , obj interface {}) {
566+ secret := obj .(* corev1.Secret )
567+ if secret .Namespace == source .Namespace && secret .Name == source .Name {
568+ log .Debugf ("AddFunc %+v" , obj )
569+ wg .Done ()
570+ } else if secret .Namespace == prefix + "test2" && secret .Name == source .Name {
571+ log .Debugf ("AddFunc %+v" , obj )
572+ wg .Done ()
573+ }
574+ },
575+ })
576+ _ , err := secrets .Create (context .TODO (), & source , metav1.CreateOptions {})
577+ require .NoError (t , err )
578+
579+ waitWithTimeout (wg , MaxWaitTime )
580+ close (stop )
581+
582+ secrets2 := client .CoreV1 ().Secrets (prefix + "test2" )
583+ updTarget , err := secrets2 .Get (context .TODO (), source .Name , metav1.GetOptions {})
584+
585+ require .NoError (t , err )
586+ require .Equal (t , []byte ("Hello Foo" ), updTarget .Data ["foo" ])
587+ require .True (t , reflect .DeepEqual (sourceLabels , updTarget .Labels ))
588+
589+ require .Equal (t , []metav1.OwnerReference (nil ), updTarget .OwnerReferences )
590+ require .NotEqual (t , source .OwnerReferences , updTarget .OwnerReferences )
591+
592+ wg , stop = waitForSecrets (client , 1 , EventHandlerFuncs {
593+ UpdateFunc : func (wg * sync.WaitGroup , oldObj interface {}, newObj interface {}) {
594+ secret := oldObj .(* corev1.Secret )
595+ if secret .Namespace == prefix + "test2" && secret .Name == source .Name {
596+ log .Debugf ("UpdateFunc %+v -> %+v" , oldObj , newObj )
597+ wg .Done ()
598+ }
599+ },
600+ })
601+
602+ _ , err = secrets .Patch (context .TODO (), source .Name , types .JSONPatchType , []byte (`[{"op": "remove", "path": "/data/foo"}]` ), metav1.PatchOptions {})
603+ require .NoError (t , err )
604+
605+ waitWithTimeout (wg , MaxWaitTime )
606+ close (stop )
607+
608+ updTarget , err = secrets2 .Get (context .TODO (), source .Name , metav1.GetOptions {})
609+ require .NoError (t , err )
610+
611+ _ , hasFoo := updTarget .Data ["foo" ]
612+ require .False (t , hasFoo )
613+ require .Equal (t , []byte ("Hello Bar" ), updTarget .Data ["bar" ])
614+ })
615+
616+ t .Run ("replication is pushed to other namespaces with ownerReferences" , func (t * testing.T ) {
617+ sourceLabels := map [string ]string {
618+ "foo" : "bar" ,
619+ "hello" : "world" ,
620+ }
621+ source := corev1.Secret {
622+ ObjectMeta : metav1.ObjectMeta {
623+ Name : "source-pushed-to-other-with-owner-references" ,
624+ Namespace : ns .Name ,
625+ Annotations : map [string ]string {
626+ common .ReplicateTo : prefix + "test2" ,
627+ common .KeepOwnerReferences : "true" ,
628+ },
629+ Labels : sourceLabels ,
630+ OwnerReferences : []metav1.OwnerReference {{
631+ APIVersion : "v1" ,
632+ Kind : "Namespace" ,
633+ Name : nsData .Name ,
634+ UID : nsData .UID ,
635+ }},
636+ },
637+ Type : corev1 .SecretTypeOpaque ,
638+ Data : map [string ][]byte {
639+ "foo" : []byte ("Hello Foo" ),
640+ "bar" : []byte ("Hello Bar" ),
641+ },
642+ }
643+
644+ wg , stop := waitForSecrets (client , 2 , EventHandlerFuncs {
645+ AddFunc : func (wg * sync.WaitGroup , obj interface {}) {
646+ secret := obj .(* corev1.Secret )
647+ if secret .Namespace == source .Namespace && secret .Name == source .Name {
648+ log .Debugf ("AddFunc %+v" , obj )
649+ wg .Done ()
650+ } else if secret .Namespace == prefix + "test2" && secret .Name == source .Name {
651+ log .Debugf ("AddFunc %+v" , obj )
652+ wg .Done ()
653+ }
654+ },
655+ })
656+ _ , err := secrets .Create (context .TODO (), & source , metav1.CreateOptions {})
657+ require .NoError (t , err )
658+
659+ waitWithTimeout (wg , MaxWaitTime )
660+ close (stop )
661+
662+ secrets2 := client .CoreV1 ().Secrets (prefix + "test2" )
663+ updTarget , err := secrets2 .Get (context .TODO (), source .Name , metav1.GetOptions {})
664+
665+ require .NoError (t , err )
666+ require .Equal (t , []byte ("Hello Foo" ), updTarget .Data ["foo" ])
667+ require .True (t , reflect .DeepEqual (sourceLabels , updTarget .Labels ))
668+
669+ require .Equal (t , source .OwnerReferences , updTarget .OwnerReferences )
670+
671+ wg , stop = waitForSecrets (client , 1 , EventHandlerFuncs {
672+ UpdateFunc : func (wg * sync.WaitGroup , oldObj interface {}, newObj interface {}) {
673+ secret := oldObj .(* corev1.Secret )
674+ if secret .Namespace == prefix + "test2" && secret .Name == source .Name {
675+ log .Debugf ("UpdateFunc %+v -> %+v" , oldObj , newObj )
676+ wg .Done ()
677+ }
678+ },
679+ })
680+
681+ _ , err = secrets .Patch (context .TODO (), source .Name , types .JSONPatchType , []byte (`[{"op": "remove", "path": "/data/foo"}]` ), metav1.PatchOptions {})
682+ require .NoError (t , err )
683+
684+ waitWithTimeout (wg , MaxWaitTime )
685+ close (stop )
686+
687+ updTarget , err = secrets2 .Get (context .TODO (), source .Name , metav1.GetOptions {})
688+ require .NoError (t , err )
689+
690+ _ , hasFoo := updTarget .Data ["foo" ]
691+ require .False (t , hasFoo )
692+ require .Equal (t , []byte ("Hello Bar" ), updTarget .Data ["bar" ])
693+ })
694+
536695 t .Run ("replication is pushed to other namespaces by label selector" , func (t * testing.T ) {
537696 source := corev1.Secret {
538697 ObjectMeta : metav1.ObjectMeta {
0 commit comments