-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix: Prevent orphaning records due to label lengths #6431
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -504,6 +504,37 @@ func FilterEndpointsByOwnerID(ownerID string, eps []*Endpoint) []*Endpoint { | |
| return filtered | ||
| } | ||
|
|
||
| // FilterEndpointsByDNSCompliance drops endpoints whose projected registry | ||
| // name (from toRegistryName) has any DNS label over RFC 1035's 63-char limit. | ||
| // Such records cannot be owned by the registry and would orphan in the zone. | ||
| // onSkip, if non-nil, is invoked per drop with the offending label so callers | ||
| // can attach metrics or events without importing this package. | ||
| func FilterEndpointsByDNSCompliance(toRegistryName func(*Endpoint) string, eps []*Endpoint, onSkip func(skipped *Endpoint, badLabel string)) []*Endpoint { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the late comment — sharing my personal take on the current design. 🙇
Also, since
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about an alternative like this instead?
// OverflowingLabel returns the first label in name exceeding RFC 1035's 63-char limit.
func OverflowingLabel(name string) (string, bool) {
for label := range strings.SplitSeq(name, ".") {
if len(label) > 63 {
return label, true
}
}
return "", false
}registry/txt/registry.go — validate in generateTXTRecord explicitly, removing generateTXTRecordWithFilter: func (im *TXTRegistry) generateTXTRecord(r *endpoint.Endpoint) ([]*endpoint.Endpoint, error) {
recordType := r.RecordType
if shouldUseCNAMEForTxtRecord(r) {
recordType = endpoint.RecordTypeCNAME
}
if im.oldOwnerID != "" && r.Labels[endpoint.OwnerLabelKey] == im.oldOwnerID {
r.Labels[endpoint.OwnerLabelKey] = im.ownerID
}
txtName := im.mapper.ToTXTName(r.DNSName, recordType)
if label, ok := endpoint.OverflowingLabel(txtName); ok {
return nil, fmt.Errorf("%s %s: projected TXT name %q has label %q exceeding RFC 1035's 63-char limit", r.RecordType, r.DNSName, txtName, label)
}
txtNew := endpoint.NewEndpoint(txtName, endpoint.RecordTypeTXT, r.Labels.Serialize(true, im.txtEncryptEnabled, im.txtEncryptAESKey))
txtNew.WithSetIdentifier(r.SetIdentifier)
txtNew.Labels[endpoint.OwnedRecordLabelKey] = r.DNSName
txtNew.ProviderSpecific = r.ProviderSpecific
return []*endpoint.Endpoint{txtNew}, nil
}registry/txt/registry.go — Create loop in ApplyChanges, with isAbsent made explicit at the call site: filteredChanges := &plan.Changes{
Create: make([]*endpoint.Endpoint, 0, len(changes.Create)),
UpdateNew: endpoint.FilterEndpointsByOwnerID(im.ownerID, changes.UpdateNew),
UpdateOld: endpoint.FilterEndpointsByOwnerID(im.ownerID, changes.UpdateOld),
Delete: endpoint.FilterEndpointsByOwnerID(im.ownerID, changes.Delete),
}
for _, r := range changes.Create {
if r.Labels == nil {
r.Labels = make(map[string]string)
}
r.Labels[endpoint.OwnerLabelKey] = im.ownerID
txts, err := im.generateTXTRecord(r)
if err != nil {
log.Errorf("Skipping owned record: %v; record would become unmanageable", err)
recordSkippedLabelTooLong(r)
continue
}
filteredChanges.Create = append(filteredChanges.Create, r)
for _, txt := range txts {
if im.existingTXTs.isAbsent(txt) {
filteredChanges.Create = append(filteredChanges.Create, txt)
}
}
if im.cacheInterval > 0 {
im.addToCache(r)
}
}This keeps the label length check and TXT name generation in one place (generateTXTRecord), avoids leaking registry-specific logic into the endpoint package, and makes isAbsent explicit at the call site. |
||
| filtered := make([]*Endpoint, 0, len(eps)) | ||
| for _, ep := range eps { | ||
| registryName := toRegistryName(ep) | ||
| if badLabel, ok := overflowingLabel(registryName); ok { | ||
| log.Errorf(`Skipping endpoint %s %s: projected registry name %q has label %q exceeding RFC 1035's 63-char limit; record would be unmanageable`, ep.RecordType, ep.DNSName, registryName, badLabel) | ||
| if onSkip != nil { | ||
| onSkip(ep, badLabel) | ||
| } | ||
| continue | ||
| } | ||
| filtered = append(filtered, ep) | ||
| } | ||
| return filtered | ||
| } | ||
|
|
||
| // overflowingLabel returns the first label in name longer than 63 chars. | ||
| func overflowingLabel(name string) (string, bool) { | ||
| for label := range strings.SplitSeq(name, ".") { | ||
| if len(label) > 63 { | ||
| return label, true | ||
| } | ||
| } | ||
| return "", false | ||
| } | ||
|
|
||
| // RemoveDuplicates returns a slice holding the unique endpoints. | ||
| // This function doesn't contemplate the Targets of an Endpoint | ||
| // as part of the primary Key | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| 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 txt | ||
|
|
||
| import ( | ||
| "github.com/prometheus/client_golang/prometheus" | ||
|
|
||
| "sigs.k8s.io/external-dns/endpoint" | ||
| "sigs.k8s.io/external-dns/pkg/metrics" | ||
| ) | ||
|
|
||
| // registrySkippedLabelTooLongTotal counts records dropped because the projected | ||
| // TXT name has a label exceeding the 63-char RFC 1035 limit. | ||
| var registrySkippedLabelTooLongTotal = metrics.NewCounterVecWithOpts( | ||
| prometheus.CounterOpts{ | ||
| Subsystem: "registry", | ||
| Name: "skipped_records_label_too_long_total", | ||
| Help: "Total number of records skipped because the projected TXT registry name has a DNS label exceeding RFC 1035's 63-char limit (vector).", | ||
| }, | ||
| []string{"record_type", "domain"}, | ||
| ) | ||
|
|
||
| func init() { | ||
| metrics.RegisterMetric.MustRegister(registrySkippedLabelTooLongTotal) | ||
| } | ||
|
|
||
| // recordSkippedLabelTooLong is the FilterEndpointsByDNSCompliance skip callback. | ||
| func recordSkippedLabelTooLong(skipped *endpoint.Endpoint, _ string) { | ||
| registrySkippedLabelTooLongTotal.CounterVec.WithLabelValues(skipped.RecordType, skipped.GetNakedDomain()).Inc() | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The term
parentis used here, butowned recordmight be a better fit — it maps directly to theownedRecordlabel key inendpoint/labels.goand reflects how the registry already models this relationship.