Skip to content

Commit

Permalink
fix(event publish): improve error handling and use default as namespa…
Browse files Browse the repository at this point in the history
…ce if resource does not provide one.
  • Loading branch information
buehler committed Aug 27, 2021
1 parent 31de13a commit 0fe6c66
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/KubeOps/Operator/Events/EventManager.cs
Expand Up @@ -31,7 +31,7 @@ public EventManager(IKubernetesClient client, OperatorSettings settings, ILogger
string message,
EventType type = EventType.Normal)
{
var resourceNamespace = resource.Namespace() ?? await _client.GetCurrentNamespace();
var resourceNamespace = resource.Namespace() ?? "default";

_logger.LogTrace(
"Encoding event name with: {resourceName}.{resourceNamespace}.{reason}.{message}.{type}.",
Expand All @@ -50,7 +50,7 @@ public EventManager(IKubernetesClient client, OperatorSettings settings, ILogger
{
Kind = Corev1Event.KubeKind,
ApiVersion = $"{Corev1Event.KubeGroup}/{Corev1Event.KubeApiVersion}",
Metadata = new V1ObjectMeta
Metadata = new()
{
Name = eventName,
NamespaceProperty = resourceNamespace,
Expand All @@ -65,7 +65,10 @@ public EventManager(IKubernetesClient client, OperatorSettings settings, ILogger
Message = message,
ReportingComponent = _settings.Name,
ReportingInstance = Environment.MachineName,
Source = new V1EventSource { Component = _settings.Name },
Source = new()
{
Component = _settings.Name,
},
InvolvedObject = resource.MakeObjectReference(),
FirstTimestamp = DateTime.UtcNow,
LastTimestamp = DateTime.UtcNow,
Expand All @@ -79,13 +82,25 @@ public EventManager(IKubernetesClient client, OperatorSettings settings, ILogger
@event.Count,
@event.LastTimestamp);

await _client.Save(@event);
_logger.LogInformation(
@"Created or updated event with name ""{name}"" to new count {count} on resource ""{kind}/{name}"".",
eventName,
@event.Count,
resource.Kind,
resource.Name());
try
{
await _client.Save(@event);
_logger.LogInformation(
@"Created or updated event with name ""{name}"" to new count {count} on resource ""{kind}/{name}"".",
eventName,
@event.Count,
resource.Kind,
resource.Name());
}
catch (Exception e)
{
_logger.LogError(
e,
@"Could not publish event with name ""{name}"" on resource ""{kind}/{name}"".",
eventName,
resource.Kind,
resource.Name());
}
}

public Task PublishAsync(Corev1Event @event)
Expand Down

0 comments on commit 0fe6c66

Please sign in to comment.