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

ClassCastException: class io.fabric8.kubernetes.api.model.Secret cannot be cast to class io.fabric8.kubernetes.api.model.GenericKubernetesResource #3328

Closed
lburgazzoli opened this issue Jul 17, 2021 · 6 comments · Fixed by #3331
Assignees
Labels
Milestone

Comments

@lburgazzoli
Copy link
Contributor

I have an operator where I set up watcher using the customResource to be able to watch any kind of resource, something like:

CustomResourceDefinitionContext ctx = createCRDContext("v1", "Secret");

return kubernetesClient
    .customResource(ctx)
    .inNamespace(namespace)
    .watch(namespace, null, labels, new ListOptionsBuilder().build(), watcher);

When an event happen on a secret, the following exception is thrown:

15:22:51.278 ERROR [io.fabric8.kubernetes.client.dsl.internal.AbstractWatchManager] (OkHttp http://localhost:57207/...) Received wrong type of object for watch: java.lang.ClassCastException: class io.fabric8.kubernetes.api.model.Secret cannot be cast to class io.fabric8.kubernetes.api.model.GenericKubernetesResource (io.fabric8.kubernetes.api.model.Secret and io.fabric8.kubernetes.api.model.GenericKubernetesResource are in unnamed module of loader io.quarkus.bootstrap.classloading.QuarkusClassLoader @32a822c0)
        at io.fabric8.kubernetes.client.dsl.internal.RawCustomResourceOperationsImpl$DelegateWatcher.eventReceived(RawCustomResourceOperationsImpl.java:936)
        at io.fabric8.kubernetes.client.utils.WatcherToggle.eventReceived(WatcherToggle.java:49)
        at io.fabric8.kubernetes.client.dsl.internal.AbstractWatchManager.eventReceived(AbstractWatchManager.java:176)
        at io.fabric8.kubernetes.client.dsl.internal.AbstractWatchManager.onMessage(AbstractWatchManager.java:232)
        at io.fabric8.kubernetes.client.dsl.internal.WatcherWebSocketListener.onMessage(WatcherWebSocketListener.java:93)
        at okhttp3.internal.ws.RealWebSocket.onReadMessage(RealWebSocket.java:322)
        at okhttp3.internal.ws.WebSocketReader.readMessageFrame(WebSocketReader.java:219)
        at okhttp3.internal.ws.WebSocketReader.processNextFrame(WebSocketReader.java:105)
        at okhttp3.internal.ws.RealWebSocket.loopReader(RealWebSocket.java:273)
        at okhttp3.internal.ws.RealWebSocket$1.onResponse(RealWebSocket.java:209)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
        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:829)
@lburgazzoli
Copy link
Contributor Author

@manusa unfortunately I didn't spot this issue when you originally asked me to check the PR where GenericKubernetesResource has been introduced.

@shawkins
Copy link
Contributor

shawkins commented Jul 17, 2021 via email

@manusa manusa added the bug label Jul 19, 2021
@manusa
Copy link
Member

manusa commented Jul 19, 2021

I wouldn't agree 100% on this being a bug.

If you are dealing with a known type, why do you want to handle this as a Raw/Unstructured/CustomResource type?
I mean, the runtime error is really bad and it would be much better to prevent this, but my point is that you should be using kubernetesClient.secrets() instead.

What's the main reason you are using a CRD definition and the extra hassle instead of using the known type?

@manusa manusa added this to the 5.6.0 milestone Jul 19, 2021
@lburgazzoli
Copy link
Contributor Author

I wouldn't agree 100% on this being a bug.

If you are dealing with a known type, why do you want to handle this as a Raw/Unstructured/CustomResource type?
I mean, the runtime error is really bad and it would be much better to prevent this, but my point is that you should be using kubernetesClient.secrets() instead.

I can't because my operator does not know the type of the resources as they are retrieved from another service, then the operator set-up a watcher on them and reacts upon changes in a generic way.

Of course I can eventually come up with something like a switch case to check if it is a know resource, but since the operator does not care about the type, I think that it would just make the code hard to maintain.

@shawkins shawkins self-assigned this Jul 19, 2021
@shawkins
Copy link
Contributor

Since it's just a small fix and makes the unstructured logic work with anything, I'll produce a pr for this.

shawkins added a commit to shawkins/kubernetes-client that referenced this issue Jul 19, 2021
shawkins added a commit to shawkins/kubernetes-client that referenced this issue Jul 19, 2021
@manusa
Copy link
Member

manusa commented Jul 20, 2021

I can't because my operator does not know the type of the resources as they are retrieved from another service, then the operator set-up a watcher on them and reacts upon changes in a generic way.

OK, this is what I imagined. In your snippet you provided a hard-coded "v1"+"Secret" group-version + kind combination, I understand that in your operator these values are dynamic.

One of the next steps for the GenericKubernetesResource is to be able to use this with resource with DSL methods such as resouce or resourceList without the need to provide additional context. This should eventually take care of the full round-trip (deserialize any kind of resource, both known and unknown, and be able to handle it).

Since it's just a small fix and makes the unstructured logic work with anything, I'll produce a pr for this.

🙌 cool, thx!

@manusa manusa closed this as completed in c241520 Jul 20, 2021
@manusa manusa linked a pull request Jul 20, 2021 that will close this issue
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants