Skip to content

Commit 0d4cf99

Browse files
committed
Address review comments
1 parent 4345ce6 commit 0d4cf99

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

internal/testutils/endpoint.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ import (
2525

2626
/** test utility functions for endpoints verifications */
2727

28+
type byNames endpoint.ProviderSpecific
29+
30+
func (p byNames) Len() int { return len(p) }
31+
func (p byNames) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
32+
func (p byNames) Less(i, j int) bool { return p[i].Name < p[j].Name }
33+
2834
type byAllFields []*endpoint.Endpoint
2935

3036
func (b byAllFields) Len() int { return len(b) }
@@ -102,5 +108,9 @@ func SamePlanChanges(a, b map[string][]*endpoint.Endpoint) bool {
102108

103109
// SameProviderSpecific verifies that two maps contain the same string/string key/value pairs
104110
func SameProviderSpecific(a, b endpoint.ProviderSpecific) bool {
105-
return reflect.DeepEqual(a, b)
111+
sa := a
112+
sb := b
113+
sort.Sort(byNames(sa))
114+
sort.Sort(byNames(sb))
115+
return reflect.DeepEqual(sa, sb)
106116
}

provider/aws/aws.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,6 @@ func (p *AWSProvider) DeleteRecords(ctx context.Context, endpoints []*endpoint.E
402402

403403
func (p *AWSProvider) doRecords(ctx context.Context, action string, endpoints []*endpoint.Endpoint) error {
404404
zones, err := p.Zones(ctx)
405-
p.AdjustEndpoints(endpoints)
406-
407405
if err != nil {
408406
return errors.Wrapf(err, "failed to list zones, aborting %s doRecords action", action)
409407
}
@@ -412,6 +410,9 @@ func (p *AWSProvider) doRecords(ctx context.Context, action string, endpoints []
412410
if err != nil {
413411
log.Errorf("failed to list records while preparing %s doRecords action: %s", action, err)
414412
}
413+
414+
p.AdjustEndpoints(endpoints)
415+
415416
return p.submitChanges(ctx, p.newChanges(action, endpoints, records, zones), zones)
416417
}
417418

@@ -561,11 +562,16 @@ func (p *AWSProvider) newChanges(action string, endpoints []*endpoint.Endpoint,
561562
return changes
562563
}
563564

565+
// AdjustEndpoints modifies the provided endpoints (coming from various sources) to match
566+
// the endpoints that the provider returns in `Records` so that the change plan will not have
567+
// unneeded (potentially failing) changes.
568+
// Example: CNAME endpoints pointing to ELBs will have a `alias` provider-specific property
569+
// added to match the endpoints generated from existing alias records in Route53.
564570
func (p *AWSProvider) AdjustEndpoints(endpoints []*endpoint.Endpoint) []*endpoint.Endpoint {
565571
for _, ep := range endpoints {
566572
alias := false
567-
if _, ok := ep.GetProviderSpecificProperty(providerSpecificAlias); ok {
568-
alias = true
573+
if aliasString, ok := ep.GetProviderSpecificProperty(providerSpecificAlias); ok {
574+
alias = aliasString.Value == "true"
569575
} else if useAlias(ep, p.preferCNAME) {
570576
alias = true
571577
log.Debugf("Modifying endpoint: %v, setting %s=true", ep, providerSpecificAlias)
@@ -843,7 +849,8 @@ func useAlias(ep *endpoint.Endpoint, preferCNAME bool) bool {
843849
return false
844850
}
845851

846-
// isAWSAlias determines if a given hostname is an AWS Alias record
852+
// isAWSAlias determines if a given endpoint is supposed to create an AWS Alias record
853+
// and (if so) returns the target hosted zone ID
847854
func isAWSAlias(ep *endpoint.Endpoint) string {
848855
prop, exists := ep.GetProviderSpecificProperty(providerSpecificAlias)
849856
if exists && prop.Value == "true" && ep.RecordType == endpoint.RecordTypeCNAME && len(ep.Targets) > 0 {

provider/aws/aws_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,30 @@ func TestAWSRecords(t *testing.T) {
354354
})
355355
}
356356

357+
func TestAWSAdjustEndpoints(t *testing.T) {
358+
provider, _ := newAWSProvider(t, endpoint.NewDomainFilter([]string{"ext-dns-test-2.teapot.zalan.do."}), provider.NewZoneIDFilter([]string{}), provider.NewZoneTypeFilter(""), defaultEvaluateTargetHealth, false, []*endpoint.Endpoint{})
359+
360+
records := []*endpoint.Endpoint{
361+
endpoint.NewEndpoint("a-test.zone-1.ext-dns-test-2.teapot.zalan.do", endpoint.RecordTypeA, "8.8.8.8"),
362+
endpoint.NewEndpoint("cname-test.zone-1.ext-dns-test-2.teapot.zalan.do", endpoint.RecordTypeCNAME, "foo.example.com"),
363+
endpoint.NewEndpoint("cname-test-alias.zone-1.ext-dns-test-2.teapot.zalan.do", endpoint.RecordTypeCNAME, "alias-target.zone-2.ext-dns-test-2.teapot.zalan.do").WithProviderSpecific(providerSpecificAlias, "true"),
364+
endpoint.NewEndpoint("cname-test-elb.zone-2.ext-dns-test-2.teapot.zalan.do", endpoint.RecordTypeCNAME, "foo.eu-central-1.elb.amazonaws.com"),
365+
endpoint.NewEndpoint("cname-test-elb-no-alias.zone-2.ext-dns-test-2.teapot.zalan.do", endpoint.RecordTypeCNAME, "foo.eu-central-1.elb.amazonaws.com").WithProviderSpecific(providerSpecificAlias, "false"),
366+
endpoint.NewEndpoint("cname-test-elb-no-eth.ext-dns-test-2.teapot.zalan.do", endpoint.RecordTypeCNAME, "foo.eu-central-1.elb.amazonaws.com").WithProviderSpecific(providerSpecificEvaluateTargetHealth, "false"), // eth = evaluate target health
367+
}
368+
369+
provider.AdjustEndpoints(records)
370+
371+
validateEndpoints(t, records, []*endpoint.Endpoint{
372+
endpoint.NewEndpoint("a-test.zone-1.ext-dns-test-2.teapot.zalan.do", endpoint.RecordTypeA, "8.8.8.8"),
373+
endpoint.NewEndpoint("cname-test.zone-1.ext-dns-test-2.teapot.zalan.do", endpoint.RecordTypeCNAME, "foo.example.com"),
374+
endpoint.NewEndpoint("cname-test-alias.zone-1.ext-dns-test-2.teapot.zalan.do", endpoint.RecordTypeCNAME, "alias-target.zone-2.ext-dns-test-2.teapot.zalan.do").WithProviderSpecific(providerSpecificAlias, "true").WithProviderSpecific(providerSpecificEvaluateTargetHealth, "true"),
375+
endpoint.NewEndpoint("cname-test-elb.zone-2.ext-dns-test-2.teapot.zalan.do", endpoint.RecordTypeCNAME, "foo.eu-central-1.elb.amazonaws.com").WithProviderSpecific(providerSpecificAlias, "true").WithProviderSpecific(providerSpecificEvaluateTargetHealth, "true"),
376+
endpoint.NewEndpoint("cname-test-elb-no-alias.zone-2.ext-dns-test-2.teapot.zalan.do", endpoint.RecordTypeCNAME, "foo.eu-central-1.elb.amazonaws.com").WithProviderSpecific(providerSpecificAlias, "false"),
377+
endpoint.NewEndpoint("cname-test-elb-no-eth.ext-dns-test-2.teapot.zalan.do", endpoint.RecordTypeCNAME, "foo.eu-central-1.elb.amazonaws.com").WithProviderSpecific(providerSpecificAlias, "true").WithProviderSpecific(providerSpecificEvaluateTargetHealth, "false"), // eth = evaluate target health
378+
})
379+
}
380+
357381
func TestAWSCreateRecords(t *testing.T) {
358382
customTTL := endpoint.TTL(60)
359383
provider, _ := newAWSProvider(t, endpoint.NewDomainFilter([]string{"ext-dns-test-2.teapot.zalan.do."}), provider.NewZoneIDFilter([]string{}), provider.NewZoneTypeFilter(""), defaultEvaluateTargetHealth, false, []*endpoint.Endpoint{})

0 commit comments

Comments
 (0)