-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAkkaHostingExtensions.cs
142 lines (137 loc) · 5.98 KB
/
AkkaHostingExtensions.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// -----------------------------------------------------------------------
// <copyright file="AkkaHostingExtensions.cs" company="Akka.NET Project">
// Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
// -----------------------------------------------------------------------
using System;
using Akka.Actor;
using Akka.Hosting;
namespace Akka.Discovery.KubernetesApi
{
public static class AkkaHostingExtensions
{
/// <summary>
/// Adds Akka.Discovery.KubernetesApi support to the <see cref="ActorSystem"/>.
/// Note that this only adds the discovery plugin, you will still need to add ClusterBootstrap for
/// a complete solution.
/// </summary>
/// <param name="builder">
/// The builder instance being configured.
/// </param>
/// <param name="podLabelSelector">
/// <para>
/// Optional. Pod label selector value to query pod API with. See the official Kubernetes documentation on
/// <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors">
/// label selectors</a> for more information.
/// </para>
/// <b>Default</b>: "app={0}", where "{0}" will be replaced by
/// <c>ClusterBootstrapSetup.ContactPointDiscovery.ServiceName</c> value.
/// </param>
/// <returns>
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.
/// </returns>
/// <example>
/// <code>
/// // In this code sample, the final label selector would be "app=testService".
/// services.AddAkka("mySystem", builder => {
/// builder
/// .WithClustering()
/// .WithClusterBootstrap(options =>
/// {
/// options.ContactPointDiscovery.ServiceName = "testService";
/// }, autoStart: true)
/// .WithKubernetesDiscovery();
/// }
/// </code>
/// </example>
public static AkkaConfigurationBuilder WithKubernetesDiscovery(
this AkkaConfigurationBuilder builder,
string? podLabelSelector = null)
{
return builder.WithKubernetesDiscovery(new KubernetesDiscoveryOptions
{
PodLabelSelector = podLabelSelector
});
}
/// <summary>
/// Adds Akka.Discovery.KubernetesApi support to the <see cref="ActorSystem"/>.
/// Note that this only adds the discovery plugin, you will still need to add ClusterBootstrap for
/// a complete solution.
/// </summary>
/// <param name="builder">
/// The builder instance being configured.
/// </param>
/// <param name="configure">
/// An action that modifies an <see cref="KubernetesDiscoveryOptions"/> instance, used
/// to configure Akka.Discovery.KubernetesApi.
/// </param>
/// <returns>
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.
/// </returns>
/// <example>
/// <code>
/// services.AddAkka("mySystem", builder => {
/// builder
/// .WithClustering()
/// .WithClusterBootstrap(options =>
/// {
/// options.ContactPointDiscovery.ServiceName = "testService";
/// }, autoStart: true)
/// .WithKubernetesDiscovery(options => {
/// options.PodNamespace = "my-cluster-namespace";
/// });
/// }
/// </code>
/// </example>
public static AkkaConfigurationBuilder WithKubernetesDiscovery(
this AkkaConfigurationBuilder builder,
Action<KubernetesDiscoveryOptions> configure)
{
var options = new KubernetesDiscoveryOptions();
configure(options);
return builder.WithKubernetesDiscovery(options);
}
/// <summary>
/// Adds Akka.Discovery.KubernetesApi support to the <see cref="ActorSystem"/>.
/// Note that this only adds the discovery plugin, you will still need to add ClusterBootstrap for
/// a complete solution.
/// </summary>
/// <param name="builder">
/// The builder instance being configured.
/// </param>
/// <param name="options">
/// The <see cref="KubernetesDiscoveryOptions"/> instance used to configure Akka.Discovery.KubernetesApi.
/// </param>
/// <returns>
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.
/// </returns>
/// <example>
/// <code>
/// services.AddAkka("mySystem", builder => {
/// builder
/// .WithClustering()
/// .WithClusterBootstrap(options =>
/// {
/// options.ContactPointDiscovery.ServiceName = "testService";
/// }, autoStart: true)
/// .WithKubernetesDiscovery(new KubernetesDiscoveryOptions {
/// PodNamespace = "my-cluster-namespace"
/// });
/// }
/// </code>
/// </example>
public static AkkaConfigurationBuilder WithKubernetesDiscovery(
this AkkaConfigurationBuilder builder,
KubernetesDiscoveryOptions options)
{
options.Apply(builder);
// Force start the module
builder.AddStartup((system, _) =>
{
KubernetesDiscovery.Get(system);
});
return builder;
}
}
}