public static IngressPath ToModel(this V1HTTPIngressPath path) { return(new IngressPath { Path = path.Path, PathType = ParsePathType(path.PathType), BackendServiceName = path.Backend?.Service.Name, BackendServicePort = path.Backend?.Service.Port.Number?.ToString("D", CultureInfo.InvariantCulture), }); }
private static string FixupPathMatch(V1HTTPIngressPath path) { var pathMatch = path.Path; // Prefix match is the default for implementation specific. if (string.Equals(path.PathType, "Prefix", StringComparison.OrdinalIgnoreCase) || string.Equals(path.PathType, "ImplementationSpecific", StringComparison.OrdinalIgnoreCase)) { if (!pathMatch.EndsWith("/", StringComparison.Ordinal)) { pathMatch += "/"; } // remember for prefix matches, /foo/ works for either /foo or /foo/ pathMatch += "{**catch-all}"; } return(pathMatch); }
private static void HandleIngressRulePath(YarpIngressContext context, List <Endpoints> endpoints, IList <V1EndpointSubset> defaultSubsets, V1IngressRule rule, V1HTTPIngressPath path) { var backend = path.Backend; var service = backend.Service; var subsets = defaultSubsets; var clusters = context.ClusterTransfers; var routes = context.Routes; if (!string.IsNullOrEmpty(service?.Name)) { subsets = endpoints.SingleOrDefault(x => x.Name == service?.Name).Subsets; } // make sure cluster is present foreach (var subset in subsets ?? Enumerable.Empty <V1EndpointSubset>()) { foreach (var port in subset.Ports ?? Enumerable.Empty <V1EndpointPort>()) { var key = $"{service?.Name}:{port.Port}"; if (!clusters.ContainsKey(key)) { clusters.Add(key, new ClusterTrasfer()); } var cluster = clusters[key]; cluster.ClusterId = key; foreach (var address in subset.Addresses ?? Enumerable.Empty <V1EndpointAddress>()) { var ip = address.Ip; if (!MatchesPort(port, service?.Port)) { continue; } var protocol = context.Options.Https ? "https" : "http"; var uri = $"{protocol}://{address.Ip}:{port.Port}"; cluster.Destinations[uri] = new Destination() { Address = uri }; var pathMatch = FixupPathMatch(path); var host = rule.Host; routes.Add(new ProxyRoute() { Match = new ProxyMatch() { Hosts = host != null ? new[] { host } : Array.Empty <string>(), Path = pathMatch }, ClusterId = cluster.ClusterId, RouteId = path.Path }); } } } }
private static void HandleIngressRulePath(YarpIngressContext ingressContext, V1ServicePort servicePort, List <Endpoints> endpoints, IList <V1EndpointSubset> defaultSubsets, V1IngressRule rule, V1HTTPIngressPath path, YarpConfigContext configContext) { var backend = path.Backend; var ingressServiceBackend = backend.Service; var subsets = defaultSubsets; var clusters = configContext.ClusterTransfers; var routes = configContext.Routes; if (!string.IsNullOrEmpty(ingressServiceBackend?.Name)) { subsets = endpoints.SingleOrDefault(x => x.Name == ingressServiceBackend?.Name).Subsets; } // Each ingress rule path can only be for one service var key = ingressServiceBackend.Port.Number.HasValue ? $"{ingressServiceBackend?.Name}:{ingressServiceBackend?.Port.Number}" : $"{ingressServiceBackend?.Name}:{ingressServiceBackend?.Port.Name}"; if (!clusters.ContainsKey(key)) { clusters.Add(key, new ClusterTransfer()); } var cluster = clusters[key]; cluster.ClusterId = key; // make sure cluster is present foreach (var subset in subsets ?? Enumerable.Empty <V1EndpointSubset>()) { foreach (var port in subset.Ports ?? Enumerable.Empty <V1EndpointPort>()) { if (!MatchesPort(port, servicePort.TargetPort)) { continue; } var pathMatch = FixupPathMatch(path); var host = rule.Host; routes.Add(new RouteConfig() { Match = new RouteMatch() { Hosts = host != null ? new[] { host } : Array.Empty <string>(), Path = pathMatch }, ClusterId = cluster.ClusterId, RouteId = $"{ingressContext.Ingress.Metadata.Name}:{path.Path}" }); // Add destination for every endpoint address foreach (var address in subset.Addresses ?? Enumerable.Empty <V1EndpointAddress>()) { var protocol = ingressContext.Options.Https ? "https" : "http"; var uri = $"{protocol}://{address.Ip}:{port.Port}"; cluster.Destinations[uri] = new DestinationConfig() { Address = uri }; } } } }