Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace KubeOps.Operator.Test.Controller;

public class EntityControllerIntegrationTest : IntegrationTestBase
public sealed class EntityControllerIntegrationTest : IntegrationTestBase
{
private readonly InvocationCounter<V1OperatorIntegrationTestEntity> _mock = new();
private readonly IKubernetesClient _client = new KubernetesClient.KubernetesClient();
Expand All @@ -25,7 +25,7 @@ public class EntityControllerIntegrationTest : IntegrationTestBase
[Fact]
public async Task Should_Call_Reconcile_On_New_Entity()
{
await _client.CreateAsync(new V1OperatorIntegrationTestEntity("test-entity", "username", _ns.Namespace));
await _client.CreateAsync(new V1OperatorIntegrationTestEntity("test-entity", "username", _ns.Namespace, true));
await _mock.WaitForInvocations;

_mock.Invocations.Count.Should().Be(1);
Expand Down Expand Up @@ -119,12 +119,22 @@ protected override void ConfigureHost(HostApplicationBuilder builder)
.AddController<TestController, V1OperatorIntegrationTestEntity>();
}

private class TestController(InvocationCounter<V1OperatorIntegrationTestEntity> svc) : IEntityController<V1OperatorIntegrationTestEntity>
private sealed class TestController(
InvocationCounter<V1OperatorIntegrationTestEntity> svc,
IKubernetesClient client) : IEntityController<V1OperatorIntegrationTestEntity>
{
public Task<ReconciliationResult<V1OperatorIntegrationTestEntity>> ReconcileAsync(V1OperatorIntegrationTestEntity entity, CancellationToken cancellationToken)
public async Task<ReconciliationResult<V1OperatorIntegrationTestEntity>> ReconcileAsync(V1OperatorIntegrationTestEntity entity, CancellationToken cancellationToken)
{
if (entity.Spec.CouldChangeStatus)
{
// status change: issue 1001 (https://github.com/dotnet/dotnet-operator-sdk/issues/1001)
entity.Status.Status = "reconciled";
entity = await client.UpdateStatusAsync(entity, cancellationToken);
}

svc.Invocation(entity);
return Task.FromResult(ReconciliationResult<V1OperatorIntegrationTestEntity>.Success(entity));

return ReconciliationResult<V1OperatorIntegrationTestEntity>.Success(entity);
}

public Task<ReconciliationResult<V1OperatorIntegrationTestEntity>> DeletedAsync(V1OperatorIntegrationTestEntity entity, CancellationToken cancellationToken)
Expand Down
4 changes: 2 additions & 2 deletions test/KubeOps.Operator.Test/IntegrationTestCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public sealed class TestNamespaceProvider : IAsyncLifetime
private readonly IKubernetesClient _client = new KubernetesClient.KubernetesClient();
private V1Namespace _namespace = null!;

public string Namespace { get; } = Guid.NewGuid().ToString().ToLower();
public string Namespace { get; } = $"kubeops-{Guid.NewGuid().ToString().ToLower()}";

public async Task InitializeAsync()
{
_namespace =
await _client.CreateAsync(new V1Namespace()
await _client.CreateAsync(new V1Namespace
{
Metadata = new() { Name = Namespace },
}.Initialize());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System.Text.Json.Serialization;

using k8s.Models;

using KubeOps.Abstractions.Entities;
Expand All @@ -18,10 +20,11 @@ public V1OperatorIntegrationTestEntity()
Kind = "OperatorIntegrationTest";
}

public V1OperatorIntegrationTestEntity(string name, string username, string ns) : this()
public V1OperatorIntegrationTestEntity(string name, string username, string ns, bool couldChangeStatus = false) : this()
{
Metadata.Name = name;
Spec.Username = username;
Spec.CouldChangeStatus = couldChangeStatus;
Metadata.NamespaceProperty = ns;
}

Expand All @@ -30,6 +33,8 @@ public V1OperatorIntegrationTestEntity(string name, string username, string ns)
public sealed class EntitySpec
{
public string Username { get; set; } = string.Empty;

public bool CouldChangeStatus { get; set; }
}

public sealed class EntityStatus
Expand Down