Skip to content

Commit bcd9589

Browse files
committed
feat: handle path list for KUBECONFIG env var
1 parent 966bcb3 commit bcd9589

File tree

3 files changed

+93
-11
lines changed

3 files changed

+93
-11
lines changed

klient/conf/config.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"os/user"
2525
"path"
26+
"path/filepath"
2627

2728
"k8s.io/client-go/rest"
2829
"k8s.io/client-go/tools/clientcmd"
@@ -97,7 +98,15 @@ func ResolveKubeConfigFile() string {
9798
// if KUBECONFIG env is defined then use that
9899
kubeConfigPath = os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
99100
if kubeConfigPath != "" {
100-
return kubeConfigPath
101+
// handle the variable as a path list, similar to client-go.
102+
filePaths := filepath.SplitList(kubeConfigPath)
103+
for _, filename := range filePaths {
104+
if _, err := os.Stat(filename); err == nil {
105+
return filename
106+
}
107+
}
108+
// return the last path in the list if none exist yet.
109+
return filePaths[len(filePaths)-1]
101110
}
102111

103112
var (

klient/conf/config_test.go

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package conf
1818

1919
import (
20+
"fmt"
21+
"os"
2022
"path/filepath"
2123
"testing"
2224

@@ -25,15 +27,64 @@ import (
2527

2628
var kubeconfig string
2729

28-
func TestResolveKubeConfigFile(t *testing.T) {
29-
home := homedir.HomeDir()
30+
func TestResolveKubeConfigFileFlag(t *testing.T) {
3031
filename := ResolveKubeConfigFile()
31-
32-
if filename != filepath.Join(home, "test", ".kube", "config") {
32+
if filename != kubeconfigpath {
3333
t.Errorf("unexpected config path: %s", filename)
3434
}
3535
}
3636

37+
func TestResolveKubeConfigFileEnv(t *testing.T) {
38+
clearKubeconfigFlags()
39+
defer setKubeconfigFlags()
40+
41+
t.Run("WithEnvEmpty", func(t *testing.T) {
42+
t.Setenv("KUBECONFIG", "")
43+
44+
filename := ResolveKubeConfigFile()
45+
46+
// this will fallback to the true home directory.
47+
if filename != filepath.Join(homedir.HomeDir(), ".kube", "config") {
48+
t.Errorf("unexpected config path: %s", filename)
49+
}
50+
})
51+
52+
t.Run("WithEnvPath", func(t *testing.T) {
53+
kubeConfigPath := filepath.Join(t.TempDir(), "config")
54+
if _, err := os.Create(kubeConfigPath); err != nil {
55+
t.Errorf("failed to create kubeconfig: %v", err)
56+
}
57+
58+
t.Setenv("KUBECONFIG", kubeConfigPath)
59+
60+
filename := ResolveKubeConfigFile()
61+
62+
if filename != kubeConfigPath {
63+
t.Errorf("unexpected config path: %s", filename)
64+
}
65+
})
66+
67+
t.Run("WithEnvPathList", func(t *testing.T) {
68+
kubeConfigPath1 := filepath.Join(t.TempDir(), "config")
69+
if _, err := os.Create(kubeConfigPath1); err != nil {
70+
t.Errorf("failed to create kubeconfig: %v", err)
71+
}
72+
73+
kubeConfigPath2 := filepath.Join(t.TempDir(), "config")
74+
if _, err := os.Create(kubeConfigPath2); err != nil {
75+
t.Errorf("failed to create kubeconfig: %v", err)
76+
}
77+
78+
t.Setenv("KUBECONFIG", fmt.Sprintf("%s:%s", kubeConfigPath1, kubeConfigPath2))
79+
80+
filename := ResolveKubeConfigFile()
81+
82+
if filename != kubeConfigPath2 {
83+
t.Errorf("unexpected config path: %s", filename)
84+
}
85+
})
86+
}
87+
3788
func TestNew(t *testing.T) {
3889
cfg, err := New(ResolveKubeConfigFile())
3990
if err != nil {

klient/conf/main_test.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ import (
2828
"k8s.io/client-go/util/homedir"
2929
)
3030

31+
var (
32+
kubeconfigpath string
33+
kubeContext string
34+
)
35+
3136
func TestMain(m *testing.M) {
3237
setup()
38+
setKubeconfigFlags()
3339
code := m.Run()
3440
teardown()
3541
os.Exit(code)
@@ -39,8 +45,8 @@ func setup() {
3945
home := homedir.HomeDir()
4046

4147
kubeconfigdir := filepath.Join(home, "test", ".kube")
42-
kubeconfigpath := filepath.Join(kubeconfigdir, "config")
43-
kubeContext := "test-context"
48+
kubeconfigpath = filepath.Join(kubeconfigdir, "config")
49+
kubeContext = "test-context"
4450

4551
// check if file exists
4652
_, err := os.Stat(kubeconfigpath)
@@ -66,17 +72,33 @@ func setup() {
6672

6773
flag.StringVar(&kubeconfig, "kubeconfig", "", "Paths to a kubeconfig. Only required if out-of-cluster.")
6874
flag.StringVar(&kubeContext, "context", "", "The name of the kubeconfig context to use. Only required if out-of-cluster.")
75+
}
6976

77+
func setKubeconfigFlags() {
7078
// set --kubeconfig flag
71-
err = flag.Set("kubeconfig", kubeconfigpath)
72-
if err != nil {
79+
if err := flag.Set("kubeconfig", kubeconfigpath); err != nil {
7380
log.ErrorS(err, "unexpected error while setting kubeconfig flag value")
7481
return
7582
}
7683

7784
// set --context flag
78-
err = flag.Set("context", kubeContext)
79-
if err != nil {
85+
if err := flag.Set("context", kubeContext); err != nil {
86+
log.ErrorS(err, "unexpected error while setting context flag value")
87+
return
88+
}
89+
90+
flag.Parse()
91+
}
92+
93+
func clearKubeconfigFlags() {
94+
// clear --kubeconfig flag
95+
if err := flag.Set("kubeconfig", ""); err != nil {
96+
log.ErrorS(err, "unexpected error while setting kubeconfig flag value")
97+
return
98+
}
99+
100+
// clear --context flag
101+
if err := flag.Set("context", ""); err != nil {
80102
log.ErrorS(err, "unexpected error while setting context flag value")
81103
return
82104
}

0 commit comments

Comments
 (0)