|
| 1 | +using Aspire.Hosting.Lifecycle; |
| 2 | +using Aspire.Hosting.Utils; |
| 3 | +using HealthChecks.Elasticsearch; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using Microsoft.Extensions.Diagnostics.HealthChecks; |
| 6 | + |
| 7 | +namespace Aspire.Hosting; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Provides extension methods for adding Elasticsearch resources to the application model. |
| 11 | +/// </summary> |
| 12 | +public static class ElasticsearchBuilderExtensions |
| 13 | +{ |
| 14 | + private const int ElasticsearchPort = 9200; |
| 15 | + private const int ElasticsearchInternalPort = 9300; |
| 16 | + private const int KibanaPort = 5601; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Adds a Elasticsearch container to the application model. The default image is "docker.elastic.co/elasticsearch/elasticsearch". This version the package defaults to the 8.17.0 tag of the Elasticsearch container image |
| 20 | + /// </summary> |
| 21 | + /// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param> |
| 22 | + /// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param> |
| 23 | + /// <param name="port">The host port to bind the underlying container to.</param> |
| 24 | + /// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns> |
| 25 | + public static IResourceBuilder<ElasticsearchResource> AddElasticsearch(this IDistributedApplicationBuilder builder, [ResourceName] string name, int? port = null) |
| 26 | + { |
| 27 | + ArgumentNullException.ThrowIfNull(builder); |
| 28 | + ArgumentNullException.ThrowIfNull(name); |
| 29 | + |
| 30 | + var elasticsearch = new ElasticsearchResource(name); |
| 31 | + |
| 32 | + string? connectionString = null; |
| 33 | + ElasticsearchOptions? options = null; |
| 34 | + |
| 35 | + builder.Eventing.Subscribe<ConnectionStringAvailableEvent>(elasticsearch, async (@event, ct) => |
| 36 | + { |
| 37 | + connectionString = await elasticsearch.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false); |
| 38 | + if (connectionString is null) |
| 39 | + { |
| 40 | + throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{elasticsearch.Name}' resource but the connection string was null."); |
| 41 | + } |
| 42 | + |
| 43 | + options = new ElasticsearchOptions(); |
| 44 | + options.UseServer(connectionString); |
| 45 | + }); |
| 46 | + |
| 47 | + var healthCheckKey = $"{name}_check"; |
| 48 | + builder.Services.AddHealthChecks() |
| 49 | + .Add(new HealthCheckRegistration( |
| 50 | + healthCheckKey, |
| 51 | + sp => new ElasticsearchHealthCheck(options!), |
| 52 | + failureStatus: default, |
| 53 | + tags: default, |
| 54 | + timeout: default)); |
| 55 | + |
| 56 | + return builder.AddResource(elasticsearch) |
| 57 | + .WithImage(ElasticsearchContainerImageTags.Image, ElasticsearchContainerImageTags.Tag) |
| 58 | + .WithImageRegistry(ElasticsearchContainerImageTags.ElasticsearchRegistry) |
| 59 | + .WithHttpEndpoint(targetPort: ElasticsearchPort, port: port, name: ElasticsearchResource.PrimaryEndpointName) |
| 60 | + .WithEndpoint(targetPort: ElasticsearchInternalPort, name: ElasticsearchResource.InternalEndpointName) |
| 61 | + .WithEnvironment("discovery.type", "single-node") |
| 62 | + .WithEnvironment("xpack.security.enabled", "false") |
| 63 | + .WithEnvironment("action.destructive_requires_name", "false") |
| 64 | + .WithEnvironment("ES_JAVA_OPTS", "-Xms1g -Xmx1g") |
| 65 | + .WithHealthCheck(healthCheckKey) |
| 66 | + .PublishAsConnectionString(); |
| 67 | + } |
| 68 | + |
| 69 | + public static IResourceBuilder<ElasticsearchResource> WithKibana(this IResourceBuilder<ElasticsearchResource> builder, Action<IResourceBuilder<KibanaResource>>? configureContainer = null, string? containerName = null) |
| 70 | + { |
| 71 | + ArgumentNullException.ThrowIfNull(builder); |
| 72 | + |
| 73 | + if (builder.ApplicationBuilder.Resources.OfType<KibanaResource>().SingleOrDefault() is { } existingKibanaResource) |
| 74 | + { |
| 75 | + var builderForExistingResource = builder.ApplicationBuilder.CreateResourceBuilder(existingKibanaResource); |
| 76 | + configureContainer?.Invoke(builderForExistingResource); |
| 77 | + return builder; |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + containerName ??= $"{builder.Resource.Name}-kibana"; |
| 82 | + |
| 83 | + builder.ApplicationBuilder.Services.TryAddLifecycleHook<KibanaConfigWriterHook>(); |
| 84 | + |
| 85 | + var resource = new KibanaResource(containerName); |
| 86 | + var resourceBuilder = builder.ApplicationBuilder.AddResource(resource) |
| 87 | + .WithImage(ElasticsearchContainerImageTags.KibanaImage, ElasticsearchContainerImageTags.Tag) |
| 88 | + .WithImageRegistry(ElasticsearchContainerImageTags.KibanaRegistry) |
| 89 | + .WithHttpEndpoint(targetPort: KibanaPort, name: containerName) |
| 90 | + .WithEnvironment("xpack.security.enabled", "false") |
| 91 | + .ExcludeFromManifest(); |
| 92 | + |
| 93 | + configureContainer?.Invoke(resourceBuilder); |
| 94 | + |
| 95 | + return builder; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public static IResourceBuilder<ElasticsearchResource> WithDataVolume(this IResourceBuilder<ElasticsearchResource> builder, string? name = null) |
| 100 | + { |
| 101 | + ArgumentNullException.ThrowIfNull(builder); |
| 102 | + |
| 103 | + return builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/usr/share/elasticsearch/data"); |
| 104 | + } |
| 105 | + |
| 106 | + public static IResourceBuilder<ElasticsearchResource> WithDataBindMount(this IResourceBuilder<ElasticsearchResource> builder, string source) |
| 107 | + { |
| 108 | + ArgumentNullException.ThrowIfNull(builder); |
| 109 | + ArgumentNullException.ThrowIfNull(source); |
| 110 | + |
| 111 | + return builder.WithBindMount(source, "/usr/share/elasticsearch/data"); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +internal static class ElasticsearchContainerImageTags |
| 116 | +{ |
| 117 | + public const string ElasticsearchRegistry = "docker.io"; |
| 118 | + public const string Image = "exceptionless/elasticsearch"; |
| 119 | + public const string KibanaRegistry = "docker.elastic.co"; |
| 120 | + public const string KibanaImage = "kibana/kibana"; |
| 121 | + public const string Tag = "8.17.0"; |
| 122 | +} |
0 commit comments