@@ -27,6 +27,7 @@ import (
27
27
"github.com/pkg/errors"
28
28
"k8s.io/api/core/v1"
29
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30
+ "k8s.io/apimachinery/pkg/util/intstr"
30
31
"k8s.io/client-go/kubernetes"
31
32
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
32
33
"k8s.io/client-go/kubernetes/typed/core/v1/fake"
@@ -36,12 +37,14 @@ import (
36
37
)
37
38
38
39
type MockClientGetter struct {
39
- servicesMap map [string ]corev1.ServiceInterface
40
+ servicesMap map [string ]corev1.ServiceInterface
41
+ endpointsMap map [string ]corev1.EndpointsInterface
40
42
}
41
43
42
44
func (m * MockClientGetter ) GetCoreClient () (corev1.CoreV1Interface , error ) {
43
45
return & MockCoreClient {
44
- servicesMap : m .servicesMap ,
46
+ servicesMap : m .servicesMap ,
47
+ endpointsMap : m .endpointsMap ,
45
48
}, nil
46
49
}
47
50
@@ -51,7 +54,8 @@ func (m *MockClientGetter) GetClientset(timeout time.Duration) (*kubernetes.Clie
51
54
52
55
type MockCoreClient struct {
53
56
fake.FakeCoreV1
54
- servicesMap map [string ]corev1.ServiceInterface
57
+ servicesMap map [string ]corev1.ServiceInterface
58
+ endpointsMap map [string ]corev1.EndpointsInterface
55
59
}
56
60
57
61
var serviceNamespaces = map [string ]corev1.ServiceInterface {
@@ -68,8 +72,18 @@ var defaultNamespaceServiceInterface = &MockServiceInterface{
68
72
},
69
73
Spec : v1.ServiceSpec {
70
74
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
+ },
73
87
},
74
88
},
75
89
},
@@ -86,8 +100,14 @@ var defaultNamespaceServiceInterface = &MockServiceInterface{
86
100
},
87
101
}
88
102
103
+ var endpointNamespaces = map [string ]corev1.EndpointsInterface {
104
+ "default" : defaultNamespaceEndpointInterface ,
105
+ }
106
+
107
+ var defaultNamespaceEndpointInterface = & MockEndpointsInterface {}
108
+
89
109
func (m * MockCoreClient ) Endpoints (namespace string ) corev1.EndpointsInterface {
90
- return & MockEndpointsInterface {}
110
+ return m . endpointsMap [ namespace ]
91
111
}
92
112
93
113
func (m * MockCoreClient ) Services (namespace string ) corev1.ServiceInterface {
@@ -124,6 +144,22 @@ var endpointMap = map[string]*v1.Endpoints{
124
144
},
125
145
},
126
146
},
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
+ },
127
163
}
128
164
129
165
func (e MockEndpointsInterface ) Get (name string , _ metav1.GetOptions ) (* v1.Endpoints , error ) {
@@ -195,7 +231,8 @@ func TestGetServiceListFromServicesByLabel(t *testing.T) {
195
231
func TestPrintURLsForService (t * testing.T ) {
196
232
defaultTemplate := template .Must (template .New ("svc-template" ).Parse ("http://{{.IP}}:{{.Port}}" ))
197
233
client := & MockCoreClient {
198
- servicesMap : serviceNamespaces ,
234
+ servicesMap : serviceNamespaces ,
235
+ endpointsMap : endpointNamespaces ,
199
236
}
200
237
var tests = []struct {
201
238
description string
@@ -219,6 +256,13 @@ func TestPrintURLsForService(t *testing.T) {
219
256
tmpl : template .Must (template .New ("svc-arbitrary-template" ).Parse ("{{.IP}}:{{.Port}}" )),
220
257
expectedOutput : []string {"127.0.0.1:1111" , "127.0.0.1:2222" },
221
258
},
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
+ },
222
266
{
223
267
description : "empty slice for no node ports" ,
224
268
serviceName : "mock-dashboard-no-ports" ,
@@ -361,7 +405,8 @@ func TestGetServiceURLs(t *testing.T) {
361
405
t .Parallel ()
362
406
363
407
K8s = & MockClientGetter {
364
- servicesMap : serviceNamespaces ,
408
+ servicesMap : serviceNamespaces ,
409
+ endpointsMap : endpointNamespaces ,
365
410
}
366
411
urls , err := GetServiceURLs (test .api , test .namespace , defaultTemplate )
367
412
if err != nil && ! test .err {
@@ -428,7 +473,8 @@ func TestGetServiceURLsForService(t *testing.T) {
428
473
t .Run (test .description , func (t * testing.T ) {
429
474
t .Parallel ()
430
475
K8s = & MockClientGetter {
431
- servicesMap : serviceNamespaces ,
476
+ servicesMap : serviceNamespaces ,
477
+ endpointsMap : endpointNamespaces ,
432
478
}
433
479
urls , err := GetServiceURLsForService (test .api , test .namespace , test .service , defaultTemplate )
434
480
if err != nil && ! test .err {
0 commit comments