From df18a1d31bac543de05c498b1f2054031157a3c2 Mon Sep 17 00:00:00 2001 From: Justin Kotalik Date: Tue, 3 Nov 2020 14:25:28 -0800 Subject: [PATCH] Adds preservePath for ingress (#747) --- src/Microsoft.Tye.Core/ApplicationFactory.cs | 1 + src/Microsoft.Tye.Core/ConfigModel/ConfigIngressRule.cs | 1 + src/Microsoft.Tye.Core/IngressRuleBuilder.cs | 2 ++ src/Microsoft.Tye.Core/KubernetesManifestGenerator.cs | 2 +- .../Serialization/ConfigIngressParser.cs | 7 +++++++ src/Microsoft.Tye.Hosting/DockerRunner.cs | 4 +++- src/Microsoft.Tye.Hosting/HttpProxyService.cs | 3 +-- src/Microsoft.Tye.Hosting/Model/IngressRule.cs | 4 +++- src/schema/tye-schema.json | 4 ++++ src/tye/ApplicationBuilderExtensions.cs | 2 +- 10 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.Tye.Core/ApplicationFactory.cs b/src/Microsoft.Tye.Core/ApplicationFactory.cs index 9f506378..d47fe28a 100644 --- a/src/Microsoft.Tye.Core/ApplicationFactory.cs +++ b/src/Microsoft.Tye.Core/ApplicationFactory.cs @@ -465,6 +465,7 @@ namespace Microsoft.Tye { Host = configRule.Host, Path = configRule.Path, + PreservePath = configRule.PreservePath, Service = configRule.Service!, // validated elsewhere }; ingress.Rules.Add(rule); diff --git a/src/Microsoft.Tye.Core/ConfigModel/ConfigIngressRule.cs b/src/Microsoft.Tye.Core/ConfigModel/ConfigIngressRule.cs index 521cce32..701e9ab0 100644 --- a/src/Microsoft.Tye.Core/ConfigModel/ConfigIngressRule.cs +++ b/src/Microsoft.Tye.Core/ConfigModel/ConfigIngressRule.cs @@ -10,6 +10,7 @@ namespace Microsoft.Tye.ConfigModel { public string? Path { get; set; } public string? Host { get; set; } + public bool PreservePath { get; set; } [Required] public string? Service { get; set; } diff --git a/src/Microsoft.Tye.Core/IngressRuleBuilder.cs b/src/Microsoft.Tye.Core/IngressRuleBuilder.cs index 5b6a11e7..97b883b7 100644 --- a/src/Microsoft.Tye.Core/IngressRuleBuilder.cs +++ b/src/Microsoft.Tye.Core/IngressRuleBuilder.cs @@ -8,6 +8,8 @@ namespace Microsoft.Tye { public string? Path { get; set; } public string? Host { get; set; } + public bool PreservePath { get; set; } + public string Service { get; set; } = default!; } } diff --git a/src/Microsoft.Tye.Core/KubernetesManifestGenerator.cs b/src/Microsoft.Tye.Core/KubernetesManifestGenerator.cs index d3cfeb25..034e14ef 100644 --- a/src/Microsoft.Tye.Core/KubernetesManifestGenerator.cs +++ b/src/Microsoft.Tye.Core/KubernetesManifestGenerator.cs @@ -92,7 +92,7 @@ namespace Microsoft.Tye // // Therefore our rewrite-target is set to $2 - we want to make sure we have // two capture groups. - if (string.IsNullOrEmpty(ingressRule.Path) || ingressRule.Path == "/") + if (string.IsNullOrEmpty(ingressRule.Path) || ingressRule.Path == "/" || ingressRule.PreservePath) { path.Add("path", "/()(.*)"); // () is an empty capture group. } diff --git a/src/Microsoft.Tye.Core/Serialization/ConfigIngressParser.cs b/src/Microsoft.Tye.Core/Serialization/ConfigIngressParser.cs index 1629c3ff..bea20051 100644 --- a/src/Microsoft.Tye.Core/Serialization/ConfigIngressParser.cs +++ b/src/Microsoft.Tye.Core/Serialization/ConfigIngressParser.cs @@ -98,6 +98,13 @@ namespace Tye.Serialization case "path": rule.Path = YamlParser.GetScalarValue(key, child.Value); break; + case "preservePath": + if (!bool.TryParse(YamlParser.GetScalarValue(key, child.Value), out var preservePath)) + { + throw new TyeYamlException(child.Value.Start, CoreStrings.FormatMustBeABoolean(key)); + } + rule.PreservePath = preservePath; + break; case "service": rule.Service = YamlParser.GetScalarValue(key, child.Value).ToLowerInvariant(); break; diff --git a/src/Microsoft.Tye.Hosting/DockerRunner.cs b/src/Microsoft.Tye.Hosting/DockerRunner.cs index 6342b66d..ef4bc378 100644 --- a/src/Microsoft.Tye.Hosting/DockerRunner.cs +++ b/src/Microsoft.Tye.Hosting/DockerRunner.cs @@ -53,7 +53,9 @@ namespace Microsoft.Tye.Hosting var proxies = new List(); foreach (var service in application.Services.Values) { - if (service.Description.RunInfo is DockerRunInfo || service.Description.Bindings.Count == 0) + if (service.Description.RunInfo is DockerRunInfo || + service.Description.RunInfo is IngressRunInfo || + service.Description.Bindings.Count == 0) { continue; } diff --git a/src/Microsoft.Tye.Hosting/HttpProxyService.cs b/src/Microsoft.Tye.Hosting/HttpProxyService.cs index f5b42c43..98e37302 100644 --- a/src/Microsoft.Tye.Hosting/HttpProxyService.cs +++ b/src/Microsoft.Tye.Hosting/HttpProxyService.cs @@ -151,10 +151,9 @@ namespace Microsoft.Tye.Hosting await context.Response.WriteAsync("Bad gateway"); return; } - var uri = new UriBuilder(uris[next].Uri) { - Path = (string)context.Request.RouteValues["path"]!, + Path = rule.PreservePath ? $"{context.Request.Path}{context.Request.RouteValues["path"]}" : (string)context.Request.RouteValues["path"] ?? "/", Query = context.Request.QueryString.Value }; diff --git a/src/Microsoft.Tye.Hosting/Model/IngressRule.cs b/src/Microsoft.Tye.Hosting/Model/IngressRule.cs index 0a4bfbc5..8d88818a 100644 --- a/src/Microsoft.Tye.Hosting/Model/IngressRule.cs +++ b/src/Microsoft.Tye.Hosting/Model/IngressRule.cs @@ -11,15 +11,17 @@ namespace Microsoft.Tye.Hosting.Model { public class IngressRule { - public IngressRule(string? host, string? path, string service) + public IngressRule(string? host, string? path, string service, bool preservePath) { Host = host; Path = path; + PreservePath = preservePath; Service = service; } public string? Host { get; } public string? Path { get; } + public bool PreservePath { get; } public string Service { get; } } } diff --git a/src/schema/tye-schema.json b/src/schema/tye-schema.json index 0d9a3b31..f971871a 100644 --- a/src/schema/tye-schema.json +++ b/src/schema/tye-schema.json @@ -447,6 +447,10 @@ "protocol": { "description": "The protocol used by the binding", "type": "string" + }, + "preservePath": { + "description": "Whether to keep the path that was originally present or not.", + "type": "boolean" } } }, diff --git a/src/tye/ApplicationBuilderExtensions.cs b/src/tye/ApplicationBuilderExtensions.cs index 2e79c9dc..815accec 100644 --- a/src/tye/ApplicationBuilderExtensions.cs +++ b/src/tye/ApplicationBuilderExtensions.cs @@ -190,7 +190,7 @@ namespace Microsoft.Tye foreach (var rule in ingress.Rules) { - rules.Add(new IngressRule(rule.Host, rule.Path, rule.Service!)); + rules.Add(new IngressRule(rule.Host, rule.Path, rule.Service!, rule.PreservePath)); } var runInfo = new IngressRunInfo(rules);