@@ -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"
@@ -103,9 +104,8 @@ var serviceCmd = &cobra.Command{
103
104
}
104
105
105
106
var data [][]string
106
- var openUrls []string
107
107
for _ , svc := range services {
108
- openUrls , err := service .WaitForService (co .API , co .Config .Name , namespace , svc .Name , serviceURLTemplate , true , https , wait , interval )
108
+ openUrls , err := service .WaitForService (co .API , co .Config .Name , namespace , svc .Name , serviceURLTemplate , serviceURLMode , https , wait , interval )
109
109
110
110
if err != nil {
111
111
var s * service.SVCNotFoundError
@@ -128,35 +128,21 @@ You may select another namespace by using 'minikube service {{.service}} -n <nam
128
128
}
129
129
130
130
data = append (data , []string {svc .Namespace , svc .Name , servicePortNames , serviceURLs })
131
- }
132
- }
133
131
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 ]))
132
+ if serviceURLMode && ! driver .NeedsPortForward (co .Config .Driver ) {
133
+ out .String (fmt .Sprintf ("%s\n " , serviceURLs ))
134
+ }
139
135
}
140
136
}
141
137
142
138
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 )
139
+ startKicServiceTunnel (services , cname , co .Config .Driver )
140
+ } else if ! serviceURLMode {
141
+ openURLs (data )
149
142
}
150
143
},
151
144
}
152
145
153
- func shouldOpen (args []string ) bool {
154
- if ! serviceURLMode && ! all && len (args ) == 1 {
155
- return true
156
- }
157
- return false
158
- }
159
-
160
146
func init () {
161
147
serviceCmd .Flags ().StringVarP (& namespace , "namespace" , "n" , "default" , "The service namespace" )
162
148
serviceCmd .Flags ().BoolVar (& serviceURLMode , "url" , false , "Display the Kubernetes service URL in the CLI instead of opening it in the default browser" )
@@ -168,7 +154,7 @@ func init() {
168
154
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
155
}
170
156
171
- func startKicServiceTunnel (args [] string , services service.URLs , configName , driverName string ) {
157
+ func startKicServiceTunnel (services service.URLs , configName , driverName string ) {
172
158
ctrlC := make (chan os.Signal , 1 )
173
159
signal .Notify (ctrlC , os .Interrupt )
174
160
@@ -186,35 +172,72 @@ func startKicServiceTunnel(args []string, services service.URLs, configName, dri
186
172
sshPort := strconv .Itoa (port )
187
173
sshKey := filepath .Join (localpath .MiniPath (), "machines" , configName , "id_rsa" )
188
174
189
- serviceTunnel := kic .NewServiceTunnel (sshPort , sshKey , clientset .CoreV1 ())
175
+ serviceTunnel := kic .NewServiceTunnel (sshPort , sshKey , clientset .CoreV1 (), serviceURLMode )
190
176
urls , err := serviceTunnel .Start (svc .Name , namespace )
177
+
178
+ //mutate response urls to HTTPS if needed
179
+ urls = mutateURLs (svc .Name , urls )
180
+
191
181
if err != nil {
192
182
exit .Error (reason .SvcTunnelStart , "error starting tunnel" , err )
193
183
}
194
184
defer serviceTunnel .Stop ()
185
+ svc .URLs = urls
195
186
data = append (data , []string {namespace , svc .Name , "" , strings .Join (urls , "\n " )})
196
187
}
197
188
198
189
time .Sleep (1 * time .Second )
199
190
200
- if ! serviceURLMode && serviceURLFormat != defaultServiceFormatTemplate && ! all {
191
+ if ! serviceURLMode {
201
192
service .PrintServiceList (os .Stdout , data )
193
+ } else {
194
+ for _ , row := range data {
195
+ out .String (fmt .Sprintf ("%s\n " , row [3 ]))
196
+ }
202
197
}
203
198
204
- if shouldOpen ( args ) {
205
- openURLs (services [ 0 ]. Name , services [ 0 ]. URLs )
199
+ if ! serviceURLMode {
200
+ openURLs (data )
206
201
}
207
202
208
203
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
204
210
205
<- ctrlC
211
206
}
212
207
213
- func openURLs (svc string , urls []string ) {
208
+ func mutateURLs (serviceName string , urls []string ) []string {
209
+ formattedUrls := make ([]string , 0 )
210
+ for _ , rawUrl := range urls {
211
+ var doc bytes.Buffer
212
+ parsedUrl , err := url .Parse (rawUrl )
213
+ if err != nil {
214
+ exit .Error (reason .SvcTunnelStart , "No valid URL found for tunnel." , err )
215
+ }
216
+ port , err := strconv .Atoi (parsedUrl .Port ())
217
+ if err != nil {
218
+ exit .Error (reason .SvcTunnelStart , "No valid port found for tunnel." , err )
219
+ }
220
+ err = serviceURLTemplate .Execute (& doc , struct {
221
+ IP string
222
+ Port int32
223
+ Name string
224
+ }{
225
+ parsedUrl .Hostname (),
226
+ int32 (port ),
227
+ serviceName ,
228
+ })
229
+ httpsUrl , _ := service .OptionallyHTTPSFormattedURLString (doc .String (), https )
230
+ formattedUrls = append (formattedUrls , httpsUrl )
231
+ }
232
+
233
+ return formattedUrls
234
+ }
235
+
236
+ func openURLs (urls [][]string ) {
214
237
for _ , u := range urls {
215
- _ , err := url .Parse (u )
238
+ _ , err := url .Parse (u [ 3 ] )
216
239
if err != nil {
217
- klog .Warningf ("failed to parse url %q: %v (will not open)" , u , err )
240
+ klog .Warningf ("failed to parse url %q: %v (will not open)" , u [ 3 ] , err )
218
241
out .String (fmt .Sprintf ("%s\n " , u ))
219
242
continue
220
243
}
@@ -224,8 +247,8 @@ func openURLs(svc string, urls []string) {
224
247
continue
225
248
}
226
249
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 {
250
+ out .Styled (style .Celebrate , "Opening service {{.namespace_name}}/{{.service_name}} in default browser..." , out.V {"namespace_name" : namespace , "service_name" : u [ 1 ] })
251
+ if err := browser .OpenURL (u [ 3 ] ); err != nil {
229
252
exit .Error (reason .HostBrowser , fmt .Sprintf ("open url failed: %s" , u ), err )
230
253
}
231
254
}
0 commit comments