From dbfaeb4af36417858f1b8c8707cf38d727bffae7 Mon Sep 17 00:00:00 2001 From: Dominik Tomasi Date: Tue, 9 Feb 2021 15:47:17 +0100 Subject: [PATCH] Fix for KubeAPI Port assignment when using config files (#490, @dtomasi) --- cmd/cluster/clusterCreate.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/cmd/cluster/clusterCreate.go b/cmd/cluster/clusterCreate.go index b87cd57b..07d16f46 100644 --- a/cmd/cluster/clusterCreate.go +++ b/cmd/cluster/clusterCreate.go @@ -24,6 +24,7 @@ package cluster import ( "fmt" + "github.com/docker/go-connections/nat" "os" "runtime" "strings" @@ -350,25 +351,41 @@ func applyCLIOverrides(cfg conf.SimpleConfig) (conf.SimpleConfig, error) { // -> API-PORT // parse the port mapping - var exposeAPI *k3d.ExposureOpts + var ( + err error + exposeAPI *k3d.ExposureOpts + ) + + // Apply config file values as defaults + exposeAPI = &k3d.ExposureOpts{ + PortMapping: nat.PortMapping{ + Binding: nat.PortBinding{ + HostIP: cfg.ExposeAPI.HostIP, + HostPort: cfg.ExposeAPI.HostPort, + }, + }, + Host: cfg.ExposeAPI.Host, + } + + // Overwrite if cli arg is set if ppViper.IsSet("cli.api-port") { if cfg.ExposeAPI.HostPort != "" { log.Debugf("Overriding pre-defined kubeAPI Exposure Spec %+v with CLI argument %s", cfg.ExposeAPI, ppViper.GetString("cli.api-port")) } - var err error exposeAPI, err = cliutil.ParsePortExposureSpec(ppViper.GetString("cli.api-port"), k3d.DefaultAPIPort) if err != nil { return cfg, err } } - if exposeAPI == nil { - var err 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 } } + cfg.ExposeAPI = conf.SimpleExposureOpts{ Host: exposeAPI.Host, HostIP: exposeAPI.Binding.HostIP,