Skip to content

Commit 0bb4b96

Browse files
committed
#1802 Check if server is available from kubeconfig.
1 parent 134aacc commit 0bb4b96

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

util/src/main/java/io/kubernetes/client/util/ClientBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ public static ClientBuilder kubeconfig(KubeConfig config) throws IOException {
277277
final ClientBuilder builder = new ClientBuilder();
278278

279279
String server = config.getServer();
280+
if (server == null) {
281+
throw new IllegalArgumentException("No server in kubeconfig");
282+
}
280283
if (!server.contains("://")) {
281284
if (server.contains(":443")) {
282285
server = "https://" + server;

util/src/test/java/io/kubernetes/client/util/ClientBuilderTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.hamcrest.MatcherAssert.assertThat;
1919
import static org.hamcrest.core.Is.is;
2020
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertThrows;
2122
import static org.mockito.Mockito.mock;
2223
import static org.mockito.Mockito.verify;
2324

@@ -31,6 +32,7 @@
3132
import java.io.IOException;
3233
import java.nio.file.Files;
3334
import java.nio.file.Paths;
35+
3436
import org.junit.Test;
3537

3638
/** Tests for the ConfigBuilder helper class */
@@ -299,4 +301,13 @@ public void testSettingPassphraseForKubeConfigShouldWork() throws IOException {
299301
((ClientCertificateAuthentication) receivingAuthn.getDelegateAuthentication())
300302
.getPassphrase());
301303
}
304+
305+
@Test
306+
public void testDetectsServerNotSet() {
307+
assertThrows("No server in kubeconfig", IllegalArgumentException.class, () -> {
308+
KubeConfig kubeConfigWithoutServer = mock(KubeConfig.class);
309+
310+
ClientBuilder.kubeconfig(kubeConfigWithoutServer);
311+
});
312+
}
302313
}

0 commit comments

Comments
 (0)