kubernetes-client / csharp

Officially supported dotnet Kubernetes Client library
Apache License 2.0
1.09k stars 293 forks source link

New watch api #709

Closed zhiweiv closed 2 years ago

zhiweiv commented 2 years ago

Seems there is a new watch api https://github.com/kubernetes-client/csharp/pull/586, can you add an example about how to use it? There is no update in https://github.com/kubernetes-client/csharp/blob/master/examples/watch/Program.cs.

brendandburns commented 2 years ago

cc @tg123

phspies commented 2 years ago

Any update on this? The new Watcher code doesn't seem to fire the onEvent task.

tg123 commented 2 years ago

@phspies Did not get a chance to create one Old API should work as the same. otherwise, it is a bug. Could you please show me the code?

phspies commented 2 years ago
Task<HttpOperationResponse<V1ReplicaSetList>> replicasetlistResp = k8Client.ListReplicaSetForAllNamespacesWithHttpMessagesAsync(watch: true);
                replicationsetWatcher = replicasetlistResp.Watch<V1ReplicaSet, V1ReplicaSetList>(
                    onEvent: (eventType, replicationset) =>
                {
                    Application.MainLoop.Invoke(() =>
                    {
                        Log.Info($"Processing ReplicationSet Event: {eventType} : {JsonConvert.SerializeObject(replicationset)}");
                        processEventDetails(eventType, replicationset);
                        switch (eventType)
                        {
                            case WatchEventType.Added:
                                replicasetsList.AddUpdateDelete(new ReplicaSetType(replicationset), CRUDOperation.Add);
                                break;
                            case WatchEventType.Modified:
                                replicasetsList.AddUpdateDelete(new ReplicaSetType(replicationset), CRUDOperation.Change);
                                break;
                            case WatchEventType.Deleted:
                                replicasetsList.AddUpdateDelete(new ReplicaSetType(replicationset), CRUDOperation.Delete);
                                break;
                            case WatchEventType.Error:
                                Log.Error("Error Event: error in watch thread");
                                break;
                            case WatchEventType.Bookmark:
                                Log.Error("Bookmark Event: error in watch thread");
                                break;
                            default:
                                Log.Error("default event: error in watch thread");
                                break;
                        }
                        replicasetsTableView.SetNeedsDisplay();
                        UpdateTabHeaders();
                    });
                }, onError: (ex) =>
                {
                    Log.Error(ex);
                },
                    onClosed: () =>
                    {
                        Log.Error("ReplicaSets Watcher closed connection");
                    });
tg123 commented 2 years ago
Task<HttpOperationResponse<V1ReplicaSetList>> replicasetlistResp = k8Client.ListReplicaSetForAllNamespacesWithHttpMessagesAsync(watch: true);
               replicationsetWatcher = replicasetlistResp.Watch<V1ReplicaSet, V1ReplicaSetList>(
                   onEvent: (eventType, replicationset) =>
               {
                   Application.MainLoop.Invoke(() =>
                   {
                       Log.Info($"Processing ReplicationSet Event: {eventType} : {JsonConvert.SerializeObject(replicationset)}");
                       processEventDetails(eventType, replicationset);
                       switch (eventType)
                       {
                           case WatchEventType.Added:
                               replicasetsList.AddUpdateDelete(new ReplicaSetType(replicationset), CRUDOperation.Add);
                               break;
                           case WatchEventType.Modified:
                               replicasetsList.AddUpdateDelete(new ReplicaSetType(replicationset), CRUDOperation.Change);
                               break;
                           case WatchEventType.Deleted:
                               replicasetsList.AddUpdateDelete(new ReplicaSetType(replicationset), CRUDOperation.Delete);
                               break;
                           case WatchEventType.Error:
                               Log.Error("Error Event: error in watch thread");
                               break;
                           case WatchEventType.Bookmark:
                               Log.Error("Bookmark Event: error in watch thread");
                               break;
                           default:
                               Log.Error("default event: error in watch thread");
                               break;
                       }
                       replicasetsTableView.SetNeedsDisplay();
                       UpdateTabHeaders();
                   });
               }, onError: (ex) =>
               {
                   Log.Error(ex);
               },
                   onClosed: () =>
                   {
                       Log.Error("ReplicaSets Watcher closed connection");
                   });

I tested your code by keeping Log.Info only. it works on my test env. could you please take a deeper look at your code again?

zhiweiv commented 2 years ago

So update on the example? I'd like to use the new api but not figured out how to do it.

zhiweiv commented 2 years ago

@tg123 One more question, in old api, there is an onClose event, we can use it to monitor the watch close and open a new watch.

In new pattern, if the watch closed, I guess the loop will end, then we can dispose the podlistResp and open a new watch, is it right? For example:

while (true) 
{
    using var podlistResp = client.ListNamespacedPodWithHttpMessagesAsync("default", watch: true);
    await foreach (var (type, item) in podlistResp.WatchAsync<V1Pod, V1PodList>())
    {
        //
    }
}