Skip to content

Commit

Permalink
Enable DAPR to work with services listening on addresses different fr…
Browse files Browse the repository at this point in the history
…om localhost (#1548)

* Enable DAPR to work with services listening on addresses different from localhost

* Fix bad merge and address @philliphoff feedback
  • Loading branch information
karolz-ms authored Jan 11, 2024
1 parent fda8262 commit 360c6a9
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/Aspire.Hosting.Dapr/DaprDistributedApplicationLifecycleHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public async Task BeforeStartAsync(DistributedApplicationModel appModel, Cancell
var daprHttpPortArg = (string? port) => ModelNamedArg("--dapr-http-port", port);
var daprMetricsPortArg = (string? port) => ModelNamedArg("--metrics-port", port);
var daprProfilePortArg = (string? port) => ModelNamedArg("--profile-port", port);
var daprAppChannelAddressArg = (string? address) => ModelNamedArg("--app-channel-address", address);

var appId = sidecarOptions?.AppId ?? resource.Name;

Expand Down Expand Up @@ -143,19 +144,25 @@ public async Task BeforeStartAsync(DistributedApplicationModel appModel, Cancell
return;
}
string? grpcEndpoint, httpEndpoint;
if (resource is ContainerResource)
{
// By default, the Dapr sidecar will listen on localhost, which is not accessible from the container.
var grpcEndpoint = $"http://localhost:{{{{- portFor \"{daprCliResourceName}_grpc\" -}}}}";
var httpEndpoint = $"http://localhost:{{{{- portFor \"{daprCliResourceName}_http\" -}}}}";
grpcEndpoint = $"http://localhost:{{{{- portFor \"{daprCliResourceName}_grpc\" -}}}}";
httpEndpoint = $"http://localhost:{{{{- portFor \"{daprCliResourceName}_http\" -}}}}";
context.EnvironmentVariables.TryAdd("DAPR_GRPC_ENDPOINT", HostNameResolver.ReplaceLocalhostWithContainerHost(grpcEndpoint, _configuration));
context.EnvironmentVariables.TryAdd("DAPR_HTTP_ENDPOINT", HostNameResolver.ReplaceLocalhostWithContainerHost(httpEndpoint, _configuration));
}
context.EnvironmentVariables.TryAdd("DAPR_GRPC_PORT", $"{{{{- portFor \"{daprCliResourceName}_grpc\" -}}}}");
context.EnvironmentVariables.TryAdd("DAPR_HTTP_PORT", $"{{{{- portFor \"{daprCliResourceName}_http\" -}}}}");
else
{
grpcEndpoint = $"http://{{{{- addressFor \"{daprCliResourceName}_grpc\" -}}}}:{{{{- portFor \"{daprCliResourceName}_grpc\" -}}}}";
httpEndpoint = $"http://{{{{- addressFor \"{daprCliResourceName}_http\" -}}}}:{{{{- portFor \"{daprCliResourceName}_http\" -}}}}";
context.EnvironmentVariables.TryAdd("DAPR_GRPC_ENDPOINT", grpcEndpoint);
context.EnvironmentVariables.TryAdd("DAPR_HTTP_ENDPOINT", httpEndpoint);
}
}));

daprCli.Annotations.Add(new EndpointAnnotation(ProtocolType.Tcp, name: "grpc", port: sidecarOptions?.DaprGrpcPort));
Expand All @@ -176,9 +183,10 @@ public async Task BeforeStartAsync(DistributedApplicationModel appModel, Cancell
new ExecutableArgsCallbackAnnotation(
updatedArgs =>
{
AllocatedEndpointAnnotation? httpEndPoint = null;
if (resource.TryGetAllocatedEndPoints(out var projectEndPoints))
{
var httpEndPoint = projectEndPoints.FirstOrDefault(endPoint => endPoint.Name == "http");
httpEndPoint = projectEndPoints.FirstOrDefault(endPoint => endPoint.Name == "http");
if (httpEndPoint is not null && sidecarOptions?.AppPort is null)
{
Expand All @@ -193,6 +201,10 @@ public async Task BeforeStartAsync(DistributedApplicationModel appModel, Cancell
{
updatedArgs.AddRange(daprProfilePortArg($"{{{{- portForServing \"{daprCliResourceName}_profile\" -}}}}")());
}
if (sidecarOptions?.AppChannelAddress is null && httpEndPoint is not null)
{
updatedArgs.AddRange(daprAppChannelAddressArg(httpEndPoint.Address)());
}
}));

// Apply environment variables to the CLI...
Expand Down

0 comments on commit 360c6a9

Please sign in to comment.