mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-07-02 18:21:48 +00:00
Add logic to determine kubeconfig path
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
parent
d2267aa60c
commit
7c2f8ffa75
29
cmd/kubectx/kubeconfig.go
Normal file
29
cmd/kubectx/kubeconfig.go
Normal file
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func kubeconfigPath() (string, error) {
|
||||
// KUBECONFIG env var
|
||||
if v := os.Getenv("KUBECONFIG"); v != "" {
|
||||
list := filepath.SplitList(v)
|
||||
if len(list) > 1 {
|
||||
// TODO KUBECONFIG=file1:file2 currently not supported
|
||||
return "", errors.New("multiple files in KUBECONFIG currently not supported")
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// return default path
|
||||
home := os.Getenv("HOME")
|
||||
if home == "" {
|
||||
home = os.Getenv("USERPROFILE") // windows
|
||||
}
|
||||
if home == "" {
|
||||
return "", errors.New("HOME or USERPROFILE environment variable not set")
|
||||
}
|
||||
return filepath.Join(home, ".kube", "config"), nil
|
||||
}
|
76
cmd/kubectx/kubeconfig_test.go
Normal file
76
cmd/kubectx/kubeconfig_test.go
Normal file
@ -0,0 +1,76 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_kubeconfigPath_homePath(t *testing.T) {
|
||||
origHome := os.Getenv("HOME")
|
||||
os.Setenv("HOME", "/foo/bar")
|
||||
defer os.Setenv("HOME", origHome)
|
||||
|
||||
got, err := kubeconfigPath()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expected := filepath.Join(filepath.FromSlash("/foo/bar"), ".kube", "config")
|
||||
|
||||
if got != expected{
|
||||
t.Fatalf("wrong value: expected=%s got=%s", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_kubeconfigPath_userprofile(t *testing.T) {
|
||||
origHome := os.Getenv("HOME")
|
||||
os.Unsetenv("HOME")
|
||||
os.Setenv("USERPROFILE", "/foo/bar")
|
||||
defer os.Setenv("HOME", origHome)
|
||||
|
||||
got, err := kubeconfigPath()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expected := filepath.Join(filepath.FromSlash("/foo/bar"), ".kube", "config")
|
||||
|
||||
if got != expected{
|
||||
t.Fatalf("wrong value: expected=%s got=%s", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_kubeconfigPath_noEnvVars(t *testing.T) {
|
||||
origHome := os.Getenv("HOME")
|
||||
origUserprofile := os.Getenv("USERPROFILE")
|
||||
os.Unsetenv("HOME")
|
||||
os.Unsetenv("USERPROFILE")
|
||||
defer os.Setenv("HOME", origHome)
|
||||
defer os.Setenv("USERPROFILE", origUserprofile)
|
||||
|
||||
_, err := kubeconfigPath()
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func Test_kubeconfigPath_envOvveride(t *testing.T) {
|
||||
os.Setenv("KUBECONFIG", "foo")
|
||||
defer os.Unsetenv("KUBECONFIG")
|
||||
|
||||
v, err := kubeconfigPath()
|
||||
if err != nil { t.Fatal(err)}
|
||||
if expected := "foo"; v != expected {
|
||||
t.Fatalf("expected=%q, got=%q", expected, v)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_kubeconfigPath_envOvverideDoesNotSupportPathSeparator(t *testing.T) {
|
||||
path := strings.Join([]string{"file1","file2"}, string(os.PathListSeparator))
|
||||
os.Setenv("KUBECONFIG", path)
|
||||
defer os.Unsetenv("KUBECONFIG")
|
||||
|
||||
_, err := kubeconfigPath()
|
||||
if err == nil { t.Fatal("expected error")}
|
||||
|
||||
}
|
1
cmd/kubectx/list.go
Normal file
1
cmd/kubectx/list.go
Normal file
@ -0,0 +1 @@
|
||||
package main
|
Loading…
Reference in New Issue
Block a user