diff --git a/registry/txt.go b/registry/txt.go index 1ed7cb0b9..06a8314a7 100644 --- a/registry/txt.go +++ b/registry/txt.go @@ -146,6 +146,11 @@ func (im *TXTRegistry) Records(ctx context.Context) ([]*endpoint.Endpoint, error continue } // We simply assume that TXT records for the registry will always have only one target. + // If there are no targets (e.g for routing policy based records in google), direct targets will be empty + if len(record.Targets) == 0 { + log.Errorf("TXT record has no targets %s", record.DNSName) + continue + } labels, err := endpoint.NewLabelsFromString(record.Targets[0], im.txtEncryptAESKey) if errors.Is(err, endpoint.ErrInvalidHeritage) { // if no heritage is found or it is invalid diff --git a/registry/txt_test.go b/registry/txt_test.go index cdd190b12..6cb598b25 100644 --- a/registry/txt_test.go +++ b/registry/txt_test.go @@ -26,6 +26,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + log "github.com/sirupsen/logrus" + "sigs.k8s.io/external-dns/endpoint" "sigs.k8s.io/external-dns/internal/testutils" "sigs.k8s.io/external-dns/plan" @@ -1798,3 +1800,40 @@ func TestApplyChangesWithNewFormatOnly(t *testing.T) { "TXT record should have 'a-' prefix when using new format only") } } + +func TestTXTRegistryRecordsWithEmptyTargets(t *testing.T) { + ctx := context.Background() + p := inmemory.NewInMemoryProvider() + p.CreateZone(testZone) + p.ApplyChanges(ctx, &plan.Changes{ + Create: []*endpoint.Endpoint{ + { + DNSName: "empty-targets.test-zone.example.org", + RecordType: endpoint.RecordTypeTXT, + Targets: endpoint.Targets{}, + }, + { + DNSName: "valid-targets.test-zone.example.org", + RecordType: endpoint.RecordTypeTXT, + Targets: endpoint.Targets{"target1"}, + }, + }, + }) + + r, _ := NewTXTRegistry(p, "", "", "owner", time.Hour, "", []string{}, []string{}, false, nil, false) + b := testutils.LogsToBuffer(log.ErrorLevel, t) + records, err := r.Records(ctx) + require.NoError(t, err) + + expectedRecords := []*endpoint.Endpoint{ + { + DNSName: "valid-targets.test-zone.example.org", + Targets: endpoint.Targets{"target1"}, + RecordType: endpoint.RecordTypeTXT, + Labels: map[string]string{}, + }, + } + + assert.True(t, testutils.SameEndpoints(records, expectedRecords)) + assert.Contains(t, b.String(), "TXT record has no targets empty-targets.test-zone.example.org") +}