-
Notifications
You must be signed in to change notification settings - Fork 693
[release/9.0] Experimental custom domain support for ACA. #6391
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
4 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
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
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
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
103 changes: 103 additions & 0 deletions
103
src/Aspire.Hosting.Azure.AppContainers/ContainerAppExtensions.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,103 @@ | ||
// 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 Azure.Provisioning.AppContainers; | ||
using Azure.Provisioning.Expressions; | ||
using Azure.Provisioning; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Aspire.Hosting.Azure; | ||
|
||
namespace Aspire.Hosting; | ||
|
||
/// <summary> | ||
/// Provides extension methods for customizing Azure Container App resource. | ||
/// </summary> | ||
public static class ContainerAppExtensions | ||
{ | ||
/// <summary> | ||
/// Configures the custom domain for the container app. | ||
/// </summary> | ||
/// <param name="app">The container app resource to configure for custom domain usage.</param> | ||
/// <param name="customDomain">A resource builder for a parameter resource capturing the name of the custom domain.</param> | ||
/// <param name="certificateName">A resource builder for a parameter resource capturing the name of the certficate configured in the Azure Portal.</param> | ||
/// <exception cref="ArgumentException">Throws if the container app resource is not parented to a <see cref="AzureResourceInfrastructure"/>.</exception> | ||
/// <remarks> | ||
/// <para>The <see cref="ConfigureCustomDomain(ContainerApp, IResourceBuilder{ParameterResource}, IResourceBuilder{ParameterResource})"/> extension method | ||
/// simplifies the process of assigning a custom domain to a container app resource when it is deployed. It has no impact on local development.</para> | ||
/// <para>The <see cref="ConfigureCustomDomain(ContainerApp, IResourceBuilder{ParameterResource}, IResourceBuilder{ParameterResource})"/> method is used | ||
/// in conjunction with the <see cref="AzureContainerAppContainerExtensions.PublishAsAzureContainerApp{T}(IResourceBuilder{T}, Action{AzureResourceInfrastructure, ContainerApp})"/> | ||
/// callback. Assigning a custom domain to a container app resource is a multi-step process and requires multiple deployments.</para> | ||
/// <para>The <see cref="ConfigureCustomDomain(ContainerApp, IResourceBuilder{ParameterResource}, IResourceBuilder{ParameterResource})"/> method takes | ||
/// two arguments which are parameter resource builders. The first is a parameter that represents the custom domain and the second is a parameter that | ||
/// represents the name of the managed certificate provisioned via the Azure Portal</para> | ||
/// <para>When deploying with custom domains configured for the first time leave the <paramref name="certificateName"/> parameter empty (when prompted | ||
/// by the Azure Developer CLI). Once the applicatio is deployed acucessfully access to the Azure Portal to bind the custom domain to a managed SSL | ||
/// certificate. Once the certificate is successfully provisioned, subsequent deployments of the application can use this certificate name when the | ||
/// <paramref name="certificateName"/> is prompted.</para> | ||
/// <para>For deployments triggered locally by the Azure Developer CLI the <c>config.json</c> file in the <c>.azure/{environment name}</c> path | ||
/// can by modified with the certificate name since Azure Developer CLI will not prompt again for the value.</para> | ||
/// </remarks> | ||
/// <example> | ||
/// This example shows declaring two parameters to capture the custom domain and certificate name and | ||
/// passing them to the <see cref="ConfigureCustomDomain(ContainerApp, IResourceBuilder{ParameterResource}, IResourceBuilder{ParameterResource})"/> | ||
/// method via the <see cref="AzureContainerAppContainerExtensions.PublishAsAzureContainerApp{T}(IResourceBuilder{T}, Action{AzureResourceInfrastructure, ContainerApp})"/> | ||
/// extension method. | ||
/// <code lang="C#"> | ||
/// var builder = DistributedApplication.CreateBuilder(); | ||
/// var customDomain = builder.AddParameter("customDomain"); // Value provided at first deployment. | ||
/// var certificateName = builder.AddParameter("certificateName"); // Value provided at second and subsequent deployments. | ||
/// builder.AddProject<Projects.InventoryService>("inventory") | ||
/// .PublishAsAzureContainerApp((module, app) => | ||
/// { | ||
/// app.ConfigureCustomDomain(customDomain, certificateName); | ||
/// }); | ||
/// </code> | ||
/// </example> | ||
[Experimental("ASPIREACADOMAINS001", UrlFormat = "https://aka.ms/dotnet/aspire/diagnostics#{0}")] | ||
public static void ConfigureCustomDomain(this ContainerApp app, IResourceBuilder<ParameterResource> customDomain, IResourceBuilder<ParameterResource> certificateName) | ||
{ | ||
if (app.ParentInfrastructure is not AzureResourceInfrastructure module) | ||
{ | ||
throw new ArgumentException("Cannot configure custom domain when resource is not parented by ResourceModuleConstruct.", nameof(app)); | ||
} | ||
|
||
var containerAppManagedEnvironmentIdParameter = module.GetResources().OfType<ProvisioningParameter>().Single( | ||
p => p.IdentifierName == "outputs_azure_container_apps_environment_id"); | ||
var certificatNameParameter = certificateName.AsProvisioningParameter(module); | ||
var customDomainParameter = customDomain.AsProvisioningParameter(module); | ||
|
||
var bindingTypeConditional = new ConditionalExpression( | ||
new BinaryExpression( | ||
new IdentifierExpression(certificatNameParameter.IdentifierName), | ||
BinaryOperator.NotEqual, | ||
new StringLiteral(string.Empty)), | ||
new StringLiteral("SniEnabled"), | ||
new StringLiteral("Disabled") | ||
); | ||
|
||
var certificateOrEmpty = new ConditionalExpression( | ||
new BinaryExpression( | ||
new IdentifierExpression(certificatNameParameter.IdentifierName), | ||
BinaryOperator.NotEqual, | ||
new StringLiteral(string.Empty)), | ||
new InterpolatedString( | ||
"{0}/managedCertificates/{1}", | ||
[ | ||
new IdentifierExpression(containerAppManagedEnvironmentIdParameter.IdentifierName), | ||
new IdentifierExpression(certificatNameParameter.IdentifierName) | ||
]), | ||
new NullLiteral() | ||
); | ||
|
||
app.Configuration.Value!.Ingress!.Value!.CustomDomains = new BicepList<ContainerAppCustomDomain>() | ||
{ | ||
new ContainerAppCustomDomain() | ||
{ | ||
BindingType = bindingTypeConditional, | ||
Name = new IdentifierExpression(customDomainParameter.IdentifierName), | ||
CertificateId = certificateOrEmpty | ||
} | ||
}; | ||
} | ||
} |
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
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
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.
I think this method should go in the existing
AzureContainerAppExtensions
class.cc @mitchdenny @davidfowl
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.
This was intentionally named I think because @mitchdenny was thinking it would eventually be a cdk method. But not sure
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.
Class named to match the type name.