From d033650547fab1ed2f544ea5d9b63413bd1551cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Ole=C5=9B?= Date: Thu, 29 Jul 2021 23:19:13 +0200 Subject: [PATCH] Do not override hostIP when hostPort is missing Closes: #692 --- cmd/cluster/clusterCreate.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cmd/cluster/clusterCreate.go b/cmd/cluster/clusterCreate.go index f5fc4af52..7758b9284 100644 --- a/cmd/cluster/clusterCreate.go +++ b/cmd/cluster/clusterCreate.go @@ -26,6 +26,7 @@ import ( "fmt" "os" "runtime" + "strconv" "strings" "time" @@ -390,10 +391,15 @@ func applyCLIOverrides(cfg conf.SimpleConfig) (conf.SimpleConfig, error) { // Set to random port if port is empty string if len(exposeAPI.Binding.HostPort) == 0 { - exposeAPI, err = cliutil.ParsePortExposureSpec("random", k3d.DefaultAPIPort) - if err != nil { - return cfg, err + var freePort string + port, err := cliutil.GetFreePort() + freePort = strconv.Itoa(port) + if err != nil || port == 0 { + log.Warnf("Failed to get random free port: %+v", err) + log.Warnf("Falling back to internal port %s (may be blocked though)...", k3d.DefaultAPIPort) + freePort = k3d.DefaultAPIPort } + exposeAPI.Binding.HostPort = freePort } cfg.ExposeAPI = conf.SimpleExposureOpts{