|
| 1 | +# Events |
| 2 | + |
| 3 | +Kubernetes knows "Events" which can be sort of attached to a resource |
| 4 | +(i.e. a Kubernetes object). |
| 5 | + |
| 6 | +To create and use events, inject the @"KubeOps.Operator.Events.IEventManager" |
| 7 | +into your controller. It is registered as a transient resource in the DI |
| 8 | +container. |
| 9 | + |
| 10 | +## IEventManager |
| 11 | + |
| 12 | +### Publish events |
| 13 | + |
| 14 | +The event manager allows you to either publish an event that you created |
| 15 | +by yourself, or helps you publish events with predefined data. |
| 16 | + |
| 17 | +If you want to use the helper: |
| 18 | +```c# |
| 19 | +// fetch from DI, or inject into your controller. |
| 20 | +IEventManager manager = services.GetRequiredService<IEventManager>; |
| 21 | + |
| 22 | +// Publish the event. |
| 23 | +// This creates an event and publishes it. |
| 24 | +// If the event was previously published, it is fetched |
| 25 | +// and the "count" number is increased. This essentially |
| 26 | +// creates an event-series. |
| 27 | +await manager.Publish(resource, "reason", "my fancy message"); |
| 28 | +``` |
| 29 | + |
| 30 | +If you want full control over the event: |
| 31 | +```c# |
| 32 | +// fetch from DI, or inject into your controller. |
| 33 | +IEventManager manager = services.GetRequiredService<IEventManager>; |
| 34 | + |
| 35 | +var @event = new Corev1Event |
| 36 | + { |
| 37 | + // ... fill out all fields. |
| 38 | + } |
| 39 | + |
| 40 | +// Publish the event. |
| 41 | +// This essentially calls IKubernetesClient.Save. |
| 42 | +await manager.Publish(@event); |
| 43 | +``` |
| 44 | + |
| 45 | +### Use publisher delegates |
| 46 | + |
| 47 | +If you don't want to call the |
| 48 | +@"KubeOps.Operator.Events.IEventManager.Publish(k8s.IKubernetesObject{k8s.Models.V1ObjectMeta},System.String,System.String,KubeOps.Operator.Events.EventType)" |
| 49 | +all the time with the same arguments, you can create delegates. |
| 50 | + |
| 51 | +There exist two different delegates: |
| 52 | +- @"KubeOps.Operator.Events.IEventManager.StaticPublisher": Predefined event |
| 53 | + on a predefined resource. |
| 54 | +- @"KubeOps.Operator.Events.IEventManager.Publisher": Predefined event |
| 55 | + on a variable resource. |
| 56 | + |
| 57 | +Both are created with their specific overload: |
| 58 | +- @"KubeOps.Operator.Events.IEventManager.CreatePublisher(k8s.IKubernetesObject{k8s.Models.V1ObjectMeta},System.String,System.String,KubeOps.Operator.Events.EventType)" |
| 59 | +- @"KubeOps.Operator.Events.IEventManager.CreatePublisher(System.String,System.String,KubeOps.Operator.Events.EventType)" |
| 60 | + |
| 61 | +To use the static publisher: |
| 62 | +```c# |
| 63 | +var publisher = manager.CreatePublisher(resource, "reason", "message"); |
| 64 | +await publisher(); |
| 65 | + |
| 66 | +// and later on: |
| 67 | +await publisher(); // again without specifying reason / message and so on. |
| 68 | +``` |
| 69 | + |
| 70 | +To use the dynamic publisher: |
| 71 | +```c# |
| 72 | +var publisher = manager.CreatePublisher("reason", "message"); |
| 73 | +await publisher(resource); |
| 74 | + |
| 75 | +// and later on: |
| 76 | +await publisher(resource); // again without specifying reason / message and so on. |
| 77 | +``` |
| 78 | + |
| 79 | +The dynamic publisher can be used to predefine the event for your resources. |
| 80 | + |
| 81 | +As an example in a controller: |
| 82 | +```c# |
| 83 | +public class TestController : ResourceControllerBase<V1TestEntity> |
| 84 | +{ |
| 85 | + private readonly IEventManager.Publisher _publisher; |
| 86 | + |
| 87 | + public TestController(IEventManager eventManager, IResourceServices<V1TestEntity> services) |
| 88 | + : base(services) |
| 89 | + { |
| 90 | + _publisher = eventManager.CreatePublisher("reason", "my fancy message"); |
| 91 | + } |
| 92 | + |
| 93 | + protected override async Task<TimeSpan?> Created(V1TestEntity resource) |
| 94 | + { |
| 95 | + // Here, the event is published with predefined strings |
| 96 | + // but for a "variable" resource. |
| 97 | + await _publisher(resource); |
| 98 | + return await base.Created(resource); |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
0 commit comments