How to use Watcher in kubernetes client?

4/22/2019

I'm using kubernetes client for C#.

I encountered an issue that after 40-50 min the console application continue working but doesn't register any events. That was because of Watcher's expiring. So I found that I can use timeoutSeconds for method I used - WatchObjectAsync. I changed the code - added int.MaxValue for timeoutSeconds:

    var path = $"api/v1/watch/secrets";
    _secretWatcher = await client.WatchObjectAsync<V1Secret>(
        timeoutSeconds: int.MaxValue,
        path: path, 
        onEvent: new Action<WatchEventType, V1Secret>(DoSmthWithEvent));

But the problem still remains. But now it happens after 1.5 - 2 hours.

So I want to have console application that is always running and reacting to object changes in kubernetes.

And my question is - how to properly use WatchObjectAsync or alternative method? Should I sometimes recreate the Watcher object in _secretWatcher? And if so, then why does the parameter timeoutSeconds exist?

Additional info:

  • According to this issue, watches need to be wrapped in a loop.
  • I tried recreating the watcher in onClosed callback - works fine. Or better to use while loop?
-- qwermike
.net-core
c#
kubernetes

0 Answers