- 
                Notifications
    You must be signed in to change notification settings 
- Fork 715
Model Docker Compose as compute environment #8828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            3 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
  
    
      
          
            33 changes: 33 additions & 0 deletions
          
          33 
        
  src/Aspire.Hosting.Docker/DockerComposeEnvironmentExtensions.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|  | ||
| using Aspire.Hosting.ApplicationModel; | ||
| using Aspire.Hosting.Docker; | ||
| using Aspire.Hosting.Lifecycle; | ||
|  | ||
| namespace Aspire.Hosting; | ||
|  | ||
| /// <summary> | ||
| /// Provides extension methods for adding Docker Compose environment resources to the application model. | ||
| /// </summary> | ||
| public static class DockerComposeEnvironmentExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds a Docker Compose environment to the application model. | ||
| /// </summary> | ||
| /// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param> | ||
| /// <param name="name">The name of the Docker Compose environment resource.</param> | ||
| /// <returns>A reference to the <see cref="IResourceBuilder{DockerComposeEnvironmentResource}"/>.</returns> | ||
| public static IResourceBuilder<DockerComposeEnvironmentResource> AddDockerComposeEnvironment( | ||
| this IDistributedApplicationBuilder builder, | ||
| string name) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(builder); | ||
| ArgumentException.ThrowIfNullOrEmpty(name); | ||
|  | ||
| var resource = new DockerComposeEnvironmentResource(name); | ||
| builder.Services.TryAddLifecycleHook<DockerComposeInfrastructure>(); | ||
| builder.AddDockerComposePublisher(name); | ||
| return builder.AddResource(resource); | ||
| } | ||
| } | 
        
          
  
    
      
          
            22 changes: 22 additions & 0 deletions
          
          22 
        
  src/Aspire.Hosting.Docker/DockerComposeEnvironmentResource.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|  | ||
| using Aspire.Hosting.ApplicationModel; | ||
|  | ||
| namespace Aspire.Hosting.Docker; | ||
|  | ||
| /// <summary> | ||
| /// Represents a Docker Compose environment resource that can host application resources. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Initializes a new instance of the <see cref="DockerComposeEnvironmentResource"/> class. | ||
| /// </remarks> | ||
| /// <param name="name">The name of the Docker Compose environment.</param> | ||
| public class DockerComposeEnvironmentResource(string name) : Resource(name) | ||
| { | ||
| /// <summary> | ||
| /// Gets the collection of environment variables captured from the Docker Compose environment. | ||
| /// These will be populated into a top-level .env file adjacent to the Docker Compose file. | ||
| /// </summary> | ||
| internal Dictionary<string, (string Description, string? DefaultValue)> CapturedEnvironmentVariables { get; } = []; | ||
| } | ||
        
          
  
    
      
          
            204 changes: 204 additions & 0 deletions
          
          204 
        
  src/Aspire.Hosting.Docker/DockerComposeInfrastructure.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|  | ||
