-
Couldn't load subscription status.
- Fork 712
Add hosting integration for Azure Container Registry #9059
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>$(DefaultTargetFramework)</TargetFramework> | ||
| <IsPackable>true</IsPackable> | ||
| <PackageTags>aspire integration hosting azure containerregistry</PackageTags> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be
https://www.nuget.org/packages/Azure.Containers.ContainerRegistry has them split apart. |
||
| <Description>Azure Container Registry resource types for .NET Aspire.</Description> | ||
| <EnablePackageValidation>false</EnablePackageValidation> | ||
| <SuppressFinalPackageVersion>true</SuppressFinalPackageVersion> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="$(RepoRoot)src\Shared\AzureRoleAssignmentUtils.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Aspire.Hosting.Azure\Aspire.Hosting.Azure.csproj" /> | ||
| <PackageReference Include="Azure.Provisioning" /> | ||
| <PackageReference Include="Azure.Provisioning.ContainerRegistry" /> | ||
| </ItemGroup> | ||
|
|
||
|
|
||
| </Project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| #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. | ||
|
|
||
| // 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.Azure; | ||
| using Aspire.Hosting.Azure.ContainerRegistry; | ||
| using Azure.Provisioning; | ||
| using Azure.Provisioning.ContainerRegistry; | ||
|
|
||
| namespace Aspire.Hosting; | ||
|
|
||
| /// <summary> | ||
| /// Provides extension methods for adding Azure Container Registry resources to the application model. | ||
| /// </summary> | ||
| public static class AzureContainerRegistryExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds an Azure Container Registry resource to the application model. | ||
| /// </summary> | ||
| /// <param name="builder">The builder for the distributed application.</param> | ||
| /// <param name="name">The name of the resource.</param> | ||
| /// <returns>A reference to the <see cref="IResourceBuilder{AzureContainerRegistryResource}"/> builder.</returns> | ||
| /// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> is null.</exception> | ||
| /// <exception cref="ArgumentException">Thrown when <paramref name="name"/> is null or empty.</exception> | ||
| public static IResourceBuilder<AzureContainerRegistryResource> AddAzureContainerRegistry(this IDistributedApplicationBuilder builder, [ResourceName] string name) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(builder); | ||
| ArgumentException.ThrowIfNullOrEmpty(name); | ||
|
|
||
| builder.AddAzureProvisioning(); | ||
|
|
||
| var configureInfrastructure = (AzureResourceInfrastructure infrastructure) => | ||
| { | ||
| var registry = AzureProvisioningResource.CreateExistingOrNewProvisionableResource(infrastructure, | ||
| (identifier, name) => | ||
| { | ||
| var resource = ContainerRegistryService.FromExisting(identifier); | ||
| resource.Name = name; | ||
| return resource; | ||
| }, | ||
| (infrastructure) => new ContainerRegistryService(infrastructure.AspireResource.GetBicepIdentifier()) | ||
| { | ||
| Sku = new() { Name = ContainerRegistrySkuName.Basic }, | ||
| Tags = { { "aspire-resource-name", infrastructure.AspireResource.Name } } | ||
| }); | ||
|
|
||
| infrastructure.Add(registry); | ||
|
|
||
| infrastructure.Add(new ProvisioningOutput("name", typeof(string)) { Value = registry.Name }); | ||
| infrastructure.Add(new ProvisioningOutput("loginServer", typeof(string)) { Value = registry.LoginServer }); | ||
| }; | ||
|
|
||
| var resource = new AzureContainerRegistryResource(name, configureInfrastructure); | ||
| return builder.AddResource(resource) | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .WithAnnotation(new DefaultRoleAssignmentsAnnotation(new HashSet<RoleDefinition>())); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configures a resource that implements <see cref="IContainerRegistry"/> to use the specified Azure Container Registry. | ||
| /// </summary> | ||
| /// <typeparam name="T">The resource type that implements <see cref="IContainerRegistry"/>.</typeparam> | ||
| /// <param name="builder">The resource builder for a resource that implements <see cref="IContainerRegistry"/>.</param> | ||
| /// <param name="registryBuilder">The resource builder for the <see cref="AzureContainerRegistryResource"/> to use.</param> | ||
| /// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns> | ||
| /// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> or <paramref name="registryBuilder"/> is null.</exception> | ||
| public static IResourceBuilder<T> WithAzureContainerRegistry<T>(this IResourceBuilder<T> builder, IResourceBuilder<AzureContainerRegistryResource> registryBuilder) | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| where T : IResource, IComputeEnvironmentResource | ||
| { | ||
| ArgumentNullException.ThrowIfNull(builder); | ||
| ArgumentNullException.ThrowIfNull(registryBuilder); | ||
|
|
||
| // Add a ContainerRegistryReferenceAnnotation to indicate that the resource is using a specific registry | ||
| builder.WithAnnotation(new ContainerRegistryReferenceAnnotation(registryBuilder.Resource)); | ||
|
|
||
| return builder; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #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. | ||
|
|
||
| // 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.ContainerRegistry; | ||
| using Azure.Provisioning.Primitives; | ||
|
|
||
| namespace Aspire.Hosting.Azure.ContainerRegistry; | ||
|
|
||
| /// <summary> | ||
| /// Represents an Azure Container Registry resource. | ||
| /// </summary> | ||
| public class AzureContainerRegistryResource(string name, Action<AzureResourceInfrastructure> configureInfrastructure) | ||
| : AzureProvisioningResource(name, configureInfrastructure), IContainerRegistry | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want IResourceWithConnectionString? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to avoid this until we had the chance to figure out what it means to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree we should avoid it for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should remove the |
||
| { | ||
| /// <summary> | ||
| /// The name of the Azure Container Registry. | ||
| /// </summary> | ||
| public BicepOutputReference RegistryName => new("name", this); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do these need to be prefixed with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought so otherwise it would conflict with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NameOutputReference is what we call it for keyvault. |
||
|
|
||
| /// <summary> | ||
| /// The endpoint of the Azure Container Registry. | ||
| /// </summary> | ||
| public BicepOutputReference RegistryEndpoint => new("loginServer", this); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really want the bicep output to be named |
||
|
|
||
| /// <inheritdoc/> | ||
| ReferenceExpression IContainerRegistry.Name => ReferenceExpression.Create($"{RegistryName}"); | ||
|
|
||
| /// <inheritdoc/> | ||
| ReferenceExpression IContainerRegistry.Endpoint => ReferenceExpression.Create($"{RegistryEndpoint}"); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override ProvisionableResource AddAsExistingResource(AzureResourceInfrastructure infra) | ||
| { | ||
| var store = ContainerRegistryService.FromExisting(this.GetBicepIdentifier()); | ||
| store.Name = RegistryName.AsProvisioningParameter(infra); | ||
| infra.Add(store); | ||
| return store; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #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. | ||
|
|
||
| // 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.Azure; | ||
|
|
||
| /// <summary> | ||
| /// Annotation that indicates a resource is using a specific container registry. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Initializes a new instance of the <see cref="ContainerRegistryReferenceAnnotation"/> class. | ||
| /// </remarks> | ||
| /// <param name="registry">The container registry resource.</param> | ||
| public class ContainerRegistryReferenceAnnotation(IContainerRegistry registry) : IResourceAnnotation | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be in the same place |
||
| { | ||
| /// <summary> | ||
| /// Gets the container registry resource. | ||
| /// </summary> | ||
| public IContainerRegistry Registry { get; } = registry; | ||
| } | ||
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 can go away now that we are suppressing on the whole file.