@@ -12,6 +12,7 @@ import (
1212 "github.com/stretchr/testify/assert"
1313 "github.com/stretchr/testify/require"
1414 corev1 "k8s.io/api/core/v1"
15+ apierrors "k8s.io/apimachinery/pkg/api/errors"
1516 apimeta "k8s.io/apimachinery/pkg/api/meta"
1617 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1718 "k8s.io/apimachinery/pkg/runtime"
@@ -857,6 +858,138 @@ func TestGarbageCollectDNSRecordSets_DoesNotDeleteInUseRecords(t *testing.T) {
857858 require .NoError (t , cl .Get (ctx , client.ObjectKey {Namespace : ns , Name : rsName }, & remaining ))
858859}
859860
861+ // ---------------------------------------------------------------------------
862+ // TestCleanupDNSRecordSets
863+ // ---------------------------------------------------------------------------
864+
865+ func TestCleanupDNSRecordSets_DeletesManagedRecords (t * testing.T ) {
866+ const ns = "test-ns"
867+ ctx := log .IntoContext (context .Background (), zap .New ())
868+ s := newDNSTestScheme (t )
869+
870+ testConfig := config.NetworkServicesOperator {
871+ Gateway : config.GatewayConfig {
872+ TargetDomain : "gateways.test.local" ,
873+ EnableDNSIntegration : true ,
874+ },
875+ }
876+
877+ gw := newTestGatewayForDNS (ns , "my-gw" )
878+ makeRS := func (name , hostname string ) * dnsv1alpha1.DNSRecordSet {
879+ return & dnsv1alpha1.DNSRecordSet {
880+ ObjectMeta : metav1.ObjectMeta {
881+ Namespace : ns ,
882+ Name : name ,
883+ UID : uuid .NewUUID (),
884+ Labels : map [string ]string {
885+ labelManagedBy : labelManagedByValue ,
886+ labelDNSManaged : "true" ,
887+ labelDNSSourceKind : KindGateway ,
888+ labelDNSSourceName : gw .Name ,
889+ labelDNSSourceNS : ns ,
890+ },
891+ Annotations : map [string ]string {
892+ annotationDNSHostname : hostname ,
893+ },
894+ },
895+ Spec : dnsv1alpha1.DNSRecordSetSpec {
896+ DNSZoneRef : corev1.LocalObjectReference {Name : "example-com" },
897+ RecordType : dnsv1alpha1 .RRTypeCNAME ,
898+ Records : []dnsv1alpha1.RecordEntry {{Name : hostname + "." }},
899+ },
900+ }
901+ }
902+
903+ rs1 := makeRS ("my-gw-api" , "api.example.com" )
904+ rs2 := makeRS ("my-gw-other" , "other.example.com" )
905+ allObjects := []client.Object {gw , rs1 , rs2 }
906+ for _ , obj := range allObjects {
907+ if obj .GetUID () == "" {
908+ obj .SetUID (uuid .NewUUID ())
909+ }
910+ obj .SetCreationTimestamp (metav1 .Now ())
911+ }
912+
913+ cl := buildFakeUpstreamClientForDNS (s , allObjects ... )
914+ reconciler := newDNSReconciler (testConfig )
915+
916+ result := reconciler .cleanupDNSRecordSets (ctx , cl , gw )
917+ require .NoError (t , result .Err )
918+
919+ // Both records should be deleted.
920+ var deleted1 dnsv1alpha1.DNSRecordSet
921+ err1 := cl .Get (ctx , client.ObjectKey {Namespace : ns , Name : rs1 .Name }, & deleted1 )
922+ assert .True (t , apierrors .IsNotFound (err1 ), "DNSRecordSet %q should have been deleted: %v" , rs1 .Name , err1 )
923+
924+ var deleted2 dnsv1alpha1.DNSRecordSet
925+ err2 := cl .Get (ctx , client.ObjectKey {Namespace : ns , Name : rs2 .Name }, & deleted2 )
926+ assert .True (t , apierrors .IsNotFound (err2 ), "DNSRecordSet %q should have been deleted: %v" , rs2 .Name , err2 )
927+ }
928+
929+ func TestCleanupDNSRecordSets_RetainsAndStripsLabelsWhenAnnotationSet (t * testing.T ) {
930+ const ns = "test-ns"
931+ ctx := log .IntoContext (context .Background (), zap .New ())
932+ s := newDNSTestScheme (t )
933+
934+ testConfig := config.NetworkServicesOperator {
935+ Gateway : config.GatewayConfig {
936+ TargetDomain : "gateways.test.local" ,
937+ EnableDNSIntegration : true ,
938+ },
939+ }
940+
941+ gw := newTestGatewayForDNS (ns , "my-gw" )
942+ rsName := dnsRecordSetName (gw .Name , "api.example.com" )
943+ rs := & dnsv1alpha1.DNSRecordSet {
944+ ObjectMeta : metav1.ObjectMeta {
945+ Namespace : ns ,
946+ Name : rsName ,
947+ UID : uuid .NewUUID (),
948+ Labels : map [string ]string {
949+ labelManagedBy : labelManagedByValue ,
950+ labelDNSManaged : "true" ,
951+ labelDNSSourceKind : KindGateway ,
952+ labelDNSSourceName : gw .Name ,
953+ labelDNSSourceNS : ns ,
954+ },
955+ Annotations : map [string ]string {
956+ annotationDNSHostname : "api.example.com" ,
957+ annotationDNSRetain : "true" ,
958+ },
959+ },
960+ Spec : dnsv1alpha1.DNSRecordSetSpec {
961+ DNSZoneRef : corev1.LocalObjectReference {Name : "example-com" },
962+ RecordType : dnsv1alpha1 .RRTypeCNAME ,
963+ Records : []dnsv1alpha1.RecordEntry {{Name : "api.example.com." }},
964+ },
965+ }
966+
967+ allObjects := []client.Object {gw , rs }
968+ for _ , obj := range allObjects {
969+ if obj .GetUID () == "" {
970+ obj .SetUID (uuid .NewUUID ())
971+ }
972+ obj .SetCreationTimestamp (metav1 .Now ())
973+ }
974+
975+ cl := buildFakeUpstreamClientForDNS (s , allObjects ... )
976+ reconciler := newDNSReconciler (testConfig )
977+
978+ result := reconciler .cleanupDNSRecordSets (ctx , cl , gw )
979+ require .NoError (t , result .Err )
980+
981+ // Record should still exist.
982+ var updated dnsv1alpha1.DNSRecordSet
983+ require .NoError (t , cl .Get (ctx , client.ObjectKey {Namespace : ns , Name : rsName }, & updated ))
984+
985+ // Managed-by labels should have been removed.
986+ assert .Empty (t , updated .Labels [labelDNSManaged ], "label %q should be removed" , labelDNSManaged )
987+ assert .Empty (t , updated .Labels [labelManagedBy ], "label %q should be removed" , labelManagedBy )
988+ assert .Empty (t , updated .Labels [labelDNSSourceKind ], "label %q should be removed" , labelDNSSourceKind )
989+ assert .Empty (t , updated .Labels [labelDNSSourceName ], "label %q should be removed" , labelDNSSourceName )
990+ assert .Empty (t , updated .Labels [labelDNSSourceNS ], "label %q should be removed" , labelDNSSourceNS )
991+ }
992+
860993// ---------------------------------------------------------------------------
861994// TestReconcileDNSStatus
862995// ---------------------------------------------------------------------------
0 commit comments