From c6564801f37e80141b3edc4d609a96ea87bdb104 Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Mon, 4 May 2020 18:25:14 +0200 Subject: [PATCH] getKubeconfig: always return output filepath --- cmd/create/createCluster.go | 2 +- cmd/get/getKubeconfig.go | 2 +- pkg/cluster/kubeconfig.go | 16 ++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cmd/create/createCluster.go b/cmd/create/createCluster.go index 3cb04ae8..cf1acba8 100644 --- a/cmd/create/createCluster.go +++ b/cmd/create/createCluster.go @@ -87,7 +87,7 @@ func NewCmdCreateCluster() *cobra.Command { if updateKubeconfig { log.Debugf("Updating default kubeconfig with a new context for cluster %s", cluster.Name) - if err := k3dCluster.GetAndWriteKubeConfig(runtimes.SelectedRuntime, cluster, "", &k3dCluster.WriteKubeConfigOptions{UpdateExisting: true, OverwriteExisting: false, UpdateCurrentContext: false}); err != nil { + if _, err := k3dCluster.GetAndWriteKubeConfig(runtimes.SelectedRuntime, cluster, "", &k3dCluster.WriteKubeConfigOptions{UpdateExisting: true, OverwriteExisting: false, UpdateCurrentContext: false}); err != nil { log.Fatalln(err) } } diff --git a/cmd/get/getKubeconfig.go b/cmd/get/getKubeconfig.go index 028f5a75..29b191c3 100644 --- a/cmd/get/getKubeconfig.go +++ b/cmd/get/getKubeconfig.go @@ -77,7 +77,7 @@ func NewCmdGetKubeconfig() *cobra.Command { errorGettingKubeconfig := false for _, c := range clusters { log.Debugf("Getting kubeconfig for cluster '%s'", c.Name) - if err := cluster.GetAndWriteKubeConfig(runtimes.SelectedRuntime, c, getKubeconfigFlags.output, &writeKubeConfigOptions); err != nil { + if getKubeconfigFlags.output, err = cluster.GetAndWriteKubeConfig(runtimes.SelectedRuntime, c, getKubeconfigFlags.output, &writeKubeConfigOptions); err != nil { log.Errorln(err) errorGettingKubeconfig = true } diff --git a/pkg/cluster/kubeconfig.go b/pkg/cluster/kubeconfig.go index 9f736d27..5209f22f 100644 --- a/pkg/cluster/kubeconfig.go +++ b/pkg/cluster/kubeconfig.go @@ -47,25 +47,25 @@ type WriteKubeConfigOptions struct { // 1. fetches the KubeConfig from the first master node retrieved for a given cluster // 2. modifies it by updating some fields with cluster-specific information // 3. writes it to the specified output -func GetAndWriteKubeConfig(runtime runtimes.Runtime, cluster *k3d.Cluster, output string, writeKubeConfigOptions *WriteKubeConfigOptions) error { +func GetAndWriteKubeConfig(runtime runtimes.Runtime, cluster *k3d.Cluster, output string, writeKubeConfigOptions *WriteKubeConfigOptions) (string, error) { // get kubeconfig from cluster node kubeconfig, err := GetKubeconfig(runtime, cluster) if err != nil { - return err + return output, err } // empty output parameter = write to default if output == "" { output, err = GetDefaultKubeConfigPath() if err != nil { - return err + return output, err } } // simply write to the output, ignoring existing contents if writeKubeConfigOptions.OverwriteExisting || output == "-" { - return WriteKubeConfigToPath(kubeconfig, output) + return output, WriteKubeConfigToPath(kubeconfig, output) } // load config from existing file or fail if it has non-kubeconfig contents @@ -82,13 +82,13 @@ func GetAndWriteKubeConfig(runtime runtimes.Runtime, cluster *k3d.Cluster, outpu // create directory path if err := os.MkdirAll(path.Dir(output), 0755); err != nil { log.Errorf("Failed to create output directory '%s'", path.Dir(output)) - return err + return output, err } // create output file if _, err := os.Create(output); err != nil { log.Errorf("Failed to create output file '%s'", output) - return err + return output, err } // try again, but do not try to create the file this time @@ -96,13 +96,13 @@ func GetAndWriteKubeConfig(runtime runtimes.Runtime, cluster *k3d.Cluster, outpu continue } log.Errorf("Failed to open output file '%s' or it's not a KubeConfig", output) - return err + return output, err } break } // update existing kubeconfig, but error out if there are conflicting fields but we don't want to update them - return UpdateKubeConfig(kubeconfig, existingKubeConfig, output, writeKubeConfigOptions.UpdateExisting, writeKubeConfigOptions.UpdateCurrentContext) + return output, UpdateKubeConfig(kubeconfig, existingKubeConfig, output, writeKubeConfigOptions.UpdateExisting, writeKubeConfigOptions.UpdateCurrentContext) }