Skip to content

Commit ddc02a7

Browse files
committed
try to use kubeconfig from user home
1 parent 03c33ef commit ddc02a7

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

kubectl.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"os/exec"
7+
"path/filepath"
78
"strings"
89
)
910

@@ -14,10 +15,19 @@ var (
1415
func withKubeConfig(noTempKubeConfig bool, f func() error) error {
1516
if !noTempKubeConfig {
1617
realKubeconfigPath := os.Getenv("KUBECONFIG")
18+
if len(realKubeconfigPath) == 0 {
19+
userHomeDir, err := os.UserHomeDir()
20+
if err != nil {
21+
return fmt.Errorf("get user home dir: %w", err)
22+
}
23+
realKubeconfigPath = filepath.Join(userHomeDir, ".kube", "config")
24+
}
25+
1726
tmpFile, err := os.CreateTemp(os.TempDir(), "testpod-kubeconfig-*.yaml")
1827
if err != nil {
1928
return fmt.Errorf("create temp kubeconfig: %w", err)
2029
}
30+
tmpFile.Close()
2131
tempKubeconfigPath = tmpFile.Name()
2232
fmt.Println("clone kubeconfig", realKubeconfigPath, "to", tempKubeconfigPath)
2333
data, err := os.ReadFile(realKubeconfigPath)
@@ -29,7 +39,7 @@ func withKubeConfig(noTempKubeConfig bool, f func() error) error {
2939
}
3040
defer func() {
3141
if err := os.Remove(tempKubeconfigPath); err != nil {
32-
fmt.Println("WARN: failed to delete temp kubeconfig file", tempKubeconfigPath)
42+
fmt.Println("WARN: failed to delete temp kubeconfig file", tempKubeconfigPath+":", err)
3343
} else {
3444
fmt.Println("temp kubeconfig file", tempKubeconfigPath, "deleted")
3545
}

0 commit comments

Comments
 (0)