From da2f5fae71711ba3390047a8b6d40922fb89e53c Mon Sep 17 00:00:00 2001 From: Wayne Witzel III Date: Mon, 4 Nov 2019 20:00:28 -0500 Subject: [PATCH] ensure bindViper is called before fetching kubeconfig Signed-off-by: Wayne Witzel III --- internal/commands/dash.go | 14 ++++++-------- internal/commands/dash_test.go | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 internal/commands/dash_test.go diff --git a/internal/commands/dash.go b/internal/commands/dash.go index 5a4dc936e7..11378e2404 100644 --- a/internal/commands/dash.go +++ b/internal/commands/dash.go @@ -26,7 +26,6 @@ import ( ) func newOctantCmd(version string) *cobra.Command { - var kubeConfig string var verboseLevel int octantCmd := &cobra.Command{ @@ -69,11 +68,15 @@ func newOctantCmd(version string) *cobra.Command { logger.Debugf("disable-open-browser: %s", viper.Get("disable-open-browser")) + if viper.GetString("kubeconfig") == "" { + viper.Set("kubeconfig", clientcmd.NewDefaultClientConfigLoadingRules().GetDefaultFilename()) + } + go func() { options := dash.Options{ DisableClusterOverview: viper.GetBool("disable-cluster-overview"), EnableOpenCensus: viper.GetBool("enable-opencensus"), - KubeConfig: kubeConfig, + KubeConfig: viper.GetString("kubeconfig"), Namespace: viper.GetString("namespace"), FrontendURL: viper.GetString("ui-url"), Context: viper.GetString("context"), @@ -134,12 +137,7 @@ func newOctantCmd(version string) *cobra.Command { octantCmd.Flags().BoolP("disable-cluster-overview", "", false, "disable cluster overview") octantCmd.Flags().BoolP("disable-open-browser", "", false, "disable automatic launching of the browser") - kubeConfig = viper.GetString("kubeconfig") - if kubeConfig == "" { - kubeConfig = clientcmd.NewDefaultClientConfigLoadingRules().GetDefaultFilename() - } - - octantCmd.Flags().StringVar(&kubeConfig, "kubeconfig", kubeConfig, "absolute path to kubeConfig file") + octantCmd.Flags().String("kubeconfig", "", "absolute path to kubeConfig file") return octantCmd } diff --git a/internal/commands/dash_test.go b/internal/commands/dash_test.go new file mode 100644 index 0000000000..faf42749fa --- /dev/null +++ b/internal/commands/dash_test.go @@ -0,0 +1,35 @@ +/* +Copyright (c) 2019 the Octant contributors. All Rights Reserved. +SPDX-License-Identifier: Apache-2.0 +*/ + +package commands + +import ( + "os" + "testing" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_bindViper_KUBECONFIG(t *testing.T) { + cmd := &cobra.Command{} + + expected := "/testdata/kubeconfig.yml" + os.Setenv("KUBECONFIG", expected) + defer os.Unsetenv("KUBECONFIG") + + // Before bindViper + actual := viper.GetString("kubeconfig") + assert.Equal(t, "", actual) + + err := bindViper(cmd) + require.NoError(t, err) + + // After bindViper + actual = viper.GetString("kubeconfig") + assert.Equal(t, expected, actual) +}