Skip to content

Commit 8c1848f

Browse files
committed
fix: add entity UID into event name to generate relation.
This fixes [bug]: Events are not correctly generated in relation with the source entity #681.
1 parent 0b4aa96 commit 8c1848f

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/KubeOps.Operator/Builder/OperatorBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private static Func<IServiceProvider, EventPublisher> CreateEventPublisher()
128128
message,
129129
type);
130130

131-
var eventName = $"{entity.Name()}.{@namespace}.{reason}.{message}.{type}";
131+
var eventName = $"{entity.Uid()}.{entity.Name()}.{@namespace}.{reason}.{message}.{type}";
132132
var encodedEventName =
133133
Convert.ToHexString(
134134
SHA512.HashData(

src/KubeOps.Transpiler/Crds.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ private static V1JSONSchemaProps Map(this MetadataLoadContext context, Type type
330330
"System.ValueType" => context.MapValueType(type),
331331
"System.Enum" => new V1JSONSchemaProps
332332
{
333-
Type = String, EnumProperty = Enum.GetNames(type).Cast<object>().ToList(),
333+
Type = String,
334+
EnumProperty = Enum.GetNames(type).Cast<object>().ToList(),
334335
},
335336
_ => throw InvalidType(type),
336337
};

test/KubeOps.Operator.Test/Events/EventPublisher.Integration.Test.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ public class EventPublisherIntegrationTest : IntegrationTestBase
2525
[Fact]
2626
public async Task Should_Create_New_Event()
2727
{
28-
var eventName = $"single-entity.{_ns.Namespace}.REASON.message.Normal";
28+
var entity =
29+
await _client.CreateAsync(new V1OperatorIntegrationTestEntity("single-entity", "username", _ns.Namespace));
30+
await _mock.WaitForInvocations;
31+
32+
var eventName = $"{entity.Uid()}.single-entity.{_ns.Namespace}.REASON.message.Normal";
2933
var encodedEventName =
3034
Convert.ToHexString(
3135
SHA512.HashData(
3236
Encoding.UTF8.GetBytes(eventName)));
3337

34-
await _client.CreateAsync(new V1OperatorIntegrationTestEntity("single-entity", "username", _ns.Namespace));
35-
await _mock.WaitForInvocations;
36-
3738
var e = await _client.GetAsync<Corev1Event>(encodedEventName, _ns.Namespace);
3839
e!.Count.Should().Be(1);
3940
e.Metadata.Annotations.Should().Contain(a => a.Key == "originalName" && a.Value == eventName);
@@ -43,15 +44,16 @@ public async Task Should_Create_New_Event()
4344
public async Task Should_Increase_Count_On_Existing_Event()
4445
{
4546
_mock.TargetInvocationCount = 5;
46-
var eventName = $"test-entity.{_ns.Namespace}.REASON.message.Normal";
47+
var entity =
48+
await _client.CreateAsync(new V1OperatorIntegrationTestEntity("test-entity", "username", _ns.Namespace));
49+
await _mock.WaitForInvocations;
50+
51+
var eventName = $"{entity.Uid()}.test-entity.{_ns.Namespace}.REASON.message.Normal";
4752
var encodedEventName =
4853
Convert.ToHexString(
4954
SHA512.HashData(
5055
Encoding.UTF8.GetBytes(eventName)));
5156

52-
await _client.CreateAsync(new V1OperatorIntegrationTestEntity("test-entity", "username", _ns.Namespace));
53-
await _mock.WaitForInvocations;
54-
5557
var e = await _client.GetAsync<Corev1Event>(encodedEventName, _ns.Namespace);
5658
e!.Count.Should().Be(5);
5759
e.Metadata.Annotations.Should().Contain(a => a.Key == "originalName" && a.Value == eventName);
@@ -78,9 +80,10 @@ protected override void ConfigureHost(HostApplicationBuilder builder)
7880
.AddController<TestController, V1OperatorIntegrationTestEntity>();
7981
}
8082

81-
private class TestController(InvocationCounter<V1OperatorIntegrationTestEntity> svc,
82-
EntityRequeue<V1OperatorIntegrationTestEntity> requeue,
83-
EventPublisher eventPublisher)
83+
private class TestController(
84+
InvocationCounter<V1OperatorIntegrationTestEntity> svc,
85+
EntityRequeue<V1OperatorIntegrationTestEntity> requeue,
86+
EventPublisher eventPublisher)
8487
: IEntityController<V1OperatorIntegrationTestEntity>
8588
{
8689
public async Task ReconcileAsync(V1OperatorIntegrationTestEntity entity)

0 commit comments

Comments
 (0)