Skip to content

Commit

Permalink
GH-545 restrict default geo to local
Browse files Browse the repository at this point in the history
  • Loading branch information
maksymvavilov committed Apr 25, 2024
1 parent 9654134 commit 2f217c4
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 169 deletions.
25 changes: 9 additions & 16 deletions controllers/dns_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,28 +244,21 @@ func (dh *dnsHelper) getLoadBalancedEndpoints(mcgTarget *multicluster.GatewayTar
}
endpoints = append(endpoints, clusterEndpoints...)

//Create lbName CNAME (lb-a1b2.shop.example.com -> default.lb-a1b2.shop.example.com)
endpoint = createOrUpdateEndpoint(lbName, []string{geoLbName}, kuadrantdnsv1alpha1.CNAMERecordType, string(geoCode), DefaultCnameTTL, currentEndpoints)

//Deal with the default geo endpoint first
if geoCode.IsDefaultCode() {
defaultEndpoint = endpoint
// continue here as we will add the `defaultEndpoint` later
continue
} else if (geoCode == mcgTarget.GetDefaultGeo()) || defaultEndpoint == nil {
// Ensure that a `defaultEndpoint` is always set, but the expected default takes precedence
//Deal with the default geo endpoint
if geoCode == mcgTarget.GetDefaultGeo() {
// Ensure that `defaultEndpoint` is set only if geo of the current cluster is desired default geo
defaultEndpoint = createOrUpdateEndpoint(lbName, []string{geoLbName}, kuadrantdnsv1alpha1.CNAMERecordType, "default", DefaultCnameTTL, currentEndpoints)
defaultEndpoint.SetProviderSpecificProperty(kuadrantdnsv1alpha1.ProviderSpecificGeoCode, string(v1alpha1.WildcardGeo))
endpoints = append(endpoints, defaultEndpoint)
}
//Create lbName CNAME (lb-a1b2.shop.example.com -> default.lb-a1b2.shop.example.com)
ep := createOrUpdateEndpoint(lbName, []string{geoLbName}, kuadrantdnsv1alpha1.CNAMERecordType, string(geoCode), DefaultCnameTTL, currentEndpoints)
ep.SetProviderSpecificProperty(kuadrantdnsv1alpha1.ProviderSpecificGeoCode, string(geoCode))
endpoints = append(endpoints, ep)

endpoint.SetProviderSpecificProperty(kuadrantdnsv1alpha1.ProviderSpecificGeoCode, string(geoCode))

endpoints = append(endpoints, endpoint)
}

if len(endpoints) > 0 {
// Add the `defaultEndpoint`, this should always be set by this point if `endpoints` isn't empty
defaultEndpoint.SetProviderSpecificProperty(kuadrantdnsv1alpha1.ProviderSpecificGeoCode, string(v1alpha1.WildcardGeo))
endpoints = append(endpoints, defaultEndpoint)
//Create gwListenerHost CNAME (shop.example.com -> lb-a1b2.shop.example.com)
endpoint = createOrUpdateEndpoint(hostname, []string{lbName}, kuadrantdnsv1alpha1.CNAMERecordType, "", DefaultCnameTTL, currentEndpoints)
endpoints = append(endpoints, endpoint)
Expand Down
320 changes: 172 additions & 148 deletions controllers/dnspolicy_controller_multi_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,156 +183,13 @@ var _ = Describe("DNSPolicy Multi Cluster", func() {

Context("loadbalanced routing strategy", func() {

Context("weighted", func() {
Context("geo+weighted with default weights", func() {

BeforeEach(func() {
dnsPolicy = v1alpha1.NewDNSPolicy("test-dns-policy", testNamespace).
WithTargetGateway(TestGatewayName).
WithRoutingStrategy(v1alpha1.LoadBalancedRoutingStrategy).
WithLoadBalancingWeightedFor(120, nil)
Expect(k8sClient.Create(ctx, dnsPolicy)).To(Succeed())
})

It("should create dns records", func() {
Eventually(func(g Gomega, ctx context.Context) {
recordList := &kuadrantdnsv1alpha1.DNSRecordList{}
err := k8sClient.List(ctx, recordList, &client.ListOptions{Namespace: testNamespace})
g.Expect(err).NotTo(HaveOccurred())
g.Expect(recordList.Items).To(HaveLen(2))

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("default.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: "120"}}),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal("default.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: "120"}}),
})),
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("default.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(*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("default.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: "120"}}),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal("default.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: "120"}}),
})),
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("default.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)),
})),
),
}),
}),
)
}, TestTimeoutMedium, TestRetryIntervalMedium, ctx).Should(Succeed())
})

})

