-
Notifications
You must be signed in to change notification settings - Fork 2k
/
KubernetesHostingExtensions.cs
75 lines (67 loc) · 3.23 KB
/
KubernetesHostingExtensions.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
using k8s;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Orleans.Configuration;
using Orleans.Hosting.Kubernetes;
using Orleans.Runtime;
using System;
namespace Orleans.Hosting
{
/// <summary>
/// Extensions for hosting a silo in Kubernetes.
/// </summary>
public static class KubernetesHostingExtensions
{
/// <summary>
/// Adds Kubernetes hosting support.
/// </summary>
public static ISiloBuilder UseKubernetesHosting(this ISiloBuilder siloBuilder)
{
return siloBuilder.ConfigureServices(services => services.UseKubernetesHosting(configureOptions: null));
}
/// <summary>
/// Adds Kubernetes hosting support.
/// </summary>
public static ISiloBuilder UseKubernetesHosting(this ISiloBuilder siloBuilder, Action<OptionsBuilder<KubernetesHostingOptions>> configureOptions)
{
return siloBuilder.ConfigureServices(services => services.UseKubernetesHosting(configureOptions));
}
/// <summary>
/// Adds Kubernetes hosting support.
/// </summary>
public static IServiceCollection UseKubernetesHosting(this IServiceCollection services) => services.UseKubernetesHosting(configureOptions: null);
/// <summary>
/// Adds Kubernetes hosting support.
/// </summary>
public static IServiceCollection UseKubernetesHosting(this IServiceCollection services, Action<OptionsBuilder<KubernetesHostingOptions>> configureOptions)
{
configureOptions?.Invoke(services.AddOptions<KubernetesHostingOptions>());
// Configure defaults based on the current environment.
services.AddSingleton<IConfigureOptions<ClusterOptions>, ConfigureKubernetesHostingOptions>();
services.AddSingleton<IConfigureOptions<SiloOptions>, ConfigureKubernetesHostingOptions>();
services.AddSingleton<IPostConfigureOptions<EndpointOptions>, ConfigureKubernetesHostingOptions>();
services.AddSingleton<IConfigureOptions<KubernetesHostingOptions>, ConfigureKubernetesHostingOptions>();
services.AddSingleton<IValidateOptions<KubernetesHostingOptions>, KubernetesHostingOptionsValidator>();
services.AddSingleton<ILifecycleParticipant<ISiloLifecycle>, KubernetesClusterAgent>();
// Configure the Kubernetes client.
services.AddHttpClient("Orleans.Kubernetes.Agent")
.AddTypedClient<IKubernetes>((httpClient, serviceProvider) =>
{
var config = serviceProvider.GetRequiredService<KubernetesHostingOptions>().ClientConfiguration;
return new k8s.Kubernetes(
config,
httpClient);
}).ConfigurePrimaryHttpMessageHandler(serviceProvider =>
{
var config = serviceProvider.GetRequiredService<KubernetesHostingOptions>().ClientConfiguration;
return config.CreateDefaultHttpClientHandler();
})
#if NETSTANDARD2_0
.AddHttpMessageHandler(KubernetesClientConfiguration.CreateWatchHandler);
#else
;
#endif
return services;
}
}
}