Browse Source

Allow executable-based services to have Dapr sidecars (#1116)

* Start Dapr sidecars for executable services.

* Switch to `dapr` instead of starting `daprd` sidecar directly.

* Update encoding.
philliphoff-multiplatform
Phillip Hoff 5 years ago
committed by GitHub
parent
commit
d5b6f4cee3
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/Microsoft.Tye.Core/ExecutableServiceBuilder.cs
  2. 20
      src/Microsoft.Tye.Core/LaunchedServiceBuilder.cs
  3. 6
      src/Microsoft.Tye.Core/ProjectServiceBuilder.cs
  4. 25
      src/Microsoft.Tye.Extensions/Dapr/DaprExtension.cs

6
src/Microsoft.Tye.Core/ExecutableServiceBuilder.cs

@ -6,7 +6,7 @@ using System.Collections.Generic;
namespace Microsoft.Tye namespace Microsoft.Tye
{ {
public sealed class ExecutableServiceBuilder : ServiceBuilder public sealed class ExecutableServiceBuilder : LaunchedServiceBuilder
{ {
public ExecutableServiceBuilder(string name, string executable) public ExecutableServiceBuilder(string name, string executable)
: base(name) : base(name)
@ -20,10 +20,6 @@ namespace Microsoft.Tye
public string? Args { get; set; } public string? Args { get; set; }
public int Replicas { get; set; } = 1;
public List<EnvironmentVariableBuilder> EnvironmentVariables { get; } = new List<EnvironmentVariableBuilder>();
public ProbeBuilder? Liveness { get; set; } public ProbeBuilder? Liveness { get; set; }
public ProbeBuilder? Readiness { get; set; } public ProbeBuilder? Readiness { get; set; }

20
src/Microsoft.Tye.Core/LaunchedServiceBuilder.cs

@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
namespace Microsoft.Tye
{
public abstract class LaunchedServiceBuilder : ServiceBuilder
{
public LaunchedServiceBuilder(string name)
: base(name)
{
}
public List<EnvironmentVariableBuilder> EnvironmentVariables { get; } = new List<EnvironmentVariableBuilder>();
public int Replicas { get; set; } = 1;
}
}

6
src/Microsoft.Tye.Core/ProjectServiceBuilder.cs

@ -6,7 +6,7 @@ using System.Collections.Generic;
namespace Microsoft.Tye namespace Microsoft.Tye
{ {
public class ProjectServiceBuilder : ServiceBuilder public class ProjectServiceBuilder : LaunchedServiceBuilder
{ {
public ProjectServiceBuilder(string name) public ProjectServiceBuilder(string name)
: base(name) : base(name)
@ -14,8 +14,6 @@ namespace Microsoft.Tye
} }
public bool IsAspNet { get; set; } public bool IsAspNet { get; set; }
public int Replicas { get; set; } = 1;
public bool Build { get; set; } public bool Build { get; set; }
public string? Args { get; set; } public string? Args { get; set; }
@ -26,8 +24,6 @@ namespace Microsoft.Tye
// Data used for building Kubernetes manifests // Data used for building Kubernetes manifests
public KubernetesManifestInfo? ManifestInfo { get; set; } public KubernetesManifestInfo? ManifestInfo { get; set; }
public List<EnvironmentVariableBuilder> EnvironmentVariables { get; } = new List<EnvironmentVariableBuilder>();
// Used when running in a container locally. // Used when running in a container locally.
public List<VolumeBuilder> Volumes { get; } = new List<VolumeBuilder>(); public List<VolumeBuilder> Volumes { get; } = new List<VolumeBuilder>();

25
src/Microsoft.Tye.Extensions/Dapr/DaprExtension.cs

@ -77,8 +77,11 @@ namespace Microsoft.Tye.Extensions.Dapr
} }
// For local run, enumerate all projects, and add services for each dapr proxy. // For local run, enumerate all projects, and add services for each dapr proxy.
var projects = context.Application.Services.OfType<ProjectServiceBuilder>().ToList(); var projects = context.Application.Services.OfType<ProjectServiceBuilder>().Cast<LaunchedServiceBuilder>();
foreach (var project in projects) var executables = context.Application.Services.OfType<ExecutableServiceBuilder>().Cast<LaunchedServiceBuilder>();
var services = projects.Concat(executables).ToList();
foreach (var project in services)
{ {
// Dapr requires http. If this project isn't listening to HTTP then it's not daprized. // Dapr requires http. If this project isn't listening to HTTP then it's not daprized.
var httpBinding = project.Bindings.FirstOrDefault(b => b.Protocol == "http"); var httpBinding = project.Bindings.FirstOrDefault(b => b.Protocol == "http");
@ -107,7 +110,7 @@ namespace Microsoft.Tye.Extensions.Dapr
// These environment variables are replaced with environment variables // These environment variables are replaced with environment variables
// defined for this service. // 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% --placement-host-address localhost:{daprPlacementPort}", Args = $"run --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-host-address localhost:{daprPlacementPort}",
}; };
// When running locally `-config` specifies a filename, not a configuration name. By convention // When running locally `-config` specifies a filename, not a configuration name. By convention
@ -117,7 +120,7 @@ namespace Microsoft.Tye.Extensions.Dapr
var configFile = Path.Combine(context.Application.Source.DirectoryName!, "components", $"{daprConfig}.yaml"); var configFile = Path.Combine(context.Application.Source.DirectoryName!, "components", $"{daprConfig}.yaml");
if (File.Exists(configFile)) if (File.Exists(configFile))
{ {
proxy.Args += $" -config \"{configFile}\""; proxy.Args += $" --config \"{configFile}\"";
} }
else else
{ {
@ -127,12 +130,12 @@ namespace Microsoft.Tye.Extensions.Dapr
if (config.Data.TryGetValue("log-level", out obj) && obj?.ToString() is string logLevel) if (config.Data.TryGetValue("log-level", out obj) && obj?.ToString() is string logLevel)
{ {
proxy.Args += $" -log-level {logLevel}"; proxy.Args += $" --log-level {logLevel}";
} }
if (config.Data.TryGetValue("components-path", out obj) && obj?.ToString() is string componentsPath) if (config.Data.TryGetValue("components-path", out obj) && obj?.ToString() is string componentsPath)
{ {
proxy.Args += $" -components-path {componentsPath}"; proxy.Args += $" --components-path {componentsPath}";
} }
// Add dapr proxy as a service available to everyone. // Add dapr proxy as a service available to everyone.
proxy.Dependencies.UnionWith(context.Application.Services.Select(s => s.Name)); proxy.Dependencies.UnionWith(context.Application.Services.Select(s => s.Name));
@ -268,10 +271,10 @@ namespace Microsoft.Tye.Extensions.Dapr
private string GetDaprExecutablePath() private string GetDaprExecutablePath()
{ {
// Starting with dapr version 11, dapr is installed in user profile/home.
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{ {
var windowsPath = Environment.ExpandEnvironmentVariables("%USERPROFILE%/.dapr/bin/daprd.exe"); // The Dapr Windows installation script defaults to "C:\dapr".
var windowsPath = Environment.ExpandEnvironmentVariables("C:/dapr/dapr.exe");
if (File.Exists(windowsPath)) if (File.Exists(windowsPath))
{ {
return windowsPath; return windowsPath;
@ -279,15 +282,15 @@ namespace Microsoft.Tye.Extensions.Dapr
} }
else else
{ {
var nixpath = Environment.ExpandEnvironmentVariables("%HOME%/.dapr/bin/daprd"); var nixpath = Environment.ExpandEnvironmentVariables("/usr/local/bin/dapr");
if (File.Exists(nixpath)) if (File.Exists(nixpath))
{ {
return nixpath; return nixpath;
} }
} }
// Older version of dapr don't have dapr in the bin directory, but it is usually on the path. // Dapr is usually on the path.
return "daprd"; return "dapr";
} }
} }
} }

Loading…
Cancel
Save