Skip to content

Commit 17c8a60

Browse files
committed
feat(ske): respect KUBECONFIG environment variable (#875)
1 parent 5430a95 commit 17c8a60

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

internal/pkg/services/ske/utils/utils.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,12 @@ func WriteConfigFile(configPath, data string) error {
284284
return nil
285285
}
286286

287-
// GetDefaultKubeconfigPath returns the default location for the kubeconfig file.
287+
// GetDefaultKubeconfigPath returns the default location for the kubeconfig file or the value of KUBECONFIG if set.
288288
func GetDefaultKubeconfigPath() (string, error) {
289+
if kubeconfigEnv := os.Getenv("KUBECONFIG"); kubeconfigEnv != "" {
290+
return kubeconfigEnv, nil
291+
}
292+
289293
userHome, err := os.UserHomeDir()
290294
if err != nil {
291295
return "", fmt.Errorf("get user home directory: %w", err)

internal/pkg/services/ske/utils/utils_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,3 +698,42 @@ func TestGetDefaultKubeconfigPath(t *testing.T) {
698698
})
699699
}
700700
}
701+
702+
func TestGetDefaultKubeconfigPathWithEnvVar(t *testing.T) {
703+
tests := []struct {
704+
description string
705+
kubeconfigEnvVar string
706+
expected string
707+
userHome string
708+
}{
709+
{
710+
description: "base",
711+
kubeconfigEnvVar: "~/.kube/custom/config",
712+
expected: "~/.kube/custom/config",
713+
userHome: "/home/test-user",
714+
},
715+
{
716+
description: "return user home when environment var is empty",
717+
kubeconfigEnvVar: "",
718+
expected: "/home/test-user/.kube/config",
719+
userHome: "/home/test-user",
720+
},
721+
}
722+
723+
for _, tt := range tests {
724+
t.Run(tt.description, func(t *testing.T) {
725+
// Setup environment variables
726+
os.Setenv("KUBECONFIG", tt.kubeconfigEnvVar)
727+
os.Setenv("HOME", tt.userHome)
728+
729+
output, err := GetDefaultKubeconfigPath()
730+
731+
if err != nil {
732+
t.Errorf("failed on valid input")
733+
}
734+
if output != tt.expected {
735+
t.Errorf("expected output to be %s, got %s", tt.expected, output)
736+
}
737+
})
738+
}
739+
}

0 commit comments

Comments
 (0)