headlamp-k8s / headlamp

A Kubernetes web UI that is fully-featured, user-friendly and extensible
https://headlamp.dev
Apache License 2.0
1.84k stars 138 forks source link

StreamResult function unexpected behaviour #1298

Open callmevladik opened 11 months ago

callmevladik commented 11 months ago

Example use case:

const [mySecretSecret] = SecretKubeObject.useGet('secret-secret', namespace);

I as a dev want to subsribe to specific resource via useGet method. The problem is that useGet(deep in depth streamResult function) works only for updates, so two cases(delete, add) are not handled.

Function location: frontend/src/lib/k8s/apiProxy.ts Line: 662

socket = stream(watchUrl, x => cb(x.object), { isJson: true });

Callback fires always with the same behaviour even if X object type is DELETED. For example, streamResults function handles it in a different way:

case 'DELETED': delete results[object.metadata.uid]; break;

Expected cases Delete: a resource exists, then it gets deleted and it becomes null and returned via useGet hook. Add: a resource doesn't exist yet, then it gets created and returned via useGet hook.

joaquimrocha commented 11 months ago

I think the delete case makes sense, and we should fix it. The add case may be more complicated because there's not yet a resource we can watch (so it would require some more work). As a workaround for the "add", I think you can watch events and use the selector and namespace, etc. options to try to look for events related to a resource being created. Would this help?

callmevladik commented 11 months ago

At the moment as a workaround I use useList with labelSelector, but that's temporary solution