From 94a50ada464bfe7736180a634080a43e4727ec9c Mon Sep 17 00:00:00 2001 From: Jaromir Vanek Date: Fri, 8 Jan 2021 22:35:06 -0800 Subject: [PATCH] Correct format of SRV record for NodePort --- source/service.go | 15 +++++++++------ source/service_test.go | 14 +++++++------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/source/service.go b/source/service.go index ee2bef99d..18ab29041 100644 --- a/source/service.go +++ b/source/service.go @@ -628,14 +628,17 @@ func (sc *serviceSource) extractNodePortEndpoints(svc *v1.Service, nodeTargets e for _, port := range svc.Spec.Ports { if port.NodePort > 0 { + // following the RFC 2782, SRV record must have a following format + // _service._proto.name. TTL class SRV priority weight port + // see https://en.wikipedia.org/wiki/SRV_record + // build a target with a priority of 0, weight of 0, and pointing the given port on the given host target := fmt.Sprintf("0 50 %d %s", port.NodePort, hostname) - // figure out the portname - portName := port.Name - if portName == "" { - portName = fmt.Sprintf("%d", port.NodePort) - } + // take the service name from the K8s Service object + // it is safe to use since it is DNS compatible + // see https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names + serviceName := svc.ObjectMeta.Name // figure out the protocol protocol := strings.ToLower(string(port.Protocol)) @@ -643,7 +646,7 @@ func (sc *serviceSource) extractNodePortEndpoints(svc *v1.Service, nodeTargets e protocol = "tcp" } - recordName := fmt.Sprintf("_%s._%s.%s", portName, protocol, hostname) + recordName := fmt.Sprintf("_%s._%s.%s", serviceName, protocol, hostname) var ep *endpoint.Endpoint if ttl.IsConfigured() { diff --git a/source/service_test.go b/source/service_test.go index b13084683..9349e7698 100644 --- a/source/service_test.go +++ b/source/service_test.go @@ -1642,7 +1642,7 @@ func TestNodePortServices(t *testing.T) { }, nil, []*endpoint.Endpoint{ - {DNSName: "_30192._tcp.foo.example.org", Targets: endpoint.Targets{"0 50 30192 foo.example.org"}, RecordType: endpoint.RecordTypeSRV}, + {DNSName: "_foo._tcp.foo.example.org", Targets: endpoint.Targets{"0 50 30192 foo.example.org"}, RecordType: endpoint.RecordTypeSRV}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"54.10.11.1", "54.10.11.2"}, RecordType: endpoint.RecordTypeA}, }, false, @@ -1729,7 +1729,7 @@ func TestNodePortServices(t *testing.T) { map[string]string{}, nil, []*endpoint.Endpoint{ - {DNSName: "_30192._tcp.foo.bar.example.com", Targets: endpoint.Targets{"0 50 30192 foo.bar.example.com"}, RecordType: endpoint.RecordTypeSRV}, + {DNSName: "_foo._tcp.foo.bar.example.com", Targets: endpoint.Targets{"0 50 30192 foo.bar.example.com"}, RecordType: endpoint.RecordTypeSRV}, {DNSName: "foo.bar.example.com", Targets: endpoint.Targets{"54.10.11.1", "54.10.11.2"}, RecordType: endpoint.RecordTypeA}, }, false, @@ -1775,7 +1775,7 @@ func TestNodePortServices(t *testing.T) { }, nil, []*endpoint.Endpoint{ - {DNSName: "_30192._tcp.foo.example.org", Targets: endpoint.Targets{"0 50 30192 foo.example.org"}, RecordType: endpoint.RecordTypeSRV}, + {DNSName: "_foo._tcp.foo.example.org", Targets: endpoint.Targets{"0 50 30192 foo.example.org"}, RecordType: endpoint.RecordTypeSRV}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"10.0.1.1", "10.0.1.2"}, RecordType: endpoint.RecordTypeA}, }, false, @@ -1819,7 +1819,7 @@ func TestNodePortServices(t *testing.T) { }, nil, []*endpoint.Endpoint{ - {DNSName: "_30192._tcp.foo.example.org", Targets: endpoint.Targets{"0 50 30192 foo.example.org"}, RecordType: endpoint.RecordTypeSRV}, + {DNSName: "_foo._tcp.foo.example.org", Targets: endpoint.Targets{"0 50 30192 foo.example.org"}, RecordType: endpoint.RecordTypeSRV}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"54.10.11.2"}, RecordType: endpoint.RecordTypeA}, }, false, @@ -1865,7 +1865,7 @@ func TestNodePortServices(t *testing.T) { }, nil, []*endpoint.Endpoint{ - {DNSName: "_30192._tcp.foo.example.org", Targets: endpoint.Targets{"0 50 30192 foo.example.org"}, RecordType: endpoint.RecordTypeSRV}, + {DNSName: "_foo._tcp.foo.example.org", Targets: endpoint.Targets{"0 50 30192 foo.example.org"}, RecordType: endpoint.RecordTypeSRV}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"54.10.11.2"}, RecordType: endpoint.RecordTypeA}, }, false, @@ -1912,7 +1912,7 @@ func TestNodePortServices(t *testing.T) { }, nil, []*endpoint.Endpoint{ - {DNSName: "_30192._tcp.foo.example.org", Targets: endpoint.Targets{"0 50 30192 foo.example.org"}, RecordType: endpoint.RecordTypeSRV}, + {DNSName: "_foo._tcp.foo.example.org", Targets: endpoint.Targets{"0 50 30192 foo.example.org"}, RecordType: endpoint.RecordTypeSRV}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"10.0.1.1", "10.0.1.2"}, RecordType: endpoint.RecordTypeA}, }, false, @@ -1959,7 +1959,7 @@ func TestNodePortServices(t *testing.T) { }, nil, []*endpoint.Endpoint{ - {DNSName: "_30192._tcp.foo.example.org", Targets: endpoint.Targets{"0 50 30192 foo.example.org"}, RecordType: endpoint.RecordTypeSRV}, + {DNSName: "_foo._tcp.foo.example.org", Targets: endpoint.Targets{"0 50 30192 foo.example.org"}, RecordType: endpoint.RecordTypeSRV}, {DNSName: "foo.example.org", Targets: endpoint.Targets{"54.10.11.1", "54.10.11.2"}, RecordType: endpoint.RecordTypeA}, }, false,