|
3 | 3 |
|
4 | 4 | using Aspire.Hosting.ApplicationModel;
|
5 | 5 | using Aspire.Hosting.Lifecycle;
|
6 |
| -using Aspire.Hosting.Publishing; |
7 | 6 | using Microsoft.Extensions.Logging;
|
8 | 7 |
|
9 | 8 | namespace Aspire.Hosting.Docker;
|
@@ -57,148 +56,12 @@ public async Task BeforeStartAsync(DistributedApplicationModel appModel, Cancell
|
57 | 56 | var serviceResource = await dockerComposeEnvironmentContext.CreateDockerComposeServiceResourceAsync(r, executionContext, cancellationToken).ConfigureAwait(false);
|
58 | 57 |
|
59 | 58 | // Add deployment target annotation to the resource
|
60 |
| - r.Annotations.Add(new DeploymentTargetAnnotation(serviceResource)); |
61 |
| - } |
62 |
| - } |
63 |
| - |
64 |
| - internal sealed class DockerComposeEnvironmentContext(DockerComposeEnvironmentResource environment, ILogger logger) |
65 |
| - { |
66 |
| - private readonly Dictionary<IResource, DockerComposeServiceResource> _resourceMapping = []; |
67 |
| - private readonly PortAllocator _portAllocator = new(); |
68 |
| - |
69 |
| - public async Task<DockerComposeServiceResource> CreateDockerComposeServiceResourceAsync(IResource resource, DistributedApplicationExecutionContext executionContext, CancellationToken cancellationToken) |
70 |
| - { |
71 |
| - if (_resourceMapping.TryGetValue(resource, out var existingResource)) |
72 |
| - { |
73 |
| - return existingResource; |
74 |
| - } |
75 |
| - |
76 |
| - logger.LogInformation("Creating Docker Compose resource for {ResourceName}", resource.Name); |
77 |
| - |
78 |
| - var serviceResource = new DockerComposeServiceResource(resource.Name, resource, environment); |
79 |
| - _resourceMapping[resource] = serviceResource; |
80 |
| - |
81 |
| - // Process endpoints |
82 |
| - ProcessEndpoints(serviceResource); |
83 |
| - |
84 |
| - // Process volumes |
85 |
| - ProcessVolumes(serviceResource); |
86 |
| - |
87 |
| - // Process environment variables |
88 |
| - await ProcessEnvironmentVariablesAsync(serviceResource, executionContext, cancellationToken).ConfigureAwait(false); |
89 |
| - |
90 |
| - // Process command line arguments |
91 |
| - await ProcessArgumentsAsync(serviceResource, executionContext, cancellationToken).ConfigureAwait(false); |
92 |
| - |
93 |
| - return serviceResource; |
94 |
| - } |
95 |
| - |
96 |
| - private void ProcessEndpoints(DockerComposeServiceResource serviceResource) |
97 |
| - { |
98 |
| - if (!serviceResource.TargetResource.TryGetEndpoints(out var endpoints)) |
99 |
| - { |
100 |
| - return; |
101 |
| - } |
102 |
| - |
103 |
| - foreach (var endpoint in endpoints) |
104 |
| - { |
105 |
| - var internalPort = endpoint.TargetPort ?? _portAllocator.AllocatePort(); |
106 |
| - _portAllocator.AddUsedPort(internalPort); |
107 |
| - |
108 |
| - var exposedPort = _portAllocator.AllocatePort(); |
109 |
| - _portAllocator.AddUsedPort(exposedPort); |
110 |
| - |
111 |
| - serviceResource.EndpointMappings.Add(endpoint.Name, new(endpoint.UriScheme, serviceResource.TargetResource.Name, internalPort, exposedPort, false)); |
112 |
| - } |
113 |
| - } |
114 |
| - |
115 |
| - private static void ProcessVolumes(DockerComposeServiceResource serviceResource) |
116 |
| - { |
117 |
| - if (!serviceResource.TargetResource.TryGetContainerMounts(out var mounts)) |
118 |
| - { |
119 |
| - return; |
120 |
| - } |
121 |
| - |
122 |
| - foreach (var mount in mounts) |
123 |
| - { |
124 |
| - if (mount.Source is null || mount.Target is null) |
125 |
| - { |
126 |
| - throw new InvalidOperationException("Volume source and target must be set"); |
127 |
| - } |
128 |
| - |
129 |
| - serviceResource.Volumes.Add(new Resources.ServiceNodes.Volume |
130 |
| - { |
131 |
| - Name = mount.Source, |
132 |
| - Source = mount.Source, |
133 |
| - Target = mount.Target, |
134 |
| - Type = mount.Type == ContainerMountType.BindMount ? "bind" : "volume", |
135 |
| - ReadOnly = mount.IsReadOnly |
136 |
| - }); |
137 |
| - } |
138 |
| - } |
139 |
| - |
140 |
| - private async Task ProcessEnvironmentVariablesAsync(DockerComposeServiceResource serviceResource, DistributedApplicationExecutionContext executionContext, CancellationToken cancellationToken) |
141 |
| - { |
142 |
| - if (serviceResource.TargetResource.TryGetAnnotationsOfType<EnvironmentCallbackAnnotation>(out var environmentCallbacks)) |
| 59 | +#pragma warning disable ASPIRECOMPUTE001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. |
| 60 | + r.Annotations.Add(new DeploymentTargetAnnotation(serviceResource) |
143 | 61 | {
|
144 |
| - var context = new EnvironmentCallbackContext(executionContext, serviceResource.TargetResource, cancellationToken: cancellationToken); |
145 |
| - |
146 |
| - foreach (var callback in environmentCallbacks) |
147 |
| - { |
148 |
| - await callback.Callback(context).ConfigureAwait(false); |
149 |
| - } |
150 |
| - |
151 |
| - // Remove HTTPS service discovery variables as Docker Compose doesn't handle certificates |
152 |
| - RemoveHttpsServiceDiscoveryVariables(context.EnvironmentVariables); |
153 |
| - |
154 |
| - foreach (var kv in context.EnvironmentVariables) |
155 |
| - { |
156 |
| - var value = await serviceResource.ProcessValueAsync(this, executionContext, kv.Value).ConfigureAwait(false); |
157 |
| - serviceResource.EnvironmentVariables.Add(kv.Key, value?.ToString() ?? string.Empty); |
158 |
| - } |
159 |
| - } |
160 |
| - } |
161 |
| - |
162 |
| - private static void RemoveHttpsServiceDiscoveryVariables(Dictionary<string, object> environmentVariables) |
163 |
| - { |
164 |
| - var keysToRemove = environmentVariables |
165 |
| - .Where(kvp => kvp.Value is EndpointReference epRef && epRef.Scheme == "https" && kvp.Key.StartsWith("services__")) |
166 |
| - .Select(kvp => kvp.Key) |
167 |
| - .ToList(); |
168 |
| - |
169 |
| - foreach (var key in keysToRemove) |
170 |
| - { |
171 |
| - environmentVariables.Remove(key); |
172 |
| - } |
173 |
| - } |
174 |
| - |
175 |
| - private async Task ProcessArgumentsAsync(DockerComposeServiceResource serviceResource, DistributedApplicationExecutionContext executionContext, CancellationToken cancellationToken) |
176 |
| - { |
177 |
| - if (serviceResource.TargetResource.TryGetAnnotationsOfType<CommandLineArgsCallbackAnnotation>(out var commandLineArgsCallbacks)) |
178 |
| - { |
179 |
| - var context = new CommandLineArgsCallbackContext([], cancellationToken: cancellationToken); |
180 |
| - |
181 |
| - foreach (var callback in commandLineArgsCallbacks) |
182 |
| - { |
183 |
| - await callback.Callback(context).ConfigureAwait(false); |
184 |
| - } |
185 |
| - |
186 |
| - foreach (var arg in context.Args) |
187 |
| - { |
188 |
| - var value = await serviceResource.ProcessValueAsync(this, executionContext, arg).ConfigureAwait(false); |
189 |
| - if (value is not string str) |
190 |
| - { |
191 |
| - throw new NotSupportedException("Command line args must be strings"); |
192 |
| - } |
193 |
| - |
194 |
| - serviceResource.Commands.Add(str); |
195 |
| - } |
196 |
| - } |
197 |
| - } |
198 |
| - |
199 |
| - public void AddEnv(string name, string description, string? defaultValue = null) |
200 |
| - { |
201 |
| - environment.CapturedEnvironmentVariables[name] = (description, defaultValue); |
| 62 | + ComputeEnvironment = environment, |
| 63 | + }); |
| 64 | +#pragma warning restore ASPIRECOMPUTE001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. |
202 | 65 | }
|
203 | 66 | }
|
204 | 67 | }
|
0 commit comments