From 16b8192783e6921e25cba139ba982160eff55ccf Mon Sep 17 00:00:00 2001 From: Shchukin Konstantin Date: Tue, 24 Jan 2023 12:10:28 +0700 Subject: [PATCH] create an alias when creord in a zone other than the target --- provider/pdns/pdns.go | 15 +++++++++++---- provider/pdns/pdns_test.go | 19 +++++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/provider/pdns/pdns.go b/provider/pdns/pdns.go index 6f9ca0266..3bfb72490 100644 --- a/provider/pdns/pdns.go +++ b/provider/pdns/pdns.go @@ -258,6 +258,7 @@ func NewPDNSProvider(ctx context.Context, config PDNSConfig) (*PDNSProvider, err func (p *PDNSProvider) convertRRSetToEndpoints(rr pgo.RrSet) (endpoints []*endpoint.Endpoint, _ error) { endpoints = []*endpoint.Endpoint{} targets := []string{} + rrType_ := rr.Type_ for _, record := range rr.Records { // If a record is "Disabled", it's not supposed to be "visible" @@ -265,8 +266,10 @@ func (p *PDNSProvider) convertRRSetToEndpoints(rr pgo.RrSet) (endpoints []*endpo targets = append(targets, record.Content) } } - - endpoints = append(endpoints, endpoint.NewEndpointWithTTL(rr.Name, rr.Type_, endpoint.TTL(rr.Ttl), targets...)) + if rr.Type_ == "ALIAS" { + rrType_ = "CNAME" + } + endpoints = append(endpoints, endpoint.NewEndpointWithTTL(rr.Name, rrType_, endpoint.TTL(rr.Ttl), targets...)) return endpoints, nil } @@ -311,16 +314,20 @@ func (p *PDNSProvider) ConvertEndpointsToZones(eps []*endpoint.Endpoint, changet // per (ep.DNSName, ep.RecordType) tuple, which holds true for // external-dns v5.0.0-alpha onwards records := []pgo.Record{} + RecordType_ := ep.RecordType for _, t := range ep.Targets { - if ep.RecordType == "CNAME" { + if ep.RecordType == "CNAME" || ep.RecordType == "ALIAS" { t = provider.EnsureTrailingDot(t) + if t != zone.Name && !strings.HasSuffix(t, "."+zone.Name) { + RecordType_ = "ALIAS" + } } records = append(records, pgo.Record{Content: t}) } rrset := pgo.RrSet{ Name: dnsname, - Type_: ep.RecordType, + Type_: RecordType_, Records: records, Changetype: string(changetype), } diff --git a/provider/pdns/pdns_test.go b/provider/pdns/pdns_test.go index 5e7eeb365..b7108e1ed 100644 --- a/provider/pdns/pdns_test.go +++ b/provider/pdns/pdns_test.go @@ -82,9 +82,19 @@ var ( Type_: "CNAME", Ttl: 300, Records: []pgo.Record{ - {Content: "example.by.any.other.name.com", Disabled: false, SetPtr: false}, + {Content: "example.com.", Disabled: false, SetPtr: false}, }, } + + RRSetALIASRecord = pgo.RrSet{ + Name: "alias.example.com.", + Type_: "ALIAS", + Ttl: 300, + Records: []pgo.Record{ + {Content: "example.by.any.other.name.com.", Disabled: false, SetPtr: false}, + }, + } + RRSetTXTRecord = pgo.RrSet{ Name: "example.com.", Type_: "TXT", @@ -129,9 +139,10 @@ var ( } endpointsMixedRecords = []*endpoint.Endpoint{ - endpoint.NewEndpointWithTTL("cname.example.com", endpoint.RecordTypeCNAME, endpoint.TTL(300), "example.by.any.other.name.com"), + endpoint.NewEndpointWithTTL("cname.example.com", endpoint.RecordTypeCNAME, endpoint.TTL(300), "example.com"), endpoint.NewEndpointWithTTL("example.com", endpoint.RecordTypeTXT, endpoint.TTL(300), "'would smell as sweet'"), endpoint.NewEndpointWithTTL("example.com", endpoint.RecordTypeA, endpoint.TTL(300), "8.8.8.8", "8.8.4.4", "4.4.4.4"), + endpoint.NewEndpointWithTTL("alias.example.com", endpoint.RecordTypeCNAME, endpoint.TTL(300), "example.by.any.other.name.com"), } endpointsMultipleZones = []*endpoint.Endpoint{ @@ -215,7 +226,7 @@ var ( Type_: "Zone", Url: "/api/v1/servers/localhost/zones/example.com.", Kind: "Native", - Rrsets: []pgo.RrSet{RRSetCNAMERecord, RRSetTXTRecord, RRSetMultipleRecords}, + Rrsets: []pgo.RrSet{RRSetCNAMERecord, RRSetTXTRecord, RRSetMultipleRecords, RRSetALIASRecord}, } ZoneEmptyToSimplePatch = pgo.Zone{ @@ -909,7 +920,7 @@ func (suite *NewPDNSProviderTestSuite) TestPDNSConvertEndpointsToZones() { for _, z := range zlist { for _, rs := range z.Rrsets { - if "CNAME" == rs.Type_ { + if rs.Type_ == "CNAME" { for _, r := range rs.Records { assert.Equal(suite.T(), uint8(0x2e), r.Content[len(r.Content)-1]) }