Skip to content

Commit e8c2951

Browse files
[deckhouse-cli] Add the ability to use some d8 functions in Windows (#402)
Signed-off-by: Losev Valery <valery.losev@flant.com> Signed-off-by: Roman Berezkin <roman.berezkin@flant.com> Co-authored-by: Roman Berezkin <roman.berezkin@flant.com>
1 parent 34fec52 commit e8c2951

6 files changed

Lines changed: 30 additions & 33 deletions

File tree

internal/backup/cmd/flags.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package backup
22

33
import (
4-
"os"
5-
64
"github.com/spf13/pflag"
5+
6+
"github.com/deckhouse/deckhouse-cli/internal/utilk8s"
77
)
88

99
func addPersistentFlags(flagSet *pflag.FlagSet) {
10-
defaultKubeconfigPath := os.ExpandEnv("$HOME/.kube/config")
11-
if p := os.Getenv("KUBECONFIG"); p != "" {
12-
defaultKubeconfigPath = p
13-
}
10+
defaultKubeconfigPath := utilk8s.DefaultKubeconfigPath()
1411

1512
flagSet.StringP(
1613
"kubeconfig", "k",
1714
defaultKubeconfigPath,
18-
"KubeConfig of the cluster. (default is $KUBECONFIG when it is set, $HOME/.kube/config otherwise)",
15+
"Path to kubeconfig file. (default is $KUBECONFIG when it is set, otherwise the default kubeconfig path for the current OS user)",
1916
)
2017

2118
flagSet.String("context", "", "The name of the kubeconfig context to use")

internal/plugins/flags/flags.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/spf13/pflag"
2323

2424
rppflags "github.com/deckhouse/deckhouse-cli/internal/rpp/flags"
25+
"github.com/deckhouse/deckhouse-cli/internal/utilk8s"
2526
)
2627

2728
const (
@@ -61,11 +62,7 @@ func skipClusterChecksDefault() bool {
6162
}
6263

6364
func defaultKubeconfigPath() string {
64-
if v := os.Getenv("KUBECONFIG"); v != "" {
65-
return v
66-
}
67-
68-
return os.ExpandEnv("$HOME/.kube/config")
65+
return utilk8s.DefaultKubeconfigPath()
6966
}
7067

7168
func AddFlags(flagSet *pflag.FlagSet) {

internal/status/cmd/flags.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,18 @@ limitations under the License.
1717
package status
1818

1919
import (
20-
"os"
21-
2220
"github.com/spf13/pflag"
21+
22+
"github.com/deckhouse/deckhouse-cli/internal/utilk8s"
2323
)
2424

2525
func addPersistentFlags(flagSet *pflag.FlagSet) {
26-
defaultKubeconfigPath := os.ExpandEnv("$HOME/.kube/config")
27-
if p := os.Getenv("KUBECONFIG"); p != "" {
28-
defaultKubeconfigPath = p
29-
}
26+
defaultKubeconfigPath := utilk8s.DefaultKubeconfigPath()
3027

3128
flagSet.StringP(
3229
"kubeconfig", "k",
3330
defaultKubeconfigPath,
34-
"KubeConfig of the cluster. (default is $KUBECONFIG when it is set, $HOME/.kube/config otherwise)",
31+
"Path to kubeconfig file. (default is $KUBECONFIG when it is set, otherwise the default kubeconfig path for the current OS user)",
3532
)
3633

3734
flagSet.String("context", "", "The name of the kubeconfig context to use")

internal/system/flags/flags.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,19 @@ limitations under the License.
1717
package flags
1818

1919
import (
20-
"os"
21-
2220
"github.com/spf13/cobra"
21+
22+
"github.com/deckhouse/deckhouse-cli/internal/utilk8s"
2323
)
2424

2525
func AddPersistentFlags(cmd *cobra.Command) {
26-
defaultKubeconfigPath := os.ExpandEnv("$HOME/.kube/config")
27-
if p := os.Getenv("KUBECONFIG"); p != "" {
28-
defaultKubeconfigPath = p
29-
}
26+
defaultKubeconfigPath := utilk8s.DefaultKubeconfigPath()
3027

3128
cmd.PersistentFlags().StringP(
3229
"kubeconfig",
3330
"k",
3431
defaultKubeconfigPath,
35-
"KubeConfig of the cluster. (default is $KUBECONFIG when it is set, $HOME/.kube/config otherwise)",
32+
"Path to kubeconfig file. (default is $KUBECONFIG when it is set, otherwise the default kubeconfig path for the current OS user)",
3633
)
3734

3835
cmd.PersistentFlags().String("context", "", "The name of the kubeconfig context to use")

internal/tools/sigmigrate/cmd/flags.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20-
"os"
21-
2220
"github.com/spf13/pflag"
2321

2422
"github.com/deckhouse/deckhouse-cli/internal/tools/sigmigrate"
23+
"github.com/deckhouse/deckhouse-cli/internal/utilk8s"
2524
)
2625

2726
const (
@@ -47,15 +46,12 @@ func addFlags(flags *pflag.FlagSet) {
4746
"Set the log level (INFO, DEBUG, TRACE). Defaults to DEBUG.",
4847
)
4948

50-
defaultKubeconfigPath := os.ExpandEnv("$HOME/.kube/config")
51-
if p := os.Getenv("KUBECONFIG"); p != "" {
52-
defaultKubeconfigPath = p
53-
}
49+
defaultKubeconfigPath := utilk8s.DefaultKubeconfigPath()
5450

5551
flags.String(
5652
"kubeconfig",
5753
defaultKubeconfigPath,
58-
"Path to the kubeconfig file to use for CLI requests. (default is $KUBECONFIG when it is set, $HOME/.kube/config otherwise)",
54+
"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)",
5955
)
6056

6157
flags.String(

internal/utilk8s/clientset.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utilk8s
22

33
import (
44
"fmt"
5+
"os"
56
"path/filepath"
67

78
"k8s.io/client-go/kubernetes"
@@ -11,6 +12,18 @@ import (
1112

1213
const DefaultKubeContext = ""
1314

15+
// DefaultKubeconfigPath returns the default path to the kubeconfig file.
16+
// It respects the "KUBECONFIG" environment variable (clientcmd.RecommendedConfigPathEnvVar)
17+
// when set, and falls back to the platform-appropriate default path (clientcmd.RecommendedHomeFile).
18+
// This correctly resolves the home directory on all platforms, including "Windows" where $HOME is not set but %USERPROFILE% is.
19+
func DefaultKubeconfigPath() string {
20+
if p := os.Getenv(clientcmd.RecommendedConfigPathEnvVar); p != "" {
21+
return p
22+
}
23+
24+
return clientcmd.RecommendedHomeFile
25+
}
26+
1427
// SetupK8sClientSet reads kubeconfig file at kubeconfigPath and constructs a kubernetes clientset from it.
1528
// If contextName is not empty, context under that name is used instead of default.
1629
func SetupK8sClientSet(kubeconfigPath, contextName string) (*rest.Config, *kubernetes.Clientset, error) {

0 commit comments

Comments
 (0)