File tree Expand file tree Collapse file tree 2 files changed +32
-1
lines changed
main/java/io/kubernetes/client/util
test/java/io/kubernetes/client/util Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -104,10 +104,14 @@ public static ClientBuilder standard(boolean persistConfig) throws IOException {
104104 }
105105
106106 private static File findConfigFromEnv () {
107- String kubeConfigPath = System .getenv (ENV_KUBECONFIG );
107+ final KubeConfigEnvParser kubeConfigEnvParser = new KubeConfigEnvParser ();
108+
109+ final String kubeConfigPath =
110+ kubeConfigEnvParser .parseKubeConfigPath (System .getenv (ENV_KUBECONFIG ));
108111 if (kubeConfigPath == null ) {
109112 return null ;
110113 }
114+
111115 final File kubeConfig = new File (kubeConfigPath );
112116 if (kubeConfig .exists ()) {
113117 return kubeConfig ;
@@ -117,6 +121,24 @@ private static File findConfigFromEnv() {
117121 }
118122 }
119123
124+ private static class KubeConfigEnvParser {
125+ private String parseKubeConfigPath (String kubeConfigEnv ) {
126+ if (kubeConfigEnv == null ) {
127+ return null ;
128+ }
129+
130+ final String [] filePaths = kubeConfigEnv .split (File .pathSeparator );
131+ final String kubeConfigPath = filePaths [0 ];
132+ if (filePaths .length > 1 ) {
133+ log .warn (
134+ "Found multiple kubeconfigs files, $KUBECONFIG: " + kubeConfigEnv + " using first: {}" ,
135+ kubeConfigPath );
136+ }
137+
138+ return kubeConfigPath ;
139+ }
140+ }
141+
120142 private static File findHomeDir () {
121143 final String envHome = System .getenv (ENV_HOME );
122144 if (envHome != null && envHome .length () > 0 ) {
Original file line number Diff line number Diff line change 2121import com .google .common .io .Resources ;
2222import io .kubernetes .client .ApiClient ;
2323import io .kubernetes .client .util .credentials .Authentication ;
24+ import java .io .File ;
2425import java .io .IOException ;
2526import java .nio .file .Files ;
2627import java .nio .file .Paths ;
@@ -76,6 +77,14 @@ public void testDefaultClientReadsKubeConfig() throws Exception {
7677 assertEquals ("http://kubeconfig.dir.com" , client .getBasePath ());
7778 }
7879
80+ @ Test
81+ public void testDefaultClientReadsKubeConfigMultiple () throws Exception {
82+ final String kubeConfigEnv = KUBECONFIG_FILE_PATH + File .pathSeparator + "/non-existent" ;
83+ environmentVariables .set ("KUBECONFIG" , kubeConfigEnv );
84+ final ApiClient client = ClientBuilder .defaultClient ();
85+ assertEquals ("http://kubeconfig.dir.com" , client .getBasePath ());
86+ }
87+
7988 @ Test
8089 public void testKubeconfigPreferredOverHomeDir () throws Exception {
8190 environmentVariables .set ("HOME" , HOME_PATH );
You can’t perform that action at this time.
0 commit comments