Merge pull request #3294 from rikatz/deal-with-nil-endpoint

Fix null pointer on generateTxtRecord
This commit is contained in:
Kubernetes Prow Robot 2023-01-06 09:43:59 -08:00 committed by GitHub
commit 7b7b84dce2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 5 deletions

View File

@ -191,19 +191,26 @@ func (im *TXTRegistry) generateTXTRecord(r *endpoint.Endpoint) []*endpoint.Endpo
if r.RecordType == endpoint.RecordTypeTXT {
return nil
}
endpoints := make([]*endpoint.Endpoint, 0)
// old TXT record format
txt := endpoint.NewEndpoint(im.mapper.toTXTName(r.DNSName), endpoint.RecordTypeTXT, r.Labels.Serialize(true)).WithSetIdentifier(r.SetIdentifier)
txt.ProviderSpecific = r.ProviderSpecific
txt := endpoint.NewEndpoint(im.mapper.toTXTName(r.DNSName), endpoint.RecordTypeTXT, r.Labels.Serialize(true))
if txt != nil {
txt.WithSetIdentifier(r.SetIdentifier)
txt.ProviderSpecific = r.ProviderSpecific
endpoints = append(endpoints, txt)
}
// new TXT record format (containing record type)
txtNew := endpoint.NewEndpoint(im.mapper.toNewTXTName(r.DNSName, r.RecordType), endpoint.RecordTypeTXT, r.Labels.Serialize(true))
if txtNew != nil {
txtNew.WithSetIdentifier(r.SetIdentifier)
txtNew.ProviderSpecific = r.ProviderSpecific
} else {
return []*endpoint.Endpoint{txt}
endpoints = append(endpoints, txtNew)
}
return []*endpoint.Endpoint{txt, txtNew}
return endpoints
}
// ApplyChanges updates dns provider with the changes

View File

@ -1169,6 +1169,23 @@ func TestGenerateTXT(t *testing.T) {
assert.Equal(t, expectedTXT, gotTXT)
}
func TestFailGenerateTXT(t *testing.T) {
cnameRecord := &endpoint.Endpoint{
DNSName: "foo-some-really-big-name-not-supported-and-will-fail-000000000000000000.test-zone.example.org",
Targets: endpoint.Targets{"new-foo.loadbalancer.com"},
RecordType: endpoint.RecordTypeCNAME,
Labels: map[string]string{},
}
// A bad DNS name returns empty expected TXT
expectedTXT := []*endpoint.Endpoint{}
p := inmemory.NewInMemoryProvider()
p.CreateZone(testZone)
r, _ := NewTXTRegistry(p, "", "", "owner", time.Hour, "", []string{})
gotTXT := r.generateTXTRecord(cnameRecord)
assert.Equal(t, expectedTXT, gotTXT)
}
/**
helper methods