Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubernetesClient.resources(HasMetadata.class).inNamespace("default").withLabels(Map.of("foo", "bar").list() throws NullPointerException #3990

Closed
stefanbethke opened this issue Mar 22, 2022 · 5 comments · Fixed by #4133
Assignees
Milestone

Comments

@stefanbethke
Copy link

stefanbethke commented Mar 22, 2022

Describe the bug

When trying to obtain a list of any resource matching a certain set of labels, a NullPointerException is thrown.

foo = kubernetesClient.resources(HasMetadata.class).inNamespace("default").withLabels(Map.of("foo", "bar").list()

The NPE is thrown here io.fabric8.kubernetes.client.dsl.base.OperationSupport#getRootUrl:

      return new URL(URLUtils.join(config.getMasterUrl().toString(), "api", apiGroupVersion));

apiGroupVersion is null at this point.

I'm sure I'm invoking the API incorrectly, but I couldn't find how to do this in a different way.

I'm using the operator, and I think it's pulling in the client version 5.12, but I can't select that below.

Fabric8 Kubernetes Client version

5.10.0

Steps to reproduce

Run Java code as shown above and observe the NPE.

Expected behavior

A list of resources is returned.

Runtime

other (please specify in additional context)

Kubernetes API Server version

1.22.3@latest

Environment

macOS

Fabric8 Kubernetes Client Logs

No response

Additional context

v1.22.6+k3s1 (k3d)

@stefanbethke stefanbethke changed the title kubernetesClient.resources(HasMetadata.class).inNamespace(cmcc.getMetadata().getNamespace()).withLabels(Map.of("foo", "bar").list() throws NullPointerException kubernetesClient.resources(HasMetadata.class).inNamespace("default").withLabels(Map.of("foo", "bar").list() throws NullPointerException Mar 22, 2022
@shawkins
Copy link
Contributor

This is not currently expected to work, but it should throw a better exception. There's currently no facility for scanning all resources in a given namespace. See the workaround and the issue in general #887 (comment)

@manusa
Copy link
Member

manusa commented Apr 4, 2022

This issue should be closed after we implement a fix that logs a better message.

In any case, it shouldn't be supported a .resources(HasMetadata.class). Operations to query on multiple resource types should be more explicit.

@manusa manusa added this to the 6.0.0 milestone Apr 5, 2022
@rohanKanojia rohanKanojia self-assigned this May 10, 2022
@rohanKanojia
Copy link
Member

I can reproduce this issue on v5.12.2

However, on the master branch(6.0-SNAPSHOT) I can't. Instead, client proceeds to request resources for hasmetadatas:

May 10, 2022 2:37:36 PM okhttp3.mockwebserver.MockWebServer$2 execute
INFO: MockWebServer[42711] starting to accept connections
May 10, 2022 2:37:36 PM okhttp3.mockwebserver.MockWebServer$2 execute
INFO: MockWebServer[54931] starting to accept connections
May 10, 2022 2:37:36 PM okhttp3.mockwebserver.MockWebServer$3 processOneRequest
INFO: MockWebServer[54931] received request: GET /api/v1/hasmetadatas?labelSelector=foo%3Dbar HTTP/1.1 and responded: HTTP/1.1 404 Client Error
May 10, 2022 2:37:36 PM okhttp3.mockwebserver.MockWebServer$2 acceptConnections
INFO: MockWebServer[54931] done accepting connections: Socket closed

io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: GET at: https://localhost:54931/api/v1/hasmetadatas?labelSelector=foo%3Dbar. Message: Not Found.

	at io.fabric8.kubernetes.client.dsl.internal.OperationSupport.requestFailure(OperationSupport.java:735)
	at io.fabric8.kubernetes.client.dsl.internal.OperationSupport.requestFailure(OperationSupport.java:713)
	at io.fabric8.kubernetes.client.dsl.internal.OperationSupport.assertResponseCode(OperationSupport.java:664)
	at io.fabric8.kubernetes.client.dsl.internal.OperationSupport.lambda$handleResponse$0(OperationSupport.java:580)
	at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:642)
	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:506)
	at java.base/java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:2073)
	at io.fabric8.kubernetes.client.okhttp.OkHttpClientImpl$4.onResponse(OkHttpClientImpl.java:263)
	at okhttp3.RealCall$AsyncCall.execute(RealCall.java:203)
	at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)


Process finished with exit code 255

rohanKanojia added a commit to rohanKanojia/kubernetes-client that referenced this issue May 10, 2022
…in `resources(...)` API (fabric8io#3990)

Access to `resources(...)` DSL method with generic HasMetadata interface
shouldn't be allowed.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
rohanKanojia added a commit to rohanKanojia/kubernetes-client that referenced this issue May 10, 2022
…in `resources(...)` API (fabric8io#3990)

Access to `resources(...)` DSL method with generic HasMetadata interface
shouldn't be allowed.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
rohanKanojia added a commit to rohanKanojia/kubernetes-client that referenced this issue May 12, 2022
…in `resources(...)` API (fabric8io#3990)

Access to `resources(...)` DSL method with generic HasMetadata interface
shouldn't be allowed.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
manusa pushed a commit to rohanKanojia/kubernetes-client that referenced this issue May 18, 2022
…in `resources(...)` API (fabric8io#3990)

Access to `resources(...)` DSL method with generic HasMetadata interface
shouldn't be allowed.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
manusa pushed a commit that referenced this issue May 18, 2022
…in `resources(...)` API (#3990)

Access to `resources(...)` DSL method with generic HasMetadata interface
shouldn't be allowed.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
@sachinkaushik
Copy link

sachinkaushik commented Aug 11, 2023

#Hi @rohanKanojia @manusa @shawkins @thiagoarrais ,

I'm getting below exception, when we try to watch Openshift resource using the provided code.

Exception :: Caused by: java.lang.IllegalArgumentException: resources cannot be called with an interface

CODE::

      ` client.resources(HasMetadata.class).inAnyNamespace().watch(new Watcher<HasMetadata>() {
        @Override
        public void eventReceived(Action action, HasMetadata hasMetadata) {                
          
         
        }

        @Override
        public void onClose(WatcherException e) {

        }
    });`

Maven dep ::

<dependency> <groupId>io.fabric8</groupId> <artifactId>openshift-client</artifactId> <version>6.2.0</version> </dependency>

Is there anyway to watch all resources in single watcher, we tried to use Event, but that doesn't has all metadata of resource.
Can you please help here ?

Thank you in advance :)

@manusa
Copy link
Member

manusa commented Aug 22, 2023

Is there anyway to watch all resources in single watcher, we tried to use Event, but that doesn't has all metadata of resource.
Can you please help here ?

No, it's not possible. The Kubernetes API doesn't offer such feature.
You have to open a watcher for each resource you want to observe (Pod, Service, and so on)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants