Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<Compile Include="$(SharedDir)AzurePortalUrls.cs" Link="AzurePortalUrls.cs" />
<Compile Include="$(SharedDir)BicepFormattingHelpers.cs" LinkBase="Shared\BicepFormattingHelpers.cs" />
<Compile Include="$(SharedDir)ComputeEnvironmentEndpointResolver.cs" LinkBase="Shared\ComputeEnvironmentEndpointResolver.cs" />
<Compile Include="$(SharedDir)ContainerRegistryInfrastructure.cs" LinkBase="Shared\ContainerRegistryInfrastructure.cs" />
<Compile Include="$(SharedDir)ResourceNameComparer.cs" LinkBase="Shared\ResourceNameComparer.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ BicepValue<string> GetHostValue(string? prefix = null, string? suffix = null)

if (value is EndpointReference ep)
{
// The referenced endpoint may belong to a resource deployed to a different compute
// environment (for example a Foundry hosted agent). In that case delegate to the owning
// compute environment instead of looking it up in this environment's local endpoint map.
if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression(
ep, [_containerAppEnvironmentContext.Environment], out var crossExpr))
{
return ProcessValue(crossExpr, secretType, parent);
}

var context = ep.Resource == resource
? this
: _containerAppEnvironmentContext.GetContainerAppContext(ep.Resource);
Expand Down Expand Up @@ -274,6 +283,12 @@ BicepValue<string> GetHostValue(string? prefix = null, string? suffix = null)

if (value is EndpointReferenceExpression epExpr)
{
if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression(
epExpr, [_containerAppEnvironmentContext.Environment], out var crossExpr))
{
return ProcessValue(crossExpr, secretType, parent);
}

