forked from dotnet/yarp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYarpParser.cs
More file actions
337 lines (304 loc) · 14.2 KB
/
YarpParser.cs
File metadata and controls
337 lines (304 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using k8s.Models;
using YamlDotNet.Serialization;
using Yarp.ReverseProxy.Configuration;
using Yarp.Kubernetes.Controller.Caching;
using System.Runtime.InteropServices;
namespace Yarp.Kubernetes.Controller.Converters;
internal static class YarpParser
{
private const string ExternalNameServiceType = "ExternalName";
private static readonly Deserializer YamlDeserializer = new();
internal static void ConvertFromKubernetesIngress(YarpIngressContext ingressContext, YarpConfigContext configContext)
{
var spec = ingressContext.Ingress.Spec;
var defaultBackend = spec?.DefaultBackend;
var defaultService = defaultBackend?.Service;
IList<V1EndpointSubset> defaultSubsets = default;
if (!string.IsNullOrEmpty(defaultService?.Name))
{
defaultSubsets = ingressContext.Endpoints.SingleOrDefault(x => x.Name == defaultService?.Name).Subsets;
}
// cluster can contain multiple replicas for each destination, need to know the lookup base don endpoints
var options = HandleAnnotations(ingressContext, ingressContext.Ingress.Metadata);
foreach (var rule in spec?.Rules ?? Enumerable.Empty<V1IngressRule>())
{
HandleIngressRule(ingressContext, ingressContext.Endpoints, defaultSubsets, rule, configContext);
}
}
private static void HandleIngressRule(YarpIngressContext ingressContext, List<Endpoints> endpoints, IList<V1EndpointSubset> defaultSubsets, V1IngressRule rule, YarpConfigContext configContext)
{
var http = rule.Http;
foreach (var path in http.Paths ?? Enumerable.Empty<V1HTTPIngressPath>())
{
var service = ingressContext.Services.SingleOrDefault(s => s.Metadata.Name == path.Backend.Service.Name);
if (service.Spec != null)
{
if (string.Equals(service.Spec.Type, ExternalNameServiceType, StringComparison.OrdinalIgnoreCase))
{
HandleExternalIngressRulePath(ingressContext, service.Spec.ExternalName, rule, path, configContext);
}
else
{
var servicePort = service.Spec.Ports.SingleOrDefault(p => MatchesPort(p, path.Backend.Service.Port));
if (servicePort != null)
{
HandleIngressRulePath(ingressContext, servicePort, endpoints, defaultSubsets, rule, path, configContext);
}
}
}
}
}
private static void HandleExternalIngressRulePath(YarpIngressContext ingressContext, string externalName, V1IngressRule rule, V1HTTPIngressPath path, YarpConfigContext configContext)
{
var backend = path.Backend;
var ingressServiceBackend = backend.Service;
var routes = configContext.Routes;
var cluster = GetOrAddCluster(ingressContext, configContext, ingressServiceBackend);
var pathMatch = FixupPathMatch(path);
var host = rule.Host;
routes.Add(CreateRoute(ingressContext, path, cluster, pathMatch, host));
AddDestination(cluster, ingressContext, externalName, ingressServiceBackend.Port.Number);
}
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 routes = configContext.Routes;
if (!string.IsNullOrEmpty(ingressServiceBackend?.Name))
{
subsets = endpoints.SingleOrDefault(x => x.Name == ingressServiceBackend?.Name).Subsets;
}
var cluster = GetOrAddCluster(ingressContext, configContext, ingressServiceBackend);
// make sure cluster is present
foreach (var subset in subsets ?? Enumerable.Empty<V1EndpointSubset>())
{
foreach (var port in subset.Ports ?? Enumerable.Empty<Corev1EndpointPort>())
{
if (!MatchesPort(port, servicePort))
{
continue;
}
var pathMatch = FixupPathMatch(path);
var host = rule.Host;
routes.Add(CreateRoute(ingressContext, path, cluster, pathMatch, host));
// Add destination for every endpoint address
foreach (var address in subset.Addresses ?? Enumerable.Empty<V1EndpointAddress>())
{
AddDestination(cluster, ingressContext, address.Ip, port.Port);
}
}
}
}
private static void AddDestination(ClusterTransfer cluster, YarpIngressContext ingressContext, string host, int? port)
{
var protocol = ingressContext.Options.Https ? "https" : "http";
var uri = $"{protocol}://{host}";
if (port.HasValue)
{
uri += $":{port}";
}
cluster.Destinations[uri] = new DestinationConfig()
{
Address = uri
};
}
private static RouteConfig CreateRoute(YarpIngressContext ingressContext, V1HTTPIngressPath path, ClusterTransfer cluster, string pathMatch, string host)
{
return new RouteConfig()
{
Match = new RouteMatch()
{
Methods = ingressContext.Options.RouteMethods,
Hosts = host is not null ? new[] { host } : Array.Empty<string>(),
Path = pathMatch,
Headers = ingressContext.Options.RouteHeaders,
QueryParameters = ingressContext.Options.RouteQueryParameters
},
ClusterId = cluster.ClusterId,
RouteId = $"{ingressContext.Ingress.Metadata.Name}.{ingressContext.Ingress.Metadata.NamespaceProperty}:{host}{path.Path}",
Transforms = ingressContext.Options.Transforms,
AuthorizationPolicy = ingressContext.Options.AuthorizationPolicy,
RateLimiterPolicy = ingressContext.Options.RateLimiterPolicy,
OutputCachePolicy = ingressContext.Options.OutputCachePolicy,
Timeout = ingressContext.Options.Timeout,
TimeoutPolicy = ingressContext.Options.TimeoutPolicy,
CorsPolicy = ingressContext.Options.CorsPolicy,
Metadata = ingressContext.Options.RouteMetadata,
Order = ingressContext.Options.RouteOrder,
};
}
private static ClusterTransfer GetOrAddCluster(YarpIngressContext ingressContext, YarpConfigContext configContext, V1IngressServiceBackend ingressServiceBackend)
{
var clusters = configContext.ClusterTransfers;
// Each ingress rule path can only be for one service
var key = UpstreamName(ingressContext.Ingress.Metadata.NamespaceProperty, ingressServiceBackend);
var cluster = CollectionsMarshal.GetValueRefOrAddDefault(clusters, key, out _) ??= new ClusterTransfer();
cluster.ClusterId = key;
cluster.LoadBalancingPolicy = ingressContext.Options.LoadBalancingPolicy;
cluster.SessionAffinity = ingressContext.Options.SessionAffinity;
cluster.HealthCheck = ingressContext.Options.HealthCheck;
cluster.HttpClientConfig = ingressContext.Options.HttpClientConfig;
return cluster;
}
private static string UpstreamName(string namespaceName, V1IngressServiceBackend ingressServiceBackend)
{
if (ingressServiceBackend is not null)
{
if (ingressServiceBackend.Port.Number.HasValue && ingressServiceBackend.Port.Number.Value > 0)
{
return $"{ingressServiceBackend.Name}.{namespaceName}:{ingressServiceBackend.Port.Number}";
}
if (!string.IsNullOrWhiteSpace(ingressServiceBackend.Port.Name))
{
return $"{ingressServiceBackend.Name}.{namespaceName}:{ingressServiceBackend.Port.Name}";
}
}
return $"{namespaceName}-INVALID";
}
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('/'))
{
pathMatch += "/";
}
// remember for prefix matches, /foo/ works for either /foo or /foo/
pathMatch += "{**catch-all}";
}
return pathMatch;
}
private static YarpIngressOptions HandleAnnotations(YarpIngressContext context, V1ObjectMeta metadata)
{
var options = context.Options;
var annotations = metadata.Annotations;
if (annotations is null)
{
return options;
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/backend-protocol", out var http))
{
options.Https = http.Equals("https", StringComparison.OrdinalIgnoreCase);
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/transforms", out var transforms))
{
options.Transforms = YamlDeserializer.Deserialize<List<Dictionary<string, string>>>(transforms);
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/authorization-policy", out var authorizationPolicy))
{
options.AuthorizationPolicy = authorizationPolicy;
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/rate-limiter-policy", out var rateLimiterPolicy))
{
options.RateLimiterPolicy = rateLimiterPolicy;
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/output-cache-policy", out var outputCachePolicy))
{
options.OutputCachePolicy = outputCachePolicy;
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/timeout", out var timeout))
{
options.Timeout = TimeSpan.Parse(timeout, CultureInfo.InvariantCulture);
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/timeout-policy", out var timeoutPolicy))
{
options.TimeoutPolicy = timeoutPolicy;
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/cors-policy", out var corsPolicy))
{
options.CorsPolicy = corsPolicy;
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/session-affinity", out var sessionAffinity))
{
options.SessionAffinity = YamlDeserializer.Deserialize<SessionAffinityConfig>(sessionAffinity);
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/load-balancing", out var loadBalancing))
{
options.LoadBalancingPolicy = loadBalancing;
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/http-client", out var httpClientConfig))
{
options.HttpClientConfig = YamlDeserializer.Deserialize<HttpClientConfig>(httpClientConfig);
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/health-check", out var healthCheck))
{
options.HealthCheck = YamlDeserializer.Deserialize<HealthCheckConfig>(healthCheck);
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/route-metadata", out var routeMetadata))
{
options.RouteMetadata = YamlDeserializer.Deserialize<Dictionary<string, string>>(routeMetadata);
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/route-headers", out var routeHeaders))
{
// YamlDeserializer does not support IReadOnlyList<string> in RouteHeader for now, so we use RouteHeaderWrapper to solve this problem.
options.RouteHeaders = YamlDeserializer.Deserialize<List<RouteHeaderWrapper>>(routeHeaders).Select(p => p.ToRouteHeader()).ToList();
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/route-queryparameters", out var routeQueryParameters))
{
// YamlDeserializer does not support IReadOnlyList<string> in RouteParameters for now, so we use RouterQueryParameterWrapper to solve this problem.
options.RouteQueryParameters = YamlDeserializer.Deserialize<List<RouteQueryParameterWrapper>>(routeQueryParameters).Select(p => p.ToRouteQueryParameter()).ToList();
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/route-order", out var routeOrder))
{
options.RouteOrder = int.Parse(routeOrder, CultureInfo.InvariantCulture);
}
if (annotations.TryGetValue("yarp.ingress.kubernetes.io/route-methods", out var routeMethods))
{
options.RouteMethods = YamlDeserializer.Deserialize<List<string>>(routeMethods);
}
// metadata to support:
// rewrite target
// auth
// http or https
// default backend
// CORS
// GRPC
// HTTP2
// Conneciton limits
// rate limits
// backend health checks.
return options;
}
private static bool MatchesPort(Corev1EndpointPort port1, V1ServicePort port2)
{
if (port1 is null || port2?.TargetPort is null)
{
return false;
}
if (int.TryParse(port2.TargetPort, out var port2Number) && port2Number == port1.Port)
{
return true;
}
if (string.Equals(port2.Name, port1.Name, StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
}
private static bool MatchesPort(V1ServicePort port1, V1ServiceBackendPort port2)
{
if (port1 is null || port2 is null)
{
return false;
}
if (port2.Number is not null && port2.Number == port1.Port)
{
return true;
}
if (port2.Name is not null && string.Equals(port2.Name, port1.Name, StringComparison.Ordinal))
{
return true;
}
return false;
}
}