The release of the Kubernetes Java Client 17.0.0 delivers support for Kubernetes 1.25 offering the power to dynamically retrieve info, for instance for monitoring functions, and permits altering and deleting gadgets within the Kubernetes cluster. The Kubernetes shopper could also be used as a substitute for the command line Kubernetes instrument: kubectl [argument]
.
The Kubernetes Java Consumer can be utilized after including the next Maven dependency:
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<model>17.0.0</model>
</dependency>
Alternatively the next Gradle dependency could also be used:
compile 'io.kubernetes:client-java:15.0.1'
The CoreV1API affords a considerable amount of strategies, similar to 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 title: " + pod.getMetadata().getName());
The listPodForAllNamespaces()
technique affords many configuration choices by specifying the arguments of the tactic:
public V1PodList listPodForAllNamespaces(
Boolean allowWatchBookmarks,
String _continue,
String fieldSelector,
String labelSelector,
Integer restrict,
String fairly,
String resourceVersion,
String resourceVersionMatch,
Integer timeoutSeconds,
Boolean watch)
Other than retrieving info, it is also potential to alter gadgets, and even delete gadgets similar to a pod
from a namespace
:
Name name = deleteNamespacedPodCall(
String title,
String namespace,
String fairly,
String dryRun,
Integer gracePeriodSeconds,
Boolean orphanDependents,
String propagationPolicy,
V1DeleteOptions physique,
last ApiCallback _callback)
The kubectl logs
command shows logs from a operating container, akin to the next API name:
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);
Other than retrieving single outcomes, it is also potential to observe occasions by setting the watch
argument of a way to Boolean.TRUE
. That is akin to the kubectl get <useful resource> -w
command. For instance, to observe adjustments in a namespace
and print them:
Watch<V1Namespace> watch =
Watch.createWatch(
shopper,
api.listNamespaceCall(null, null, null, null, null, 5, null, null,
null, Boolean.TRUE, null),
new TypeToken<Watch.Response<V1Namespace>>() .getType());
attempt
for (Watch.Response<V1Namespace> reponse : watch)
System.out.printf("Response kind:" + response.kind + " title: " +
response.object.getMetadata().getName());
lastly
watch.shut();
Some superior use instances require the client-java-extended
module which can be utilized after including the next Maven dependency:
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java-extended</artifactId>
<model>17.0.0</model>
</dependency>
Alternatively, the next Gradle dependency could also be used:
implementation 'io.kubernetes:client-java-extended:17.0.0'
One of many extra superior use instances is pagination for record requests, which reduces the server aspect load and community site visitors. For instance, by retrieving 5 namespaces at a time, as an alternative of all namespaces directly:
Pager<V1Namespace, V1NamespaceList> pager = new Pager<>((Pager.PagerParams
param) ->
attempt
return api.listNamespaceCall(null, null, param.getContinueToken(),
null, null, param.getLimit(), null, null, 1, null, null);
catch (Exception e)
// Deal with exception
, shopper, 5, V1NamespaceList.class);
for (V1Namespace namespace : pager)
System.out.println("Namespace title: " + namespace.getMetadata().getName());
Extra info and examples may be discovered within the documentation.