diff --git a/internal/backup/cmd/flags.go b/internal/backup/cmd/flags.go index f16d42fd..3271ab55 100644 --- a/internal/backup/cmd/flags.go +++ b/internal/backup/cmd/flags.go @@ -1,21 +1,18 @@ package backup import ( - "os" - "github.com/spf13/pflag" + + "github.com/deckhouse/deckhouse-cli/internal/utilk8s" ) func addPersistentFlags(flagSet *pflag.FlagSet) { - defaultKubeconfigPath := os.ExpandEnv("$HOME/.kube/config") - if p := os.Getenv("KUBECONFIG"); p != "" { - defaultKubeconfigPath = p - } + defaultKubeconfigPath := utilk8s.DefaultKubeconfigPath() flagSet.StringP( "kubeconfig", "k", defaultKubeconfigPath, - "KubeConfig of the cluster. (default is $KUBECONFIG when it is set, $HOME/.kube/config otherwise)", + "Path to kubeconfig file. (default is $KUBECONFIG when it is set, otherwise the default kubeconfig path for the current OS user)", ) flagSet.String("context", "", "The name of the kubeconfig context to use") diff --git a/internal/plugins/flags/flags.go b/internal/plugins/flags/flags.go index e9183825..6c26718e 100644 --- a/internal/plugins/flags/flags.go +++ b/internal/plugins/flags/flags.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/pflag" rppflags "github.com/deckhouse/deckhouse-cli/internal/rpp/flags" + "github.com/deckhouse/deckhouse-cli/internal/utilk8s" ) const ( @@ -61,11 +62,7 @@ func skipClusterChecksDefault() bool { } func defaultKubeconfigPath() string { - if v := os.Getenv("KUBECONFIG"); v != "" { - return v - } - - return os.ExpandEnv("$HOME/.kube/config") + return utilk8s.DefaultKubeconfigPath() } func AddFlags(flagSet *pflag.FlagSet) { diff --git a/internal/status/cmd/flags.go b/internal/status/cmd/flags.go index 9c16861d..5c9f3b09 100644 --- a/internal/status/cmd/flags.go +++ b/internal/status/cmd/flags.go @@ -17,21 +17,18 @@ limitations under the License. package status import ( - "os" - "github.com/spf13/pflag" + + "github.com/deckhouse/deckhouse-cli/internal/utilk8s" ) func addPersistentFlags(flagSet *pflag.FlagSet) { - defaultKubeconfigPath := os.ExpandEnv("$HOME/.kube/config") - if p := os.Getenv("KUBECONFIG"); p != "" { - defaultKubeconfigPath = p - } + defaultKubeconfigPath := utilk8s.DefaultKubeconfigPath() flagSet.StringP( "kubeconfig", "k", defaultKubeconfigPath, - "KubeConfig of the cluster. (default is $KUBECONFIG when it is set, $HOME/.kube/config otherwise)", + "Path to kubeconfig file. (default is $KUBECONFIG when it is set, otherwise the default kubeconfig path for the current OS user)", ) flagSet.String("context", "", "The name of the kubeconfig context to use") diff --git a/internal/system/flags/flags.go b/internal/system/flags/flags.go index fbf7e9d0..f507ab2a 100644 --- a/internal/system/flags/flags.go +++ b/internal/system/flags/flags.go @@ -17,22 +17,19 @@ limitations under the License. package flags import ( - "os" - "github.com/spf13/cobra" + + "github.com/deckhouse/deckhouse-cli/internal/utilk8s" ) func AddPersistentFlags(cmd *cobra.Command) { - defaultKubeconfigPath := os.ExpandEnv("$HOME/.kube/config") - if p := os.Getenv("KUBECONFIG"); p != "" { - defaultKubeconfigPath = p - } + defaultKubeconfigPath := utilk8s.DefaultKubeconfigPath() cmd.PersistentFlags().StringP( "kubeconfig", "k", defaultKubeconfigPath, - "KubeConfig of the cluster. (default is $KUBECONFIG when it is set, $HOME/.kube/config otherwise)", + "Path to kubeconfig file. (default is $KUBECONFIG when it is set, otherwise the default kubeconfig path for the current OS user)", ) cmd.PersistentFlags().String("context", "", "The name of the kubeconfig context to use") diff --git a/internal/tools/sigmigrate/cmd/flags.go b/internal/tools/sigmigrate/cmd/flags.go index 80acd177..e607dc1e 100644 --- a/internal/tools/sigmigrate/cmd/flags.go +++ b/internal/tools/sigmigrate/cmd/flags.go @@ -17,11 +17,10 @@ limitations under the License. package cmd import ( - "os" - "github.com/spf13/pflag" "github.com/deckhouse/deckhouse-cli/internal/tools/sigmigrate" + "github.com/deckhouse/deckhouse-cli/internal/utilk8s" ) const ( @@ -47,15 +46,12 @@ func addFlags(flags *pflag.FlagSet) { "Set the log level (INFO, DEBUG, TRACE). Defaults to DEBUG.", ) - defaultKubeconfigPath := os.ExpandEnv("$HOME/.kube/config") - if p := os.Getenv("KUBECONFIG"); p != "" { - defaultKubeconfigPath = p - } + defaultKubeconfigPath := utilk8s.DefaultKubeconfigPath() flags.String( "kubeconfig", defaultKubeconfigPath, - "Path to the kubeconfig file to use for CLI requests. (default is $KUBECONFIG when it is set, $HOME/.kube/config otherwise)", + "Path to the kubeconfig file to use for CLI requests. (default is $KUBECONFIG when it is set, otherwise the default kubeconfig path for the current OS user)", ) flags.String( diff --git a/internal/utilk8s/clientset.go b/internal/utilk8s/clientset.go index 2c9024f1..420333e0 100644 --- a/internal/utilk8s/clientset.go +++ b/internal/utilk8s/clientset.go @@ -2,6 +2,7 @@ package utilk8s import ( "fmt" + "os" "path/filepath" "k8s.io/client-go/kubernetes" @@ -11,6 +12,18 @@ import ( const DefaultKubeContext = "" +// DefaultKubeconfigPath returns the default path to the kubeconfig file. +// It respects the "KUBECONFIG" environment variable (clientcmd.RecommendedConfigPathEnvVar) +// when set, and falls back to the platform-appropriate default path (clientcmd.RecommendedHomeFile). +// This correctly resolves the home directory on all platforms, including "Windows" where $HOME is not set but %USERPROFILE% is. +func DefaultKubeconfigPath() string { + if p := os.Getenv(clientcmd.RecommendedConfigPathEnvVar); p != "" { + return p + } + + return clientcmd.RecommendedHomeFile +} + // SetupK8sClientSet reads kubeconfig file at kubeconfigPath and constructs a kubernetes clientset from it. // If contextName is not empty, context under that name is used instead of default. func SetupK8sClientSet(kubeconfigPath, contextName string) (*rest.Config, *kubernetes.Clientset, error) {