Openshift REST endpoint model jars

5/25/2020

I am trying to call the REST endpoints Redhat openshift exposes from my Java application to get list of deployments in a namespace.

GET /apis/extensions/v1beta1/namespaces/$NAMESPACE/deployments HTTP/1.1
Authorization: Bearer $TOKEN
Accept: application/json
Connection: close

I created the model Java class corresponding to the response this endpoint return, only manually. I hardcoded the bearer token in the HTTP header and invoked the rest end point in java to get the list of deployments. After i get the response, I parsed the response into these model classes i created.

Question 1:

Is there a place i can get the jars corresponding to the response objects which i can directly import in my java code instead of manually creating the response models and parsing the response json into these models?

Question 2:

What is the Openshift REST endpoint to retrieve the Bearer token.

-- lr-pal
java
kubernetes-helm
openshift
redhat
spring-boot

1 Answer

5/26/2020

When interacting with Kubernetes / OpenShift from Java, I would recommend to use the Java Kubernetes Client: https://github.com/kubernetes-client/java

You can also find examples on how to use the Java API in the repository:

[..]

public class Example {
  public static void main(String[] args) throws IOException, ApiException {
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);

    CoreV1Api api = new CoreV1Api();
    V1PodList list =
        api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
    for (V1Pod item : list.getItems()) {
      System.out.println(item.getMetadata().getName());
    }
  }
}

When using the above library, getting a Bearer token is not necessary, as this is all handled by the client.

Nevertheless, OpenShift implements OAuth, this means you can query the OpenShift OAuth server using the standard OAuth endpoints.

If you want to use a ServiceAccount from an external source, you can find the description on how to get the token in the documentation:

$ oc describe secret robot-token-uzkbh -n top-secret
Name:       robot-token-uzkbh
Labels:     <none>
Annotations:    kubernetes.io/service-account.name=robot,kubernetes.io/service-account.uid=49f19e2e-16c6-11e5-afdc-3c970e4b7ffe

Type:   kubernetes.io/service-account-token

Data

token:  eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...
[..]
-- Simon
Source: StackOverflow