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

Timed entity queue should take into account namespace as well as name #801

Merged
merged 4 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
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
23 changes: 19 additions & 4 deletions src/KubeOps.Operator/Queue/TimedEntityQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal sealed class TimedEntityQueue<TEntity> : IDisposable
public void Enqueue(TEntity entity, TimeSpan requeueIn)
{
_management.AddOrUpdate(
entity.Name() ?? throw new InvalidOperationException("Cannot enqueue entities without name."),
GetKey(entity) ?? throw new InvalidOperationException("Cannot enqueue entities without name."),
key =>
{
var entry = new TimedQueueEntry<TEntity>(entity, requeueIn);
Expand Down Expand Up @@ -81,15 +81,30 @@ public async IAsyncEnumerator<TEntity> GetAsyncEnumerator(CancellationToken canc

public void Remove(TEntity entity)
{
var name = entity.Name();
if (name is null)
var key = GetKey(entity);
if (key is null)
{
return;
}

if (_management.Remove(name, out var task))
if (_management.Remove(key, out var task))
{
task.Cancel();
}
}

private string? GetKey(TEntity entity)
{
if (entity.Name() is null)
{
return null;
}

if (entity.Namespace() is null)
{
return entity.Name();
}

return $"{entity.Namespace()}/{entity.Name()}";
}
}
49 changes: 49 additions & 0 deletions test/KubeOps.Operator.Test/Queue/TimedEntityQueue.Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using k8s.Models;

using KubeOps.Operator.Queue;

namespace KubeOps.Operator.Test.Queue;

public class TimedEntityQueueTest
{
[Fact]
public async Task Can_Enqueue_Multiple_Entities_With_Same_Name()
{
var queue = new TimedEntityQueue<V1Secret>();

queue.Enqueue(CreateSecret("app-ns1", "secret-name"), TimeSpan.FromSeconds(1));
queue.Enqueue(CreateSecret("app-ns2", "secret-name"), TimeSpan.FromSeconds(1));

var items = new List<V1Secret>();

var tokenSource = new CancellationTokenSource();
tokenSource.CancelAfter(TimeSpan.FromSeconds(2));

var enumerator = queue.GetAsyncEnumerator(tokenSource.Token);

try
{
while (await enumerator.MoveNextAsync())
{
items.Add(enumerator.Current);
}
}
catch (OperationCanceledException)
{
// We expect to timeout watching the queue so that we can assert the items received
}

Assert.Equal(2, items.Count);
}

private V1Secret CreateSecret(string secretNamespace, string secretName)
{
var secret = new V1Secret();
secret.EnsureMetadata();

secret.Metadata.SetNamespace(secretNamespace);
secret.Metadata.Name = secretName;

return secret;
}
}
Loading