Skip to content

Commit

Permalink
Merge pull request #276 from Kuadrant/gh-251
Browse files Browse the repository at this point in the history
Handle GEO- prefix for contintents for AWS
  • Loading branch information
maleck13 authored Oct 24, 2024
2 parents 341c786 + 61e3711 commit 49adc7a
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 7 deletions.
21 changes: 18 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,34 @@ 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) {
continent := strings.Replace(prop, providerContinentPrefix, "", -1)
if !provider.IsContinentCode(continent) {
return nil, fmt.Errorf("unexpected continent code. %s", continent)
}
ep.WithProviderSpecific(providerSpecificGeolocationContinentCode, continent)
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
111 changes: 111 additions & 0 deletions internal/provider/aws/aws_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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 err != nil {
t.Fatalf("did not expect an error but got %s", err)
}
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)
}
}

},
},
{
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 err == nil {
t.Fatalf("expected an error but got none")
}
if len(eps) != 0 {
t.Fatalf("expected no endpoints but got %v", len(eps))
}

},
},
{
Name: "test valid ISO_3166 success for country code",
Validate: func(t *testing.T, eps []*externaldnsendpoint.Endpoint, err error) {
if err != nil {
t.Fatalf("did not expect an error but got %s", err)
}
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 err != nil {
t.Fatalf("did not expect an error but got %s", err)
}

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)
}
}

},
},
}

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)

})
}
}
24 changes: 24 additions & 0 deletions internal/provider/iso3166.go → internal/provider/codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,27 @@ func GetISO3166Alpha2Codes() []string {
func IsISO3166Alpha2Code(code string) bool {
return slice.ContainsString(GetISO3166Alpha2Codes(), code)
}

const (
AWS_CONTINENT_CODE_AFRICA = "AF"
AWS_CONTINENT_CODE_ANTARTICA = "AN"
AWS_CONTINENT_CODE_ASIA = "AS"
AWS_CONTINENT_CODE_EUROPE = "EU"
AWS_CONTINENT_CODE_OCEANIA = "OC"
AWS_CONTINENT_CODE_NORTH_AMERICA = "NA"
AWS_CONTINENT_CODE_SOUTH_AMERICA = "SA"
)

var AWSContinentCodes = []string{
AWS_CONTINENT_CODE_AFRICA,
AWS_CONTINENT_CODE_ANTARTICA,
AWS_CONTINENT_CODE_ASIA,
AWS_CONTINENT_CODE_EUROPE,
AWS_CONTINENT_CODE_OCEANIA,
AWS_CONTINENT_CODE_NORTH_AMERICA,
AWS_CONTINENT_CODE_SOUTH_AMERICA,
}

func IsContinentCode(code string) bool {
return slice.ContainsString(AWSContinentCodes, code)
}
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
2 changes: 1 addition & 1 deletion test/e2e/provider_errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ 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'"
expectedProviderErr = "unexpected geo code. Prefix with GEO- for continents or use ISO_3166 Alpha 2 supported code for countries"
validGeoCode = "US"
}

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 49adc7a

Please sign in to comment.