Skip to content

Commit 0f93bdd

Browse files
authoredApr 9, 2019
Merge pull request #4011 from arnaudjardine/addPortNameAttribute
Add port name to service struct used in minikube service
2 parents 1ed3900 + c57deb5 commit 0f93bdd

File tree

2 files changed

+80
-27
lines changed

2 files changed

+80
-27
lines changed
 

‎pkg/minikube/service/service.go

+25-18
Original file line numberDiff line numberDiff line change
@@ -170,29 +170,36 @@ func printURLsForService(c corev1.CoreV1Interface, ip, service, namespace string
170170
if err != nil {
171171
return nil, errors.Wrapf(err, "service '%s' could not be found running", service)
172172
}
173-
var nodePorts []int32
174-
if len(svc.Spec.Ports) > 0 {
175-
for _, port := range svc.Spec.Ports {
176-
if port.NodePort > 0 {
177-
nodePorts = append(nodePorts, port.NodePort)
173+
174+
e := c.Endpoints(namespace)
175+
endpoints, err := e.Get(service, metav1.GetOptions{})
176+
m := make(map[int32]string)
177+
if endpoints != nil && len(endpoints.Subsets) > 0 {
178+
for _, ept := range endpoints.Subsets {
179+
for _, p := range ept.Ports {
180+
m[int32(p.Port)] = p.Name
178181
}
179182
}
180183
}
184+
181185
urls := []string{}
182-
for _, port := range nodePorts {
183-
var doc bytes.Buffer
184-
err = t.Execute(&doc, struct {
185-
IP string
186-
Port int32
187-
}{
188-
ip,
189-
port,
190-
})
191-
if err != nil {
192-
return nil, err
186+
for _, port := range svc.Spec.Ports {
187+
if port.NodePort > 0 {
188+
var doc bytes.Buffer
189+
err = t.Execute(&doc, struct {
190+
IP string
191+
Port int32
192+
Name string
193+
}{
194+
ip,
195+
port.NodePort,
196+
m[port.TargetPort.IntVal],
197+
})
198+
if err != nil {
199+
return nil, err
200+
}
201+
urls = append(urls, doc.String())
193202
}
194-
195-
urls = append(urls, doc.String())
196203
}
197204
return urls, nil
198205
}

‎pkg/minikube/service/service_test.go

+55-9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/pkg/errors"
2828
"k8s.io/api/core/v1"
2929
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30+
"k8s.io/apimachinery/pkg/util/intstr"
3031
"k8s.io/client-go/kubernetes"
3132
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
3233
"k8s.io/client-go/kubernetes/typed/core/v1/fake"
@@ -36,12 +37,14 @@ import (
3637
)
3738

3839
type MockClientGetter struct {
39-
servicesMap map[string]corev1.ServiceInterface
40+
servicesMap map[string]corev1.ServiceInterface
41+
endpointsMap map[string]corev1.EndpointsInterface
4042
}
4143

4244
func (m *MockClientGetter) GetCoreClient() (corev1.CoreV1Interface, error) {
4345
return &MockCoreClient{
44-
servicesMap: m.servicesMap,
46+
servicesMap: m.servicesMap,
47+
endpointsMap: m.endpointsMap,
4548
}, nil
4649
}
4750

@@ -51,7 +54,8 @@ func (m *MockClientGetter) GetClientset(timeout time.Duration) (*kubernetes.Clie
5154

5255
type MockCoreClient struct {
5356
fake.FakeCoreV1
54-
servicesMap map[string]corev1.ServiceInterface
57+
servicesMap map[string]corev1.ServiceInterface
58+
endpointsMap map[string]corev1.EndpointsInterface
5559
}
5660

5761
var serviceNamespaces = map[string]corev1.ServiceInterface{
@@ -68,8 +72,18 @@ var defaultNamespaceServiceInterface = &MockServiceInterface{
6872
},
6973
Spec: v1.ServiceSpec{
7074
Ports: []v1.ServicePort{
71-
{NodePort: int32(1111)},
72-
{NodePort: int32(2222)},
75+
{
76+
NodePort: int32(1111),
77+
TargetPort: intstr.IntOrString{
78+
IntVal: int32(11111),
79+
},
80+
},
81+
{
82+
NodePort: int32(2222),
83+
TargetPort: intstr.IntOrString{
84+
IntVal: int32(22222),
85+
},
86+
},
7387
},
7488
},
7589
},
@@ -86,8 +100,14 @@ var defaultNamespaceServiceInterface = &MockServiceInterface{
86100
},
87101
}
88102

103+
var endpointNamespaces = map[string]corev1.EndpointsInterface{
104+
"default": defaultNamespaceEndpointInterface,
105+
}
106+
107+
var defaultNamespaceEndpointInterface = &MockEndpointsInterface{}
108+
89109
func (m *MockCoreClient) Endpoints(namespace string) corev1.EndpointsInterface {
90-
return &MockEndpointsInterface{}
110+
return m.endpointsMap[namespace]
91111
}
92112

93113
func (m *MockCoreClient) Services(namespace string) corev1.ServiceInterface {
@@ -124,6 +144,22 @@ var endpointMap = map[string]*v1.Endpoints{
124144
},
125145
},
126146
},
147+
"mock-dashboard": {
148+
Subsets: []v1.EndpointSubset{
149+
{
150+
Ports: []v1.EndpointPort{
151+
{
152+
Name: "port1",
153+
Port: int32(11111),
154+
},
155+
{
156+
Name: "port2",
157+
Port: int32(22222),
158+
},
159+
},
160+
},
161+
},
162+
},
127163
}
128164

129165
func (e MockEndpointsInterface) Get(name string, _ metav1.GetOptions) (*v1.Endpoints, error) {
@@ -195,7 +231,8 @@ func TestGetServiceListFromServicesByLabel(t *testing.T) {
195231
func TestPrintURLsForService(t *testing.T) {
196232
defaultTemplate := template.Must(template.New("svc-template").Parse("http://{{.IP}}:{{.Port}}"))
197233
client := &MockCoreClient{
198-
servicesMap: serviceNamespaces,
234+
servicesMap: serviceNamespaces,
235+
endpointsMap: endpointNamespaces,
199236
}
200237
var tests = []struct {
201238
description string
@@ -219,6 +256,13 @@ func TestPrintURLsForService(t *testing.T) {
219256
tmpl: template.Must(template.New("svc-arbitrary-template").Parse("{{.IP}}:{{.Port}}")),
220257
expectedOutput: []string{"127.0.0.1:1111", "127.0.0.1:2222"},
221258
},
259+
{
260+
description: "should get the name of all target ports with arbitrary format",
261+
serviceName: "mock-dashboard",
262+
namespace: "default",
263+
tmpl: template.Must(template.New("svc-arbitrary-template").Parse("{{.Name}}={{.IP}}:{{.Port}}")),
264+
expectedOutput: []string{"port1=127.0.0.1:1111", "port2=127.0.0.1:2222"},
265+
},
222266
{
223267
description: "empty slice for no node ports",
224268
serviceName: "mock-dashboard-no-ports",
@@ -361,7 +405,8 @@ func TestGetServiceURLs(t *testing.T) {
361405
t.Parallel()
362406

363407
K8s = &MockClientGetter{
364-
servicesMap: serviceNamespaces,
408+
servicesMap: serviceNamespaces,
409+
endpointsMap: endpointNamespaces,
365410
}
366411
urls, err := GetServiceURLs(test.api, test.namespace, defaultTemplate)
367412
if err != nil && !test.err {
@@ -428,7 +473,8 @@ func TestGetServiceURLsForService(t *testing.T) {
428473
t.Run(test.description, func(t *testing.T) {
429474
t.Parallel()
430475
K8s = &MockClientGetter{
431-
servicesMap: serviceNamespaces,
476+
servicesMap: serviceNamespaces,
477+
endpointsMap: endpointNamespaces,
432478
}
433479
urls, err := GetServiceURLsForService(test.api, test.namespace, test.service, defaultTemplate)
434480
if err != nil && !test.err {

0 commit comments

Comments
 (0)
Please sign in to comment.