-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAnnotationCommandHandler.cs
91 lines (84 loc) · 3.45 KB
/
AnnotationCommandHandler.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System.Threading.Tasks;
using Akka.Util;
using Akka.Util.Extensions;
using Arcane.Operator.Extensions;
using Arcane.Operator.Models.Base;
using Arcane.Operator.Models.StreamDefinitions.Base;
using Arcane.Operator.Services.Base.CommandHandlers;
using Arcane.Operator.Services.Base.Repositories.CustomResources;
using k8s.Models;
using Microsoft.Extensions.Logging;
using Snd.Sdk.Kubernetes.Base;
using Snd.Sdk.Tasks;
namespace Arcane.Operator.Services.CommandHandlers;
public class AnnotationCommandHandler :
ICommandHandler<SetAnnotationCommand<IStreamDefinition>>,
ICommandHandler<RemoveAnnotationCommand<IStreamDefinition>>,
ICommandHandler<SetAnnotationCommand<V1Job>>
{
private readonly IStreamClassRepository streamClassRepository;
private readonly IKubeCluster kubeCluster;
private readonly ILogger<AnnotationCommandHandler> logger;
public AnnotationCommandHandler(
IStreamClassRepository streamClassRepository,
IKubeCluster kubeCluster,
ILogger<AnnotationCommandHandler> logger)
{
this.streamClassRepository = streamClassRepository;
this.kubeCluster = kubeCluster;
this.logger = logger;
}
public Task Handle(SetAnnotationCommand<IStreamDefinition> command)
{
var ((nameSpace, kind, name), annotationKey, annotationValue) = command;
return this.streamClassRepository.Get(nameSpace, kind).Map(crdConf =>
{
if (crdConf is { HasValue: false })
{
this.logger.LogError("Failed to get configuration for kind {kind}", kind);
return Task.CompletedTask;
}
return this.kubeCluster.AnnotateObject(crdConf.Value.ToNamespacedCrd(),
annotationKey,
annotationValue,
name,
nameSpace);
});
}
public Task Handle(SetAnnotationCommand<V1Job> command)
{
var ((nameSpace, name), annotationKey, annotationValue) = command;
return this.kubeCluster.AnnotateJob(name, nameSpace, annotationKey, annotationValue)
.TryMap(job => job.AsOption(),
exception =>
{
this.logger.LogError(exception, "Failed to annotate {streamId} with {annotationKey}:{annotationValue}",
command.affectedResource, command.annotationKey, command.annotationValue);
return Option<V1Job>.None;
});
}
public Task Handle(RemoveAnnotationCommand<IStreamDefinition> command)
{
var ((nameSpace, kind, name), annotationKey) = command;
return this.streamClassRepository.Get(nameSpace, kind).FlatMap(crdConf =>
{
if (crdConf is { HasValue: false })
{
this.logger.LogError("Failed to get configuration for kind {kind}", kind);
return Task.FromResult(Option<object>.None);
}
var crd = crdConf.Value.ToNamespacedCrd();
return this.kubeCluster
.RemoveObjectAnnotation(crd, annotationKey, name, nameSpace)
.TryMap(result => result.AsOption(), exception =>
{
this.logger.LogError(exception,
"Failed to remove annotation {annotationKey} from {nameSpace}/{name}",
annotationKey,
nameSpace,
name);
return default;
});
});
}
}