Seena Fallah 736a2d58ae
feat!: generalize PTR record support from rfc2136 to all providers (#6232)
* feat(metrics): add source wrapper metrics for invalid and deduplicated endpoints

Add GaugeVecMetric.Reset() to clear stale label combinations between cycles.

Introduce invalidEndpoints and deduplicatedEndpoints gauge vectors in the
source wrappers package, partitioned by record_type and source_type. The
dedup source wrapper now tracks rejected (invalid) and de-duplicated
endpoints per collection cycle.

Update the metrics documentation and bump the known metrics count.

Signed-off-by: Seena Fallah <seenafallah@gmail.com>

* feat(source): add PTR source wrapper for automatic reverse DNS

Implement ptrSource, a source wrapper that generates PTR endpoints from
A/AAAA records. The wrapper supports:

- Global default via WithCreatePTR (maps to --create-ptr flag)
- Per-endpoint override via record-type provider-specific property
- Grouping multiple hostnames sharing an IP into a single PTR endpoint
- Skipping wildcard DNS names

Add WithPTRSupported and WithCreatePTR options to the wrapper Config
and wire the PTR wrapper into the WrapSources chain when PTR is in
managed-record-types.

Signed-off-by: Seena Fallah <seenafallah@gmail.com>

* feat(config): add --create-ptr flag and deprecate --rfc2136-create-ptr

Add the generic --create-ptr boolean flag to Config, enabling automatic
PTR record creation for any provider. Add IsPTRSupported() helper that
checks whether PTR is included in --managed-record-types.

Add validation: --create-ptr (or legacy --rfc2136-create-ptr) now
requires PTR in --managed-record-types, preventing misconfiguration.

Mark --rfc2136-create-ptr as deprecated in the flag description.

Signed-off-by: Seena Fallah <seenafallah@gmail.com>

* refactor(rfc2136): remove inline PTR logic in favor of PTR source wrapper

Remove the createPTR field, AddReverseRecord, RemoveReverseRecord, and
GenerateReverseRecord methods from the rfc2136 provider. PTR record
generation is now handled generically by the PTR source wrapper before
records reach the provider.

Update the PTR creation test to supply pre-generated PTR endpoints
(simulating what the source wrapper produces) instead of relying on
the provider to create them internally.

Signed-off-by: Seena Fallah <seenafallah@gmail.com>

* feat(controller): wire PTR source wrapper into buildSource

Pass the top-level Config to buildSource so it can read IsPTRSupported()
and the CreatePTR / RFC2136CreatePTR flags. When PTR is in
managed-record-types, the PTR source wrapper is installed in the
wrapper chain with the combined create-ptr default.

Signed-off-by: Seena Fallah <seenafallah@gmail.com>

* chore(pdns): remove stale comment and fix whitespace

Remove an outdated comment about a single-target-per-tuple assumption
that no longer applies.

Signed-off-by: Seena Fallah <seenafallah@gmail.com>

* docs: add PTR records documentation and update existing guides

Add docs/advanced/ptr-records.md covering the --create-ptr flag,
per-resource annotation overrides, prerequisites, and usage examples.

Update:
- annotations.md: document record-type annotation
- flags.md: add --create-ptr, mark --rfc2136-create-ptr as deprecated
- tutorials/rfc2136.md: point to generic --create-ptr flag
- contributing/source-wrappers.md: add PTR wrapper to the chain
- mkdocs.yml: add PTR Records navigation entry

Signed-off-by: Seena Fallah <seenafallah@gmail.com>

* feat(rfc2136)!: remove rfc2136-create-ptr in favor of create-ptr

Signed-off-by: Seena Fallah <seenafallah@gmail.com>

---------

Signed-off-by: Seena Fallah <seenafallah@gmail.com>
2026-03-30 13:36:16 +05:30

227 lines
8.6 KiB
Go

/*
Copyright 2026 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package wrappers
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/internal/testutils"
"sigs.k8s.io/external-dns/source"
)
var _ source.Source = &ptrSource{}
func TestPTRSource(t *testing.T) {
tests := []struct {
name string
defaultEnabled bool
endpoints []*endpoint.Endpoint
expected []*endpoint.Endpoint
}{
{
name: "A record produces PTR",
defaultEnabled: true,
endpoints: []*endpoint.Endpoint{
{DNSName: "web.example.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"192.168.49.2"}},
},
expected: []*endpoint.Endpoint{
{DNSName: "web.example.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"192.168.49.2"}},
{DNSName: "2.49.168.192.in-addr.arpa", RecordType: endpoint.RecordTypePTR, Targets: endpoint.Targets{"web.example.com"}},
},
},
{
name: "disabled by default, no PTR",
defaultEnabled: false,
endpoints: []*endpoint.Endpoint{
{DNSName: "web.example.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"192.168.49.2"}},
},
expected: []*endpoint.Endpoint{
{DNSName: "web.example.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"192.168.49.2"}},
},
},
{
name: "CNAME skipped",
defaultEnabled: true,
endpoints: []*endpoint.Endpoint{
{DNSName: "alias.example.com", RecordType: endpoint.RecordTypeCNAME, Targets: endpoint.Targets{"web.example.com"}},
},
expected: []*endpoint.Endpoint{
{DNSName: "alias.example.com", RecordType: endpoint.RecordTypeCNAME, Targets: endpoint.Targets{"web.example.com"}},
},
},
{
name: "wildcard skipped",
defaultEnabled: true,
endpoints: []*endpoint.Endpoint{
{DNSName: "*.example.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"192.168.49.2"}},
},
expected: []*endpoint.Endpoint{
{DNSName: "*.example.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"192.168.49.2"}},
},
},
{
name: "same IP merges into single PTR",
defaultEnabled: true,
endpoints: []*endpoint.Endpoint{
{DNSName: "a.example.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"192.168.49.2"}},
{DNSName: "b.example.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"192.168.49.2"}},
},
expected: []*endpoint.Endpoint{
{DNSName: "a.example.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"192.168.49.2"}},
{DNSName: "b.example.com", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"192.168.49.2"}},
{DNSName: "2.49.168.192.in-addr.arpa", RecordType: endpoint.RecordTypePTR, Targets: endpoint.Targets{"a.example.com", "b.example.com"}},
},
},
{
name: "TTL preserved",
defaultEnabled: true,
endpoints: []*endpoint.Endpoint{
endpoint.NewEndpointWithTTL("web.example.com", endpoint.RecordTypeA, 300, "10.0.0.1"),
},
expected: []*endpoint.Endpoint{
endpoint.NewEndpointWithTTL("web.example.com", endpoint.RecordTypeA, 300, "10.0.0.1"),
endpoint.NewEndpointWithTTL("1.0.0.10.in-addr.arpa", endpoint.RecordTypePTR, 300, "web.example.com"),
},
},
{
name: "conflicting TTLs use minimum",
defaultEnabled: true,
endpoints: []*endpoint.Endpoint{
endpoint.NewEndpointWithTTL("a.example.com", endpoint.RecordTypeA, 300, "10.0.0.1"),
endpoint.NewEndpointWithTTL("b.example.com", endpoint.RecordTypeA, 60, "10.0.0.1"),
},
expected: []*endpoint.Endpoint{
endpoint.NewEndpointWithTTL("a.example.com", endpoint.RecordTypeA, 300, "10.0.0.1"),
endpoint.NewEndpointWithTTL("b.example.com", endpoint.RecordTypeA, 60, "10.0.0.1"),
endpoint.NewEndpointWithTTL("1.0.0.10.in-addr.arpa", endpoint.RecordTypePTR, 60, "a.example.com", "b.example.com"),
},
},
{
name: "conflicting TTLs use minimum reversed order",
defaultEnabled: true,
endpoints: []*endpoint.Endpoint{
endpoint.NewEndpointWithTTL("a.example.com", endpoint.RecordTypeA, 60, "10.0.0.1"),
endpoint.NewEndpointWithTTL("b.example.com", endpoint.RecordTypeA, 300, "10.0.0.1"),
},
expected: []*endpoint.Endpoint{
endpoint.NewEndpointWithTTL("a.example.com", endpoint.RecordTypeA, 60, "10.0.0.1"),
endpoint.NewEndpointWithTTL("b.example.com", endpoint.RecordTypeA, 300, "10.0.0.1"),
endpoint.NewEndpointWithTTL("1.0.0.10.in-addr.arpa", endpoint.RecordTypePTR, 60, "a.example.com", "b.example.com"),
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mockSource := new(testutils.MockSource)
mockSource.On("Endpoints").Return(tc.endpoints, nil)
src := NewPTRSource(mockSource, tc.defaultEnabled)
result, err := src.Endpoints(t.Context())
require.NoError(t, err)
assert.Len(t, result, len(tc.expected))
for i, ep := range result {
assert.Equal(t, tc.expected[i].DNSName, ep.DNSName)
assert.Equal(t, tc.expected[i].RecordType, ep.RecordType)
assert.Equal(t, tc.expected[i].RecordTTL, ep.RecordTTL)
assert.ElementsMatch(t, tc.expected[i].Targets, ep.Targets)
}
})
}
}
func TestPTRSource_AnnotationOverride(t *testing.T) {
t.Run("annotation opts in when flag is off", func(t *testing.T) {
eps := []*endpoint.Endpoint{
endpoint.NewEndpoint("web.example.com", endpoint.RecordTypeA, "192.168.49.2").
WithProviderSpecific(endpoint.ProviderSpecificRecordType, "ptr"),
}
mockSource := testutils.NewMockSource(eps...)
src := NewPTRSource(mockSource, false)
result, err := src.Endpoints(t.Context())
require.NoError(t, err)
assert.Len(t, result, 2)
assert.Equal(t, endpoint.RecordTypePTR, result[1].RecordType)
// provider-specific property should be removed after processing
_, ok := result[0].GetProviderSpecificProperty(endpoint.ProviderSpecificRecordType)
assert.False(t, ok, "record-type property should be removed from original endpoint")
})
t.Run("annotation opts out when flag is on", func(t *testing.T) {
eps := []*endpoint.Endpoint{
endpoint.NewEndpoint("web.example.com", endpoint.RecordTypeA, "192.168.49.2").
WithProviderSpecific(endpoint.ProviderSpecificRecordType, ""),
}
mockSource := testutils.NewMockSource(eps...)
src := NewPTRSource(mockSource, true)
result, err := src.Endpoints(t.Context())
require.NoError(t, err)
assert.Len(t, result, 1) // only the original A record
// provider-specific property should be removed after processing
_, ok := result[0].GetProviderSpecificProperty(endpoint.ProviderSpecificRecordType)
assert.False(t, ok, "record-type property should be removed from original endpoint")
})
t.Run("no annotation uses flag default true", func(t *testing.T) {
eps := []*endpoint.Endpoint{
endpoint.NewEndpoint("web.example.com", endpoint.RecordTypeA, "192.168.49.2"),
}
mockSource := testutils.NewMockSource(eps...)
src := NewPTRSource(mockSource, true)
result, err := src.Endpoints(t.Context())
require.NoError(t, err)
assert.Len(t, result, 2)
})
t.Run("no annotation uses flag default false", func(t *testing.T) {
eps := []*endpoint.Endpoint{
endpoint.NewEndpoint("web.example.com", endpoint.RecordTypeA, "192.168.49.2"),
}
mockSource := testutils.NewMockSource(eps...)
src := NewPTRSource(mockSource, false)
result, err := src.Endpoints(t.Context())
require.NoError(t, err)
assert.Len(t, result, 1)
})
}
func TestPTRSource_IPv6(t *testing.T) {
eps := []*endpoint.Endpoint{
{DNSName: "v6.example.com", RecordType: endpoint.RecordTypeAAAA, Targets: endpoint.Targets{"2001:db8::1"}},
}
mockSource := testutils.NewMockSource(eps...)
src := NewPTRSource(mockSource, true)
result, err := src.Endpoints(t.Context())
require.NoError(t, err)
require.Len(t, result, 2)
assert.Equal(t, "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", result[1].DNSName)
assert.Equal(t, endpoint.RecordTypePTR, result[1].RecordType)
}
func TestPTRSource_AddEventHandler(t *testing.T) {
mockSource := testutils.NewMockSource()
src := NewPTRSource(mockSource, true)
src.AddEventHandler(t.Context(), func() {})
mockSource.AssertNumberOfCalls(t, "AddEventHandler", 1)
}