Skip to content

Commit b7c38af

Browse files
author
Adrien Di Paolo
committed
feat: add dapr sidecar name option
1 parent 0e44c1e commit b7c38af

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed

src/CommunityToolkit.Aspire.Hosting.Dapr/DaprDistributedApplicationLifecycleHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ private async Task OnBeforeStartAsync(BeforeStartEvent @event, CancellationToken
210210
ModelNamedArg("--unix-domain-socket", sidecarOptions?.UnixDomainSocket),
211211
PostOptionsArgs(Args(sidecarOptions?.Command)));
212212

213-
var daprCliResourceName = $"{daprSidecar.Name}-cli";
213+
var daprCliResourceName = sidecarOptions?.SidecarName ?? $"{daprSidecar.Name}-cli";
214214
var daprCli = new ExecutableResource(daprCliResourceName, fileName, appHostDirectory);
215215

216216
// Propagate WaitAnnotations from the original resource to the Dapr CLI executable

src/CommunityToolkit.Aspire.Hosting.Dapr/DaprPolyglotOptions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ internal sealed record DaprSidecarExportOptions
5656
public string? RuntimePath { get; init; }
5757
public string? SchedulerHostAddress { get; init; }
5858
public string? UnixDomainSocket { get; init; }
59+
public string? SidecarName { get; init; }
5960

6061
public DaprSidecarOptions ToDaprSidecarOptions()
6162
{
@@ -93,7 +94,8 @@ public DaprSidecarOptions ToDaprSidecarOptions()
9394
RunFile = RunFile,
9495
RuntimePath = RuntimePath,
9596
SchedulerHostAddress = SchedulerHostAddress,
96-
UnixDomainSocket = UnixDomainSocket
97+
UnixDomainSocket = UnixDomainSocket,
98+
SidecarName = SidecarName,
9799
};
98100
#pragma warning restore CS0618
99101
}

src/CommunityToolkit.Aspire.Hosting.Dapr/DaprSidecarOptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,13 @@ public string? DaprReadBufferSize
241241
/// If specified, the Dapr sidecar will use Unix Domain Sockets for API calls.
242242
/// </remarks>
243243
public string? UnixDomainSocket { get; init; }
244+
245+
246+
/// <summary>
247+
/// Gets or sets the name of the Dapr sidecar.
248+
/// </summary>
249+
/// <remarks>
250+
/// If specified, the Dapr sidecar name will be used.
251+
/// </remarks>
252+
public string? SidecarName { get; init; }
244253
}

src/CommunityToolkit.Aspire.Hosting.Dapr/IDistributedApplicationComponentBuilderExtensions.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ public static IResourceBuilder<T> WithDaprSidecar<T>(this IResourceBuilder<T> bu
3636
[AspireExportIgnore(Reason = "Use the exported DTO-based overload instead to avoid ambiguous polyglot wrapper generation.")]
3737
public static IResourceBuilder<T> WithDaprSidecar<T>(this IResourceBuilder<T> builder, DaprSidecarOptions? options = null) where T : IResource
3838
{
39-
return builder.WithDaprSidecar(
39+
var sidecarName = string.IsNullOrWhiteSpace(options?.SidecarName)
40+
? $"{builder.Resource.Name}-dapr"
41+
: options.SidecarName;
42+
43+
return builder.WithDaprSidecarCore(
44+
sidecarName,
4045
sidecarBuilder =>
4146
{
4247
if (options is not null)
@@ -61,11 +66,37 @@ internal static IResourceBuilder<T> WithDaprSidecarExport<T>(this IResourceBuild
6166
/// <returns>The resource builder instance.</returns>
6267
[AspireExport("configureDaprSidecar", MethodName = "configureDaprSidecar", Description = "Adds a Dapr sidecar to the resource and exposes it for callback configuration")]
6368
public static IResourceBuilder<T> WithDaprSidecar<T>(this IResourceBuilder<T> builder, Action<IResourceBuilder<IDaprSidecarResource>> configureSidecar) where T : IResource
69+
{
70+
return builder.WithDaprSidecarCore($"{builder.Resource.Name}-dapr", configureSidecar);
71+
}
72+
73+
/// <summary>
74+
/// Ensures that a Dapr sidecar is started for the resource with a custom sidecar name.
75+
/// </summary>
76+
/// <typeparam name="T">The type of the resource.</typeparam>
77+
/// <param name="builder">The resource builder instance.</param>
78+
/// <param name="sidecarName">The Aspire resource name for the Dapr sidecar.</param>
79+
/// <param name="configureSidecar">A callback that can be use to configure the Dapr sidecar.</param>
80+
/// <returns>The resource builder instance.</returns>
81+
public static IResourceBuilder<T> WithDaprSidecar<T>(
82+
this IResourceBuilder<T> builder,
83+
string sidecarName,
84+
Action<IResourceBuilder<IDaprSidecarResource>> configureSidecar) where T : IResource
85+
{
86+
return builder.WithDaprSidecarCore(
87+
string.IsNullOrWhiteSpace(sidecarName) ? $"{builder.Resource.Name}-dapr" : sidecarName,
88+
configureSidecar);
89+
}
90+
91+
private static IResourceBuilder<T> WithDaprSidecarCore<T>(
92+
this IResourceBuilder<T> builder,
93+
string sidecarName,
94+
Action<IResourceBuilder<IDaprSidecarResource>> configureSidecar) where T : IResource
6495
{
6596
// Add Dapr is idempotent, so we can call it multiple times.
6697
builder.ApplicationBuilder.AddDapr();
6798

68-
var sidecarBuilder = builder.ApplicationBuilder.AddResource(new DaprSidecarResource($"{builder.Resource.Name}-dapr"))
99+
var sidecarBuilder = builder.ApplicationBuilder.AddResource(new DaprSidecarResource(sidecarName))
69100
.WithInitialState(new()
70101
{
71102
Properties = [],

tests/CommunityToolkit.Aspire.Hosting.Dapr.Tests/DaprTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Aspire.Hosting;
55
using Aspire.Hosting.Utils;
66
using CommunityToolkit.Aspire.Testing;
7+
using Microsoft.AspNetCore.Http;
78

89
namespace CommunityToolkit.Aspire.Hosting.Dapr.Tests;
910

@@ -165,4 +166,24 @@ public async Task WithDaprSideCarAddsAnnotationBasedOnTheSidecarAppOptions(strin
165166
Assert.Contains($"--app-protocol {expectedSchema}", commandline);
166167
Assert.NotNull(container.Annotations.OfType<DaprSidecarAnnotation>());
167168
}
169+
170+
[Theory]
171+
[InlineData("MyName", "MyName")]
172+
[InlineData(null, "name-dapr")]
173+
public void WithDaprSidecarName(string? sidecarName, string expectedSidecarName)
174+
{
175+
using var builder = TestDistributedApplicationBuilder.Create();
176+
177+
var containerResource = builder.AddContainer("name", "image");
178+
179+
containerResource.WithDaprSidecar(new DaprSidecarOptions
180+
{
181+
SidecarName = sidecarName
182+
});
183+
184+
var annotation = Assert.Single(
185+
containerResource.Resource.Annotations.OfType<DaprSidecarAnnotation>());
186+
187+
Assert.Equal(expectedSidecarName, annotation.Sidecar.Name);
188+
}
168189
}

0 commit comments

Comments
 (0)