Skip to content

Commit 2a9a458

Browse files
author
Christoph Bühler
committed
fix: check the exception in the resource watcher before informing
This fixes #17. The error was that sometimes the server did close the connection on purpose. This then threw an exception.
1 parent 7498304 commit 2a9a458

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/KubeOps/Operator/Queue/ResourceEventQueue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public ResourceEventQueue(
4949

5050
public async Task Start()
5151
{
52-
_logger.LogTrace(@"Event queue startup for type ""{type}"".", typeof(TEntity));
52+
_logger.LogDebug(@"Event queue startup for type ""{type}"".", typeof(TEntity));
5353
_cancellation ??= new CancellationTokenSource();
5454
_watcher.WatcherEvent += OnWatcherEvent;
5555
await _watcher.Start();

src/KubeOps/Operator/Watcher/ResourceWatcher.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Threading;
34
using System.Threading.Tasks;
45
using k8s;
@@ -30,7 +31,7 @@ public ResourceWatcher(ILogger<ResourceWatcher<TEntity>> logger, IKubernetesClie
3031

3132
public Task Start()
3233
{
33-
_logger.LogTrace(@"Resource Watcher startup for type ""{type}"".", typeof(TEntity));
34+
_logger.LogDebug(@"Resource Watcher startup for type ""{type}"".", typeof(TEntity));
3435
return WatchResource();
3536
}
3637

@@ -69,15 +70,15 @@ private async Task WatchResource()
6970
}
7071
else
7172
{
72-
_logger.LogDebug(@"Watcher for type ""{type}"" already running.", typeof(TEntity));
73+
_logger.LogTrace(@"Watcher for type ""{type}"" already running.", typeof(TEntity));
7374
return;
7475
}
7576
}
7677

7778
_cancellation = new CancellationTokenSource();
7879
// TODO: namespaced resources
7980
_watcher = await _client.Watch<TEntity>(
80-
TimeSpan.FromHours(1),
81+
TimeSpan.FromMinutes(1),
8182
OnWatcherEvent,
8283
OnException,
8384
OnClose,
@@ -119,11 +120,20 @@ private void OnWatcherEvent(WatchEventType type, TEntity resource)
119120

120121
private void OnException(Exception e)
121122
{
122-
_logger.LogError(e, @"There was an error while watching the resource ""{resource}"".", typeof(TEntity));
123123
_cancellation?.Cancel();
124124
_watcher?.Dispose();
125125
_watcher = null;
126126

127+
if (e is TaskCanceledException && e.InnerException is IOException)
128+
{
129+
_logger.LogTrace(
130+
@"Either the server or the client did close the connection on watcher for resource ""{resource}"". Restart.",
131+
typeof(TEntity));
132+
WatchResource().ConfigureAwait(false);
133+
return;
134+
}
135+
136+
_logger.LogError(e, @"There was an error while watching the resource ""{resource}"".", typeof(TEntity));
127137
var backoff = _reconnectHandler.Retry(TimeSpan.FromSeconds(5));
128138
_logger.LogInformation("Trying to reconnect with exponential backoff {backoff}.", backoff);
129139
}
@@ -132,7 +142,7 @@ private void OnClose()
132142
{
133143
if (_cancellation != null && !_cancellation.IsCancellationRequested)
134144
{
135-
_logger.LogInformation("The server closed the connection. Trying to reconnect.");
145+
_logger.LogDebug("The server closed the connection. Trying to reconnect.");
136146
RestartWatcher();
137147
}
138148
}

0 commit comments

Comments
 (0)