alphamikevictor
alphamikevictor

Reputation: 647

Retrieve list of openshift resources with fabric8 openshift-client

I am trying to get a list of all possible resources of a given cluster using the fabric8 openshift-client (or kubernetes-client), so trying to obtain same as command oc api-resources. So far I am able to get the list of apiGroups with a code like this

OpenShiftClient client = new DefaultOpenshiftClient();
List<APIService> apiservices = client.apiServices().list().getItems();
for (APIService apiservice : apiservices){
  System.out.println(apiservice.getSpec().getGroup());
}

Now I am looking how to obtain a list of Resources (and I see in the code there is a class names APIResource) belonging to a particular group, but I am not able to find it.

EDIT:

While I see in code there is a getApiResources() method, for some reason this is not shipped with the quarkus-kubernetes-client (or quarkus-openshift-client) on Quarkus 2.3

As a workaround I have used kubernetes API using RestClient to access /apis/{group}/{version} and /api/v1

Upvotes: 2

Views: 910

Answers (1)

Rohan Kumar
Rohan Kumar

Reputation: 5882

Fabric8 Kubernetes Client has client.getApiGroups() method to get a list of all available api groups. You can then get api resource for each version using client.getApiResources() to get output like kubectl api-resources.

I was able to do it with something like this. I am using Fabric8 Kubernetes Client v5.9.0:

try (KubernetesClient client = new DefaultKubernetesClient()) {
  APIGroupList apiGroupList = client.getApiGroups();

  apiGroupList.getGroups()
    .forEach(group -> group.getVersions().forEach(gv -> {
      APIResourceList apiResourceList = client.getApiResources(gv.getGroupVersion());
      apiResourceList.getResources()
        .stream()
        .filter(r -> !r.getName().contains("/"))
        .forEach(r -> System.out.printf("%s  %s   %s   %s   %s%n", r.getName(), String.join( ",", r.getShortNames()), 
          gv.getGroupVersion(), r.getNamespaced(), r.getKind()));
    }));
}

Upvotes: 2

Related Questions