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

Watching resource with old version should restart #735

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 48 additions & 28 deletions src/KubeOps.Operator/Watcher/ResourceWatcher{TEntity}.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using System.Net;
using System.Runtime.Serialization;
using System.Text.Json;

Expand Down Expand Up @@ -59,39 +60,58 @@ private async Task WatchClientEventsAsync(CancellationToken stoppingToken)
{
try
{
await foreach ((WatchEventType type, TEntity? entity) in client.WatchAsync<TEntity>(
settings.Namespace,
cancellationToken: stoppingToken))
while (!stoppingToken.IsCancellationRequested)
{
await foreach ((WatchEventType type, TEntity? entity) in client.WatchAsync<TEntity>(
settings.Namespace,
cancellationToken: stoppingToken))
{
#pragma warning disable SA1312
using var _ = logger.BeginScope(new
using var _ = logger.BeginScope(new
#pragma warning restore SA1312
{
EventType = type,

// ReSharper disable once RedundantAnonymousTypePropertyName
Kind = entity?.Kind,
Name = entity?.Name(),
ResourceVersion = entity?.ResourceVersion(),
});
logger.LogInformation(
"""Received watch event "{EventType}" for "{Kind}/{Name}", last observed resource version: {ResourceVersion}.""",
type,
entity?.Kind,
entity?.Name(),
entity?.ResourceVersion());
try
{
await OnEventAsync(type, entity, stoppingToken);
}
catch (Exception e)
{
logger.LogError(
e,
"Reconciliation of {EventType} for {Kind}/{Name} failed.",
{
EventType = type,

// ReSharper disable once RedundantAnonymousTypePropertyName
Kind = entity?.Kind,
Name = entity?.Name(),
ResourceVersion = entity?.ResourceVersion(),
});
logger.LogInformation(
"""Received watch event "{EventType}" for "{Kind}/{Name}", last observed resource version: {ResourceVersion}.""",
type,
entity?.Kind,
entity.Name());
entity?.Name(),
entity?.ResourceVersion());
try
{
await OnEventAsync(type, entity, stoppingToken);
}
catch (KubernetesException e)
{
if (e.Status.Code == (int)HttpStatusCode.Gone)
{
logger.LogDebug("Watch restarting due to 410 HTTP Gone");

break;
}

LogReconciliationFailed(e);
}
catch (Exception e)
{
LogReconciliationFailed(e);
}

void LogReconciliationFailed(Exception exception)
{
logger.LogError(
exception,
"Reconciliation of {EventType} for {Kind}/{Name} failed.",
type,
entity?.Kind,
entity.Name());
}
}
}
}
Expand Down
Loading