-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathConfigMonitorService.cs
47 lines (41 loc) · 1.46 KB
/
ConfigMonitorService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
namespace KCert.Services;
[Service]
public class ConfigMonitorService(ILogger<ConfigMonitorService> log, KCertConfig cfg, ExponentialBackoff exp, K8sWatchClient watch, CertChangeService certChange) : IHostedService
{
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
public Task StartAsync(CancellationToken cancellationToken)
{
if (cfg.WatchConfigMaps)
{
log.LogInformation("Watching for configmaps is enabled");
var action = () => WatchConfigMapsAsync(cancellationToken);
_ = exp.DoWithExponentialBackoffAsync("Watch configmaps", action, cancellationToken);
}
else
{
log.LogInformation("Watching for configmaps is disabled");
}
return Task.CompletedTask;
}
private async Task WatchConfigMapsAsync(CancellationToken cancellationToken)
{
log.LogInformation("Watching for configmaps changes");
await watch.WatchConfigMapsAsync((_, _) => HandleConfigMapEventAsync(), cancellationToken);
}
private Task HandleConfigMapEventAsync()
{
try
{
certChange.RunCheck();
}
catch (TaskCanceledException ex)
{
log.LogError(ex, "ConfigMap event handler cancelled.");
}
catch (Exception ex)
{
log.LogError(ex, "ConfigMap event handler failed unexpectedly");
}
return Task.CompletedTask;
}
}