Skip to content
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

[release/8.0-preview5] Fix the dapr scheme to be http so that the sidecar works #3297

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
<PackageVersion Include="Testcontainers.Redis" Version="$(TestcontainersPackageVersion)" />
<PackageVersion Include="Testcontainers.Nats" Version="$(TestcontainersPackageVersion)" />
<!-- playground apps dependencies -->
<PackageVersion Include="Dapr.AspNetCore" Version="1.12.0" />
<PackageVersion Include="Dapr.AspNetCore" Version="1.13.0" />
<PackageVersion Include="Microsoft.Orleans.Clustering.AzureStorage" Version="8.1.0-nightly.20240126.1" />
<PackageVersion Include="Microsoft.Orleans.Persistence.AzureStorage" Version="8.1.0-nightly.20240126.1" />
<PackageVersion Include="Microsoft.Orleans.Server" Version="8.1.0-nightly.20240126.1" />
Expand Down
4 changes: 4 additions & 0 deletions src/Aspire.Hosting.Dapr/Aspire.Hosting.Dapr.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@
<ProjectReference Include="..\Aspire.Hosting\Aspire.Hosting.csproj" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Aspire.Hosting.Tests" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public static IResourceBuilder<IDaprComponentResource> AddDaprComponent(this IDi

return builder
.AddResource(resource)
.WithInitialState(new()
{
Properties = [],
ResourceType = "DaprComponent",
State = "Hidden"
})
.WithAnnotation(new ManifestPublishingCallbackAnnotation(context => WriteDaprComponentResourceToManifest(context, resource)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ public static IResourceBuilder<T> WithDaprSidecar<T>(this IResourceBuilder<T> 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);

Expand Down
3 changes: 2 additions & 1 deletion src/Aspire.Hosting/DistributedApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
1 change: 1 addition & 0 deletions tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<ProjectReference Include="..\..\src\Aspire.Hosting.Azure.OperationalInsights\Aspire.Hosting.Azure.OperationalInsights.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\..\src\Aspire.Hosting.Azure.ApplicationInsights\Aspire.Hosting.Azure.ApplicationInsights.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\..\src\Aspire.Hosting.Azure.AppConfiguration\Aspire.Hosting.Azure.AppConfiguration.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\..\src\Aspire.Hosting.Dapr\Aspire.Hosting.Dapr.csproj" />
<ProjectReference Include="..\..\src\Aspire.Hosting.MongoDB\Aspire.Hosting.MongoDB.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\..\src\Aspire.Hosting.MySql\Aspire.Hosting.MySql.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\..\src\Aspire.Hosting.Nats\Aspire.Hosting.Nats.csproj" IsAspireProjectResource="false" />
Expand Down
82 changes: 82 additions & 0 deletions tests/Aspire.Hosting.Tests/Dapr/DaprTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// 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.AddDapr(o =>
{
// Fake path to avoid throwing
o.DaprPath = "dapr";
});

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<DistributedApplicationModel>();

Assert.Equal(3, model.Resources.Count);
var container = Assert.Single(model.Resources.OfType<ContainerResource>());
var sidecarResource = Assert.Single(model.Resources.OfType<IDaprSidecarResource>());
var sideCarCli = Assert.Single(model.Resources.OfType<ExecutableResource>());

Assert.True(sideCarCli.TryGetEndpoints(out var endpoints));

var ports = new Dictionary<string, int>
{
["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<DaprSidecarAnnotation>());
}
}