Skip to content

Commit

Permalink
Merge pull request #3912 from Sewci0/fix/txt-registry-case
Browse files Browse the repository at this point in the history
[TXT Registry] Fix `Records()` case handling
  • Loading branch information
k8s-ci-robot committed Sep 15, 2023
2 parents bb705a1 + 6970b8d commit 7519a77
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
6 changes: 3 additions & 3 deletions registry/txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,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

0 comments on commit 7519a77

Please sign in to comment.