Skip to content

Expose KubernetesEnvironmentResource.ResolveExpressionAsync as a public utility method #19840

Description

Background and Motivation

A common scenario for custom Resources and BaseKubernetesResources is the need for parameterized values in a Helm chart (e.g., {{ .Values.parameters.my_resource.my_value }}). To do this, ReferenceExpression types must be conditionally resolved or replaced with a Helm template string, similar to the previous example. With the current API, it is necessary for users to create their own implementations of this mapping, even though the KubernetesEnvironmentResource already contains a perfectly usable implementation. When custom resources provide their own publishing steps, they should have access to this functionality so they have the same first-class behavior as out-of-the-box Aspire resources.

Proposed API

public sealed class KubernetesEnvironmentResource : Resource, IComputeEnvironmentResource, IComputeEnvironmentWithVolumeMounts
{
-    private async Task<string> ResolveExpressionAsync(ReferenceExpression expression, string owningResourceName, CancellationToken cancellationToken)
-    {
-        //...
-    }
}
+ public static class KubernetesUtilities // Or some more compliant location
+ {
+     public static async Task<string> ResolveExpressionAsync(ReferenceExpression expression, string owningResourceName, CancellationToken cancellationToken)
+     {
+         //...
+     }
+ }

Usage Examples

var myParameter = builder.AddParameter("my-parameter");
var myResource = builder.AddMyCustomResource("name")
    .WithPipelineStepFactory((pipelineCtx) =>
    {
        var step = new PipelineStep()
        {
            Name = "publish-name-extensions",
            Action = async (ctx) =>
            {
                myResource.WithAnnotation(new KubernetesServiceCustomizationAnnotation(configure =>
                {
                    configure.AdditionalResources.Add(new CustomCRD()
                    {
                        CustomProperty = await KubernetesUtilities.ResolveExpressionAsync(myParameter, myResource.Resource.Name, ctx.CancellationToken);
                    });
                }));
            }
        };
    });

Alternative Designs

  • Considered exposing as an extension method of KubernetesResource or KubernetesEnvironmentResource, however, that doesn't feel very idiomatic because it's not a behavior of either of those resources.
  • Could be exposed as an extension on ReferenceExpression, but namespaced under the Aspire.Hosting.Kubernetes namespace to avoid polluting the public API.

Risks

No known risks

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions