expose kubernetes service cluster ip (#4940)

Signed-off-by: tariqibrahim <tariq.ibrahim@microsoft.com>
Signed-off-by: tariqibrahim <tariq181290@gmail.com>
This commit is contained in:
Tariq Ibrahim 2018-12-18 07:17:34 -08:00 committed by Brian Brazil
parent 68f0787c37
commit de6f3b6af7
3 changed files with 74 additions and 15 deletions

View file

@ -140,6 +140,8 @@ const (
serviceAnnotationPrefix = metaLabelPrefix + "service_annotation_" serviceAnnotationPrefix = metaLabelPrefix + "service_annotation_"
servicePortNameLabel = metaLabelPrefix + "service_port_name" servicePortNameLabel = metaLabelPrefix + "service_port_name"
servicePortProtocolLabel = metaLabelPrefix + "service_port_protocol" servicePortProtocolLabel = metaLabelPrefix + "service_port_protocol"
serviceClusterIPLabel = metaLabelPrefix + "service_cluster_ip"
serviceExternalNameLabel = metaLabelPrefix + "service_external_name"
) )
func serviceLabels(svc *apiv1.Service) model.LabelSet { func serviceLabels(svc *apiv1.Service) model.LabelSet {
@ -169,11 +171,19 @@ func (s *Service) buildService(svc *apiv1.Service) *targetgroup.Group {
for _, port := range svc.Spec.Ports { for _, port := range svc.Spec.Ports {
addr := net.JoinHostPort(svc.Name+"."+svc.Namespace+".svc", strconv.FormatInt(int64(port.Port), 10)) addr := net.JoinHostPort(svc.Name+"."+svc.Namespace+".svc", strconv.FormatInt(int64(port.Port), 10))
tg.Targets = append(tg.Targets, model.LabelSet{ labelSet := model.LabelSet{
model.AddressLabel: lv(addr), model.AddressLabel: lv(addr),
servicePortNameLabel: lv(port.Name), servicePortNameLabel: lv(port.Name),
servicePortProtocolLabel: lv(string(port.Protocol)), servicePortProtocolLabel: lv(string(port.Protocol)),
}) }
if svc.Spec.Type == apiv1.ServiceTypeExternalName {
labelSet[serviceExternalNameLabel] = lv(svc.Spec.ExternalName)
} else {
labelSet[serviceClusterIPLabel] = lv(svc.Spec.ClusterIP)
}
tg.Targets = append(tg.Targets, labelSet)
} }
return tg return tg

View file

@ -44,6 +44,8 @@ func makeMultiPortService() *v1.Service {
Port: int32(30901), Port: int32(30901),
}, },
}, },
Type: v1.ServiceTypeClusterIP,
ClusterIP: "10.0.0.1",
}, },
} }
} }
@ -62,6 +64,8 @@ func makeSuffixedService(suffix string) *v1.Service {
Port: int32(30900), Port: int32(30900),
}, },
}, },
Type: v1.ServiceTypeClusterIP,
ClusterIP: "10.0.0.1",
}, },
} }
} }
@ -70,6 +74,26 @@ func makeService() *v1.Service {
return makeSuffixedService("") return makeSuffixedService("")
} }
func makeExternalService() *v1.Service {
return &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "testservice-external",
Namespace: "default",
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{
{
Name: "testport",
Protocol: v1.ProtocolTCP,
Port: int32(31900),
},
},
Type: v1.ServiceTypeExternalName,
ExternalName: "FooExternalName",
},
}
}
func TestServiceDiscoveryAdd(t *testing.T) { func TestServiceDiscoveryAdd(t *testing.T) {
n, c, w := makeDiscovery(RoleService, NamespaceDiscovery{}) n, c, w := makeDiscovery(RoleService, NamespaceDiscovery{})
@ -79,14 +103,18 @@ func TestServiceDiscoveryAdd(t *testing.T) {
obj := makeService() obj := makeService()
c.CoreV1().Services(obj.Namespace).Create(obj) c.CoreV1().Services(obj.Namespace).Create(obj)
w.Services().Add(obj) w.Services().Add(obj)
obj = makeExternalService()
c.CoreV1().Services(obj.Namespace).Create(obj)
w.Services().Add(obj)
}, },
expectedMaxItems: 1, expectedMaxItems: 2,
expectedRes: map[string]*targetgroup.Group{ expectedRes: map[string]*targetgroup.Group{
"svc/default/testservice": { "svc/default/testservice": {
Targets: []model.LabelSet{ Targets: []model.LabelSet{
{ {
"__meta_kubernetes_service_port_protocol": "TCP", "__meta_kubernetes_service_port_protocol": "TCP",
"__address__": "testservice.default.svc:30900", "__address__": "testservice.default.svc:30900",
"__meta_kubernetes_service_cluster_ip": "10.0.0.1",
"__meta_kubernetes_service_port_name": "testport", "__meta_kubernetes_service_port_name": "testport",
}, },
}, },
@ -96,6 +124,21 @@ func TestServiceDiscoveryAdd(t *testing.T) {
}, },
Source: "svc/default/testservice", Source: "svc/default/testservice",
}, },
"svc/default/testservice-external": {
Targets: []model.LabelSet{
{
"__meta_kubernetes_service_port_protocol": "TCP",
"__address__": "testservice-external.default.svc:31900",
"__meta_kubernetes_service_port_name": "testport",
"__meta_kubernetes_service_external_name": "FooExternalName",
},
},
Labels: model.LabelSet{
"__meta_kubernetes_service_name": "testservice-external",
"__meta_kubernetes_namespace": "default",
},
Source: "svc/default/testservice-external",
},
}, },
}.Run(t) }.Run(t)
} }
@ -136,11 +179,13 @@ func TestServiceDiscoveryUpdate(t *testing.T) {
{ {
"__meta_kubernetes_service_port_protocol": "TCP", "__meta_kubernetes_service_port_protocol": "TCP",
"__address__": "testservice.default.svc:30900", "__address__": "testservice.default.svc:30900",
"__meta_kubernetes_service_cluster_ip": "10.0.0.1",
"__meta_kubernetes_service_port_name": "testport0", "__meta_kubernetes_service_port_name": "testport0",
}, },
{ {
"__meta_kubernetes_service_port_protocol": "UDP", "__meta_kubernetes_service_port_protocol": "UDP",
"__address__": "testservice.default.svc:30901", "__address__": "testservice.default.svc:30901",
"__meta_kubernetes_service_cluster_ip": "10.0.0.1",
"__meta_kubernetes_service_port_name": "testport1", "__meta_kubernetes_service_port_name": "testport1",
}, },
}, },
@ -176,6 +221,7 @@ func TestServiceDiscoveryNamespaces(t *testing.T) {
{ {
"__meta_kubernetes_service_port_protocol": "TCP", "__meta_kubernetes_service_port_protocol": "TCP",
"__address__": "testservice.ns1.svc:30900", "__address__": "testservice.ns1.svc:30900",
"__meta_kubernetes_service_cluster_ip": "10.0.0.1",
"__meta_kubernetes_service_port_name": "testport", "__meta_kubernetes_service_port_name": "testport",
}, },
}, },
@ -190,6 +236,7 @@ func TestServiceDiscoveryNamespaces(t *testing.T) {
{ {
"__meta_kubernetes_service_port_protocol": "TCP", "__meta_kubernetes_service_port_protocol": "TCP",
"__address__": "testservice.ns2.svc:30900", "__address__": "testservice.ns2.svc:30900",
"__meta_kubernetes_service_cluster_ip": "10.0.0.1",
"__meta_kubernetes_service_port_name": "testport", "__meta_kubernetes_service_port_name": "testport",
}, },
}, },

View file

@ -694,9 +694,11 @@ service port.
Available meta labels: Available meta labels:
* `__meta_kubernetes_namespace`: The namespace of the service object. * `__meta_kubernetes_namespace`: The namespace of the service object.
* `__meta_kubernetes_service_name`: The name of the service object.
* `__meta_kubernetes_service_label_<labelname>`: The label of the service object.
* `__meta_kubernetes_service_annotation_<annotationname>`: The annotation of the service object. * `__meta_kubernetes_service_annotation_<annotationname>`: The annotation of the service object.
* `__meta_kubernetes_service_cluster_ip`: The cluster IP address of the service. (Does not apply to services of type ExternalName)
* `__meta_kubernetes_service_external_name`: The DNS name of the service. (Applies to services of type ExternalName)
* `__meta_kubernetes_service_label_<labelname>`: The label of the service object.
* `__meta_kubernetes_service_name`: The name of the service object.
* `__meta_kubernetes_service_port_name`: Name of the service port for the target. * `__meta_kubernetes_service_port_name`: Name of the service port for the target.
* `__meta_kubernetes_service_port_number`: Number of the service port for the target. * `__meta_kubernetes_service_port_number`: Number of the service port for the target.
* `__meta_kubernetes_service_port_protocol`: Protocol of the service port for the target. * `__meta_kubernetes_service_port_protocol`: Protocol of the service port for the target.