Allow FQDN for ExternalName Service

This commit is contained in:
qianyong 2020-12-13 22:46:13 +08:00
parent 9c0a39636d
commit f9ffa93588
3 changed files with 63 additions and 1 deletions

View file

@ -21,6 +21,7 @@ import (
"net"
"reflect"
"strconv"
"strings"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/klog/v2"
@ -53,7 +54,8 @@ func getEndpoints(s *corev1.Service, port *corev1.ServicePort, proto corev1.Prot
targetPort := port.TargetPort.IntValue()
// if the externalName is not an IP address we need to validate is a valid FQDN
if net.ParseIP(s.Spec.ExternalName) == nil {
if errs := validation.IsDNS1123Subdomain(s.Spec.ExternalName); len(errs) > 0 {
externalName := strings.TrimSuffix(s.Spec.ExternalName, ".")
if errs := validation.IsDNS1123Subdomain(externalName); len(errs) > 0 {
klog.Errorf("Invalid DNS name %s: %v", s.Spec.ExternalName, errs)
return upsServers
}

View file

@ -107,6 +107,35 @@ func TestGetEndpoints(t *testing.T) {
},
},
},
{
"a service type ServiceTypeExternalName with an trailing dot ExternalName value should return one endpoints",
&corev1.Service{
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeExternalName,
ExternalName: "www.google.com.",
Ports: []corev1.ServicePort{
{
Name: "default",
TargetPort: intstr.FromInt(80),
},
},
},
},
&corev1.ServicePort{
Name: "default",
TargetPort: intstr.FromInt(80),
},
corev1.ProtocolTCP,
func(string) (*corev1.Endpoints, error) {
return &corev1.Endpoints{}, nil
},
[]ingress.Endpoint{
{
Address: "www.google.com",
Port: "443",
},
},
},
{
"a service type ServiceTypeExternalName with an invalid ExternalName value should no return endpoints",
&corev1.Service{