Context("geo+weighted", func() {

BeforeEach(func() {
dnsPolicy = v1alpha1.NewDNSPolicy("test-dns-policy", testNamespace).
WithTargetGateway(TestGatewayName).
WithRoutingStrategy(v1alpha1.LoadBalancedRoutingStrategy).
WithLoadBalancingGeoFor("IE").
WithLoadBalancingWeightedFor(120, nil)
WithLoadBalancingFor(120, nil, "IE")
Expect(k8sClient.Create(ctx, dnsPolicy)).To(Succeed())
})

Expand Down Expand Up @@ -491,7 +348,7 @@ var _ = Describe("DNSPolicy Multi Cluster", func() {
dnsPolicy = v1alpha1.NewDNSPolicy("test-dns-policy", testNamespace).
WithTargetGateway(TestGatewayName).
WithRoutingStrategy(v1alpha1.LoadBalancedRoutingStrategy).
WithLoadBalancingWeightedFor(120, []*v1alpha1.CustomWeight{
WithLoadBalancingFor(120, []*v1alpha1.CustomWeight{
{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
Expand All @@ -508,8 +365,7 @@ var _ = Describe("DNSPolicy Multi Cluster", func() {
},
Weight: 160,
},
}).
WithLoadBalancingGeoFor("IE")
}, "IE")
Expect(k8sClient.Create(ctx, dnsPolicy)).To(Succeed())

Eventually(func() error {
Expand Down Expand Up @@ -681,6 +537,174 @@ var _ = Describe("DNSPolicy Multi Cluster", func() {

})

Context("geo+weighted with foreign geo", func() {

BeforeEach(func() {
dnsPolicy = v1alpha1.NewDNSPolicy("test-dns-policy", testNamespace).
WithTargetGateway(TestGatewayName).
WithRoutingStrategy(v1alpha1.LoadBalancedRoutingStrategy).
WithLoadBalancingFor(120, nil, "cat")
Expect(k8sClient.Create(ctx, dnsPolicy)).To(Succeed())

Eventually(func() error {
gateway.Labels = map[string]string{}
gateway.Labels["clusters.kuadrant.io/"+TestClusterNameOne+"_lb-attribute-geo-code"] = "IE"
gateway.Labels["clusters.kuadrant.io/"+TestClusterNameTwo+"_lb-attribute-geo-code"] = "ES"
return k8sClient.Update(ctx, gateway)
}, TestTimeoutMedium, TestRetryIntervalMedium).ShouldNot(HaveOccurred())

Expect(gateway.Labels).To(HaveKeyWithValue("clusters.kuadrant.io/test-placed-control_lb-attribute-geo-code", "IE"))
Expect(gateway.Labels).To(HaveKeyWithValue("clusters.kuadrant.io/test-placed-workload-1_lb-attribute-geo-code", "ES"))
})

It("should create dns records", func() {
Eventually(func(g Gomega, ctx context.Context) {
recordList := &kuadrantdnsv1alpha1.DNSRecordList{}
err := k8sClient.List(ctx, recordList, &client.ListOptions{Namespace: testNamespace})
g.Expect(err).NotTo(HaveOccurred())
g.Expect(recordList.Items).To(HaveLen(2))

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(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("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: "120"}}),
})),
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: "120"}}),
})),
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(TestHostOne),
"Targets": ConsistOf("klb.test.example.com"),
"RecordType": Equal("CNAME"),
"SetIdentifier": Equal(""),
"RecordTTL": Equal(externaldns.TTL(300)),
})),
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"}}),
})),
),
}),
}),
)

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(clusterOneIDHash + "-" + gwHash + ".klb.example.com"),
"Targets": ConsistOf(TestIPAddressOne),
"RecordType": Equal("A"),
"SetIdentifier": Equal(""),
"RecordTTL": Equal(externaldns.TTL(60)),
})),
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: "120"}}),
})),
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: "120"}}),
})),
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(TestHostWildcard),
"Targets": ConsistOf("klb.example.com"),
"RecordType": Equal("CNAME"),
"SetIdentifier": Equal(""),
"RecordTTL": Equal(externaldns.TTL(300)),
})),
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("IE"),
"RecordTTL": Equal(externaldns.TTL(300)),
"ProviderSpecific": Equal(externaldns.ProviderSpecific{{Name: "geo-code", Value: "IE"}}),
})),
),
}),
}),
)
}, TestTimeoutMedium, TestRetryIntervalMedium, ctx).Should(Succeed())
})
})

})

})
Loading

0 comments on commit 2f217c4

Please sign in to comment.