| using Aspire.Hosting.ApplicationModel; | ||
| using Aspire.Hosting.Lifecycle; | ||
| using Aspire.Hosting.Publishing; | ||
| using Microsoft.Extensions.Logging; | ||
|  | ||
| namespace Aspire.Hosting.Docker; | ||
|  | ||
| /// <summary> | ||
| /// Represents the infrastructure for Docker Compose within the Aspire Hosting environment. | ||
| /// Implements the <see cref="IDistributedApplicationLifecycleHook"/> interface to provide lifecycle hooks for distributed applications. | ||
| /// </summary> | ||
| internal sealed class DockerComposeInfrastructure( | ||
| ILogger<DockerComposeInfrastructure> logger, | ||
| DistributedApplicationExecutionContext executionContext) : IDistributedApplicationLifecycleHook | ||
| { | ||
| public async Task BeforeStartAsync(DistributedApplicationModel appModel, CancellationToken cancellationToken = default) | ||
| { | ||
| if (executionContext.IsRunMode) | ||
| { | ||
| return; | ||
| } | ||
|  | ||
| // Find Docker Compose environment resources | ||
| var dockerComposeEnvironments = appModel.Resources.OfType<DockerComposeEnvironmentResource>().ToArray(); | ||
|  | ||
| if (dockerComposeEnvironments.Length > 1) | ||
| { | ||
| throw new NotSupportedException("Multiple Docker Compose environments are not supported."); | ||
| } | ||
|  | ||
| var environment = dockerComposeEnvironments.FirstOrDefault(); | ||
|  | ||
| if (environment == null) | ||
| { | ||
| return; | ||
| } | ||
|  | ||
| var dockerComposeEnvironmentContext = new DockerComposeEnvironmentContext(environment, logger); | ||
|  | ||
| foreach (var r in appModel.Resources) | ||
| { | ||
| if (r.TryGetLastAnnotation<ManifestPublishingCallbackAnnotation>(out var lastAnnotation) && lastAnnotation == ManifestPublishingCallbackAnnotation.Ignore) | ||
| { | ||
| continue; | ||
| } | ||
|  | ||
| // Skip resources that are not containers or projects | ||
| if (!r.IsContainer() && r is not ProjectResource) | ||
| { | ||
| continue; | ||
| } | ||
|  | ||
| // Create a Docker Compose compute resource for the resource | ||
| var serviceResource = await dockerComposeEnvironmentContext.CreateDockerComposeServiceResourceAsync(r, executionContext, cancellationToken).ConfigureAwait(false); | ||
|  | ||
| // Add deployment target annotation to the resource | ||
| r.Annotations.Add(new DeploymentTargetAnnotation(serviceResource)); | ||
| } | ||
| } | ||
|  | ||
| internal sealed class DockerComposeEnvironmentContext(DockerComposeEnvironmentResource environment, ILogger logger) | ||
| { | ||
| private readonly Dictionary<IResource, DockerComposeServiceResource> _resourceMapping = []; | ||
| private readonly PortAllocator _portAllocator = new(); | ||
|  | ||
| public async Task<DockerComposeServiceResource> CreateDockerComposeServiceResourceAsync(IResource resource, DistributedApplicationExecutionContext executionContext, CancellationToken cancellationToken) | ||
| { | ||
| if (_resourceMapping.TryGetValue(resource, out var existingResource)) | ||
| { | ||
| return existingResource; | ||
| } | ||
|  | ||
| logger.LogInformation("Creating Docker Compose resource for {ResourceName}", resource.Name); | ||
|  | ||
| var serviceResource = new DockerComposeServiceResource(resource.Name, resource, environment); | ||
| _resourceMapping[resource] = serviceResource; | ||
|  | ||
| // Process endpoints | ||
| ProcessEndpoints(serviceResource); | ||
|  | ||
| // Process volumes | ||
| ProcessVolumes(serviceResource); | ||
|  | ||
| // Process environment variables | ||
| await ProcessEnvironmentVariablesAsync(serviceResource, executionContext, cancellationToken).ConfigureAwait(false); | ||
|  | ||
| // Process command line arguments | ||
| await ProcessArgumentsAsync(serviceResource, executionContext, cancellationToken).ConfigureAwait(false); | ||
|  | ||
| return serviceResource; | ||
| } | ||
|  | ||
| private void ProcessEndpoints(DockerComposeServiceResource serviceResource) | ||
| { | ||
| if (!serviceResource.TargetResource.TryGetEndpoints(out var endpoints)) | ||
| { | ||
| return; | ||
| } | ||
|  | ||
| foreach (var endpoint in endpoints) | ||
| { | ||
| var internalPort = endpoint.TargetPort ?? _portAllocator.AllocatePort(); | ||
| _portAllocator.AddUsedPort(internalPort); | ||
|  | ||
| var exposedPort = _portAllocator.AllocatePort(); | ||
| _portAllocator.AddUsedPort(exposedPort); | ||
|  | ||
| serviceResource.EndpointMappings.Add(endpoint.Name, new(endpoint.UriScheme, serviceResource.TargetResource.Name, internalPort, exposedPort, false)); | ||
| } | ||
| } | ||
|  | ||
| private static void ProcessVolumes(DockerComposeServiceResource serviceResource) | ||
| { | ||
| if (!serviceResource.TargetResource.TryGetContainerMounts(out var mounts)) | ||
| { | ||
| return; | ||
| } | ||
|  | ||
| foreach (var mount in mounts) | ||
| { | ||
| if (mount.Source is null || mount.Target is null) | ||
| { | ||
| throw new InvalidOperationException("Volume source and target must be set"); | ||
| } | ||
|  | ||
| serviceResource.Volumes.Add(new Resources.ServiceNodes.Volume | ||
| { | ||
| Name = mount.Source, | ||
| Source = mount.Source, | ||
| Target = mount.Target, | ||
| Type = mount.Type == ContainerMountType.BindMount ? "bind" : "volume", | ||
| ReadOnly = mount.IsReadOnly | ||
| }); | ||
| } | ||
| } | ||
|  | ||
| private async Task ProcessEnvironmentVariablesAsync(DockerComposeServiceResource serviceResource, DistributedApplicationExecutionContext executionContext, CancellationToken cancellationToken) | ||
| { | ||
| if (serviceResource.TargetResource.TryGetAnnotationsOfType<EnvironmentCallbackAnnotation>(out var environmentCallbacks)) | ||
| { | ||
| var context = new EnvironmentCallbackContext(executionContext, serviceResource.TargetResource, cancellationToken: cancellationToken); | ||
|  | ||
| foreach (var callback in environmentCallbacks) | ||
| { | ||
| await callback.Callback(context).ConfigureAwait(false); | ||
| } | ||
|  | ||
| // Remove HTTPS service discovery variables as Docker Compose doesn't handle certificates | ||
| RemoveHttpsServiceDiscoveryVariables(context.EnvironmentVariables); | ||
|  | ||
| foreach (var kv in context.EnvironmentVariables) | ||
| { | ||
| var value = await serviceResource.ProcessValueAsync(this, executionContext, kv.Value).ConfigureAwait(false); | ||
| serviceResource.EnvironmentVariables.Add(kv.Key, value?.ToString() ?? string.Empty); | ||
| } | ||
| } | ||
| } | ||
|  | ||
| private static void RemoveHttpsServiceDiscoveryVariables(Dictionary<string, object> environmentVariables) | ||
| { | ||
| var keysToRemove = environmentVariables | ||
| .Where(kvp => kvp.Value is EndpointReference epRef && epRef.Scheme == "https" && kvp.Key.StartsWith("services__")) | ||
| .Select(kvp => kvp.Key) | ||
| .ToList(); | ||
|  | ||
| foreach (var key in keysToRemove) | ||
| { | ||
| environmentVariables.Remove(key); | ||
| } | ||
| } | ||
|  | ||
| private async Task ProcessArgumentsAsync(DockerComposeServiceResource serviceResource, DistributedApplicationExecutionContext executionContext, CancellationToken cancellationToken) | ||
| { | ||
| if (serviceResource.TargetResource.TryGetAnnotationsOfType<CommandLineArgsCallbackAnnotation>(out var commandLineArgsCallbacks)) | ||
| { | ||
| var context = new CommandLineArgsCallbackContext([], cancellationToken: cancellationToken); | ||
|  | ||
| foreach (var callback in commandLineArgsCallbacks) | ||
| { | ||
| await callback.Callback(context).ConfigureAwait(false); | ||
| } | ||
|  | ||
| foreach (var arg in context.Args) | ||
| { | ||
| var value = await serviceResource.ProcessValueAsync(this, executionContext, arg).ConfigureAwait(false); | ||
| if (value is not string str) | ||
| { | ||
| throw new NotSupportedException("Command line args must be strings"); | ||
| } | ||
|  | ||
| serviceResource.Commands.Add(str); | ||
| } | ||
| } | ||
| } | ||
|  | ||
| public void AddEnv(string name, string description, string? defaultValue = null) | ||
| { | ||
| environment.CapturedEnvironmentVariables[name] = (description, defaultValue); | ||
| } | ||
| } | ||
| } | 
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When #8820 is merged.