Skip to content

Commit

Permalink
add code to handle GEO- prefix for conintents for AWS
Browse files Browse the repository at this point in the history
Signed-off-by: craig <[email protected]>

rh-pre-commit.version: 2.2.0
rh-pre-commit.check-secrets: ENABLED

change geo codes in test
  • Loading branch information
maleck13 committed Oct 22, 2024
1 parent 227bdd2 commit 0acb862
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 8 deletions.
18 changes: 15 additions & 3 deletions internal/provider/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const (
awsEvaluateTargetHealth = false
awsPreferCNAME = true
awsZoneCacheDuration = 0 * time.Second
providerContinentPrefix = "GEO-"
)

type Route53DNSProvider struct {
Expand Down Expand Up @@ -122,20 +123,31 @@ func (p *Route53DNSProvider) AdjustEndpoints(endpoints []*externaldnsendpoint.En
if err != nil {
return nil, err
}

p.logger.V(1).Info("adjusting aws endpoints")
for _, ep := range endpoints {
if prop, ok := ep.GetProviderSpecificProperty(v1alpha1.ProviderSpecificWeight); ok {
ep.DeleteProviderSpecificProperty(v1alpha1.ProviderSpecificWeight)
ep.WithProviderSpecific(providerSpecificWeight, prop)
p.logger.V(1).Info("set provider specific weight", "endpoint", ep)
}

if prop, ok := ep.GetProviderSpecificProperty(v1alpha1.ProviderSpecificGeoCode); ok {
ep.DeleteProviderSpecificProperty(v1alpha1.ProviderSpecificGeoCode)
prop = strings.ToUpper(prop)
if strings.HasPrefix(prop, providerContinentPrefix) {
prop = strings.Replace(prop, providerContinentPrefix, "", -1)
ep.WithProviderSpecific(providerSpecificGeolocationContinentCode, prop)
p.logger.V(1).Info("set provider specific continent code base GEO- prefix", "endpoint", ep)
continue
}

if provider.IsISO3166Alpha2Code(prop) || prop == "*" {
ep.WithProviderSpecific(providerSpecificGeolocationCountryCode, prop)
} else {
ep.WithProviderSpecific(providerSpecificGeolocationContinentCode, prop)
p.logger.V(1).Info("set provider specific country code", "endpoint", ep)
continue
}
//if we get to here there is a value we cannot use
return nil, fmt.Errorf("unexpected geo code. Prefix with %s for continents or use ISO_3166 Alpha 2 supported code for countries", providerContinentPrefix)
}
}
return endpoints, nil
Expand Down
104 changes: 104 additions & 0 deletions internal/provider/aws/aws_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package aws

import (
"testing"

"sigs.k8s.io/external-dns/endpoint"
externaldnsendpoint "sigs.k8s.io/external-dns/endpoint"

"github.com/kuadrant/dns-operator/api/v1alpha1"
)

const recordTTL = 300

func TestAWSAdjustEndpoints(t *testing.T) {
testCases := []struct {
Name string
Endpoints []*externaldnsendpoint.Endpoint
Validate func(t *testing.T, eps []*externaldnsendpoint.Endpoint, err error)
}{
{
Name: "test geo prefix continent code endpoint success",
Endpoints: []*externaldnsendpoint.Endpoint{
endpoint.NewEndpointWithTTL("geolocation-test.zone-1.ext-dns-test-2.teapot.zalan.do", endpoint.RecordTypeA, endpoint.TTL(recordTTL), "1.2.3.4").WithSetIdentifier("test-set-1").WithProviderSpecific(v1alpha1.ProviderSpecificGeoCode, "GEO-EU"),
},
Validate: func(t *testing.T, eps []*externaldnsendpoint.Endpoint, err error) {
if len(eps) != 1 {
t.Fatalf("expected 1 endpoint but got %v", len(eps))
val, ok := eps[0].GetProviderSpecificProperty(providerSpecificGeolocationContinentCode)
if !ok {
t.Fatalf("exected a provider specific contintinent code to be set but got none")
}
if val != "EU" {
t.Fatalf("continent code set but expected the EU got %s ", val)
}
}
if err != nil {
t.Fatalf("did not expect an error but got %s", err)
}
},
},
{
Name: "test none geo prefixed continent value and none ISO_3166 to return error",
Endpoints: []*externaldnsendpoint.Endpoint{
endpoint.NewEndpointWithTTL("geolocation-test.zone-1.ext-dns-test-2.teapot.zalan.do", endpoint.RecordTypeA, endpoint.TTL(recordTTL), "1.2.3.4").WithSetIdentifier("test-set-1").WithProviderSpecific(v1alpha1.ProviderSpecificGeoCode, "EU"),
},
Validate: func(t *testing.T, eps []*externaldnsendpoint.Endpoint, err error) {
if len(eps) != 0 {
t.Fatalf("expected no endpoints but got %v", len(eps))
}
if err == nil {
t.Fatalf("expected an error but got none")
}
},
},
{
Name: "test valid ISO_3166 success for country code",
Validate: func(t *testing.T, eps []*externaldnsendpoint.Endpoint, err error) {
if len(eps) != 1 {
t.Fatalf("expected 1 endpoint but got %v", len(eps))
}
val, ok := eps[0].GetProviderSpecificProperty(providerSpecificGeolocationCountryCode)
if !ok {
t.Fatalf("exected a provider specific country code to be set but got none")
}
if val != "IE" {
t.Fatalf("continent code set but expected the IE got %s ", val)
}
},
Endpoints: []*externaldnsendpoint.Endpoint{
endpoint.NewEndpointWithTTL("geolocation-test.zone-1.ext-dns-test-2.teapot.zalan.do", endpoint.RecordTypeA, endpoint.TTL(recordTTL), "1.2.3.4").WithSetIdentifier("test-set-1").WithProviderSpecific(v1alpha1.ProviderSpecificGeoCode, "IE"),
},
},
{
Name: "test geo prefix lower case continent code endpoint success",
Endpoints: []*externaldnsendpoint.Endpoint{
endpoint.NewEndpointWithTTL("geolocation-test.zone-1.ext-dns-test-2.teapot.zalan.do", endpoint.RecordTypeA, endpoint.TTL(recordTTL), "1.2.3.4").WithSetIdentifier("test-set-1").WithProviderSpecific(v1alpha1.ProviderSpecificGeoCode, "geo-eu"),
},
Validate: func(t *testing.T, eps []*externaldnsendpoint.Endpoint, err error) {
if len(eps) != 1 {
t.Fatalf("expected 1 endpoint but got %v", len(eps))
val, ok := eps[0].GetProviderSpecificProperty(providerSpecificGeolocationContinentCode)
if !ok {
t.Fatalf("exected a provider specific contintinent code to be set but got none")
}
if val != "EU" {
t.Fatalf("continent code set but expected the EU got %s ", val)
}
}
if err != nil {
t.Fatalf("did not expect an error but got %s", err)
}
},
},
}

for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
r53Prov := &Route53DNSProvider{}
adjusted, err := r53Prov.AdjustEndpoints(testCase.Endpoints)
testCase.Validate(t, adjusted, err)

})
}
}
2 changes: 1 addition & 1 deletion test/e2e/multi_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var _ = Describe("Multi Record Test", Labels{"multi_record"}, func() {
weighted = "Weighted"
} else {
geoCode1 = "US"
geoCode2 = "EU"
geoCode2 = "GEO-EU"
weighted = "weighted"
}
})
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/provider_errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ var _ = Describe("DNSRecord Provider Errors", Labels{"provider_errors"}, func()
validGeoCode = "GEO-NA"
} else {
//AWS
expectedProviderErr = "Value 'notageocode' with length = '11' is not facet-valid with respect to length '2' for type 'ContinentCode'"
validGeoCode = "US"
expectedProviderErr = "unexpected geo code. Prefix with GEO- for continents or use ISO_3166 Alpha 2 supported code for countries"
validGeoCode = "GEO-US"
}

invalidEndpoint := &externaldnsendpoint.Endpoint{
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/single_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ var _ = Describe("Single Record Test", Labels{"single_record"}, func() {
} else if testDNSProvider == "azure" {
SetTestEnv("testGeoCode", "GEO-EU")
} else {
SetTestEnv("testGeoCode", "EU")
SetTestEnv("testGeoCode", "GEO-EU")
}

SetTestEnv("TEST_DNS_PROVIDER_SECRET_NAME", testDNSProviderSecret.Name)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ var _ = BeforeSuite(func(ctx SpecContext) {

testSuiteID = "dns-op-e2e-" + GenerateName()

geoCode := "EU"
geoCode := "GEO-EU"
if testDNSProvider == "google" {
geoCode = "europe-west1"
}
Expand Down

0 comments on commit 0acb862

Please sign in to comment.