Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions registry/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package mapper
import (
"strings"

log "github.com/sirupsen/logrus"

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

Expand Down Expand Up @@ -73,40 +75,42 @@ func (a AffixNameMapper) ToEndpointName(dns string) (string, string) {
// drop suffix
if a.isSuffix() {
dc := strings.Count(a.suffix, ".")
DNSName := strings.SplitN(lowerDNSName, ".", 2+dc)
domainWithSuffix := strings.Join(DNSName[:1+dc], ".")

r, rType := a.dropAffixExtractType(domainWithSuffix)
if !strings.Contains(lowerDNSName, ".") {
parts := strings.SplitN(lowerDNSName, ".", 2+dc)
if len(parts) <= dc {
log.Debugf("skipping TXT record %q: too few labels for suffix %q", dns, a.suffix)
return "", ""
}
r, rType := a.dropAffixExtractType(strings.Join(parts[:1+dc], "."))
if len(parts) <= 1+dc {
return r, rType
}
Comment on lines 76 to 86
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be wrong here. The early-exit guard calling dropAffixExtractType(lowerDNSName) when there are too few labels is fine, but it's misleading - the name can't possibly match the suffix at that point, so it always returns ("", ""). Being explicit is cleaner. Also, domainWithSuffix can be inlined:

if a.isSuffix() {
      dc := strings.Count(a.suffix, ".")
      parts := strings.SplitN(lowerDNSName, ".", 2+dc)
      if len(parts) <= dc {
          log.Debugf("skipping TXT record %q: too few labels for suffix %q", dns, a.suffix)
          return "", ""
      }
      r, rType := a.dropAffixExtractType(strings.Join(parts[:1+dc], "."))
      if len(parts) <= 1+dc {
          return r, rType
      }
      return r + "." + parts[1+dc], rType
  }

^ Same algorithm, no slightly misleading fallthrough call, no intermediate variable. The parts name is also more conventional than DNSName in Go.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have addressed this as well in the latest commit

return r + "." + DNSName[1+dc], rType
return r + "." + parts[1+dc], rType
}
return "", ""
}

func (a AffixNameMapper) ToTXTName(dns, recordType string) string {
DNSName := strings.SplitN(dns, ".", 2)
parts := strings.SplitN(dns, ".", 2)
recordType = strings.ToLower(recordType)
recordT := recordType + "-"

prefix := a.normalizeAffixTemplate(a.prefix, recordType)
suffix := a.normalizeAffixTemplate(a.suffix, recordType)

// If specified, replace a leading asterisk in the generated txt record name with some other string
if a.wildcardReplacement != "" && DNSName[0] == "*" {
DNSName[0] = a.wildcardReplacement
if a.wildcardReplacement != "" && parts[0] == "*" {
parts[0] = a.wildcardReplacement
}

if !a.recordTypeInAffix() {
DNSName[0] = recordT + DNSName[0]
parts[0] = recordT + parts[0]
}

if len(DNSName) < 2 {
return prefix + DNSName[0] + suffix
if len(parts) < 2 {
return prefix + parts[0] + suffix
}

return prefix + DNSName[0] + suffix + "." + DNSName[1]
return prefix + parts[0] + suffix + "." + parts[1]
}

func (a AffixNameMapper) recordTypeInAffix() bool {
Expand Down
21 changes: 21 additions & 0 deletions registry/mapper/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,27 @@ func TestAffixNameMapper_ToEndpointName(t *testing.T) {
wantEndpointName: "foo.example.com",
wantRecordType: endpoint.RecordTypeCNAME,
},
{
name: "suffix with multiple dots and trailing labels",
mapper: NewAffixNameMapper("", ".foo.bar", ""),
input: "a-example.foo.bar.com",
wantEndpointName: "example.com",
wantRecordType: endpoint.RecordTypeA,
},
{
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no direct test case for the full path with trailing labels - e.g., a-example.foo.bar.com with suffix .foo.bar should return ("example.com", "A"). The TestToEndpointNameNewTXT round-trip test for "suffix with multiple dots" covers a 3-dot suffix case indirectly, but adding an explicit assertion for trailing labels with a 2-dot suffix would close the gap and document the expected behavior.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, that was an oversight on my part. Added an explicit test for a-example.foo.bar.com with suffix .foo.bar expecting ("example.com", "A").

name: "suffix with multiple dots and no trailing labels",
mapper: NewAffixNameMapper("", ".foo.bar", ""),
input: "a-example.foo.bar",
wantEndpointName: "example",
wantRecordType: endpoint.RecordTypeA,
},
{
name: "suffix with multiple dots and too few labels does not panic",
mapper: NewAffixNameMapper("", ".foo.bar", ""),
input: "a-example.foo",
wantEndpointName: "",
wantRecordType: "",
},
{
name: "no affix with A record",
mapper: NewAffixNameMapper("", "", ""),
Expand Down