@@ -17,6 +17,7 @@ limitations under the License.
17
17
package cmd
18
18
19
19
import (
20
+ "bytes"
20
21
"errors"
21
22
"fmt"
22
23
"net/url"
@@ -102,10 +103,14 @@ var serviceCmd = &cobra.Command{
102
103
services = newServices
103
104
}
104
105
106
+ if services == nil || len (services ) == 0 {
107
+ exit .Message (reason .SvcNotFound , `Service '{{.service}}' was not found in '{{.namespace}}' namespace.
108
+ You may select another namespace by using 'minikube service {{.service}} -n <namespace>'. Or list out all the services using 'minikube service list'` , out.V {"service" : args [0 ], "namespace" : namespace })
109
+ }
110
+
105
111
var data [][]string
106
- var openUrls []string
107
112
for _ , svc := range services {
108
- openUrls , err := service .WaitForService (co .API , co .Config .Name , namespace , svc .Name , serviceURLTemplate , true , https , wait , interval )
113
+ openUrls , err := service .WaitForService (co .API , co .Config .Name , namespace , svc .Name , serviceURLTemplate , serviceURLMode , https , wait , interval )
109
114
110
115
if err != nil {
111
116
var s * service.SVCNotFoundError
@@ -128,35 +133,21 @@ You may select another namespace by using 'minikube service {{.service}} -n <nam
128
133
}
129
134
130
135
data = append (data , []string {svc .Namespace , svc .Name , servicePortNames , serviceURLs })
131
- }
132
- }
133
136
134
- if (! serviceURLMode && serviceURLFormat != defaultServiceFormatTemplate && ! all ) || all {
135
- service .PrintServiceList (os .Stdout , data )
136
- } else if serviceURLMode && ! all {
137
- for _ , u := range data {
138
- out .String (fmt .Sprintf ("%s\n " , u [3 ]))
137
+ if serviceURLMode && ! driver .NeedsPortForward (co .Config .Driver ) {
138
+ out .String (fmt .Sprintf ("%s\n " , serviceURLs ))
139
+ }
139
140
}
140
141
}
141
142
142
- if driver .NeedsPortForward (co .Config .Driver ) {
143
- startKicServiceTunnel (args , services , cname , co .Config .Driver )
144
- return
145
- }
146
-
147
- if ! serviceURLMode && ! all && len (args ) == 1 {
148
- openURLs (args [0 ], openUrls )
143
+ if driver .NeedsPortForward (co .Config .Driver ) && services != nil {
144
+ startKicServiceTunnel (services , cname , co .Config .Driver )
145
+ } else if ! serviceURLMode {
146
+ openURLs (data )
149
147
}
150
148
},
151
149
}
152
150
153
- func shouldOpen (args []string ) bool {
154
- if ! serviceURLMode && ! all && len (args ) == 1 {
155
- return true
156
- }
157
- return false
158
- }
159
-
160
151
func init () {
161
152
serviceCmd .Flags ().StringVarP (& namespace , "namespace" , "n" , "default" , "The service namespace" )
162
153
serviceCmd .Flags ().BoolVar (& serviceURLMode , "url" , false , "Display the Kubernetes service URL in the CLI instead of opening it in the default browser" )
@@ -168,7 +159,7 @@ func init() {
168
159
serviceCmd .PersistentFlags ().StringVar (& serviceURLFormat , "format" , defaultServiceFormatTemplate , "Format to output service URL in. This format will be applied to each url individually and they will be printed one at a time." )
169
160
}
170
161
171
- func startKicServiceTunnel (args [] string , services service.URLs , configName , driverName string ) {
162
+ func startKicServiceTunnel (services service.URLs , configName , driverName string ) {
172
163
ctrlC := make (chan os.Signal , 1 )
173
164
signal .Notify (ctrlC , os .Interrupt )
174
165
@@ -186,35 +177,81 @@ func startKicServiceTunnel(args []string, services service.URLs, configName, dri
186
177
sshPort := strconv .Itoa (port )
187
178
sshKey := filepath .Join (localpath .MiniPath (), "machines" , configName , "id_rsa" )
188
179
189
- serviceTunnel := kic .NewServiceTunnel (sshPort , sshKey , clientset .CoreV1 ())
180
+ serviceTunnel := kic .NewServiceTunnel (sshPort , sshKey , clientset .CoreV1 (), serviceURLMode )
190
181
urls , err := serviceTunnel .Start (svc .Name , namespace )
182
+
191
183
if err != nil {
192
184
exit .Error (reason .SvcTunnelStart , "error starting tunnel" , err )
193
185
}
186
+ // mutate response urls to HTTPS if needed
187
+ urls , err = mutateURLs (svc .Name , urls )
188
+
189
+ if err != nil {
190
+ exit .Error (reason .SvcTunnelStart , "error creatings urls" , err )
191
+ }
192
+
194
193
defer serviceTunnel .Stop ()
194
+ svc .URLs = urls
195
195
data = append (data , []string {namespace , svc .Name , "" , strings .Join (urls , "\n " )})
196
196
}
197
197
198
198
time .Sleep (1 * time .Second )
199
199
200
- if ! serviceURLMode && serviceURLFormat != defaultServiceFormatTemplate && ! all {
200
+ if ! serviceURLMode {
201
201
service .PrintServiceList (os .Stdout , data )
202
+ } else {
203
+ for _ , row := range data {
204
+ out .String (fmt .Sprintf ("%s\n " , row [3 ]))
205
+ }
202
206
}
203
207
204
- if shouldOpen ( args ) {
205
- openURLs (services [ 0 ]. Name , services [ 0 ]. URLs )
208
+ if ! serviceURLMode {
209
+ openURLs (data )
206
210
}
207
211
208
212
out .WarningT ("Because you are using a Docker driver on {{.operating_system}}, the terminal needs to be open to run it." , out.V {"operating_system" : runtime .GOOS })
209
213
210
214
<- ctrlC
211
215
}
212
216
213
- func openURLs (svc string , urls []string ) {
217
+ func mutateURLs (serviceName string , urls []string ) ([]string , error ) {
218
+ formattedUrls := make ([]string , 0 )
219
+ for _ , rawURL := range urls {
220
+ var doc bytes.Buffer
221
+ parsedURL , err := url .Parse (rawURL )
222
+ if err != nil {
223
+ exit .Error (reason .SvcTunnelStart , "No valid URL found for tunnel." , err )
224
+ }
225
+ port , err := strconv .Atoi (parsedURL .Port ())
226
+ if err != nil {
227
+ exit .Error (reason .SvcTunnelStart , "No valid port found for tunnel." , err )
228
+ }
229
+ err = serviceURLTemplate .Execute (& doc , struct {
230
+ IP string
231
+ Port int32
232
+ Name string
233
+ }{
234
+ parsedURL .Hostname (),
235
+ int32 (port ),
236
+ serviceName ,
237
+ })
238
+
239
+ if err != nil {
240
+ return nil , err
241
+ }
242
+
243
+ httpsURL , _ := service .OptionallyHTTPSFormattedURLString (doc .String (), https )
244
+ formattedUrls = append (formattedUrls , httpsURL )
245
+ }
246
+
247
+ return formattedUrls , nil
248
+ }
249
+
250
+ func openURLs (urls [][]string ) {
214
251
for _ , u := range urls {
215
- _ , err := url .Parse (u )
252
+ _ , err := url .Parse (u [ 3 ] )
216
253
if err != nil {
217
- klog .Warningf ("failed to parse url %q: %v (will not open)" , u , err )
254
+ klog .Warningf ("failed to parse url %q: %v (will not open)" , u [ 3 ] , err )
218
255
out .String (fmt .Sprintf ("%s\n " , u ))
219
256
continue
220
257
}
@@ -224,8 +261,8 @@ func openURLs(svc string, urls []string) {
224
261
continue
225
262
}
226
263
227
- out .Styled (style .Celebrate , "Opening service {{.namespace_name}}/{{.service_name}} in default browser..." , out.V {"namespace_name" : namespace , "service_name" : svc })
228
- if err := browser .OpenURL (u ); err != nil {
264
+ out .Styled (style .Celebrate , "Opening service {{.namespace_name}}/{{.service_name}} in default browser..." , out.V {"namespace_name" : namespace , "service_name" : u [ 1 ] })
265
+ if err := browser .OpenURL (u [ 3 ] ); err != nil {
229
266
exit .Error (reason .HostBrowser , fmt .Sprintf ("open url failed: %s" , u ), err )
230
267
}
231
268
}
0 commit comments