Skip to content
Open
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
11 changes: 4 additions & 7 deletions internal/backup/cmd/flags.go
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
7 changes: 2 additions & 5 deletions internal/plugins/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 4 additions & 7 deletions internal/status/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
11 changes: 4 additions & 7 deletions internal/system/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 3 additions & 7 deletions internal/tools/sigmigrate/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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(
Expand Down
13 changes: 13 additions & 0 deletions internal/utilk8s/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utilk8s

import (
"fmt"
"os"
"path/filepath"

"k8s.io/client-go/kubernetes"
Expand All @@ -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) {
Expand Down
Loading