Skip to content
Open
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
42 changes: 20 additions & 22 deletions src/KubeOps.Operator/Watcher/ResourceWatcher{TEntity}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ protected virtual async Task OnEventAsync(WatchEventType type, TEntity entity, C
{
MaybeValue<long?> cachedGeneration;

// Make sure Finalizers are running if Termination has began.
if (type != WatchEventType.Deleted && entity.Metadata.DeletionTimestamp is not null && entity.Metadata.Finalizers.Count > 0)
{
await ReconcileFinalizersSequentialAsync(entity, cancellationToken);
return;
}

switch (type)
{
case WatchEventType.Added:
Expand All @@ -161,31 +168,22 @@ protected virtual async Task OnEventAsync(WatchEventType type, TEntity entity, C

break;
case WatchEventType.Modified:
switch (entity)
{
case { Metadata.DeletionTimestamp: null }:
cachedGeneration = await _entityCache.TryGetAsync<long?>(entity.Uid(), token: cancellationToken);

// Check if entity spec has changed through "Generation" value increment. Skip reconcile if not changed.
if (cachedGeneration.HasValue && cachedGeneration >= entity.Generation())
{
logger.LogDebug(
"""Entity "{Kind}/{Name}" modification did not modify generation. Skip event.""",
entity.Kind,
entity.Name());
return;
}

// update cached generation since generation now changed
await _entityCache.SetAsync(entity.Uid(), entity.Generation() ?? 1, token: cancellationToken);
await ReconcileModificationAsync(entity, cancellationToken);
cachedGeneration = await _entityCache.TryGetAsync<long?>(entity.Uid(), token: cancellationToken);

break;
case { Metadata: { DeletionTimestamp: not null, Finalizers.Count: > 0 } }:
await ReconcileFinalizersSequentialAsync(entity, cancellationToken);
break;
// Check if entity spec has changed through "Generation" value increment. Skip reconcile if not changed.
if (cachedGeneration.HasValue && cachedGeneration >= entity.Generation())
{
logger.LogDebug(
"""Entity "{Kind}/{Name}" modification did not modify generation. Skip event.""",
entity.Kind,
entity.Name());
return;
}

// update cached generation since generation now changed
await _entityCache.SetAsync(entity.Uid(), entity.Generation() ?? 1, token: cancellationToken);
await ReconcileModificationAsync(entity, cancellationToken);

break;
case WatchEventType.Deleted:
await ReconcileDeletionAsync(entity, cancellationToken);
Expand Down