Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TXT Registry] Fix Records() case handling #3912

Merged
merged 2 commits into from
Sep 15, 2023
Merged
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
6 changes: 3 additions & 3 deletions registry/txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,9 @@ func (pr affixNameMapper) dropAffixExtractType(name string) (baseName, recordTyp

if pr.recordTypeInAffix() {
for _, t := range getSupportedTypes() {
t = strings.ToLower(t)
iPrefix := strings.ReplaceAll(prefix, recordTemplate, t)
iSuffix := strings.ReplaceAll(suffix, recordTemplate, t)
tLower := strings.ToLower(t)
iPrefix := strings.ReplaceAll(prefix, recordTemplate, tLower)
iSuffix := strings.ReplaceAll(suffix, recordTemplate, tLower)

if pr.isPrefix() && strings.HasPrefix(name, iPrefix) {
return strings.TrimPrefix(name, iPrefix), t
Expand Down
66 changes: 66 additions & 0 deletions registry/txt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ func testTXTRegistryRecords(t *testing.T) {
t.Run("With prefix", testTXTRegistryRecordsPrefixed)
t.Run("With suffix", testTXTRegistryRecordsSuffixed)
t.Run("No prefix", testTXTRegistryRecordsNoPrefix)
t.Run("With templated prefix", testTXTRegistryRecordsPrefixedTemplated)
t.Run("With templated suffix", testTXTRegistryRecordsSuffixedTemplated)
}

func testTXTRegistryRecordsPrefixed(t *testing.T) {
Expand Down Expand Up @@ -445,6 +447,70 @@ func testTXTRegistryRecordsNoPrefix(t *testing.T) {
assert.True(t, testutils.SameEndpoints(records, expectedRecords))
}

func testTXTRegistryRecordsPrefixedTemplated(t *testing.T) {
ctx := context.Background()
p := inmemory.NewInMemoryProvider()
p.CreateZone(testZone)
p.ApplyChanges(ctx, &plan.Changes{
Create: []*endpoint.Endpoint{
newEndpointWithOwner("foo.test-zone.example.org", "1.1.1.1", endpoint.RecordTypeA, ""),
newEndpointWithOwner("txt-a.foo.test-zone.example.org", "\"heritage=external-dns,external-dns/owner=owner\"", endpoint.RecordTypeTXT, ""),
},
})
expectedRecords := []*endpoint.Endpoint{
{
DNSName: "foo.test-zone.example.org",
Targets: endpoint.Targets{"1.1.1.1"},
RecordType: endpoint.RecordTypeA,
Labels: map[string]string{
endpoint.OwnerLabelKey: "owner",
},
},
}

r, _ := NewTXTRegistry(p, "txt-%{record_type}.", "", "owner", time.Hour, "wc", []string{}, false, nil)
records, _ := r.Records(ctx)

assert.True(t, testutils.SameEndpoints(records, expectedRecords))

r, _ = NewTXTRegistry(p, "TxT-%{record_type}.", "", "owner", time.Hour, "wc", []string{}, false, nil)
records, _ = r.Records(ctx)

assert.True(t, testutils.SameEndpoints(records, expectedRecords))
}

func testTXTRegistryRecordsSuffixedTemplated(t *testing.T) {
ctx := context.Background()
p := inmemory.NewInMemoryProvider()
p.CreateZone(testZone)
p.ApplyChanges(ctx, &plan.Changes{
Create: []*endpoint.Endpoint{
newEndpointWithOwner("bar.test-zone.example.org", "8.8.8.8", endpoint.RecordTypeCNAME, ""),
newEndpointWithOwner("bartxtcname.test-zone.example.org", "\"heritage=external-dns,external-dns/owner=owner\"", endpoint.RecordTypeTXT, ""),
},
})
expectedRecords := []*endpoint.Endpoint{
{
DNSName: "bar.test-zone.example.org",
Targets: endpoint.Targets{"8.8.8.8"},
RecordType: endpoint.RecordTypeCNAME,
Labels: map[string]string{
endpoint.OwnerLabelKey: "owner",
},
},
}

r, _ := NewTXTRegistry(p, "", "txt%{record_type}", "owner", time.Hour, "wc", []string{}, false, nil)
records, _ := r.Records(ctx)

assert.True(t, testutils.SameEndpoints(records, expectedRecords))

r, _ = NewTXTRegistry(p, "", "TxT%{record_type}", "owner", time.Hour, "wc", []string{}, false, nil)
records, _ = r.Records(ctx)

assert.True(t, testutils.SameEndpoints(records, expectedRecords))
}

func testTXTRegistryApplyChanges(t *testing.T) {
t.Run("With Prefix", testTXTRegistryApplyChangesWithPrefix)
t.Run("With Templated Prefix", testTXTRegistryApplyChangesWithTemplatedPrefix)
Expand Down
Loading