Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

fix issue with kubeconfig not being fetched correctly from envvar #403

Merged
merged 1 commit into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions internal/commands/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
)

func newOctantCmd(version string) *cobra.Command {
var kubeConfig string
var verboseLevel int

octantCmd := &cobra.Command{
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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
}
Expand Down
35 changes: 35 additions & 0 deletions internal/commands/dash_test.go
Original file line number Diff line number Diff line change
@@ -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)
}