5

I've been exploring the Kubernetes-Client/Java library and I can't figure out the API call to get all the deployments.

I'm looking for the K8-Client/Java API call for this command:

kubectl get deployments
NAME             DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
appservice1       3         3         3            3           5d
appservice2       3         3         3            3           1d
appservice3       1         1         1            1           22d

More specifically, I'm interested in determining the number of desired & current pods for each deployment (like above).

BlueChips23
  • 1,861
  • 5
  • 34
  • 53

4 Answers4

9

You can find all methods of the Kubernetes java client here: https://github.com/kubernetes-client/java/tree/master/kubernetes/docs

What you're searching for is the listNamespacedDeployment or listDeploymentForAllNamespaces.

The return type of those methods is V1DeploymentList so you'll find the V1DeploymentStatus which is containing all information about the current number of pods controlled by the deployment.

giande
  • 67
  • 9
alexandrevilain
  • 327
  • 1
  • 6
  • 1
    Ah I see. It's part of the `ExtensionsV1beta1Api` and I was looking at `CoreV1Api`. Thank you! – BlueChips23 May 23 '18 at 13:22
  • I've a follow up question: What's the difference between `ExtensionsV1beta1DeploymentStatus.availableReplicas` and `ExtensionsV1beta1DeploymentStatus.readyReplicas`? Also, is `ExtensionsV1beta1DeploymentStatus.replicas` same as `DESIRED` deployments as in my example above? Just want to clarify this a bit more. – BlueChips23 May 23 '18 at 18:28
  • Hi! You can refer to the following documentation file: https://github.com/kubernetes-client/java/blob/master/kubernetes/docs/AppsV1beta1DeploymentStatus.md – alexandrevilain May 24 '18 at 20:27
  • Page not found in answered link – Dhanraj May 27 '19 at 09:33
2

There is another kubernetes jave client and here is the example of deployments

Note the official library is generated by swagger and not well organized (I think).

Leon
  • 3,124
  • 31
  • 36
2

In order list some resources in kubernetes, first you must find to which apiVersion this resource type belongs to. For deployemnts, it is apiVersion: apps/v1

AppsV1Api appsV1Api = new AppsV1Api();
appsV1Api.listNamespacedDeployment or listDeploymentForAllNamespaces
Nurlan Sofiyev
  • 499
  • 6
  • 8
0
    try (KubernetesClient kubernetesClient = new KubernetesClientBuilder().withConfig(new ConfigBuilder()
            .withTrustCerts().withMasterUrl(apiUrl).withNamespace(namespace)            
            .withOauthToken(token)
            .build()).build()) {
        final OpenShiftClient openShiftClient = kubernetesClient.adapt(OpenShiftClient.class);
        System.out.println("Login Successful");

        DeploymentList dl = openShiftClient.apps().deployments().list();
        dl.getItems().forEach(System.out::println);         
    }
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Diego Borba Aug 30 '23 at 12:36