diff --git a/controllers/dnspolicy_controller_multi_cluster_test.go b/controllers/dnspolicy_controller_multi_cluster_test.go index 6a5551b7e..19dbd0e79 100644 --- a/controllers/dnspolicy_controller_multi_cluster_test.go +++ b/controllers/dnspolicy_controller_multi_cluster_test.go @@ -79,7 +79,8 @@ var _ = Describe("DNSPolicy Multi Cluster", func() { gwHash = common.ToBase36HashLen(gateway.Name+"-"+gateway.Namespace, 6) //Set multi cluster gateway status - Eventually(func() error { + Eventually(func(g Gomega) { + g.Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(gateway), gateway)).To(Succeed()) gateway.Status.Addresses = []gatewayapiv1.GatewayStatusAddress{ { Type: ptr.To(multicluster.MultiClusterIPAddressType), @@ -116,7 +117,7 @@ var _ = Describe("DNSPolicy Multi Cluster", func() { Conditions: []metav1.Condition{}, }, } - return k8sClient.Status().Update(ctx, gateway) + g.Expect(k8sClient.Status().Update(ctx, gateway)).To(Succeed()) }, TestTimeoutMedium, TestRetryIntervalMedium).ShouldNot(HaveOccurred()) recordName = fmt.Sprintf("%s-%s", TestGatewayName, TestListenerNameOne) @@ -162,41 +163,50 @@ var _ = Describe("DNSPolicy Multi Cluster", func() { err := k8sClient.List(ctx, recordList, &client.ListOptions{Namespace: testNamespace}) g.Expect(err).NotTo(HaveOccurred()) g.Expect(recordList.Items).To(HaveLen(2)) - g.Expect(recordList.Items).To( - ContainElements( - MatchFields(IgnoreExtras, Fields{ - "ObjectMeta": HaveField("Name", recordName), - "Spec": MatchFields(IgnoreExtras, Fields{ - "OwnerID": Equal(&ownerID), - "ManagedZoneRef": HaveField("Name", "mz-example-com"), - "Endpoints": ConsistOf( - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal(TestHostOne), - "Targets": ContainElements(TestIPAddressOne, TestIPAddressTwo), - "RecordType": Equal("A"), - "SetIdentifier": Equal(""), - "RecordTTL": Equal(externaldns.TTL(60)), - })), - ), - }), - }), - MatchFields(IgnoreExtras, Fields{ - "ObjectMeta": HaveField("Name", wildcardRecordName), - "Spec": MatchFields(IgnoreExtras, Fields{ - "OwnerID": Equal(&ownerID), - "ManagedZoneRef": HaveField("Name", "mz-example-com"), - "Endpoints": ConsistOf( - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal(TestHostWildcard), - "Targets": ContainElements(TestIPAddressOne, TestIPAddressTwo), - "RecordType": Equal("A"), - "SetIdentifier": Equal(""), - "RecordTTL": Equal(externaldns.TTL(60)), - })), - ), - }), - }), - )) + + dnsRecord := &kuadrantdnsv1alpha1.DNSRecord{} + err = k8sClient.Get(ctx, client.ObjectKey{Name: recordName, Namespace: testNamespace}, dnsRecord) + g.Expect(err).NotTo(HaveOccurred()) + + wildcardDnsRecord := &kuadrantdnsv1alpha1.DNSRecord{} + err = k8sClient.Get(ctx, client.ObjectKey{Name: wildcardRecordName, Namespace: testNamespace}, wildcardDnsRecord) + g.Expect(err).NotTo(HaveOccurred()) + + g.Expect(*dnsRecord).To(MatchFields(IgnoreExtras, Fields{ + "ObjectMeta": HaveField("Name", recordName), + "Spec": MatchFields(IgnoreExtras, Fields{ + "OwnerID": Equal(&ownerID), + "ManagedZoneRef": HaveField("Name", "mz-example-com"), + "Endpoints": ConsistOf( + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal(TestHostOne), + "Targets": ContainElements(TestIPAddressOne, TestIPAddressTwo), + "RecordType": Equal("A"), + "SetIdentifier": Equal(""), + "RecordTTL": Equal(externaldns.TTL(60)), + })), + ), + }), + })) + g.Expect(testEndpointsTraversable(dnsRecord.Spec.Endpoints, TestHostOne, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue()) + + g.Expect(*wildcardDnsRecord).To(MatchFields(IgnoreExtras, Fields{ + "ObjectMeta": HaveField("Name", wildcardRecordName), + "Spec": MatchFields(IgnoreExtras, Fields{ + "OwnerID": Equal(&ownerID), + "ManagedZoneRef": HaveField("Name", "mz-example-com"), + "Endpoints": ConsistOf( + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal(TestHostWildcard), + "Targets": ContainElements(TestIPAddressOne, TestIPAddressTwo), + "RecordType": Equal("A"), + "SetIdentifier": Equal(""), + "RecordTTL": Equal(externaldns.TTL(60)), + })), + ), + }), + })) + g.Expect(testEndpointsTraversable(wildcardDnsRecord.Spec.Endpoints, TestHostWildcard, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue()) }, TestTimeoutMedium, TestRetryIntervalMedium, ctx).Should(Succeed()) }) @@ -293,6 +303,7 @@ var _ = Describe("DNSPolicy Multi Cluster", func() { }), }), ) + g.Expect(testEndpointsTraversable(dnsRecord.Spec.Endpoints, TestHostOne, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue()) g.Expect(*wildcardDnsRecord).To( MatchFields(IgnoreExtras, Fields{ @@ -358,6 +369,7 @@ var _ = Describe("DNSPolicy Multi Cluster", func() { }), }), ) + g.Expect(testEndpointsTraversable(wildcardDnsRecord.Spec.Endpoints, TestHostWildcard, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue()) }, TestTimeoutMedium, TestRetryIntervalMedium, ctx).Should(Succeed()) }) }) @@ -410,149 +422,158 @@ var _ = Describe("DNSPolicy Multi Cluster", func() { err := k8sClient.List(ctx, recordList, &client.ListOptions{Namespace: testNamespace}) g.Expect(err).NotTo(HaveOccurred()) g.Expect(recordList.Items).To(HaveLen(2)) - g.Expect(recordList.Items).To( - ContainElements( - MatchFields(IgnoreExtras, Fields{ - "ObjectMeta": HaveField("Name", recordName), - "Spec": MatchFields(IgnoreExtras, Fields{ - "OwnerID": Equal(&ownerID), - "ManagedZoneRef": HaveField("Name", "mz-example-com"), - "Endpoints": ConsistOf( - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal(clusterTwoIDHash + "-" + gwHash + ".klb.test.example.com"), - "Targets": ConsistOf(TestIPAddressTwo), - "RecordType": Equal("A"), - "SetIdentifier": Equal(""), - "RecordTTL": Equal(externaldns.TTL(60)), - })), - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal("es.klb.test.example.com"), - "Targets": ConsistOf(clusterTwoIDHash + "-" + gwHash + ".klb.test.example.com"), - "RecordType": Equal("CNAME"), - "SetIdentifier": Equal(clusterTwoIDHash + "-" + gwHash + ".klb.test.example.com"), - "RecordTTL": Equal(externaldns.TTL(60)), - "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "weight", Value: "160"}}), - })), - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal("ie.klb.test.example.com"), - "Targets": ConsistOf(clusterOneIDHash + "-" + gwHash + ".klb.test.example.com"), - "RecordType": Equal("CNAME"), - "SetIdentifier": Equal(clusterOneIDHash + "-" + gwHash + ".klb.test.example.com"), - "RecordTTL": Equal(externaldns.TTL(60)), - "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "weight", Value: "100"}}), - })), - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal(clusterOneIDHash + "-" + gwHash + ".klb.test.example.com"), - "Targets": ConsistOf(TestIPAddressOne), - "RecordType": Equal("A"), - "SetIdentifier": Equal(""), - "RecordTTL": Equal(externaldns.TTL(60)), - })), - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal("klb.test.example.com"), - "Targets": ConsistOf("es.klb.test.example.com"), - "RecordType": Equal("CNAME"), - "SetIdentifier": Equal("ES"), - "RecordTTL": Equal(externaldns.TTL(300)), - "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "geo-code", Value: "ES"}}), - })), - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal("klb.test.example.com"), - "Targets": ConsistOf("ie.klb.test.example.com"), - "RecordType": Equal("CNAME"), - "SetIdentifier": Equal("IE"), - "RecordTTL": Equal(externaldns.TTL(300)), - "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "geo-code", Value: "IE"}}), - })), - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal("klb.test.example.com"), - "Targets": ConsistOf("ie.klb.test.example.com"), - "RecordType": Equal("CNAME"), - "SetIdentifier": Equal("default"), - "RecordTTL": Equal(externaldns.TTL(300)), - "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "geo-code", Value: "*"}}), - })), - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal(TestHostOne), - "Targets": ConsistOf("klb.test.example.com"), - "RecordType": Equal("CNAME"), - "SetIdentifier": Equal(""), - "RecordTTL": Equal(externaldns.TTL(300)), - })), - ), - }), - }), - MatchFields(IgnoreExtras, Fields{ - "ObjectMeta": HaveField("Name", wildcardRecordName), - "Spec": MatchFields(IgnoreExtras, Fields{ - "OwnerID": Equal(&ownerID), - "ManagedZoneRef": HaveField("Name", "mz-example-com"), - "Endpoints": ConsistOf( - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal(clusterTwoIDHash + "-" + gwHash + ".klb.example.com"), - "Targets": ConsistOf(TestIPAddressTwo), - "RecordType": Equal("A"), - "SetIdentifier": Equal(""), - "RecordTTL": Equal(externaldns.TTL(60)), - })), - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal("es.klb.example.com"), - "Targets": ConsistOf(clusterTwoIDHash + "-" + gwHash + ".klb.example.com"), - "RecordType": Equal("CNAME"), - "SetIdentifier": Equal(clusterTwoIDHash + "-" + gwHash + ".klb.example.com"), - "RecordTTL": Equal(externaldns.TTL(60)), - "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "weight", Value: "160"}}), - })), - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal("ie.klb.example.com"), - "Targets": ConsistOf(clusterOneIDHash + "-" + gwHash + ".klb.example.com"), - "RecordType": Equal("CNAME"), - "SetIdentifier": Equal(clusterOneIDHash + "-" + gwHash + ".klb.example.com"), - "RecordTTL": Equal(externaldns.TTL(60)), - "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "weight", Value: "100"}}), - })), - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal(clusterOneIDHash + "-" + gwHash + ".klb.example.com"), - "Targets": ConsistOf(TestIPAddressOne), - "RecordType": Equal("A"), - "SetIdentifier": Equal(""), - "RecordTTL": Equal(externaldns.TTL(60)), - })), - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal("klb.example.com"), - "Targets": ConsistOf("ie.klb.example.com"), - "RecordType": Equal("CNAME"), - "SetIdentifier": Equal("IE"), - "RecordTTL": Equal(externaldns.TTL(300)), - "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "geo-code", Value: "IE"}}), - })), - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal("klb.example.com"), - "Targets": ConsistOf("es.klb.example.com"), - "RecordType": Equal("CNAME"), - "SetIdentifier": Equal("ES"), - "RecordTTL": Equal(externaldns.TTL(300)), - "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "geo-code", Value: "ES"}}), - })), - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal("klb.example.com"), - "Targets": ConsistOf("ie.klb.example.com"), - "RecordType": Equal("CNAME"), - "SetIdentifier": Equal("default"), - "RecordTTL": Equal(externaldns.TTL(300)), - "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "geo-code", Value: "*"}}), - })), - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal(TestHostWildcard), - "Targets": ConsistOf("klb.example.com"), - "RecordType": Equal("CNAME"), - "SetIdentifier": Equal(""), - "RecordTTL": Equal(externaldns.TTL(300)), - })), - ), - }), - }), - )) + + dnsRecord := &kuadrantdnsv1alpha1.DNSRecord{} + err = k8sClient.Get(ctx, client.ObjectKey{Name: recordName, Namespace: testNamespace}, dnsRecord) + g.Expect(err).NotTo(HaveOccurred()) + + wildcardDnsRecord := &kuadrantdnsv1alpha1.DNSRecord{} + err = k8sClient.Get(ctx, client.ObjectKey{Name: wildcardRecordName, Namespace: testNamespace}, wildcardDnsRecord) + g.Expect(err).NotTo(HaveOccurred()) + + g.Expect(*dnsRecord).To(MatchFields(IgnoreExtras, Fields{ + "ObjectMeta": HaveField("Name", recordName), + "Spec": MatchFields(IgnoreExtras, Fields{ + "OwnerID": Equal(&ownerID), + "ManagedZoneRef": HaveField("Name", "mz-example-com"), + "Endpoints": ConsistOf( + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal(clusterTwoIDHash + "-" + gwHash + ".klb.test.example.com"), + "Targets": ConsistOf(TestIPAddressTwo), + "RecordType": Equal("A"), + "SetIdentifier": Equal(""), + "RecordTTL": Equal(externaldns.TTL(60)), + })), + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal("es.klb.test.example.com"), + "Targets": ConsistOf(clusterTwoIDHash + "-" + gwHash + ".klb.test.example.com"), + "RecordType": Equal("CNAME"), + "SetIdentifier": Equal(clusterTwoIDHash + "-" + gwHash + ".klb.test.example.com"), + "RecordTTL": Equal(externaldns.TTL(60)), + "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "weight", Value: "160"}}), + })), + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal("ie.klb.test.example.com"), + "Targets": ConsistOf(clusterOneIDHash + "-" + gwHash + ".klb.test.example.com"), + "RecordType": Equal("CNAME"), + "SetIdentifier": Equal(clusterOneIDHash + "-" + gwHash + ".klb.test.example.com"), + "RecordTTL": Equal(externaldns.TTL(60)), + "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "weight", Value: "100"}}), + })), + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal(clusterOneIDHash + "-" + gwHash + ".klb.test.example.com"), + "Targets": ConsistOf(TestIPAddressOne), + "RecordType": Equal("A"), + "SetIdentifier": Equal(""), + "RecordTTL": Equal(externaldns.TTL(60)), + })), + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal("klb.test.example.com"), + "Targets": ConsistOf("es.klb.test.example.com"), + "RecordType": Equal("CNAME"), + "SetIdentifier": Equal("ES"), + "RecordTTL": Equal(externaldns.TTL(300)), + "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "geo-code", Value: "ES"}}), + })), + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal("klb.test.example.com"), + "Targets": ConsistOf("ie.klb.test.example.com"), + "RecordType": Equal("CNAME"), + "SetIdentifier": Equal("IE"), + "RecordTTL": Equal(externaldns.TTL(300)), + "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "geo-code", Value: "IE"}}), + })), + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal("klb.test.example.com"), + "Targets": ConsistOf("ie.klb.test.example.com"), + "RecordType": Equal("CNAME"), + "SetIdentifier": Equal("default"), + "RecordTTL": Equal(externaldns.TTL(300)), + "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "geo-code", Value: "*"}}), + })), + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal(TestHostOne), + "Targets": ConsistOf("klb.test.example.com"), + "RecordType": Equal("CNAME"), + "SetIdentifier": Equal(""), + "RecordTTL": Equal(externaldns.TTL(300)), + })), + ), + }), + })) + g.Expect(testEndpointsTraversable(dnsRecord.Spec.Endpoints, TestHostOne, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue()) + + g.Expect(*wildcardDnsRecord).To(MatchFields(IgnoreExtras, Fields{ + "ObjectMeta": HaveField("Name", wildcardRecordName), + "Spec": MatchFields(IgnoreExtras, Fields{ + "OwnerID": Equal(&ownerID), + "ManagedZoneRef": HaveField("Name", "mz-example-com"), + "Endpoints": ConsistOf( + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal(clusterTwoIDHash + "-" + gwHash + ".klb.example.com"), + "Targets": ConsistOf(TestIPAddressTwo), + "RecordType": Equal("A"), + "SetIdentifier": Equal(""), + "RecordTTL": Equal(externaldns.TTL(60)), + })), + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal("es.klb.example.com"), + "Targets": ConsistOf(clusterTwoIDHash + "-" + gwHash + ".klb.example.com"), + "RecordType": Equal("CNAME"), + "SetIdentifier": Equal(clusterTwoIDHash + "-" + gwHash + ".klb.example.com"), + "RecordTTL": Equal(externaldns.TTL(60)), + "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "weight", Value: "160"}}), + })), + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal("ie.klb.example.com"), + "Targets": ConsistOf(clusterOneIDHash + "-" + gwHash + ".klb.example.com"), + "RecordType": Equal("CNAME"), + "SetIdentifier": Equal(clusterOneIDHash + "-" + gwHash + ".klb.example.com"), + "RecordTTL": Equal(externaldns.TTL(60)), + "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "weight", Value: "100"}}), + })), + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal(clusterOneIDHash + "-" + gwHash + ".klb.example.com"), + "Targets": ConsistOf(TestIPAddressOne), + "RecordType": Equal("A"), + "SetIdentifier": Equal(""), + "RecordTTL": Equal(externaldns.TTL(60)), + })), + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal("klb.example.com"), + "Targets": ConsistOf("ie.klb.example.com"), + "RecordType": Equal("CNAME"), + "SetIdentifier": Equal("IE"), + "RecordTTL": Equal(externaldns.TTL(300)), + "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "geo-code", Value: "IE"}}), + })), + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal("klb.example.com"), + "Targets": ConsistOf("es.klb.example.com"), + "RecordType": Equal("CNAME"), + "SetIdentifier": Equal("ES"), + "RecordTTL": Equal(externaldns.TTL(300)), + "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "geo-code", Value: "ES"}}), + })), + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal("klb.example.com"), + "Targets": ConsistOf("ie.klb.example.com"), + "RecordType": Equal("CNAME"), + "SetIdentifier": Equal("default"), + "RecordTTL": Equal(externaldns.TTL(300)), + "ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "geo-code", Value: "*"}}), + })), + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal(TestHostWildcard), + "Targets": ConsistOf("klb.example.com"), + "RecordType": Equal("CNAME"), + "SetIdentifier": Equal(""), + "RecordTTL": Equal(externaldns.TTL(300)), + })), + ), + }), + })) + g.Expect(testEndpointsTraversable(wildcardDnsRecord.Spec.Endpoints, TestHostWildcard, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue()) }, TestTimeoutMedium, TestRetryIntervalMedium, ctx).Should(Succeed()) }) @@ -657,6 +678,7 @@ var _ = Describe("DNSPolicy Multi Cluster", func() { }), }), ) + g.Expect(testEndpointsTraversable(dnsRecord.Spec.Endpoints, TestHostOne, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue()) g.Expect(*wildcardDnsRecord).To( MatchFields(IgnoreExtras, Fields{ @@ -722,6 +744,7 @@ var _ = Describe("DNSPolicy Multi Cluster", func() { }), }), ) + g.Expect(testEndpointsTraversable(wildcardDnsRecord.Spec.Endpoints, TestHostWildcard, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue()) }, TestTimeoutMedium, TestRetryIntervalMedium, ctx).Should(Succeed()) }) }) diff --git a/controllers/dnspolicy_controller_single_cluster_test.go b/controllers/dnspolicy_controller_single_cluster_test.go index 7e808a2ae..158d7e81e 100644 --- a/controllers/dnspolicy_controller_single_cluster_test.go +++ b/controllers/dnspolicy_controller_single_cluster_test.go @@ -77,8 +77,11 @@ var _ = Describe("DNSPolicy Single Cluster", func() { gwHash = common.ToBase36HashLen(gateway.Name+"-"+gateway.Namespace, 6) + // refresh gateway + Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(gateway), gateway)).To(Succeed()) //Set single cluster gateway status - Eventually(func() error { + Eventually(func(g Gomega) { + g.Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(gateway), gateway)).To(Succeed()) gateway.Status.Addresses = []gatewayapiv1.GatewayStatusAddress{ { Type: ptr.To(gatewayapiv1.IPAddressType), @@ -109,7 +112,7 @@ var _ = Describe("DNSPolicy Single Cluster", func() { Conditions: []metav1.Condition{}, }, } - return k8sClient.Status().Update(ctx, gateway) + g.Expect(k8sClient.Status().Update(ctx, gateway)).To(Succeed()) }, TestTimeoutMedium, TestRetryIntervalMedium).Should(Succeed()) recordName = fmt.Sprintf("%s-%s", TestGatewayName, TestListenerNameOne) @@ -171,41 +174,53 @@ var _ = Describe("DNSPolicy Single Cluster", func() { err := k8sClient.List(ctx, recordList, &client.ListOptions{Namespace: testNamespace}) g.Expect(err).NotTo(HaveOccurred()) g.Expect(recordList.Items).To(HaveLen(2)) - g.Expect(recordList.Items).To( - ContainElements( - MatchFields(IgnoreExtras, Fields{ - "ObjectMeta": HaveField("Name", recordName), - "Spec": MatchFields(IgnoreExtras, Fields{ - "OwnerID": Equal(&ownerID), - "ManagedZoneRef": HaveField("Name", "mz-example-com"), - "Endpoints": ConsistOf( - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal(TestHostOne), - "Targets": ContainElements(TestIPAddressOne, TestIPAddressTwo), - "RecordType": Equal("A"), - "SetIdentifier": Equal(""), - "RecordTTL": Equal(externaldns.TTL(60)), - })), - ), - }), + + dnsRecord := &kuadrantdnsv1alpha1.DNSRecord{} + err = k8sClient.Get(ctx, client.ObjectKey{Name: recordName, Namespace: testNamespace}, dnsRecord) + g.Expect(err).NotTo(HaveOccurred()) + + wildcardDnsRecord := &kuadrantdnsv1alpha1.DNSRecord{} + err = k8sClient.Get(ctx, client.ObjectKey{Name: wildcardRecordName, Namespace: testNamespace}, wildcardDnsRecord) + g.Expect(err).NotTo(HaveOccurred()) + + g.Expect(*dnsRecord).To( + MatchFields(IgnoreExtras, Fields{ + "ObjectMeta": HaveField("Name", recordName), + "Spec": MatchFields(IgnoreExtras, Fields{ + "OwnerID": Equal(&ownerID), + "ManagedZoneRef": HaveField("Name", "mz-example-com"), + "Endpoints": ConsistOf( + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal(TestHostOne), + "Targets": ContainElements(TestIPAddressOne, TestIPAddressTwo), + "RecordType": Equal("A"), + "SetIdentifier": Equal(""), + "RecordTTL": Equal(externaldns.TTL(60)), + })), + ), }), - MatchFields(IgnoreExtras, Fields{ - "ObjectMeta": HaveField("Name", wildcardRecordName), - "Spec": MatchFields(IgnoreExtras, Fields{ - "OwnerID": Equal(&ownerID), - "ManagedZoneRef": HaveField("Name", "mz-example-com"), - "Endpoints": ConsistOf( - PointTo(MatchFields(IgnoreExtras, Fields{ - "DNSName": Equal(TestHostWildcard), - "Targets": ContainElements(TestIPAddressOne, TestIPAddressTwo), - "RecordType": Equal("A"), - "SetIdentifier": Equal(""), - "RecordTTL": Equal(externaldns.TTL(60)), - })), - ), - }), + }), + ) + g.Expect(testEndpointsTraversable(dnsRecord.Spec.Endpoints, TestHostOne, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue()) + g.Expect(*wildcardDnsRecord).To( + MatchFields(IgnoreExtras, Fields{ + "ObjectMeta": HaveField("Name", wildcardRecordName), + "Spec": MatchFields(IgnoreExtras, Fields{ + "OwnerID": Equal(&ownerID), + "ManagedZoneRef": HaveField("Name", "mz-example-com"), + "Endpoints": ConsistOf( + PointTo(MatchFields(IgnoreExtras, Fields{ + "DNSName": Equal(TestHostWildcard), + "Targets": ContainElements(TestIPAddressOne, TestIPAddressTwo), + "RecordType": Equal("A"), + "SetIdentifier": Equal(""), + "RecordTTL": Equal(externaldns.TTL(60)), + })), + ), }), - )) + }), + ) + g.Expect(testEndpointsTraversable(wildcardDnsRecord.Spec.Endpoints, TestHostWildcard, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue()) }, TestTimeoutMedium, TestRetryIntervalMedium, ctx).Should(Succeed()) }) @@ -285,6 +300,7 @@ var _ = Describe("DNSPolicy Single Cluster", func() { }), }), ) + g.Expect(testEndpointsTraversable(dnsRecord.Spec.Endpoints, TestHostOne, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue()) g.Expect(*wildcardDnsRecord).To( MatchFields(IgnoreExtras, Fields{ "ObjectMeta": HaveField("Name", wildcardRecordName), @@ -334,6 +350,7 @@ var _ = Describe("DNSPolicy Single Cluster", func() { }), }), ) + g.Expect(testEndpointsTraversable(wildcardDnsRecord.Spec.Endpoints, TestHostWildcard, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue()) }, TestTimeoutMedium, TestRetryIntervalMedium, ctx).Should(Succeed()) }) diff --git a/controllers/helper_test.go b/controllers/helper_test.go index 9b536720b..e1941210f 100644 --- a/controllers/helper_test.go +++ b/controllers/helper_test.go @@ -4,6 +4,7 @@ package controllers import ( "context" + "slices" "strings" "time" @@ -16,6 +17,7 @@ import ( "k8s.io/utils/ptr" "sigs.k8s.io/controller-runtime/pkg/client" logf "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/external-dns/endpoint" gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1" gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" @@ -164,6 +166,37 @@ func testBuildInMemoryCredentialsSecret(name, ns string) *corev1.Secret { } } +// testEndpointsTraversable consumes an array of endpoints and returns a boolean +// indicating presence of that path from host to all destinations +// this function DOES NOT report a presence of an endpoint with one of destinations DNSNames +func testEndpointsTraversable(endpoints []*endpoint.Endpoint, host string, destinations []string) bool { + allDestinationsFound := len(destinations) > 0 + for _, destination := range destinations { + allTargetsFound := false + for _, ep := range endpoints { + // the host exists as a DNSName on an endpoint + if ep.DNSName == host { + // we found destination in the targets of the endpoint. + if slices.Contains(ep.Targets, destination) { + return true + } + // destination is not found on the endpoint. Use target as a host and check for existence of Endpoints with such a DNSName + for _, target := range ep.Targets { + // if at least one returns as true allTargetsFound will be locked in true + // this means that at least one of the targets on the endpoint leads to the destination + allTargetsFound = allTargetsFound || testEndpointsTraversable(endpoints, target, []string{destination}) + } + + } + } + // we must match all destinations + allDestinationsFound = allDestinationsFound && allTargetsFound + } + // there are no destinations to look for: len(destinations) == 0 locks allDestinationsFound into false + // or every destination was matched to a target on the endpoint + return allDestinationsFound +} + //Gateway func testBuildGatewayClass(name, ns, controllerName string) *gatewayapiv1.GatewayClass {