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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with
53 additions and
7 deletions
src/Microsoft.Tye.Core/CombineStep.cs
src/Microsoft.Tye.Hosting/DockerRunner.cs
src/Microsoft.Tye.Hosting/ProcessRunner.cs
src/Microsoft.Tye.Hosting/ProxyService.cs
src/Microsoft.Tye.Hosting/TyeDashboardApi.cs
@ -28,6 +28,7 @@ namespace Microsoft.Tye
var port = binding . Port ? ? 8 0 ;
var urls = $"http://*{(port == 80 ? "" : (" : " + port.ToString()))}" ;
service . Service . Environment . Add ( "ASPNETCORE_URLS" , urls ) ;
service . Service . Environment . Add ( "PORT" , port ) ;
break ;
}
}
@ -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" ) ;
@ -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 )
@ -69,6 +69,15 @@ namespace Microsoft.Tye.Hosting
binding . Port = GetNextPort ( ) ;
}
if ( binding . Protocol = = "http" | | binding . Protocol = = null )
{
binding . InternalPort = 8 0 ;
}
else if ( binding . Protocol = = "https" )
{
binding . InternalPort = 4 4 3 ;
}
if ( service . Description . Replicas = = 1 )
{
// No need to proxy
@ -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 ( )