Skip to content

Commit b579cfa

Browse files
committed
refactor: use shallow copy from endpoint.go
1 parent b6a2739 commit b579cfa

2 files changed

Lines changed: 41 additions & 11 deletions

File tree

endpoint/endpoint.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,26 @@ func (e *Endpoint) WithSetIdentifier(setIdentifier string) *Endpoint {
313313
return e
314314
}
315315

316+
// WithTargets returns a shallow copy of the endpoint with only Targets and RecordType replaced.
317+
// The RecordType is derived from the first target using SuitableType.
318+
// All other fields remain intact (shared with the original endpoint).
319+
func (e *Endpoint) WithTargets(targets Targets) *Endpoint {
320+
recordType := e.RecordType
321+
if len(targets) > 0 {
322+
recordType = SuitableType(targets[0])
323+
}
324+
return &Endpoint{
325+
DNSName: e.DNSName,
326+
Targets: targets,
327+
RecordType: recordType,
328+
SetIdentifier: e.SetIdentifier,
329+
RecordTTL: e.RecordTTL,
330+
Labels: e.Labels,
331+
ProviderSpecific: e.ProviderSpecific,
332+
refObject: e.refObject,
333+
}
334+
}
335+
316336
// WithProviderSpecific attaches a key/value pair to the Endpoint and returns the Endpoint.
317337
// This can be used to pass additional data through the stages of ExternalDNS's Endpoint processing.
318338
// The assumption is that most of the time this will be provider specific metadata that doesn't

source/wrappers/resolvetarget.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package wrappers
1818

1919
import (
2020
"context"
21-
"maps"
2221
"net"
2322

2423
log "github.com/sirupsen/logrus"
@@ -76,17 +75,19 @@ func (rs *resolveTarget) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, e
7675
result := make([]*endpoint.Endpoint, 0, len(endpoints))
7776
for _, ep := range endpoints {
7877
if ep == nil {
79-
result = append(result, nil)
8078
continue
8179
}
8280

83-
shouldResolve := false
84-
if v, ok := ep.GetProviderSpecificProperty("resolve-target"); ok {
85-
shouldResolve = v == "true"
86-
ep.DeleteProviderSpecificProperty("resolve-target")
81+
// Skip early if not a CNAME record, as only those can have hostname targets that need resolution.
82+
if ep.RecordType != endpoint.RecordTypeCNAME {
83+
result = append(result, ep)
84+
continue
8785
}
8886

89-
if !shouldResolve || ep.RecordType != endpoint.RecordTypeCNAME {
87+
// Check if the "resolve-target" provider-specific property is set to "true" for this endpoint.
88+
v, _ := ep.GetProviderSpecificProperty("resolve-target")
89+
ep.DeleteProviderSpecificProperty("resolve-target")
90+
if v != "true" {
9091
result = append(result, ep)
9192
continue
9293
}
@@ -106,11 +107,20 @@ func (rs *resolveTarget) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, e
106107
// All resolutions failed; skip this endpoint entirely.
107108
continue
108109
}
109-
resolved := endpoint.EndpointsForHostname(ep.DNSName, ipTargets, ep.RecordTTL, ep.ProviderSpecific, ep.SetIdentifier, "")
110-
for _, r := range resolved {
111-
maps.Copy(r.Labels, ep.Labels)
110+
111+
// Group targets by record type (A vs AAAA)
112+
byType := map[string]endpoint.Targets{}
113+
for _, t := range ipTargets {
114+
rt := endpoint.SuitableType(t)
115+
byType[rt] = append(byType[rt], t)
116+
}
117+
118+
// Emit one endpoint per record type with the same DNSName and provider-specific properties.
119+
for _, rt := range []string{endpoint.RecordTypeA, endpoint.RecordTypeAAAA} {
120+
if targets := byType[rt]; len(targets) > 0 {
121+
result = append(result, ep.WithTargets(targets))
122+
}
112123
}
113-
result = append(result, resolved...)
114124
}
115125

116126
return result, nil

0 commit comments

Comments
 (0)