mirror of
https://github.com/kubernetes-sigs/external-dns.git
synced 2025-08-06 09:36:58 +02:00
fix(service): omit nil endpoints and prefer endpointsForHostname()
Also add a test with an invalid hostname.
This commit is contained in:
parent
1eec428bf7
commit
3fac88bd64
@ -36,10 +36,6 @@ import (
|
|||||||
"sigs.k8s.io/external-dns/endpoint"
|
"sigs.k8s.io/external-dns/endpoint"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
defaultTargetsCapacity = 10
|
|
||||||
)
|
|
||||||
|
|
||||||
// serviceSource is an implementation of Source for Kubernetes service objects.
|
// serviceSource is an implementation of Source for Kubernetes service objects.
|
||||||
// It will find all services that are under our jurisdiction, i.e. annotated
|
// It will find all services that are under our jurisdiction, i.e. annotated
|
||||||
// desired hostname and matching or no controller annotation. For each of the
|
// desired hostname and matching or no controller annotation. For each of the
|
||||||
@ -360,10 +356,15 @@ func (sc *serviceSource) extractHeadlessEndpoints(svc *v1.Service, hostname stri
|
|||||||
targets = append(targets, target)
|
targets = append(targets, target)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ep *endpoint.Endpoint
|
||||||
if ttl.IsConfigured() {
|
if ttl.IsConfigured() {
|
||||||
endpoints = append(endpoints, endpoint.NewEndpointWithTTL(headlessKey.DNSName, headlessKey.RecordType, ttl, targets...))
|
ep = endpoint.NewEndpointWithTTL(headlessKey.DNSName, headlessKey.RecordType, ttl, targets...)
|
||||||
} else {
|
} else {
|
||||||
endpoints = append(endpoints, endpoint.NewEndpoint(headlessKey.DNSName, headlessKey.RecordType, targets...))
|
ep = endpoint.NewEndpoint(headlessKey.DNSName, headlessKey.RecordType, targets...)
|
||||||
|
}
|
||||||
|
|
||||||
|
if ep != nil {
|
||||||
|
endpoints = append(endpoints, ep)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -458,38 +459,14 @@ func (sc *serviceSource) setResourceLabel(service *v1.Service, endpoints []*endp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sc *serviceSource) generateEndpoints(svc *v1.Service, hostname string, providerSpecific endpoint.ProviderSpecific, setIdentifier string, useClusterIP bool) []*endpoint.Endpoint {
|
func (sc *serviceSource) generateEndpoints(svc *v1.Service, hostname string, providerSpecific endpoint.ProviderSpecific, setIdentifier string, useClusterIP bool) (endpoints []*endpoint.Endpoint) {
|
||||||
hostname = strings.TrimSuffix(hostname, ".")
|
hostname = strings.TrimSuffix(hostname, ".")
|
||||||
ttl := getTTLFromAnnotations(svc.Annotations, fmt.Sprintf("service/%s/%s", svc.Namespace, svc.Name))
|
|
||||||
|
|
||||||
epA := &endpoint.Endpoint{
|
resource := fmt.Sprintf("service/%s/%s", svc.Namespace, svc.Name)
|
||||||
RecordTTL: ttl,
|
|
||||||
RecordType: endpoint.RecordTypeA,
|
|
||||||
Labels: endpoint.NewLabels(),
|
|
||||||
Targets: make(endpoint.Targets, 0, defaultTargetsCapacity),
|
|
||||||
DNSName: hostname,
|
|
||||||
}
|
|
||||||
|
|
||||||
epAAAA := &endpoint.Endpoint{
|
ttl := getTTLFromAnnotations(svc.Annotations, resource)
|
||||||
RecordTTL: ttl,
|
|
||||||
RecordType: endpoint.RecordTypeAAAA,
|
|
||||||
Labels: endpoint.NewLabels(),
|
|
||||||
Targets: make(endpoint.Targets, 0, defaultTargetsCapacity),
|
|
||||||
DNSName: hostname,
|
|
||||||
}
|
|
||||||
|
|
||||||
epCNAME := &endpoint.Endpoint{
|
targets := getTargetsFromTargetAnnotation(svc.Annotations)
|
||||||
RecordTTL: ttl,
|
|
||||||
RecordType: endpoint.RecordTypeCNAME,
|
|
||||||
Labels: endpoint.NewLabels(),
|
|
||||||
Targets: make(endpoint.Targets, 0, defaultTargetsCapacity),
|
|
||||||
DNSName: hostname,
|
|
||||||
}
|
|
||||||
|
|
||||||
var endpoints []*endpoint.Endpoint
|
|
||||||
var targets endpoint.Targets
|
|
||||||
|
|
||||||
targets = getTargetsFromTargetAnnotation(svc.Annotations)
|
|
||||||
|
|
||||||
if len(targets) == 0 {
|
if len(targets) == 0 {
|
||||||
switch svc.Spec.Type {
|
switch svc.Spec.Type {
|
||||||
@ -517,32 +494,15 @@ func (sc *serviceSource) generateEndpoints(svc *v1.Service, hostname string, pro
|
|||||||
case v1.ServiceTypeExternalName:
|
case v1.ServiceTypeExternalName:
|
||||||
targets = extractServiceExternalName(svc)
|
targets = extractServiceExternalName(svc)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for _, t := range targets {
|
for _, endpoint := range endpoints {
|
||||||
switch suitableType(t) {
|
endpoint.ProviderSpecific = providerSpecific
|
||||||
case endpoint.RecordTypeA:
|
endpoint.SetIdentifier = setIdentifier
|
||||||
epA.Targets = append(epA.Targets, t)
|
|
||||||
case endpoint.RecordTypeAAAA:
|
|
||||||
epAAAA.Targets = append(epAAAA.Targets, t)
|
|
||||||
case endpoint.RecordTypeCNAME:
|
|
||||||
epCNAME.Targets = append(epCNAME.Targets, t)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(epA.Targets) > 0 {
|
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
|
||||||
endpoints = append(endpoints, epA)
|
|
||||||
}
|
|
||||||
if len(epAAAA.Targets) > 0 {
|
|
||||||
endpoints = append(endpoints, epAAAA)
|
|
||||||
}
|
|
||||||
if len(epCNAME.Targets) > 0 {
|
|
||||||
endpoints = append(endpoints, epCNAME)
|
|
||||||
}
|
|
||||||
for _, endpoint := range endpoints {
|
|
||||||
endpoint.ProviderSpecific = providerSpecific
|
|
||||||
endpoint.SetIdentifier = setIdentifier
|
|
||||||
}
|
|
||||||
return endpoints
|
return endpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -742,7 +702,9 @@ func (sc *serviceSource) extractNodePortEndpoints(svc *v1.Service, hostname stri
|
|||||||
ep = endpoint.NewEndpoint(recordName, endpoint.RecordTypeSRV, target)
|
ep = endpoint.NewEndpoint(recordName, endpoint.RecordTypeSRV, target)
|
||||||
}
|
}
|
||||||
|
|
||||||
endpoints = append(endpoints, ep)
|
if ep != nil {
|
||||||
|
endpoints = append(endpoints, ep)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1560,6 +1560,17 @@ func TestClusterIpServices(t *testing.T) {
|
|||||||
expected: []*endpoint.Endpoint{},
|
expected: []*endpoint.Endpoint{},
|
||||||
labelSelector: "app=web-external",
|
labelSelector: "app=web-external",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title: "invalid hostname does not generate endpoints",
|
||||||
|
svcNamespace: "testing",
|
||||||
|
svcName: "foo",
|
||||||
|
svcType: v1.ServiceTypeClusterIP,
|
||||||
|
annotations: map[string]string{
|
||||||
|
hostnameAnnotationKey: "this-is-an-exceedingly-long-label-that-external-dns-should-reject.example.org.",
|
||||||
|
},
|
||||||
|
clusterIP: "1.2.3.4",
|
||||||
|
expected: []*endpoint.Endpoint{},
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
tc := tc
|
tc := tc
|
||||||
t.Run(tc.title, func(t *testing.T) {
|
t.Run(tc.title, func(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user