@@ -97,17 +97,20 @@ func (*K8sClientGetter) GetClientset(timeout time.Duration) (*kubernetes.Clients
97
97
return client , nil
98
98
}
99
99
100
- // URL represents service URL
101
- type URL struct {
100
+ // SvcURL represents a service URL. Each item in the URLs field combines the service URL with one of the configured
101
+ // node ports. The PortNames field contains the configured names of the ports in the URLs field (sorted correspondingly -
102
+ // first item in PortNames belongs to the first item in URLs).
103
+ type SvcURL struct {
102
104
Namespace string
103
105
Name string
104
106
URLs []string
107
+ PortNames []string
105
108
}
106
109
107
110
// URLs represents a list of URL
108
- type URLs []URL
111
+ type URLs []SvcURL
109
112
110
- // GetServiceURLs returns all the node port URLs for every service in a particular namespace
113
+ // GetServiceURLs returns a SvcUrl object for every service in a particular namespace.
111
114
// Accepts a template for formatting
112
115
func GetServiceURLs (api libmachine.API , namespace string , t * template.Template ) (URLs , error ) {
113
116
host , err := cluster .CheckIfHostExistsAndLoad (api , config .GetMachineName ())
@@ -132,52 +135,49 @@ func GetServiceURLs(api libmachine.API, namespace string, t *template.Template)
132
135
return nil , err
133
136
}
134
137
135
- var serviceURLs []URL
138
+ var serviceURLs []SvcURL
136
139
for _ , svc := range svcs .Items {
137
- urls , err := printURLsForService (client , ip , svc .Name , svc .Namespace , t )
140
+ svcURL , err := printURLsForService (client , ip , svc .Name , svc .Namespace , t )
138
141
if err != nil {
139
142
return nil , err
140
143
}
141
- serviceURLs = append (serviceURLs , URL { Namespace : svc . Namespace , Name : svc . Name , URLs : urls } )
144
+ serviceURLs = append (serviceURLs , svcURL )
142
145
}
143
146
144
147
return serviceURLs , nil
145
148
}
146
149
147
- // GetServiceURLsForService returns all the node ports for a service in a namespace
148
- // with optional formatting
149
- func GetServiceURLsForService (api libmachine.API , namespace , service string , t * template.Template ) ([]string , error ) {
150
+ // GetServiceURLsForService returns a SvcUrl object for a service in a namespace. Supports optional formatting.
151
+ func GetServiceURLsForService (api libmachine.API , namespace , service string , t * template.Template ) (SvcURL , error ) {
150
152
host , err := cluster .CheckIfHostExistsAndLoad (api , config .GetMachineName ())
151
153
if err != nil {
152
- return nil , errors .Wrap (err , "Error checking if api exist and loading it" )
154
+ return SvcURL {} , errors .Wrap (err , "Error checking if api exist and loading it" )
153
155
}
154
156
155
157
ip , err := host .Driver .GetIP ()
156
158
if err != nil {
157
- return nil , errors .Wrap (err , "Error getting ip from host" )
159
+ return SvcURL {} , errors .Wrap (err , "Error getting ip from host" )
158
160
}
159
161
160
162
client , err := K8s .GetCoreClient ()
161
163
if err != nil {
162
- return nil , err
164
+ return SvcURL {} , err
163
165
}
164
166
165
167
return printURLsForService (client , ip , service , namespace , t )
166
168
}
167
169
168
- func printURLsForService (c typed_core.CoreV1Interface , ip , service , namespace string , t * template.Template ) ([] string , error ) {
170
+ func printURLsForService (c typed_core.CoreV1Interface , ip , service , namespace string , t * template.Template ) (SvcURL , error ) {
169
171
if t == nil {
170
- return nil , errors .New ("Error, attempted to generate service url with nil --format template" )
172
+ return SvcURL {} , errors .New ("Error, attempted to generate service url with nil --format template" )
171
173
}
172
174
173
- s := c .Services (namespace )
174
- svc , err := s .Get (service , meta.GetOptions {})
175
+ svc , err := c .Services (namespace ).Get (service , meta.GetOptions {})
175
176
if err != nil {
176
- return nil , errors .Wrapf (err , "service '%s' could not be found running" , service )
177
+ return SvcURL {} , errors .Wrapf (err , "service '%s' could not be found running" , service )
177
178
}
178
179
179
- e := c .Endpoints (namespace )
180
- endpoints , err := e .Get (service , meta.GetOptions {})
180
+ endpoints , err := c .Endpoints (namespace ).Get (service , meta.GetOptions {})
181
181
m := make (map [int32 ]string )
182
182
if err == nil && endpoints != nil && len (endpoints .Subsets ) > 0 {
183
183
for _ , ept := range endpoints .Subsets {
@@ -188,6 +188,7 @@ func printURLsForService(c typed_core.CoreV1Interface, ip, service, namespace st
188
188
}
189
189
190
190
urls := []string {}
191
+ portNames := []string {}
191
192
for _ , port := range svc .Spec .Ports {
192
193
if port .NodePort > 0 {
193
194
var doc bytes.Buffer
@@ -201,12 +202,13 @@ func printURLsForService(c typed_core.CoreV1Interface, ip, service, namespace st
201
202
m [port .TargetPort .IntVal ],
202
203
})
203
204
if err != nil {
204
- return nil , err
205
+ return SvcURL {} , err
205
206
}
206
207
urls = append (urls , doc .String ())
208
+ portNames = append (portNames , m [port .TargetPort .IntVal ])
207
209
}
208
210
}
209
- return urls , nil
211
+ return SvcURL { Namespace : svc . Namespace , Name : svc . Name , URLs : urls , PortNames : portNames } , nil
210
212
}
211
213
212
214
// CheckService checks if a service is listening on a port.
@@ -249,7 +251,7 @@ func OptionallyHTTPSFormattedURLString(bareURLString string, https bool) (string
249
251
// "Namespace", "Name" and "URL" columns to a writer
250
252
func PrintServiceList (writer io.Writer , data [][]string ) {
251
253
table := tablewriter .NewWriter (writer )
252
- table .SetHeader ([]string {"Namespace" , "Name" , "URL" })
254
+ table .SetHeader ([]string {"Namespace" , "Name" , "Port" , " URL" })
253
255
table .SetBorders (tablewriter.Border {Left : true , Top : true , Right : true , Bottom : true })
254
256
table .SetCenterSeparator ("|" )
255
257
table .AppendBulk (data )
@@ -269,27 +271,27 @@ func WaitAndMaybeOpenService(api libmachine.API, namespace string, service strin
269
271
return errors .Wrapf (err , "Could not find finalized endpoint being pointed to by %s" , service )
270
272
}
271
273
272
- urls , err := GetServiceURLsForService (api , namespace , service , urlTemplate )
274
+ serviceURL , err := GetServiceURLsForService (api , namespace , service , urlTemplate )
273
275
if err != nil {
274
276
return errors .Wrap (err , "Check that minikube is running and that you have specified the correct namespace" )
275
277
}
276
278
277
279
if ! urlMode {
278
280
var data [][]string
279
- if len (urls ) == 0 {
280
- data = append (data , []string {namespace , service , "No node port" })
281
+ if len (serviceURL . URLs ) == 0 {
282
+ data = append (data , []string {namespace , service , "" , " No node port" })
281
283
} else {
282
- data = append (data , []string {namespace , service , strings .Join (urls , "\n " )})
284
+ data = append (data , []string {namespace , service , strings .Join (serviceURL . PortNames , " \n " ), strings . Join ( serviceURL . URLs , "\n " )})
283
285
}
284
286
PrintServiceList (os .Stdout , data )
285
287
}
286
288
287
- if len (urls ) == 0 {
289
+ if len (serviceURL . URLs ) == 0 {
288
290
out .T (out .Sad , "service {{.namespace_name}}/{{.service_name}} has no node port" , out.V {"namespace_name" : namespace , "service_name" : service })
289
291
return nil
290
292
}
291
293
292
- for _ , bareURLString := range urls {
294
+ for _ , bareURLString := range serviceURL . URLs {
293
295
urlString , isHTTPSchemedURL := OptionallyHTTPSFormattedURLString (bareURLString , https )
294
296
295
297
if urlMode || ! isHTTPSchemedURL {
0 commit comments