From 1080c0b1575b78eea24da1dea1f9e719030b3230 Mon Sep 17 00:00:00 2001 From: Andy Zhou Date: Sun, 2 Jun 2019 12:06:24 -0700 Subject: [PATCH] Fix up kubeconfig.yaml with --api-port host --- cli/cluster.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/cli/cluster.go b/cli/cluster.go index 96e544d0..aec873a1 100644 --- a/cli/cluster.go +++ b/cli/cluster.go @@ -144,16 +144,20 @@ func createKubeConfigFile(cluster string) error { // and trimming any NULL characters trimBytes := bytes.Trim(readBytes[512:], "\x00") - // If running on a docker machine, replace localhost with - // docker machine's IP - dockerMachineIp, err := getDockerMachineIp() - if err != nil { - return err - } - - if dockerMachineIp != "" { + // Fix up kubeconfig.yaml file. + // + // K3s generates the default kubeconfig.yaml with host name as 'localhost'. + // Change the host name to the name user specified via the --api-port argument. + // + // When user did not specify the host name and when we are running against a remote docker, + // set the host name to remote docker machine's IP address. + // + // Otherwise, the hostname remains as 'localhost' + apiHost := server[0].Labels["apihost"] + + if apiHost != "" { s := string(trimBytes) - s = strings.Replace(s, "localhost", dockerMachineIp, 1) + s = strings.Replace(s, "localhost", apiHost, 1) trimBytes = []byte(s) } _, err = kubeconfigfile.Write(trimBytes)