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

Watcher could implement either keep alive or retry mechanism #486

Closed
petermicuch opened this issue Sep 28, 2020 · 7 comments
Closed

Watcher could implement either keep alive or retry mechanism #486

petermicuch opened this issue Sep 28, 2020 · 7 comments
Labels
lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.

Comments

@petermicuch
Copy link

Similar to issue mentioned here for python client, it would be really nice if Watcher does not drop connection after 30 minutes. Right now I am handling OnClose and OnError events and recreate Watcher after short period of time since connection was closed. Even better would be if watch supports Observable and one could use Rx to handle changes.

@brendandburns
Copy link
Contributor

We need to build a higher level "Informer" on top of the Watcher to handle reconnecting after a watch stops.

In general, you can't rely on a watch lasting forever. You have to List/Watch in a loop, but we should build a utility to make that easy for people to do correctly.

@petermicuch
Copy link
Author

@brendandburns does it mean this will be considered? Loop is exactly what I am doing now, so in case this would be already build in, it would be great. Use-cases for watch are usually long running tasks in my opinion. Observable would be even better as you can trigger delayed processing, buffering or throttling of events.

@brendandburns
Copy link
Contributor

We definitely should build up the Informer code. There are some starting points here:

#394

But it got hung up in the design phase.

@tomaszgawlik
Copy link

tomaszgawlik commented Oct 13, 2020

@petermicuch Would you be able to share your look example please?
I'm trying to write an application, which is watching kubernetes events and sends them to Grafana.
But having troubles with restarting the stream after it's closed.

Edit, that's what I'm testing now:

            var response = await _kubernetesClient.ListEventForAllNamespacesWithHttpMessagesAsync(watch: true);
            while (true)
            {
                _logger.LogInformation($"Starting new watch stream");
                var streamTokenSource = new CancellationTokenSource();
                
                var watch = response.Watch<V1Event, V1EventList>((type, @event) =>
                    {
                        _logger.LogInformation($"Event received action here..");
                    }, (error) => { _logger.LogError(error, $"Watch error"); },
                    () =>
                    {
                        _logger.LogInformation("Event watch onClosed triggered");
                        streamTokenSource.Cancel();
                    });

                await Task.Delay(-1, streamTokenSource.Token);
            }

@petermicuch
Copy link
Author

You have to call _kubernetesClient.ListEventForAllNamespacesWithHttpMessagesAsync(watch: true); inside while loop and not reuse the stream, then it will work. Also why do you await to get a response? That is responsibility of Watcher you are getting from watch call.

            while (true)
            {
                _logger.LogInformation($"Starting new watch stream");
                var streamTokenSource = new CancellationTokenSource();
                
                var response = _kubernetesClient.ListEventForAllNamespacesWithHttpMessagesAsync(watch: true);
                var watch = response.Watch<V1Event, V1EventList>((type, @event) =>
                    {
                        _logger.LogInformation($"Event received action here..");
                    }, (error) => { _logger.LogError(error, $"Watch error"); },
                    () =>
                    {
                        _logger.LogInformation("Event watch onClosed triggered");
                        streamTokenSource.Cancel();
                    });

                await Task.Delay(-1, streamTokenSource.Token);
                watch.Dispose();
            }

I have not compiled or tried this modified code, but should work. Also consider retrying on error with some delay. Would be nice if there is some kind of resilience implemented.

If you want to know more, just check code of Watcher itself.

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jan 14, 2021
@tg123
Copy link
Member

tg123 commented Jan 26, 2021

close and move discuss to #533
workaround here
#533 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.
Projects
None yet
Development

No branches or pull requests

6 participants