var context = epExpr.Endpoint.Resource == resource
? this
: _containerAppEnvironmentContext.GetContainerAppContext(epExpr.Endpoint.Resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ItemGroup>
<Compile Include="$(SharedDir)AzurePortalUrls.cs" Link="AzurePortalUrls.cs" />
<Compile Include="$(SharedDir)BicepFormattingHelpers.cs" LinkBase="Shared\BicepFormattingHelpers.cs" />
<Compile Include="$(SharedDir)ComputeEnvironmentEndpointResolver.cs" LinkBase="Shared\ComputeEnvironmentEndpointResolver.cs" />
<Compile Include="$(SharedDir)ContainerRegistryInfrastructure.cs" LinkBase="Shared\ContainerRegistryInfrastructure.cs" />
<Compile Include="$(SharedDir)DashboardConfigNames.cs" LinkBase="Shared\DashboardConfigNames.cs" />
<Compile Include="$(SharedDir)KnownConfigNames.cs" LinkBase="Shared\KnownConfigNames.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ private void ProcessEndpoints()

if (value is EndpointReference ep)
{
// The referenced endpoint may belong to a resource deployed to a different compute
// environment (for example a Foundry hosted agent). In that case delegate to the owning
// compute environment instead of looking it up in this environment's local endpoint map.
if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression(
ep, [environmentContext.Environment], out var crossExpr))
{
return ProcessValue(crossExpr, secretType, parent, isSlot);
}

var context = environmentContext.GetAppServiceContext(ep.Resource);
return isSlot ?
(GetEndpointValue(context._slotEndpointMapping[ep.EndpointName], EndpointProperty.Url), secretType) :
Expand Down Expand Up @@ -206,6 +215,12 @@ private void ProcessEndpoints()

if (value is EndpointReferenceExpression epExpr)
{
if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression(
epExpr, [environmentContext.Environment], out var crossExpr))
{
return ProcessValue(crossExpr, secretType, parent, isSlot);
}

var context = environmentContext.GetAppServiceContext(epExpr.Endpoint.Resource);
var mapping = isSlot ? context._slotEndpointMapping[epExpr.Endpoint.EndpointName] : context._endpointMapping[epExpr.Endpoint.EndpointName];
var val = GetEndpointValue(mapping, epExpr.Property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<PackageReference Include="Azure.Provisioning.Cdn" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(SharedDir)ComputeEnvironmentEndpointResolver.cs" LinkBase="Shared\ComputeEnvironmentEndpointResolver.cs" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Aspire.Hosting.Azure.Tests" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,11 @@ public static IResourceBuilder<AzureFrontDoorResource> WithOrigin<T>(

private static IComputeEnvironmentResource GetEffectiveComputeEnvironment(IResource resource)
{
if (resource.GetComputeEnvironment() is { } computeEnvironment)
if (ComputeEnvironmentEndpointResolver.TryGetEffectiveComputeEnvironment(resource, out var computeEnvironment))
{
return computeEnvironment;
}

if (resource.GetDeploymentTargetAnnotation()?.ComputeEnvironment is { } deploymentComputeEnvironment)
{
return deploymentComputeEnvironment;
}

throw new InvalidOperationException(
$"Resource '{resource.Name}' does not have a compute environment. " +
"Ensure a compute environment (e.g., Azure Container Apps, Azure App Service) is configured in the application model.");
Expand Down
1 change: 1 addition & 0 deletions src/Aspire.Hosting.Foundry/Aspire.Hosting.Foundry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<ItemGroup>
<Compile Include="$(RepoRoot)src\Shared\AzureRoleAssignmentUtils.cs" />
<Compile Include="$(RepoRoot)src\Shared\ContainerRegistryInfrastructure.cs" />
<Compile Include="$(SharedDir)ComputeEnvironmentEndpointResolver.cs" LinkBase="Shared\ComputeEnvironmentEndpointResolver.cs" />
<Compile Include="$(RepoRoot)src\Aspire.Hosting\Utils\FormattingHelpers.cs" Link="Utils\FormattingHelpers.cs" />
<Compile Include="..\Shared\AzureCredentialHelper.cs" Link="AzureCredentialHelper.cs" />
<Compile Remove="tools\**\*.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,7 @@ internal static async Task<Dictionary<string, string>> GetResolvedEnvironmentVar
throw CreateEndpointResolutionException(hostedAgent, resource, environmentVariableName, endpointReference, $"Endpoint '{endpoint.Name}' is internal. Foundry hosted agents can only reference externally exposed endpoints during publish.");
}

var deploymentTarget = endpointReference.Resource.GetDeploymentTargetAnnotation();
if (deploymentTarget?.ComputeEnvironment is not { } computeEnvironment)
if (!ComputeEnvironmentEndpointResolver.TryGetEffectiveComputeEnvironment(endpointReference.Resource, out var computeEnvironment))
{
var reason = $"Resource '{endpointReference.Resource.Name}' does not have a compute environment deployment target.";
throw CreateEndpointResolutionException(hostedAgent, resource, environmentVariableName, endpointReference, reason);
Expand Down
29 changes: 29 additions & 0 deletions src/Aspire.Hosting.Foundry/Project/ProjectResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,35 @@ public AzureContainerRegistryResource? ContainerRegistry
/// Get the address for the particular agent's endpoint.
/// </summary>
ReferenceExpression IComputeEnvironmentResource.GetHostAddressExpression(EndpointReference endpointReference)
=> GetAgentAddressExpression(endpointReference);

/// <summary>
/// Produces the endpoint property expression for a hosted agent endpoint owned by this Foundry project.
/// </summary>
/// <remarks>
/// The agent address is already a fully-qualified <c>https</c> URL
/// (for example <c>https://{account}.services.ai.azure.com/.../agents/{name}</c>), so it is
/// returned directly for URL-shaped properties. The default
/// <see cref="IComputeEnvironmentResource.GetEndpointPropertyExpression"/> implementation composes
/// <c>{scheme}://{host}</c>, which would produce a malformed double-scheme value
/// (for example <c>http://https://...</c>). Only the properties that <c>WithReference</c> emits for a
/// hosted agent endpoint are supported.
/// </remarks>
ReferenceExpression IComputeEnvironmentResource.GetEndpointPropertyExpression(EndpointReferenceExpression endpointReferenceExpression)
{
ArgumentNullException.ThrowIfNull(endpointReferenceExpression);

var property = endpointReferenceExpression.Property;
return property switch
{
EndpointProperty.Url => GetAgentAddressExpression(endpointReferenceExpression.Endpoint),
EndpointProperty.Scheme => ReferenceExpression.Create($"https"),
_ => throw new InvalidOperationException(
$"The endpoint property '{property}' is not supported for Foundry hosted agent endpoints. Only 'Url' and 'Scheme' are supported.")
};
}

private ReferenceExpression GetAgentAddressExpression(EndpointReference endpointReference)
{
var resource = endpointReference.Resource;
return ReferenceExpression.Create($"{Endpoint}/agents/{resource.Name}");
Comment thread
davidfowl marked this conversation as resolved.
Resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</ItemGroup>

<ItemGroup>
<Compile Include="$(SharedDir)ComputeEnvironmentEndpointResolver.cs" LinkBase="Shared\ComputeEnvironmentEndpointResolver.cs" />
<Compile Include="$(SharedDir)PublishingContextUtils.cs" LinkBase="Shared" />
<Compile Include="$(SharedDir)Yaml\*.cs" LinkBase="Shared\Yaml" />
</ItemGroup>
Expand Down
17 changes: 17 additions & 0 deletions src/Aspire.Hosting.Kubernetes/KubernetesResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,16 @@ private async Task<object> ProcessValueAsync(KubernetesEnvironmentContext contex

if (value is EndpointReference ep)
{
// The referenced endpoint may belong to a resource deployed to a different compute
// environment (for example a Foundry hosted agent). In that case delegate to the owning
// compute environment instead of looking it up in this environment's local endpoint map.
if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression(
ep, [kubernetesEnvironmentResource, kubernetesEnvironmentResource.OwningComputeEnvironment], out var crossExpr))
{
value = crossExpr;
continue;
}

var referencedResource = ep.Resource == this
? this
: await context.CreateKubernetesResourceAsync(ep.Resource, executionContext, default).ConfigureAwait(false);
Expand Down Expand Up @@ -516,6 +526,13 @@ private async Task<object> ProcessValueAsync(KubernetesEnvironmentContext contex

if (value is EndpointReferenceExpression epExpr)
{
if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression(
epExpr, [kubernetesEnvironmentResource, kubernetesEnvironmentResource.OwningComputeEnvironment], out var crossExpr))
{
value = crossExpr;
continue;
}

var referencedResource = epExpr.Endpoint.Resource == this
? this
: await context.CreateKubernetesResourceAsync(epExpr.Endpoint.Resource, executionContext, default).ConfigureAwait(false);
Expand Down
137 changes: 137 additions & 0 deletions src/Shared/ComputeEnvironmentEndpointResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using Aspire.Hosting.ApplicationModel;

namespace Aspire.Hosting;

/// <summary>
/// Helper for resolving endpoint references that point at a resource deployed to a different
/// compute environment than the one currently generating deployment artifacts.
/// </summary>
/// <remarks>
/// Compute environment publishers (App Service, Azure Container Apps, Kubernetes) resolve
/// endpoint references against their own local endpoint map. When a resource references an
/// endpoint owned by a resource deployed to a different compute environment (for example a
/// Foundry hosted agent deployed to an <c>AzureCognitiveServicesProjectResource</c>), the
/// endpoint is not present in the local map and the lookup fails. In that case the owning
/// compute environment knows how to express the endpoint property, so we delegate to it.
/// This mirrors the inverse-direction logic used by Foundry hosted agents when they reference
/// endpoints owned by App Service/ACA/Kubernetes resources.
/// </remarks>
internal static class ComputeEnvironmentEndpointResolver

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will everyone building hosting/deploy integrations require code like this? Would this be made public sometime in the future?

I want to make sure the design holds up beyond our own bits.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a public API, this makes that public API a little less verbose.

{
/// <summary>
/// Attempts to produce a <see cref="ReferenceExpression"/> for an endpoint's URL by
/// delegating to the compute environment that owns the endpoint's resource, when that
/// environment is different from the publisher's current compute environment(s).
/// </summary>
/// <param name="endpointReference">The endpoint reference to resolve.</param>
/// <param name="currentComputeEnvironments">
/// The compute environment(s) the current publisher is generating artifacts for. When the
/// endpoint's owning resource deploys to one of these, resolution is left to the local
/// endpoint map and this method returns <see langword="false"/>.
/// </param>
/// <param name="expression">
/// When this method returns <see langword="true"/>, contains the delegated reference expression.
/// </param>
/// <returns>
/// <see langword="true"/> if the endpoint is owned by a different compute environment and a
/// delegated expression was produced; otherwise <see langword="false"/>.
/// </returns>
public static bool TryGetCrossEnvironmentEndpointExpression(
EndpointReference endpointReference,
IReadOnlyList<IComputeEnvironmentResource?> currentComputeEnvironments,
[NotNullWhen(true)] out ReferenceExpression? expression)
{
ArgumentNullException.ThrowIfNull(endpointReference);

return TryGetCrossEnvironmentEndpointExpression(
endpointReference.Property(EndpointProperty.Url),
currentComputeEnvironments,
out expression);
}

/// <summary>
/// Attempts to produce a <see cref="ReferenceExpression"/> for an endpoint property by
/// delegating to the compute environment that owns the endpoint's resource, when that
/// environment is different from the publisher's current compute environment(s).
/// </summary>
/// <param name="endpointReferenceExpression">The endpoint reference expression to resolve.</param>
/// <param name="currentComputeEnvironments">
/// The compute environment(s) the current publisher is generating artifacts for. When the
/// endpoint's owning resource deploys to one of these, resolution is left to the local
/// endpoint map and this method returns <see langword="false"/>.
/// </param>
/// <param name="expression">
/// When this method returns <see langword="true"/>, contains the delegated reference expression.
/// </param>
/// <returns>
/// <see langword="true"/> if the endpoint is owned by a different compute environment and a
/// delegated expression was produced; otherwise <see langword="false"/>.
/// </returns>
public static bool TryGetCrossEnvironmentEndpointExpression(
EndpointReferenceExpression endpointReferenceExpression,
IReadOnlyList<IComputeEnvironmentResource?> currentComputeEnvironments,
[NotNullWhen(true)] out ReferenceExpression? expression)
{
ArgumentNullException.ThrowIfNull(endpointReferenceExpression);
ArgumentNullException.ThrowIfNull(currentComputeEnvironments);

expression = null;

var owningResource = endpointReferenceExpression.Endpoint.Resource;

// Resolve the compute environment the owning resource deploys to. A plain resource that is
// not deployed anywhere has none, so there is nothing to delegate to and the local lookup
// handles it.
if (!TryGetEffectiveComputeEnvironment(owningResource, out var owningComputeEnvironment))
{
return false;
}

// If the owning resource deploys to one of the current publisher's compute environments, the
// endpoint lives in the local endpoint map. Leave resolution to the existing local lookup so
// generated artifacts (bicep parameters, helm values, etc.) are unchanged.
foreach (var current in currentComputeEnvironments)
{
if (ReferenceEquals(current, owningComputeEnvironment))
{
return false;
}
}

#pragma warning disable ASPIRECOMPUTE002 // Experimental: compute environment endpoint expression
expression = owningComputeEnvironment.GetEndpointPropertyExpression(endpointReferenceExpression);
#pragma warning restore ASPIRECOMPUTE002

return true;
}

/// <summary>
/// Resolves the compute environment that a resource is deployed to. A resource may be bound to a
/// compute environment explicitly (via <see cref="ResourceExtensions.GetComputeEnvironment"/>) or
/// implicitly through its deployment target.
/// </summary>
/// <param name="resource">The resource whose compute environment should be resolved.</param>
/// <param name="computeEnvironment">
/// When this method returns <see langword="true"/>, contains the owning compute environment.
/// </param>
/// <returns>
/// <see langword="true"/> if a compute environment was resolved; otherwise <see langword="false"/>.
/// </returns>
public static bool TryGetEffectiveComputeEnvironment(
IResource resource,
[NotNullWhen(true)] out IComputeEnvironmentResource? computeEnvironment)
{
ArgumentNullException.ThrowIfNull(resource);

// Prefer an explicit compute environment binding, then fall back to the deployment target's
// compute environment. This matches how endpoint references are resolved elsewhere
// (Azure Front Door origins, Foundry hosted agents) so all call sites agree on "where is
// this resource deployed".
computeEnvironment = resource.GetComputeEnvironment() ?? resource.GetDeploymentTargetAnnotation()?.ComputeEnvironment;
return computeEnvironment is not null;
}
}
Loading
Loading