Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Commit

Permalink
#1158 Feature/dashboard port in tye yaml (#1163)
Browse files Browse the repository at this point in the history
* Set up tests for new dashboardPort functionality

* Add in new dashboardPort values to test data

* Map from tye.yaml configuration file into Application model

* Core change: apply DashboardPort from configuration (if cli --port argument is not specified)

* Add dashboardPort documentation to the tye schema page

* PR feedback: requested YAML (JSON) schema update
  • Loading branch information
daniel-simpson authored Sep 30, 2021
1 parent 5e7e07f commit 2858fce
Show file tree
Hide file tree
Showing 27 changed files with 442 additions and 7 deletions.
8 changes: 8 additions & 0 deletions docs/reference/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ Allows configuring the Docker network used for `tye run`.

If a network is configured, then all services running in containers will connect to the specified network. Otherwise a Docker network will be created with a generated name, and used to connect all containers.

#### `dashboardPort` (int)

Allows configuring the dashboard port used for `tye run`.

If a `--port` is provided via the CLI, it will be used instead.

If no `--port` argument or `dashboardPort` value is specified, Tye will use the default port (8000), or a random port if the default port is in use.

#### `ingress` (`Ingress[]`)

Specifies the list of ingresses.
Expand Down
5 changes: 4 additions & 1 deletion src/Microsoft.Tye.Core/ApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ namespace Microsoft.Tye
{
public sealed class ApplicationBuilder
{
public ApplicationBuilder(FileInfo source, string name, ContainerEngine containerEngine)
public ApplicationBuilder(FileInfo source, string name, ContainerEngine containerEngine, int? dashboardPort)
{
Source = source;
Name = name;
ContainerEngine = containerEngine;
DashboardPort = dashboardPort;
}

public FileInfo Source { get; set; }

public string Name { get; set; }

public int? DashboardPort { get; set; }

public string? Namespace { get; set; }

public ContainerRegistry? Registry { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Tye.Core/ApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static async Task<ApplicationBuilder> CreateAsync(OutputContext output, F
var rootConfig = ConfigFactory.FromFile(source);
rootConfig.Validate();

var root = new ApplicationBuilder(source, rootConfig.Name!, new ContainerEngine(rootConfig.ContainerEngineType))
var root = new ApplicationBuilder(source, rootConfig.Name!, new ContainerEngine(rootConfig.ContainerEngineType), rootConfig.DashboardPort)
{
Namespace = rootConfig.Namespace
};
Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.Tye.Core/ConfigModel/ConfigApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class ConfigApplication

public string? Name { get; set; }

public int? DashboardPort { get; set; }

public string? Namespace { get; set; }

public string? Registry { get; set; }
Expand Down
10 changes: 10 additions & 0 deletions src/Microsoft.Tye.Core/Serialization/ConfigApplicationParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ public static void HandleConfigApplication(YamlMappingNode yamlMappingNode, Conf
throw new TyeYamlException($"Unknown container engine: \"{engine}\"");
}
break;
case "dashboardPort":
if (int.TryParse(YamlParser.GetScalarValue(key, child.Value), out var dashboardPort))
{
app.DashboardPort = dashboardPort;
}
else
{
throw new TyeYamlException(child.Key.Start, CoreStrings.FormatMustBeAnInteger(key));
}
break;
case "ingress":
YamlParser.ThrowIfNotYamlSequence(key, child.Value);
ConfigIngressParser.HandleIngress((child.Value as YamlSequenceNode)!, app.Ingress);
Expand Down
5 changes: 4 additions & 1 deletion src/Microsoft.Tye.Hosting/Model/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ namespace Microsoft.Tye.Hosting.Model
{
public class Application
{
public Application(string name, FileInfo source, Dictionary<string, Service> services, ContainerEngine containerEngine)
public Application(string name, FileInfo source, int? dashboardPort, Dictionary<string, Service> services, ContainerEngine containerEngine)
{
Name = name;
Source = source.FullName;
ContextDirectory = source.DirectoryName!;
Services = services;
ContainerEngine = containerEngine;
DashboardPort = dashboardPort;
}

public string Id { get; } = Guid.NewGuid().ToString();
Expand All @@ -31,6 +32,8 @@ public Application(string name, FileInfo source, Dictionary<string, Service> ser

public ContainerEngine ContainerEngine { get; set; }

public int? DashboardPort { get; set; }

public Dictionary<string, Service> Services { get; }

public Dictionary<object, object> Items { get; } = new Dictionary<object, object>();
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Tye.Hosting/TyeHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private IHost BuildWebApplication(Application application, HostOptions options,
})
.ConfigureWebHostDefaults(builder =>
{
var port = ComputePort(options.Port);
var port = ComputePort(options.Port ?? application.DashboardPort);
_computedPort = port;
builder.Configure(ConfigureApplication)
Expand Down
4 changes: 4 additions & 0 deletions src/schema/tye-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
"description": "The Docker network to use.",
"type": "string"
},
"dashboardPort": {
"description": "Configure the dashboard port used for `tye run`. Can be overridden using the `--port` CLI argument, and falls back to port 8000 if free, or a random port if 8000 is in use.",
"type": "integer"
},
"ingress": {
"description": "The application's ingresses.",
"type": "array",
Expand Down
2 changes: 1 addition & 1 deletion src/tye/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public static Application ToHostingApplication(this ApplicationBuilder applicati
services.Add(ingress.Name, new Service(description, ServiceSource.Host));
}

return new Application(application.Name, application.Source, services, application.ContainerEngine) { Network = application.Network };
return new Application(application.Name, application.Source, application.DashboardPort, services, application.ContainerEngine) { Network = application.Network };
}

public static Tye.Hosting.Model.EnvironmentVariable ToHostingEnvironmentVariable(this EnvironmentVariableBuilder builder)
Expand Down
7 changes: 7 additions & 0 deletions test/E2ETest/Microsoft.Tye.E2ETests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
<Content Include="testassets\**\*" CopyToOutputDirectory="PreserveNewest" />
<Compile Remove="testassets\**\*" />
<None Remove="testassets\generate\apps-with-ingress.1.18.yaml" />
<None Remove="testassets\projects\non-standard-dashboard-port\test-project\appsettings.Development.json" />
<None Remove="testassets\projects\non-standard-dashboard-port\test-project\appsettings.json" />
<None Remove="testassets\projects\non-standard-dashboard-port\test-project\Properties\launchSettings.json" />
<None Remove="testassets\projects\non-standard-dashboard-port\tye.yaml" />
<None Remove="testassets\projects\non-standard-dashboard-port-5.0\test-project\appsettings.Development.json" />
<None Remove="testassets\projects\non-standard-dashboard-port-5.0\test-project\appsettings.json" />
<None Remove="testassets\projects\non-standard-dashboard-port-5.0\tye.yaml" />
<Compile Include="..\..\src\shared\KubectlDetector.cs" Link="KubectlDetector.cs" />
</ItemGroup>

Expand Down
87 changes: 87 additions & 0 deletions test/E2ETest/TyeRunTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,93 @@ public async Task RunCliArgDoesNotOverrideYamlMultipleTargetFrameworksTest()
});
}

[ConditionalTheory]
[SkipIfDockerNotRunning]
[InlineData("non-standard-dashboard-port", "mcr.microsoft.com/dotnet/core/aspnet:3.1", 8005)]
[InlineData("non-standard-dashboard-port-5.0", "mcr.microsoft.com/dotnet/aspnet:5.0", 8006)]
public async Task RunUsesYamlDashboardPort(string projectName, string baseImage, int expectedDashboardPort)
{
using var projectDirectory = CopyTestProjectDirectory(projectName);

var projectFile = new FileInfo(Path.Combine(projectDirectory.DirectoryPath, "tye.yaml"));
var outputContext = new OutputContext(_sink, Verbosity.Debug);
var application = await ApplicationFactory.CreateAsync(outputContext, projectFile);

Assert.Equal(expectedDashboardPort, application.DashboardPort);

var handler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (a, b, c, d) => true,
AllowAutoRedirect = false
};

var client = new HttpClient(new RetryHandler(handler));

await RunHostingApplication(application, new HostOptions() { Docker = true, }, async (app, uri) =>
{
// Make sure the dashboard is running on the expected port
Assert.Equal(expectedDashboardPort, uri.Port);
// Make sure we're running containers
Assert.True(app.Services.All(s => s.Value.Description.RunInfo is DockerRunInfo));
// Ensure correct image used
var dockerRunInfo = app.Services.Single().Value.Description.RunInfo as DockerRunInfo;
Assert.Equal(baseImage, dockerRunInfo?.Image);
// Ensure app runs
var testProjectUri = await GetServiceUrl(client, uri, "test-project");
var response = await client.GetAsync(testProjectUri);
Assert.True(response.IsSuccessStatusCode);
});
}

[ConditionalTheory]
[SkipIfDockerNotRunning]
[InlineData("non-standard-dashboard-port", "mcr.microsoft.com/dotnet/core/aspnet:3.1", 8005)]
[InlineData("non-standard-dashboard-port-5.0", "mcr.microsoft.com/dotnet/aspnet:5.0", 8006)]
public async Task RunCliPortOverridesYamlDashboardPort(string projectName, string baseImage, int tyeYamlDashboardPort)
{
var cliDashboardPort = 8008;

using var projectDirectory = CopyTestProjectDirectory(projectName);

var projectFile = new FileInfo(Path.Combine(projectDirectory.DirectoryPath, "tye.yaml"));
var outputContext = new OutputContext(_sink, Verbosity.Debug);
var application = await ApplicationFactory.CreateAsync(outputContext, projectFile);

Assert.Equal(tyeYamlDashboardPort, application.DashboardPort);

var handler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (a, b, c, d) => true,
AllowAutoRedirect = false
};

var client = new HttpClient(new RetryHandler(handler));

await RunHostingApplication(application, new HostOptions() { Docker = true, Port = cliDashboardPort }, async (app, uri) =>
{
// Make sure the dashboard is running on the expected port passed from the CLI, not the value from tye.yaml
Assert.Equal(cliDashboardPort, uri.Port);
Assert.NotEqual(tyeYamlDashboardPort, uri.Port);
// Make sure we're running containers
Assert.True(app.Services.All(s => s.Value.Description.RunInfo is DockerRunInfo));
// Ensure correct image used
var dockerRunInfo = app.Services.Single().Value.Description.RunInfo as DockerRunInfo;
Assert.Equal(baseImage, dockerRunInfo?.Image);
// Ensure app runs
var testProjectUri = await GetServiceUrl(client, uri, "test-project");
var response = await client.GetAsync(testProjectUri);
Assert.True(response.IsSuccessStatusCode);
});
}

private async Task<string> GetServiceUrl(HttpClient client, Uri uri, string serviceName)
{
var serviceResult = await client.GetStringAsync($"{uri}api/v1/services/{serviceName}");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test-project", "test-project\test-project.csproj", "{7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Debug|x64.ActiveCfg = Debug|Any CPU
{7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Debug|x64.Build.0 = Debug|Any CPU
{7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Debug|x86.ActiveCfg = Debug|Any CPU
{7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Debug|x86.Build.0 = Debug|Any CPU
{7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Release|Any CPU.Build.0 = Release|Any CPU
{7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Release|x64.ActiveCfg = Release|Any CPU
{7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Release|x64.Build.0 = Release|Any CPU
{7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Release|x86.ActiveCfg = Release|Any CPU
{7D3606B2-7B8E-4ABB-BE0A-E0B18285D8F5}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace test_project
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace test_project
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp5.0</TargetFramework>
<RootNamespace>test_project</RootNamespace>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# tye application configuration file
# read all about it at https://github.com/dotnet/tye
name: non-standard-dashboard-port
dashboardPort: 8006
services:
- name: test-project
project: test-project/test-project.csproj
Loading

0 comments on commit 2858fce

Please sign in to comment.