The release of the Kubernetes Java Client 17.0.0 delivers support for Kubernetes 1.25 providing the ability to dynamically retrieve information, for example for monitoring purposes, and allows changing and deleting items in the Kubernetes cluster. The Kubernetes client may be used as an alternative for the command line Kubernetes tool: kubectl [argument]
.
The Kubernetes Java Client can be used after adding the following Maven dependency:
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>17.0.0</version>
</dependency>
Alternatively the following Gradle dependency may be used:
compile 'io.kubernetes:client-java:15.0.1'
The CoreV1API offers a large amount of methods, such as retrieving all pods:
ApiClient apiClient = Config.defaultClient();
Configuration.setDefaultApiClient(apiClient);
CoreV1Api api = new CoreV1Api();
V1PodList podList = api.listPodForAllNamespaces(
null, null, null, null, null, null, null, null, null, null);
for (V1Pod pod : podList.getItems()) {
System.out.println("Pod name: " + pod.getMetadata().getName());
}
The listPodForAllNamespaces()
method offers many configuration options by specifying the arguments of the method:
public V1PodList listPodForAllNamespaces(
Boolean allowWatchBookmarks,
String _continue,
String fieldSelector,
String labelSelector,
Integer limit,
String pretty,
String resourceVersion,
String resourceVersionMatch,
Integer timeoutSeconds,
Boolean watch)
Apart from retrieving information, it's also possible to change items, or even delete items such as a pod
from a namespace
:
Call call = deleteNamespacedPodCall(
String name,
String namespace,
String pretty,
String dryRun,
Integer gracePeriodSeconds,
Boolean orphanDependents,
String propagationPolicy,
V1DeleteOptions body,
final ApiCallback _callback)
The kubectl logs
command displays logs from a running container, comparable to the following API call:
PodLogs logs = new PodLogs();
V1Pod pod = api.listNamespacedPod(
"default", "false", null, null, null, null, null, null, null, null, null)
.getItems()
.get(0);
InputStream inputStream = logs.streamNamespacedPodLog(pod);
Apart from retrieving single results, it's also possible to watch events by setting the watch
argument of a method to Boolean.TRUE
. This is comparable to the kubectl get <resource> -w
command. For example, to watch changes in a namespace
and print them:
Watch<V1Namespace> watch =
Watch.createWatch(
client,
api.listNamespaceCall(null, null, null, null, null, 5, null, null,
null, Boolean.TRUE, null),
new TypeToken<Watch.Response<V1Namespace>>() {}.getType());
try {
for (Watch.Response<V1Namespace> reponse : watch) {
System.out.printf("Response type:" + response.type + " name: " +
response.object.getMetadata().getName());
}
} finally {
watch.close();
}
Some advanced use cases require the client-java-extended
module which can be used after adding the following Maven dependency:
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java-extended</artifactId>
<version>17.0.0</version>
</dependency>
Alternatively, the following Gradle dependency may be used:
implementation 'io.kubernetes:client-java-extended:17.0.0'
One of the more advanced use cases is pagination for list requests, which reduces the server side load and network traffic. For example, by retrieving five namespaces at a time, instead of all namespaces at once:
Pager<V1Namespace, V1NamespaceList> pager = new Pager<>((Pager.PagerParams
param) -> {
try {
return api.listNamespaceCall(null, null, param.getContinueToken(),
null, null, param.getLimit(), null, null, 1, null, null);
} catch (Exception e) {
// Handle exception
}
}, client, 5, V1NamespaceList.class);
for (V1Namespace namespace : pager) {
System.out.println("Namespace name: " + namespace.getMetadata().getName());
}
More information and examples can be found in the documentation.