|
| 1 | +using Foundatio.Storage; |
| 2 | + |
| 3 | +namespace Aspire.Hosting; |
| 4 | + |
| 5 | +public static class MinIoExtensions |
| 6 | +{ |
| 7 | + public static IResourceBuilder<MinIoResource> AddMinIo( |
| 8 | + this IDistributedApplicationBuilder builder, |
| 9 | + string name, |
| 10 | + Action<MinIoBuilder>? configure = null) |
| 11 | + { |
| 12 | + var options = new MinIoBuilder(); |
| 13 | + configure?.Invoke(options); |
| 14 | + |
| 15 | + var resource = new MinIoResource(name, options.AccessKey, options.SecretKey, options.Bucket ?? "storage"); |
| 16 | + |
| 17 | + string? connectionString = null; |
| 18 | + |
| 19 | + builder.Eventing.Subscribe<ResourceReadyEvent>(resource, async (@event, ct) => |
| 20 | + { |
| 21 | + connectionString = await resource.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false); |
| 22 | + |
| 23 | + if (connectionString == null) |
| 24 | + throw new DistributedApplicationException($"ResourceReadyEvent was published for the '{resource.Name}' resource but the connection string was null."); |
| 25 | + |
| 26 | + var storage = new S3FileStorage(o => o.ConnectionString(connectionString)); |
| 27 | + try |
| 28 | + { |
| 29 | + storage.Client.PutBucketAsync(options.Bucket ?? "storage", ct).GetAwaiter().GetResult(); |
| 30 | + } |
| 31 | + catch |
| 32 | + { |
| 33 | + // ignored |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + return builder.AddResource(resource) |
| 38 | + .WithImage(MinIoContainerImageTags.Image) |
| 39 | + .WithImageRegistry(MinIoContainerImageTags.Registry) |
| 40 | + .WithImageTag(MinIoContainerImageTags.Tag) |
| 41 | + .WithArgs("server", "/data", "--console-address", $":{MinIoResource.DefaultConsolePort}") |
| 42 | + .WithEndpoint(port: options.ApiPort, targetPort: MinIoResource.DefaultApiPort, name: MinIoResource.ApiEndpointName) |
| 43 | + .WithHttpEndpoint(port: options.ConsolePort, targetPort: MinIoResource.DefaultConsolePort, name: MinIoResource.ConsoleEndpointName) |
| 44 | + .ConfigureCredentials(options) |
| 45 | + .ConfigureVolume(options); |
| 46 | + } |
| 47 | + |
| 48 | + private static IResourceBuilder<MinIoResource> ConfigureCredentials( |
| 49 | + this IResourceBuilder<MinIoResource> builder, |
| 50 | + MinIoBuilder options) |
| 51 | + { |
| 52 | + return builder |
| 53 | + .WithEnvironment("MINIO_ROOT_USER", options.AccessKey ?? "minioadmin") |
| 54 | + .WithEnvironment("MINIO_ROOT_PASSWORD", options.SecretKey ?? "minioadmin"); |
| 55 | + } |
| 56 | + |
| 57 | + private static IResourceBuilder<MinIoResource> ConfigureVolume( |
| 58 | + this IResourceBuilder<MinIoResource> builder, |
| 59 | + MinIoBuilder options) |
| 60 | + { |
| 61 | + if (!string.IsNullOrEmpty(options.DataVolumePath)) |
| 62 | + builder = builder.WithVolume(options.DataVolumePath, "/data"); |
| 63 | + |
| 64 | + return builder; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +public class MinIoResource(string name, string? accessKey = null, string? secretKey = null, string? bucket = "storage") |
| 69 | + : ContainerResource(name), IResourceWithConnectionString |
| 70 | +{ |
| 71 | + internal const string ApiEndpointName = "api"; |
| 72 | + internal const string ConsoleEndpointName = "console"; |
| 73 | + internal const int DefaultApiPort = 9000; |
| 74 | + internal const int DefaultConsolePort = 9001; |
| 75 | + |
| 76 | + private EndpointReference? _apiReference; |
| 77 | + private EndpointReference? _consoleReference; |
| 78 | + |
| 79 | + private EndpointReference ApiEndpoint => |
| 80 | + _apiReference ??= new EndpointReference(this, ApiEndpointName); |
| 81 | + |
| 82 | + private EndpointReference ConsoleEndpoint => |
| 83 | + _consoleReference ??= new EndpointReference(this, ConsoleEndpointName); |
| 84 | + |
| 85 | + public ReferenceExpression ConnectionStringExpression => |
| 86 | + ReferenceExpression.Create( |
| 87 | + $"ServiceUrl=http://{ApiEndpoint.Property(EndpointProperty.Host)}:{ApiEndpoint.Property(EndpointProperty.Port)};" + |
| 88 | + $"AccessKey={AccessKey ?? "minioadmin"};" + |
| 89 | + $"SecretKey={SecretKey ?? "minioadmin"};" + |
| 90 | + $"Bucket={Bucket}"); |
| 91 | + |
| 92 | + public string? AccessKey { get; } = accessKey; |
| 93 | + public string? SecretKey { get; } = secretKey; |
| 94 | + public string? Bucket { get; } = bucket; |
| 95 | +} |
| 96 | + |
| 97 | +public class MinIoBuilder |
| 98 | +{ |
| 99 | + public int? ApiPort { get; set; } |
| 100 | + public int? ConsolePort { get; set; } |
| 101 | + public string? AccessKey { get; set; } |
| 102 | + public string? SecretKey { get; set; } |
| 103 | + public string? Bucket { get; set; } |
| 104 | + public string? DataVolumePath { get; set; } |
| 105 | + |
| 106 | + public MinIoBuilder WithPorts(int? apiPort = null, int? consolePort = null) |
| 107 | + { |
| 108 | + ApiPort = apiPort; |
| 109 | + ConsolePort = consolePort; |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + public MinIoBuilder WithCredentials(string accessKey, string secretKey) |
| 114 | + { |
| 115 | + AccessKey = accessKey; |
| 116 | + SecretKey = secretKey; |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + public MinIoBuilder WithBucket(string bucket) |
| 121 | + { |
| 122 | + Bucket = bucket; |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + public MinIoBuilder WithDataVolume(string path) |
| 127 | + { |
| 128 | + DataVolumePath = path; |
| 129 | + return this; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +internal static class MinIoContainerImageTags |
| 134 | +{ |
| 135 | + internal const string Registry = "docker.io"; |
| 136 | + internal const string Image = "minio/minio"; |
| 137 | + internal const string Tag = "latest"; |
| 138 | +} |
0 commit comments