From f49ae3aa179445b2940e32fef48e94f64b103e67 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Fri, 29 Mar 2024 18:33:47 -0700 Subject: [PATCH 1/3] Fix the dapr scheme to be http so that the sidecar works --- Directory.Packages.props | 2 +- .../DaprDistributedApplicationLifecycleHook.cs | 6 +++--- .../IDistributedApplicationBuilderExtensions.cs | 6 ++++++ .../IDistributedApplicationComponentBuilderExtensions.cs | 8 +++++++- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index a391736cdc..62c3525527 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -136,7 +136,7 @@ - + diff --git a/src/Aspire.Hosting.Dapr/DaprDistributedApplicationLifecycleHook.cs b/src/Aspire.Hosting.Dapr/DaprDistributedApplicationLifecycleHook.cs index 3ee7e1a0f5..373fde2962 100644 --- a/src/Aspire.Hosting.Dapr/DaprDistributedApplicationLifecycleHook.cs +++ b/src/Aspire.Hosting.Dapr/DaprDistributedApplicationLifecycleHook.cs @@ -151,9 +151,9 @@ public async Task BeforeStartAsync(DistributedApplicationModel appModel, Cancell context.EnvironmentVariables.TryAdd("DAPR_HTTP_ENDPOINT", http); })); - daprCli.Annotations.Add(new EndpointAnnotation(ProtocolType.Tcp, name: "grpc", port: sidecarOptions?.DaprGrpcPort)); - daprCli.Annotations.Add(new EndpointAnnotation(ProtocolType.Tcp, name: "http", port: sidecarOptions?.DaprHttpPort)); - daprCli.Annotations.Add(new EndpointAnnotation(ProtocolType.Tcp, name: "metrics", port: sidecarOptions?.MetricsPort)); + daprCli.Annotations.Add(new EndpointAnnotation(ProtocolType.Tcp, uriScheme: "http", name: "grpc", port: sidecarOptions?.DaprGrpcPort)); + daprCli.Annotations.Add(new EndpointAnnotation(ProtocolType.Tcp, uriScheme: "http", name: "http", port: sidecarOptions?.DaprHttpPort)); + daprCli.Annotations.Add(new EndpointAnnotation(ProtocolType.Tcp, uriScheme: "http", name: "metrics", port: sidecarOptions?.MetricsPort)); if (sidecarOptions?.EnableProfiling == true) { daprCli.Annotations.Add(new EndpointAnnotation(ProtocolType.Tcp, name: "profile", port: sidecarOptions?.ProfilePort)); diff --git a/src/Aspire.Hosting.Dapr/IDistributedApplicationBuilderExtensions.cs b/src/Aspire.Hosting.Dapr/IDistributedApplicationBuilderExtensions.cs index 424c7a205a..7ee47ca3da 100644 --- a/src/Aspire.Hosting.Dapr/IDistributedApplicationBuilderExtensions.cs +++ b/src/Aspire.Hosting.Dapr/IDistributedApplicationBuilderExtensions.cs @@ -46,6 +46,12 @@ public static IResourceBuilder AddDaprComponent(this IDi return builder .AddResource(resource) + .WithInitialState(new() + { + Properties = [], + ResourceType = "DaprComponent", + State = "Hidden" + }) .WithAnnotation(new ManifestPublishingCallbackAnnotation(context => WriteDaprComponentResourceToManifest(context, resource))); } diff --git a/src/Aspire.Hosting.Dapr/IDistributedApplicationComponentBuilderExtensions.cs b/src/Aspire.Hosting.Dapr/IDistributedApplicationComponentBuilderExtensions.cs index 9f5c5819cc..0426de300e 100644 --- a/src/Aspire.Hosting.Dapr/IDistributedApplicationComponentBuilderExtensions.cs +++ b/src/Aspire.Hosting.Dapr/IDistributedApplicationComponentBuilderExtensions.cs @@ -54,7 +54,13 @@ public static IResourceBuilder WithDaprSidecar(this IResourceBuilder bu // Add Dapr is idempoent, so we can call it multiple times. builder.ApplicationBuilder.AddDapr(); - var sidecarBuilder = builder.ApplicationBuilder.AddResource(new DaprSidecarResource($"{builder.Resource.Name}-dapr")); + var sidecarBuilder = builder.ApplicationBuilder.AddResource(new DaprSidecarResource($"{builder.Resource.Name}-dapr")) + .WithInitialState(new() + { + Properties = [], + ResourceType = "DaprSidecar", + State = "Hidden" + }); configureSidecar(sidecarBuilder); From 4039ece6cbb70ca1d9c00f6574d63ebfd5742b6f Mon Sep 17 00:00:00 2001 From: David Fowler Date: Fri, 29 Mar 2024 19:48:19 -0700 Subject: [PATCH 2/3] Added tests --- .../Aspire.Hosting.Dapr.csproj | 4 + src/Aspire.Hosting/DistributedApplication.cs | 3 +- .../Aspire.Hosting.Tests.csproj | 1 + tests/Aspire.Hosting.Tests/Dapr/DaprTests.cs | 76 +++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 tests/Aspire.Hosting.Tests/Dapr/DaprTests.cs diff --git a/src/Aspire.Hosting.Dapr/Aspire.Hosting.Dapr.csproj b/src/Aspire.Hosting.Dapr/Aspire.Hosting.Dapr.csproj index fa4bb208b5..2f0b52fd6f 100644 --- a/src/Aspire.Hosting.Dapr/Aspire.Hosting.Dapr.csproj +++ b/src/Aspire.Hosting.Dapr/Aspire.Hosting.Dapr.csproj @@ -15,4 +15,8 @@ + + + + diff --git a/src/Aspire.Hosting/DistributedApplication.cs b/src/Aspire.Hosting/DistributedApplication.cs index 5e98677c38..4554edb16f 100644 --- a/src/Aspire.Hosting/DistributedApplication.cs +++ b/src/Aspire.Hosting/DistributedApplication.cs @@ -110,7 +110,8 @@ public void Run() RunAsync().Wait(); } - private async Task ExecuteBeforeStartHooksAsync(CancellationToken cancellationToken) + // Internal for testing + internal async Task ExecuteBeforeStartHooksAsync(CancellationToken cancellationToken) { AspireEventSource.Instance.AppBeforeStartHooksStart(); diff --git a/tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj b/tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj index c8f2fb23da..59a66df1b8 100644 --- a/tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj +++ b/tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj @@ -27,6 +27,7 @@ + diff --git a/tests/Aspire.Hosting.Tests/Dapr/DaprTests.cs b/tests/Aspire.Hosting.Tests/Dapr/DaprTests.cs new file mode 100644 index 0000000000..ba69a814a4 --- /dev/null +++ b/tests/Aspire.Hosting.Tests/Dapr/DaprTests.cs @@ -0,0 +1,76 @@ +// 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.Dapr; +using Aspire.Hosting.Tests.Utils; +using Aspire.Hosting.Utils; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace Aspire.Hosting.Tests.Dapr; + +public class DaprTests +{ + [Fact] + public async Task WithDaprSideCarAddsAnnotationAndSidecarResource() + { + using var builder = TestDistributedApplicationBuilder.Create(); + builder.AddContainer("name", "image") + .WithEndpoint("http", e => + { + e.Port = 8000; + e.AllocatedEndpoint = new(e, "localhost", 80); + }) + .WithDaprSidecar(); + + using var app = builder.Build(); + await app.ExecuteBeforeStartHooksAsync(default); + + var model = app.Services.GetRequiredService(); + + Assert.Equal(3, model.Resources.Count); + var container = Assert.Single(model.Resources.OfType()); + var sidecarResource = Assert.Single(model.Resources.OfType()); + var sideCarCli = Assert.Single(model.Resources.OfType()); + + Assert.True(sideCarCli.TryGetEndpoints(out var endpoints)); + + var ports = new Dictionary + { + ["http"] = 3500, + ["grpc"] = 50001, + ["metrics"] = 9090 + }; + + foreach (var e in endpoints) + { + e.AllocatedEndpoint = new(e, "localhost", ports[e.Name]); + } + + var config = await EnvironmentVariableEvaluator.GetEnvironmentVariablesAsync(container); + var sidecarArgs = await ArgumentEvaluator.GetArgumentListAsync(sideCarCli); + + Assert.Equal("http://localhost:3500", config["DAPR_HTTP_ENDPOINT"]); + Assert.Equal("http://localhost:50001", config["DAPR_GRPC_ENDPOINT"]); + + var expectedArgs = new[] + { + "run", + "--app-id", + "name", + "--app-port", + "80", + "--dapr-grpc-port", + "{{- portForServing \"name-dapr-cli_grpc\" -}}", + "--dapr-http-port", + "{{- portForServing \"name-dapr-cli_http\" -}}", + "--metrics-port", + "{{- portForServing \"name-dapr-cli_metrics\" -}}", + "--app-channel-address", + "localhost" + }; + + Assert.Equal(expectedArgs, sidecarArgs); + Assert.NotNull(container.Annotations.OfType()); + } +} From 033cc73e11c86f4254c14f32ce377a7f881aa343 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Fri, 29 Mar 2024 21:12:18 -0700 Subject: [PATCH 3/3] Set dapr path in test --- tests/Aspire.Hosting.Tests/Dapr/DaprTests.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Aspire.Hosting.Tests/Dapr/DaprTests.cs b/tests/Aspire.Hosting.Tests/Dapr/DaprTests.cs index ba69a814a4..aa8573a794 100644 --- a/tests/Aspire.Hosting.Tests/Dapr/DaprTests.cs +++ b/tests/Aspire.Hosting.Tests/Dapr/DaprTests.cs @@ -15,6 +15,12 @@ public class DaprTests public async Task WithDaprSideCarAddsAnnotationAndSidecarResource() { using var builder = TestDistributedApplicationBuilder.Create(); + builder.AddDapr(o => + { + // Fake path to avoid throwing + o.DaprPath = "dapr"; + }); + builder.AddContainer("name", "image") .WithEndpoint("http", e => {