Browse Source

Clean up port injection (#165)

* Clean up port injection:
- We inject ASPNETCORE_URLS, HTTPS_PORT and a PORTS env variable for binding.

* Use internal ports 80 and 443 when auto assigning ports

* Set the PORT env variable during deployment as well.
davidfowl/dependencies
David Fowler 7 years ago
committed by GitHub
parent
commit
2da2d998d5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      src/Microsoft.Tye.Core/CombineStep.cs
  2. 17
      src/Microsoft.Tye.Hosting/DockerRunner.cs
  3. 14
      src/Microsoft.Tye.Hosting/ProcessRunner.cs
  4. 9
      src/Microsoft.Tye.Hosting/ProxyService.cs
  5. 19
      src/Microsoft.Tye.Hosting/TyeDashboardApi.cs

1
src/Microsoft.Tye.Core/CombineStep.cs

@ -28,6 +28,7 @@ namespace Microsoft.Tye
var port = binding.Port ?? 80;
var urls = $"http://*{(port == 80 ? "" : (":" + port.ToString()))}";
service.Service.Environment.Add("ASPNETCORE_URLS", urls);
service.Service.Environment.Add("PORT", port);
break;
}
}

17
src/Microsoft.Tye.Hosting/DockerRunner.cs

@ -107,15 +107,26 @@ namespace Microsoft.Tye.Hosting
{
status.Ports = ports.Select(p => p.Port);
// These ports should also be passed in not assuming ASP.NET Core
environment["ASPNETCORE_URLS"] = string.Join(";", ports.Select(p => $"{p.Protocol ?? "http"}://*:{p.InternalPort ?? p.Port}"));
// These are the ports that the application should use for binding
// 1. Tell the docker container what port to bind to
portString = string.Join(" ", ports.Select(p => $"-p {p.Port}:{p.InternalPort ?? p.Port}"));
// 2. Configure ASP.NET Core to bind to those same ports
environment["ASPNETCORE_URLS"] = string.Join(";", ports.Select(p => $"{p.Protocol ?? "http"}://*:{p.InternalPort ?? p.Port}"));
// Set the HTTPS port for the redirect middleware
foreach (var p in ports)
{
environment[$"{p.Protocol?.ToUpper() ?? "HTTP"}_PORT"] = p.BindingPort.ToString();
if (string.Equals(p.Protocol, "https", StringComparison.OrdinalIgnoreCase))
{
// We need to set the redirect URL to the exposed port so the redirect works cleanly
environment["HTTPS_PORT"] = p.BindingPort.ToString();
}
}
// 3. For non-ASP.NET Core apps, pass the same information in the PORT env variable as a semicolon separated list.
environment["PORT"] = string.Join(";", ports.Select(p => $"{p.InternalPort ?? p.Port}"));
}
application.PopulateEnvironment(service, (key, value) => environment[key] = value, "host.docker.internal");

14
src/Microsoft.Tye.Hosting/ProcessRunner.cs

@ -153,13 +153,23 @@ namespace Microsoft.Tye.Hosting
if (hasPorts)
{
// These ports should also be passed in not assuming ASP.NET Core
// These are the ports that the application should use for binding
// 1. Configure ASP.NET Core to bind to those same ports
environment["ASPNETCORE_URLS"] = string.Join(";", ports.Select(p => $"{p.Protocol ?? "http"}://localhost:{p.Port}"));
// Set the HTTPS port for the redirect middleware
foreach (var p in ports)
{
environment[$"{p.Protocol?.ToUpper() ?? "HTTP"}_PORT"] = p.BindingPort.ToString();
if (string.Equals(p.Protocol, "https", StringComparison.OrdinalIgnoreCase))
{
// We need to set the redirect URL to the exposed port so the redirect works cleanly
environment["HTTPS_PORT"] = p.BindingPort.ToString();
}
}
// 3. For non-ASP.NET Core apps, pass the same information in the PORT env variable as a semicolon separated list.
environment["PORT"] = string.Join(";", ports.Select(p => $"{p.Port}"));
}
while (!processInfo.StoppedTokenSource.IsCancellationRequested)

9
src/Microsoft.Tye.Hosting/ProxyService.cs

@ -69,6 +69,15 @@ namespace Microsoft.Tye.Hosting
binding.Port = GetNextPort();
}
if (binding.Protocol == "http" || binding.Protocol == null)
{
binding.InternalPort = 80;
}
else if (binding.Protocol == "https")
{
binding.InternalPort = 443;
}
if (service.Description.Replicas == 1)
{
// No need to proxy

19
src/Microsoft.Tye.Hosting/TyeDashboardApi.cs

@ -164,11 +164,26 @@ namespace Microsoft.Tye.Hosting
var replicateDictionary = new Dictionary<string, V1ReplicaStatus>();
foreach (var replica in service.Replicas)
{
replicateDictionary[replica.Key] = new V1ReplicaStatus()
var replicaStatus = new V1ReplicaStatus()
{
Name = replica.Value.Name,
Ports = replica.Value.Ports
Ports = replica.Value.Ports,
};
replicateDictionary[replica.Key] = replicaStatus;
if (replica.Value is ProcessStatus processStatus)
{
replicaStatus.Pid = processStatus.Pid;
replicaStatus.ExitCode = processStatus.ExitCode;
replicaStatus.Environment = processStatus.Environment;
}
else if (replica.Value is DockerStatus dockerStatus)
{
replicaStatus.DockerCommand = dockerStatus.DockerCommand;
replicaStatus.DockerLogsPid = dockerStatus.DockerLogsPid;
replicaStatus.ContainerId = dockerStatus.ContainerId;
}
}
var v1Status = new V1ServiceStatus()

Loading…
Cancel
Save