|
| 1 | +using System.Text; |
| 2 | +using Aspire.Hosting.Lifecycle; |
| 3 | +using Aspire.Hosting.Utils; |
| 4 | +using Microsoft.Extensions.Configuration; |
| 5 | + |
| 6 | +namespace Aspire.Hosting; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Provides extension methods for adding Elasticsearch resources to the application model. |
| 10 | +/// </summary> |
| 11 | +public static class ElasticsearchBuilderExtensions |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Adds a Elasticsearch container to the application model. The default image is "docker.elastic.co/elasticsearch/elasticsearch" and tag is "latest". This version the package defaults to the 8.12.2 tag of the redis container image |
| 15 | + /// </summary> |
| 16 | + /// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param> |
| 17 | + /// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param> |
| 18 | + /// <param name="port">The host port to bind the underlying container to.</param> |
| 19 | + /// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns> |
| 20 | + public static IResourceBuilder<ElasticsearchResource> AddElasticsearch(this IDistributedApplicationBuilder builder, string name, int? port = null) |
| 21 | + { |
| 22 | + var redis = new ElasticsearchResource(name); |
| 23 | + return builder.AddResource(redis) |
| 24 | + .WithHttpEndpoint(containerPort: 9200) |
| 25 | + .WithAnnotation(new ContainerImageAnnotation { Image = "docker.elastic.co/elasticsearch/elasticsearch", Tag = "8.12.2" }) |
| 26 | + .WithEnvironment("discovery.type", "single-node") |
| 27 | + //.WithEnvironment("network.host", "0.0.0.0") |
| 28 | + .WithEnvironment("XPACK_SECURITY_ENABLED", "false") |
| 29 | + .WithEnvironment("action.destructive_requires_name", "false") |
| 30 | + .WithEnvironment("ES_JAVA_OPTS", "-Xms1g -Xmx1g") |
| 31 | + .PublishAsContainer(); |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// TODO: Doc Comments |
| 36 | + /// </summary> |
| 37 | + /// <param name="builder"></param> |
| 38 | + /// <param name="containerName"></param> |
| 39 | + /// <param name="hostPort"></param> |
| 40 | + /// <returns></returns> |
| 41 | + public static IResourceBuilder<ElasticsearchResource> WithKibana(this IResourceBuilder<ElasticsearchResource> builder, string? containerName = null, int? hostPort = null) |
| 42 | + { |
| 43 | + if (builder.ApplicationBuilder.Resources.OfType<KibanaResource>().Any()) |
| 44 | + { |
| 45 | + return builder; |
| 46 | + } |
| 47 | + |
| 48 | + builder.ApplicationBuilder.Services.TryAddLifecycleHook<KibanaConfigWriterHook>(); |
| 49 | + |
| 50 | + containerName ??= $"{builder.Resource.Name}-kibana"; |
| 51 | + |
| 52 | + var resource = new KibanaResource(containerName); |
| 53 | + builder.ApplicationBuilder.AddResource(resource) |
| 54 | + .WithAnnotation(new ContainerImageAnnotation { Image = "docker.elastic.co/kibana/kibana", Tag = "8.12.2" }) |
| 55 | + .WithEnvironment("XPACK_SECURITY_ENABLED", "false") |
| 56 | + .WithHttpEndpoint(containerPort: 5601, hostPort: hostPort, name: containerName) |
| 57 | + .ExcludeFromManifest(); |
| 58 | + |
| 59 | + return builder; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// <summary> |
| 64 | +/// A resource that represents a Elasticsearch resource independent of the hosting model. |
| 65 | +/// </summary> |
| 66 | +/// <param name="name">The name of the resource.</param> |
| 67 | +public class ElasticsearchResource(string name) : ContainerResource(name), IResourceWithConnectionString |
| 68 | +{ |
| 69 | + /// <summary> |
| 70 | + /// Gets the connection string expression for the Elasticsearch server for the manifest. |
| 71 | + /// </summary> |
| 72 | + public string? ConnectionStringExpression |
| 73 | + { |
| 74 | + get |
| 75 | + { |
| 76 | + if (this.TryGetLastAnnotation<ConnectionStringRedirectAnnotation>(out var connectionStringAnnotation)) |
| 77 | + { |
| 78 | + return connectionStringAnnotation.Resource.ConnectionStringExpression; |
| 79 | + } |
| 80 | + |
| 81 | + return $"{{{Name}.bindings.tcp.host}}:{{{Name}.bindings.tcp.port}}"; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Gets the connection string for the Elasticsearch server. |
| 87 | + /// </summary> |
| 88 | + /// <returns>A connection string for the redis server in the form "host:port".</returns> |
| 89 | + public string? GetConnectionString() |
| 90 | + { |
| 91 | + if (this.TryGetLastAnnotation<ConnectionStringRedirectAnnotation>(out var connectionStringAnnotation)) |
| 92 | + { |
| 93 | + return connectionStringAnnotation.Resource.GetConnectionString(); |
| 94 | + } |
| 95 | + |
| 96 | + if (!this.TryGetAnnotationsOfType<AllocatedEndpointAnnotation>(out var allocatedEndpoints)) |
| 97 | + { |
| 98 | + throw new DistributedApplicationException("Elasticsearch resource does not have endpoint annotation."); |
| 99 | + } |
| 100 | + |
| 101 | + // We should only have one endpoint for Elasticsearch for local scenarios. |
| 102 | + var endpoint = allocatedEndpoints.Single(); |
| 103 | + return endpoint.EndPointString; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/// <summary> |
| 108 | +/// A resource that represents a Kibana container. |
| 109 | +/// </summary> |
| 110 | +/// <param name="name">The name of the resource.</param> |
| 111 | +public class KibanaResource(string name) : ContainerResource(name) |
| 112 | +{ |
| 113 | +} |
| 114 | + |
| 115 | +internal class KibanaConfigWriterHook(IConfiguration configuration) : IDistributedApplicationLifecycleHook |
| 116 | +{ |
| 117 | + public Task AfterEndpointsAllocatedAsync(DistributedApplicationModel appModel, CancellationToken cancellationToken) |
| 118 | + { |
| 119 | + if (appModel.Resources.OfType<KibanaResource>().SingleOrDefault() is not { } kibanaResource) |
| 120 | + { |
| 121 | + // No-op if there is no commander resource (removed after hook added). |
| 122 | + return Task.CompletedTask; |
| 123 | + } |
| 124 | + |
| 125 | + var elasticsearchInstances = appModel.Resources.OfType<ElasticsearchResource>(); |
| 126 | + |
| 127 | + if (!elasticsearchInstances.Any()) |
| 128 | + { |
| 129 | + // No-op if there are no Elasticsearch resources present. |
| 130 | + return Task.CompletedTask; |
| 131 | + } |
| 132 | + |
| 133 | + var containerHostName = HostNameResolver.ReplaceLocalhostWithContainerHost("localhost", configuration); |
| 134 | + |
| 135 | + var hostsVariableBuilder = new StringBuilder(); |
| 136 | + |
| 137 | + foreach (var elasticsearchInstance in elasticsearchInstances) |
| 138 | + { |
| 139 | + if (elasticsearchInstance.TryGetAllocatedEndPoints(out var allocatedEndpoints)) |
| 140 | + { |
| 141 | + var endpoint = allocatedEndpoints.Where(ae => ae.Name == "http").Single(); |
| 142 | + |
| 143 | + var hostString = $"{(hostsVariableBuilder.Length > 0 ? "," : string.Empty)}http://{containerHostName}:{endpoint.Port}"; |
| 144 | + hostsVariableBuilder.Append(hostString); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + kibanaResource.Annotations.Add(new EnvironmentCallbackAnnotation((EnvironmentCallbackContext context) => |
| 149 | + { |
| 150 | + context.EnvironmentVariables.Add("ELASTICSEARCH_HOSTS", hostsVariableBuilder.ToString()); |
| 151 | + })); |
| 152 | + |
| 153 | + return Task.CompletedTask; |
| 154 | + } |
| 155 | +} |
0 commit comments