@@ -604,3 +604,104 @@ func checkSecret(list *corev1.SecretList, name string) bool {
604604
605605 return false
606606}
607+
608+ func TestKustomizationReconciler_BuildMetadata (t * testing.T ) {
609+ g := NewWithT (t )
610+ id := "bm-" + randStringRunes (5 )
611+ revision := "v1.0.0"
612+ resultK := & kustomizev1.Kustomization {}
613+
614+ err := createNamespace (id )
615+ g .Expect (err ).NotTo (HaveOccurred (), "failed to create test namespace" )
616+
617+ err = createKubeConfigSecret (id )
618+ g .Expect (err ).NotTo (HaveOccurred (), "failed to create kubeconfig secret" )
619+
620+ manifests := func (name string ) []testserver.File {
621+ return []testserver.File {
622+ {
623+ Name : "config.yaml" ,
624+ Body : fmt .Sprintf (`---
625+ apiVersion: v1
626+ kind: ConfigMap
627+ metadata:
628+ name: %[1]s
629+ data:
630+ key: val
631+ ` , name ),
632+ },
633+ }
634+ }
635+
636+ artifact , err := testServer .ArtifactFromFiles (manifests (id ))
637+ g .Expect (err ).NotTo (HaveOccurred ())
638+
639+ repositoryName := types.NamespacedName {
640+ Name : fmt .Sprintf ("bm-%s" , randStringRunes (5 )),
641+ Namespace : id ,
642+ }
643+
644+ err = applyGitRepository (repositoryName , artifact , revision )
645+ g .Expect (err ).NotTo (HaveOccurred ())
646+
647+ kustomizationKey := types.NamespacedName {
648+ Name : fmt .Sprintf ("bm-%s" , randStringRunes (5 )),
649+ Namespace : id ,
650+ }
651+ kustomization := & kustomizev1.Kustomization {
652+ ObjectMeta : metav1.ObjectMeta {
653+ Name : kustomizationKey .Name ,
654+ Namespace : kustomizationKey .Namespace ,
655+ },
656+ Spec : kustomizev1.KustomizationSpec {
657+ Interval : metav1.Duration {Duration : 2 * time .Minute },
658+ Path : "./" ,
659+ KubeConfig : & meta.KubeConfigReference {
660+ SecretRef : & meta.SecretKeyReference {
661+ Name : "kubeconfig" ,
662+ },
663+ },
664+ SourceRef : kustomizev1.CrossNamespaceSourceReference {
665+ Name : repositoryName .Name ,
666+ Namespace : repositoryName .Namespace ,
667+ Kind : sourcev1 .GitRepositoryKind ,
668+ },
669+ Prune : true ,
670+ BuildMetadata : []kustomizev1.BuildMetadataOption {
671+ kustomizev1 .BuildMetadataOriginAnnotations ,
672+ },
673+ TargetNamespace : id ,
674+ },
675+ }
676+
677+ g .Expect (k8sClient .Create (context .Background (), kustomization )).To (Succeed ())
678+
679+ t .Run ("sets origin annotations" , func (t * testing.T ) {
680+ g := NewWithT (t )
681+ g .Eventually (func () bool {
682+ _ = k8sClient .Get (context .Background (), client .ObjectKeyFromObject (kustomization ), resultK )
683+ return isReconcileSuccess (resultK )
684+ }, timeout , time .Second ).Should (BeTrue ())
685+ kstatusCheck .CheckErr (ctx , resultK )
686+
687+ var cm corev1.ConfigMap
688+ g .Expect (k8sClient .Get (context .Background (), client.ObjectKey {Name : id , Namespace : id }, & cm )).To (Succeed ())
689+ g .Expect (cm .GetAnnotations ()).To (HaveKey ("config.kubernetes.io/origin" ))
690+ })
691+
692+ t .Run ("removes origin annotations" , func (t * testing.T ) {
693+ g := NewWithT (t )
694+ resultK .Spec .BuildMetadata = nil
695+ g .Expect (k8sClient .Update (context .Background (), resultK )).To (Succeed ())
696+
697+ g .Eventually (func () bool {
698+ _ = k8sClient .Get (context .Background (), client .ObjectKeyFromObject (kustomization ), resultK )
699+ return isReconcileSuccess (resultK )
700+ }, timeout , time .Second ).Should (BeTrue ())
701+ kstatusCheck .CheckErr (ctx , resultK )
702+
703+ var cm corev1.ConfigMap
704+ g .Expect (k8sClient .Get (context .Background (), client.ObjectKey {Name : id , Namespace : id }, & cm )).To (Succeed ())
705+ g .Expect (cm .GetAnnotations ()).ToNot (HaveKey ("config.kubernetes.io/origin" ))
706+ })
707+ }
0 commit comments