Skip to content

Instantly share code, notes, and snippets.

@rohanKanojia
Created July 13, 2021 06:34
Show Gist options
  • Select an option

  • Save rohanKanojia/742c8b7ce67dea45162d8f2e12421ef7 to your computer and use it in GitHub Desktop.

Select an option

Save rohanKanojia/742c8b7ce67dea45162d8f2e12421ef7 to your computer and use it in GitHub Desktop.
Fabric8 Kubernetes Client Connection Test
package io.fabric8.testing;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import java.net.UnknownHostException;
public class ConnectionTest {
public static void main(String[] args) {
System.out.printf("%s to Kubernetes Cluster", isConnected() ? "Connected" : "Not Connected");
}
private static boolean isConnected() {
try (KubernetesClient client = new DefaultKubernetesClient()) {
client.getVersion();
} catch (KubernetesClientException e) {
if (e.getCause() instanceof UnknownHostException) {
return false;
}
}
return true;
// Not Supported: But it would be nice to have.
// try (KubernetesClient client = new DefaultKubernetesClient()) {
// client.getVersion();
// } catch (KubernetesNotFoundException e) {
// return false;
// }
// return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment