Skip to content

Commit 96e3a69

Browse files
fix: --format outputs any string, --https only subsitute http URL scheme
cmd/minikube service --format: outputs arbitrarily formatted strings --https: https only substituted when the URL scheme is http
1 parent 82e9914 commit 96e3a69

File tree

2 files changed

+86
-13
lines changed

2 files changed

+86
-13
lines changed

pkg/minikube/service/service.go

+22-13
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,7 @@ func printURLsForService(c corev1.CoreV1Interface, ip, service, namespace string
184184
return nil, err
185185
}
186186

187-
u, err := url.Parse(doc.String())
188-
if err != nil {
189-
return nil, err
190-
}
191-
192-
urls = append(urls, u.String())
187+
urls = append(urls, doc.String())
193188
}
194189
return urls, nil
195190
}
@@ -236,6 +231,21 @@ func checkEndpointReady(endpoints corev1.EndpointsInterface, service string) err
236231
return nil
237232
}
238233

234+
func OptionallyHttpsFormattedUrlString(bareUrlString string, https bool) (string, bool) {
235+
httpsFormattedString := bareUrlString
236+
isHttpSchemedURL := false
237+
238+
if u, parseErr := url.Parse(bareUrlString); parseErr == nil {
239+
isHttpSchemedURL = u.Scheme == "http"
240+
}
241+
242+
if isHttpSchemedURL && https {
243+
httpsFormattedString = strings.Replace(bareUrlString, "http", "https", 1)
244+
}
245+
246+
return httpsFormattedString, isHttpSchemedURL
247+
}
248+
239249
func WaitAndMaybeOpenService(api libmachine.API, namespace string, service string, urlTemplate *template.Template, urlMode bool, https bool,
240250
wait int, interval int) error {
241251
if err := util.RetryAfter(wait, func() error { return CheckService(namespace, service) }, time.Duration(interval)*time.Second); err != nil {
@@ -246,15 +256,14 @@ func WaitAndMaybeOpenService(api libmachine.API, namespace string, service strin
246256
if err != nil {
247257
return errors.Wrap(err, "Check that minikube is running and that you have specified the correct namespace")
248258
}
249-
for _, url := range urls {
250-
if https {
251-
url = strings.Replace(url, "http", "https", 1)
252-
}
253-
if urlMode || !strings.HasPrefix(url, "http") {
254-
fmt.Fprintln(os.Stdout, url)
259+
for _, bareUrlString := range urls {
260+
urlString, isHttpSchemedURL := OptionallyHttpsFormattedUrlString(bareUrlString, https)
261+
262+
if urlMode || !isHttpSchemedURL {
263+
fmt.Fprintln(os.Stdout, urlString)
255264
} else {
256265
fmt.Fprintln(os.Stderr, "Opening kubernetes service "+namespace+"/"+service+" in default browser...")
257-
browser.OpenURL(url)
266+
browser.OpenURL(urlString)
258267
}
259268
}
260269
return nil

pkg/minikube/service/service_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ func TestPrintURLsForService(t *testing.T) {
249249
tmpl: defaultTemplate,
250250
expectedOutput: []string{"http://127.0.0.1:1111", "http://127.0.0.1:2222"},
251251
},
252+
{
253+
description: "should get all node ports with arbitrary format",
254+
serviceName: "mock-dashboard",
255+
namespace: "default",
256+
tmpl: template.Must(template.New("svc-template").Parse("{{.IP}}:{{.Port}}")),
257+
expectedOutput: []string{"127.0.0.1:1111", "127.0.0.1:2222"},
258+
},
252259
{
253260
description: "empty slice for no node ports",
254261
serviceName: "mock-dashboard-no-ports",
@@ -279,6 +286,63 @@ func TestPrintURLsForService(t *testing.T) {
279286
}
280287
}
281288

289+
func TestOptionallyHttpsFormattedUrlString(t *testing.T) {
290+
291+
var tests = []struct {
292+
description string
293+
bareUrlString string
294+
https bool
295+
expectedHttpsFormattedUrlString string
296+
expectedIsHttpSchemedURL bool
297+
}{
298+
{
299+
description: "no https for http schemed with no https option",
300+
bareUrlString: "http://192.168.99.100:30563",
301+
https: false,
302+
expectedHttpsFormattedUrlString: "http://192.168.99.100:30563",
303+
expectedIsHttpSchemedURL: true,
304+
},
305+
{
306+
description: "no https for non-http schemed with no https option",
307+
bareUrlString: "xyz.http.myservice:30563",
308+
https: false,
309+
expectedHttpsFormattedUrlString: "xyz.http.myservice:30563",
310+
expectedIsHttpSchemedURL: false,
311+
},
312+
{
313+
description: "https for http schemed with https option",
314+
bareUrlString: "http://192.168.99.100:30563",
315+
https: true,
316+
expectedHttpsFormattedUrlString: "https://192.168.99.100:30563",
317+
expectedIsHttpSchemedURL: true,
318+
},
319+
{
320+
description: "no https for non-http schemed with https option and http substring",
321+
bareUrlString: "xyz.http.myservice:30563",
322+
https: true,
323+
expectedHttpsFormattedUrlString: "xyz.http.myservice:30563",
324+
expectedIsHttpSchemedURL: false,
325+
},
326+
}
327+
328+
for _, test := range tests {
329+
test := test
330+
t.Run(test.description, func(t *testing.T) {
331+
t.Parallel()
332+
httpsFormattedUrlString, isHttpSchemedURL := OptionallyHttpsFormattedUrlString(test.bareUrlString, test.https)
333+
334+
if httpsFormattedUrlString != test.expectedHttpsFormattedUrlString {
335+
t.Errorf("\nhttpsFormattedUrlString, Expected %v \nActual: %v \n\n", test.expectedHttpsFormattedUrlString, httpsFormattedUrlString)
336+
}
337+
338+
if isHttpSchemedURL != test.expectedIsHttpSchemedURL {
339+
t.Errorf("\nisHttpSchemedURL, Expected %v \nActual: %v \n\n",
340+
test.expectedHttpsFormattedUrlString, httpsFormattedUrlString)
341+
}
342+
})
343+
}
344+
}
345+
282346
func TestGetServiceURLs(t *testing.T) {
283347
defaultAPI := &tests.MockAPI{
284348
Hosts: map[string]*host.Host{

0 commit comments

Comments
 (0)