-
Notifications
You must be signed in to change notification settings - Fork 517
[Dapr Extension] Allow defining custom placement container address #767
Changes from 1 commit
07cb48a
3ac6c94
aa56c46
f2b084a
f2ab5c4
1f0c2c3
6e80a2e
4f3e2ac
abb10b1
ac1e237
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,61 @@ public override Task ProcessAsync(ExtensionContext context, ExtensionConfigurati | |
|
||
if (context.Operation == ExtensionContext.OperationKind.LocalRun) | ||
{ | ||
// default placement port number | ||
var placementPort = 50005; | ||
var isCustomPlacementPort = false; | ||
|
||
var existingDaprPlacementDefinition = context.Application.Services.FirstOrDefault(s => | ||
s is ContainerServiceBuilder serviceBuilder && serviceBuilder.Image == "daprio/dapr"); | ||
|
||
|
||
// see if a placement port number has been defined at the extension level | ||
if (config.Data.TryGetValue("placement-port", out var obj) && obj?.ToString() is string && int.TryParse(obj.ToString(), out var customPlacementPort)) | ||
{ | ||
context.Output.WriteDebugLine($"Using Dapr placement service port {customPlacementPort} from 'placement-port'"); | ||
placementPort = customPlacementPort; | ||
isCustomPlacementPort = true; | ||
} | ||
|
||
// check to see if a custom service definition for the placement container exists. | ||
if (existingDaprPlacementDefinition == null) | ||
{ | ||
context.Output.WriteDebugLine("Dapr placement service not defined in Tye.yaml. Trying to create one..."); | ||
|
||
if (!(config.Data.TryGetValue("exclude-placement-container", out obj) && | ||
obj?.ToString() is string excludePlacementContainer && excludePlacementContainer == "true")) | ||
{ | ||
context.Output.WriteDebugLine("Injecting Dapr placement service..."); | ||
var daprPlacement = new ContainerServiceBuilder("placement", "daprio/dapr") | ||
{ | ||
Args = "./placement", | ||
Bindings = { | ||
new BindingBuilder() { | ||
Port = placementPort, | ||
ContainerPort = 50005, | ||
|
||
Protocol = "http" | ||
} | ||
} | ||
}; | ||
context.Application.Services.Add(daprPlacement); | ||
} | ||
else | ||
{ | ||
context.Output.WriteDebugLine("Skipping injecting Dapr placement service because 'exclude-placement-container=true'..."); | ||
} | ||
} | ||
else | ||
{ | ||
// use the port defined in the custom service definition is possible | ||
var definedPlacementPort = existingDaprPlacementDefinition.Bindings.FirstOrDefault(b => b.Port.HasValue); | ||
|
||
if (definedPlacementPort?.Port != null && !isCustomPlacementPort) | ||
{ | ||
context.Output.WriteDebugLine($"Using Dapr placement service port {definedPlacementPort.Port.Value} from service definition..."); | ||
placementPort = definedPlacementPort.Port.Value; | ||
} | ||
|
||
context.Output.WriteDebugLine("Skipping injecting Dapr placement service because it's already defined in Tye.yaml..."); | ||
} | ||
|
||
// For local run, enumerate all projects, and add services for each dapr proxy. | ||
var projects = context.Application.Services.OfType<ProjectServiceBuilder>().ToList(); | ||
foreach (var project in projects) | ||
|
@@ -45,35 +100,15 @@ public override Task ProcessAsync(ExtensionContext context, ExtensionConfigurati | |
|
||
var daprExecutablePath = GetDaprExecutablePath(); | ||
|
||
|
||
var proxy = new ExecutableServiceBuilder($"{project.Name}-dapr", daprExecutablePath) | ||
{ | ||
WorkingDirectory = context.Application.Source.DirectoryName, | ||
|
||
// These environment variables are replaced with environment variables | ||
// defined for this service. | ||
Args = $"-app-id {project.Name} -app-port %APP_PORT% -dapr-grpc-port %DAPR_GRPC_PORT% --dapr-http-port %DAPR_HTTP_PORT% --metrics-port %METRICS_PORT%", | ||
Args = $"-app-id {project.Name} -app-port %APP_PORT% -dapr-grpc-port %DAPR_GRPC_PORT% --dapr-http-port %DAPR_HTTP_PORT% --metrics-port %METRICS_PORT% --placement-address=localhost:{placementPort}", | ||
}; | ||
|
||
if (config.Data.TryGetValue("placement-address", out var obj) && obj?.ToString() is string customPlacementAddressValue) | ||
{ | ||
proxy.Args += $" --placement-address {customPlacementAddressValue}"; | ||
} | ||
else | ||
{ | ||
var placementAddress = "localhost:50005"; | ||
|
||
// Note: This port inferring logic was causing the tests to fail. Keeping it here for now but will remove based on PR feedback. | ||
|
||
//if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
//{ | ||
// // Dapr running on WSL2 backend has placement container like 0.0.0.0:6050->50005/tcp | ||
// placementAddress = "localhost:6050"; | ||
//} | ||
|
||
proxy.Args += $" --placement-address {placementAddress}"; | ||
} | ||
|
||
// When running locally `-config` specifies a filename, not a configuration name. By convention | ||
// we'll assume the filename and config name are the same. | ||
if (config.Data.TryGetValue("config", out obj) && obj?.ToString() is string daprConfig) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be a random port instead of a specific port?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will now use the next available port, same as the other parts of the system.