Thursday, April 25, 2024
HomeJavaKubernetes Java Consumer 17.0 Offers Assist for Kubernetes 1.25

Kubernetes Java Consumer 17.0 Offers Assist for Kubernetes 1.25


The launch of the Kubernetes Java Consumer 17.0.0 delivers assist for Kubernetes 1.25 offering the flexibility to dynamically retrieve info, for instance for monitoring functions, and permits altering and deleting objects within the Kubernetes cluster. The Kubernetes consumer could also be used instead for the command line Kubernetes device: 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 provides a considerable amount of strategies, akin 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 provides 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)

Aside from retrieving info, it is also attainable to vary objects, and even delete objects akin 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,
    remaining ApiCallback _callback)

The kubectl logs command shows logs from a working container, similar 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);

Aside from retrieving single outcomes, it is also attainable to look at occasions by setting the watch argument of a technique to Boolean.TRUE. That is similar to the kubectl get <useful resource> -w command. For instance, to look at modifications in a namespace and print them:


Watch<V1Namespace> watch =
    Watch.createWatch(
        consumer,
        api.listNamespaceCall(null, null, null, null, null, 5, null, null,
            null, Boolean.TRUE, null),
        new TypeToken<Watch.Response<V1Namespace>>() {}.getType());

strive {
    for (Watch.Response<V1Namespace> reponse : watch) {
    System.out.printf("Response sort:" + response.sort + " title: " +
        response.object.getMetadata().getName());
    }
} lastly {
	watch.shut();
}

Some superior use circumstances 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 circumstances is pagination for listing requests, which reduces the server aspect load and community site visitors. For instance, by retrieving 5 namespaces at a time, as a substitute of all namespaces without delay:


Pager<V1Namespace, V1NamespaceList> pager = new Pager<>((Pager.PagerParams
        param) -> {
    strive {
        return api.listNamespaceCall(null, null, param.getContinueToken(), 
            null, null, param.getLimit(), null, null, 1, null, null);
    } catch (Exception e) {
        // Deal with exception
    }
}, consumer, 5, V1NamespaceList.class);
for (V1Namespace namespace : pager) {
    System.out.println("Namespace title: " + namespace.getMetadata().getName());
}

Extra info and examples could be discovered within the documentation.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments