-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
KubernetesServiceDiscoveryTests.cs
357 lines (322 loc) · 15.8 KB
/
KubernetesServiceDiscoveryTests.cs
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using KubeClient;
using KubeClient.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Newtonsoft.Json;
using Ocelot.AcceptanceTests.LoadBalancer;
using Ocelot.Configuration;
using Ocelot.Configuration.File;
using Ocelot.DependencyInjection;
using Ocelot.LoadBalancer.LoadBalancers;
using Ocelot.Logging;
using Ocelot.Provider.Kubernetes;
using Ocelot.Provider.Kubernetes.Interfaces;
using Ocelot.ServiceDiscovery.Providers;
using Ocelot.Values;
using System.Runtime.CompilerServices;
using System.Text;
namespace Ocelot.AcceptanceTests.ServiceDiscovery;
public sealed class KubernetesServiceDiscoveryTests : ConcurrentSteps, IDisposable
{
private readonly string _kubernetesUrl;
private readonly IKubeApiClient _clientFactory;
private readonly ServiceHandler _kubernetesHandler;
private string _receivedToken;
public KubernetesServiceDiscoveryTests()
{
_kubernetesUrl = DownstreamUrl(PortFinder.GetRandomPort());
var option = new KubeClientOptions
{
ApiEndPoint = new Uri(_kubernetesUrl),
AccessToken = "txpc696iUhbVoudg164r93CxDTrKRVWG",
AuthStrategy = KubeAuthStrategy.BearerToken,
AllowInsecure = true,
};
_clientFactory = KubeApiClient.Create(option);
_kubernetesHandler = new();
}
public override void Dispose()
{
_kubernetesHandler.Dispose();
base.Dispose();
}
[Fact]
public void ShouldReturnServicesFromK8s()
{
const string namespaces = nameof(KubernetesServiceDiscoveryTests);
const string serviceName = nameof(ShouldReturnServicesFromK8s);
var servicePort = PortFinder.GetRandomPort();
var downstreamUrl = LoopbackLocalhostUrl(servicePort);
var downstream = new Uri(downstreamUrl);
var subsetV1 = GivenSubsetAddress(downstream);
var endpoints = GivenEndpoints(subsetV1);
var route = GivenRouteWithServiceName(namespaces);
var configuration = GivenKubeConfiguration(namespaces, route);
var downstreamResponse = serviceName;
this.Given(x => GivenServiceInstanceIsRunning(downstreamUrl, downstreamResponse))
.And(x => x.GivenThereIsAFakeKubernetesProvider(endpoints, serviceName, namespaces))
.And(_ => GivenThereIsAConfiguration(configuration))
.And(_ => GivenOcelotIsRunningWithServices(WithKubernetes))
.When(_ => WhenIGetUrlOnTheApiGateway("/"))
.Then(_ => ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
.And(_ => ThenTheResponseBodyShouldBe($"1:{downstreamResponse}"))
.And(x => ThenAllServicesShouldHaveBeenCalledTimes(1))
.And(x => x.ThenTheTokenIs("Bearer txpc696iUhbVoudg164r93CxDTrKRVWG"))
.BDDfy();
}
[Theory]
[Trait("Feat", "1967")]
[InlineData("", HttpStatusCode.BadGateway)]
[InlineData("http", HttpStatusCode.OK)]
public void ShouldReturnServicesByPortNameAsDownstreamScheme(string downstreamScheme, HttpStatusCode statusCode)
{
const string serviceName = "example-web";
const string namespaces = "default";
var servicePort = PortFinder.GetRandomPort();
var downstreamUrl = LoopbackLocalhostUrl(servicePort);
var downstream = new Uri(downstreamUrl);
var subsetV1 = GivenSubsetAddress(downstream);
// Ports[0] -> port(https, 443)
// Ports[1] -> port(http, not 80)
subsetV1.Ports.Insert(0, new()
{
Name = "https", // This service instance is offline -> BadGateway
Port = 443,
});
var endpoints = GivenEndpoints(subsetV1);
var route = GivenRouteWithServiceName(namespaces);
route.DownstreamPathTemplate = "/{url}";
route.DownstreamScheme = downstreamScheme; // !!! Warning !!! Select port by name as scheme
route.UpstreamPathTemplate = "/api/example/{url}";
route.ServiceName = serviceName; // "example-web"
var configuration = GivenKubeConfiguration(namespaces, route);
this.Given(x => GivenServiceInstanceIsRunning(downstreamUrl, nameof(ShouldReturnServicesByPortNameAsDownstreamScheme)))
.And(x => x.GivenThereIsAFakeKubernetesProvider(endpoints, serviceName, namespaces))
.And(_ => GivenThereIsAConfiguration(configuration))
.And(_ => GivenOcelotIsRunningWithServices(WithKubernetes))
.When(_ => WhenIGetUrlOnTheApiGateway("/api/example/1"))
.Then(_ => ThenTheStatusCodeShouldBe(statusCode))
.And(_ => ThenTheResponseBodyShouldBe(downstreamScheme == "http"
? "1:" + nameof(ShouldReturnServicesByPortNameAsDownstreamScheme)
: string.Empty))
.And(x => ThenAllServicesShouldHaveBeenCalledTimes(downstreamScheme == "http" ? 1 : 0))
.And(x => x.ThenTheTokenIs("Bearer txpc696iUhbVoudg164r93CxDTrKRVWG"))
.BDDfy();
}
[Theory]
[Trait("Bug", "2110")]
[InlineData(1, 30)]
[InlineData(2, 50)]
[InlineData(3, 50)]
[InlineData(4, 50)]
[InlineData(5, 50)]
[InlineData(6, 99)]
[InlineData(7, 99)]
[InlineData(8, 99)]
[InlineData(9, 999)]
[InlineData(10, 999)]
public void ShouldHighlyLoadOnStableKubeProvider_WithRoundRobinLoadBalancing(int totalServices, int totalRequests)
{
const int ZeroGeneration = 0;
var (endpoints, servicePorts) = ArrangeHighLoadOnKubeProviderAndRoundRobinBalancer(totalServices);
GivenThereIsAFakeKubernetesProvider(endpoints); // stable, services will not be removed from the list
HighlyLoadOnKubeProviderAndRoundRobinBalancer(totalRequests, ZeroGeneration);
int bottom = totalRequests / totalServices,
top = totalRequests - (bottom * totalServices) + bottom;
ThenAllServicesCalledRealisticAmountOfTimes(bottom, top);
ThenServiceCountersShouldMatchLeasingCounters(_roundRobinAnalyzer, servicePorts, totalRequests);
}
[Theory]
[Trait("Bug", "2110")]
[InlineData(5, 50, 1)]
[InlineData(5, 50, 2)]
[InlineData(5, 50, 3)]
[InlineData(5, 50, 4)]
public void ShouldHighlyLoadOnUnstableKubeProvider_WithRoundRobinLoadBalancing(int totalServices, int totalRequests, int k8sGeneration)
{
int failPerThreads = (totalRequests / k8sGeneration) - 1; // k8sGeneration means number of offline services
var (endpoints, servicePorts) = ArrangeHighLoadOnKubeProviderAndRoundRobinBalancer(totalServices);
GivenThereIsAFakeKubernetesProvider(endpoints, false, k8sGeneration, failPerThreads); // false means unstable, k8sGeneration services will be removed from the list
HighlyLoadOnKubeProviderAndRoundRobinBalancer(totalRequests, k8sGeneration);
ThenAllServicesCalledOptimisticAmountOfTimes(_roundRobinAnalyzer); // with unstable checkings
ThenServiceCountersShouldMatchLeasingCounters(_roundRobinAnalyzer, servicePorts, totalRequests);
}
private (EndpointsV1 Endpoints, int[] ServicePorts) ArrangeHighLoadOnKubeProviderAndRoundRobinBalancer(
int totalServices,
[CallerMemberName] string serviceName = nameof(ArrangeHighLoadOnKubeProviderAndRoundRobinBalancer))
{
const string namespaces = nameof(KubernetesServiceDiscoveryTests);
var servicePorts = PortFinder.GetPorts(totalServices);
var downstreamUrls = servicePorts
.Select(port => LoopbackLocalhostUrl(port, Array.IndexOf(servicePorts, port)))
.ToArray(); // based on localhost aka loopback network interface
var downstreams = downstreamUrls.Select(url => new Uri(url))
.ToList();
var downstreamResponses = downstreams
.Select(ds => $"{serviceName}:{ds.Host}:{ds.Port}")
.ToArray();
var subset = new EndpointSubsetV1();
downstreams.ForEach(ds => GivenSubsetAddress(ds, subset));
var endpoints = GivenEndpoints(subset, serviceName); // totalServices service instances with different ports
var route = GivenRouteWithServiceName(namespaces, serviceName, nameof(RoundRobinAnalyzer)); // !!!
var configuration = GivenKubeConfiguration(namespaces, route);
GivenMultipleServiceInstancesAreRunning(downstreamUrls, downstreamResponses);
GivenThereIsAConfiguration(configuration);
GivenOcelotIsRunningWithServices(WithKubernetesAndRoundRobin);
return (endpoints, servicePorts);
}
private void HighlyLoadOnKubeProviderAndRoundRobinBalancer(int totalRequests, int k8sGenerationNo)
{
// Act
WhenIGetUrlOnTheApiGatewayConcurrently("/", totalRequests); // load by X parallel requests
// Assert
_k8sCounter.ShouldBeGreaterThanOrEqualTo(totalRequests); // integration endpoint called times
_k8sServiceGeneration.ShouldBe(k8sGenerationNo);
ThenAllStatusCodesShouldBe(HttpStatusCode.OK);
ThenAllServicesShouldHaveBeenCalledTimes(totalRequests);
_roundRobinAnalyzer.ShouldNotBeNull().Analyze();
_roundRobinAnalyzer.Events.Count.ShouldBe(totalRequests);
_roundRobinAnalyzer.HasManyServiceGenerations(k8sGenerationNo).ShouldBeTrue();
}
private void ThenTheTokenIs(string token)
{
_receivedToken.ShouldBe(token);
}
private EndpointsV1 GivenEndpoints(EndpointSubsetV1 subset, [CallerMemberName] string serviceName = "")
{
var e = new EndpointsV1()
{
Kind = "endpoint",
ApiVersion = "1.0",
Metadata = new()
{
Name = serviceName,
Namespace = nameof(KubernetesServiceDiscoveryTests),
},
};
e.Subsets.Add(subset);
return e;
}
private static EndpointSubsetV1 GivenSubsetAddress(Uri downstream, EndpointSubsetV1 subset = null)
{
subset ??= new();
subset.Addresses.Add(new()
{
Ip = Dns.GetHostAddresses(downstream.Host).Select(x => x.ToString()).First(a => a.Contains('.')), // 127.0.0.1
Hostname = downstream.Host,
});
subset.Ports.Add(new()
{
Name = downstream.Scheme,
Port = downstream.Port,
});
return subset;
}
private FileRoute GivenRouteWithServiceName(string serviceNamespace,
[CallerMemberName] string serviceName = null,
string loadBalancerType = nameof(LeastConnection)) => new()
{
DownstreamPathTemplate = "/",
DownstreamScheme = null, // the scheme should not be defined in service discovery scenarios by default, only ServiceName
UpstreamPathTemplate = "/",
UpstreamHttpMethod = new() { HttpMethods.Get },
ServiceName = serviceName, // !!!
ServiceNamespace = serviceNamespace,
LoadBalancerOptions = new() { Type = loadBalancerType },
};
private FileConfiguration GivenKubeConfiguration(string serviceNamespace, params FileRoute[] routes)
{
var u = new Uri(_kubernetesUrl);
var configuration = GivenConfiguration(routes);
configuration.GlobalConfiguration.ServiceDiscoveryProvider = new()
{
Scheme = u.Scheme,
Host = u.Host,
Port = u.Port,
Type = nameof(Kube),
PollingInterval = 0,
Namespace = serviceNamespace,
};
return configuration;
}
private void GivenThereIsAFakeKubernetesProvider(EndpointsV1 endpoints,
[CallerMemberName] string serviceName = nameof(KubernetesServiceDiscoveryTests), string namespaces = nameof(KubernetesServiceDiscoveryTests))
=> GivenThereIsAFakeKubernetesProvider(endpoints, true, 0, 0, serviceName, namespaces);
private void GivenThereIsAFakeKubernetesProvider(EndpointsV1 endpoints, bool isStable, int offlineServicesNo, int offlinePerThreads,
[CallerMemberName] string serviceName = nameof(KubernetesServiceDiscoveryTests), string namespaces = nameof(KubernetesServiceDiscoveryTests))
{
_k8sCounter = 0;
_kubernetesHandler.GivenThereIsAServiceRunningOn(_kubernetesUrl, async context =>
{
await Task.Delay(Random.Shared.Next(1, 10)); // emulate integration delay up to 10 milliseconds
if (context.Request.Path.Value == $"/api/v1/namespaces/{namespaces}/endpoints/{serviceName}")
{
string json;
lock (K8sCounterLocker)
{
_k8sCounter++;
var subset = endpoints.Subsets[0];
// Each offlinePerThreads-th request to integrated K8s endpoint should fail
if (!isStable && _k8sCounter % offlinePerThreads == 0 && _k8sCounter >= offlinePerThreads)
{
while (offlineServicesNo-- > 0)
{
int index = subset.Addresses.Count - 1; // Random.Shared.Next(0, subset.Addresses.Count - 1);
subset.Addresses.RemoveAt(index);
subset.Ports.RemoveAt(index);
}
_k8sServiceGeneration++;
}
endpoints.Metadata.Generation = _k8sServiceGeneration;
json = JsonConvert.SerializeObject(endpoints);
}
if (context.Request.Headers.TryGetValue("Authorization", out var values))
{
_receivedToken = values.First();
}
context.Response.Headers.Append("Content-Type", "application/json");
await context.Response.WriteAsync(json);
}
});
}
private void WithKubernetes(IServiceCollection services) => services
.AddOcelot().AddKubernetes()
.Services.RemoveAll<IKubeApiClient>().AddSingleton(_clientFactory);
private void WithKubernetesAndRoundRobin(IServiceCollection services) => services
.AddOcelot().AddKubernetes()
.AddCustomLoadBalancer<RoundRobinAnalyzer>(GetRoundRobinAnalyzer)
.Services
.RemoveAll<IKubeApiClient>().AddSingleton(_clientFactory)
.RemoveAll<IKubeServiceCreator>().AddSingleton<IKubeServiceCreator, FakeKubeServiceCreator>();
private int _k8sCounter, _k8sServiceGeneration;
private static readonly object K8sCounterLocker = new();
private RoundRobinAnalyzer _roundRobinAnalyzer;
private RoundRobinAnalyzer GetRoundRobinAnalyzer(DownstreamRoute route, IServiceDiscoveryProvider provider)
{
lock (K8sCounterLocker)
{
return _roundRobinAnalyzer ??= new RoundRobinAnalyzerCreator().Create(route, provider)?.Data as RoundRobinAnalyzer; //??= new RoundRobinAnalyzer(provider.GetAsync, route.ServiceName);
}
}
}
internal class FakeKubeServiceCreator : KubeServiceCreator
{
public FakeKubeServiceCreator(IOcelotLoggerFactory factory) : base(factory) { }
protected override ServiceHostAndPort GetServiceHostAndPort(KubeRegistryConfiguration configuration, EndpointsV1 endpoint, EndpointSubsetV1 subset, EndpointAddressV1 address)
{
var ports = subset.Ports;
var index = subset.Addresses.IndexOf(address);
var portV1 = ports[index];
Logger.LogDebug(() => $"K8s service with key '{configuration.KeyOfServiceInK8s}' and address {address.Ip}; Detected port is {portV1.Name}:{portV1.Port}. Total {ports.Count} ports of [{string.Join(',', ports.Select(p => p.Name))}].");
return new ServiceHostAndPort(address.Ip, portV1.Port, portV1.Name);
}
protected override IEnumerable<string> GetServiceTags(KubeRegistryConfiguration configuration, EndpointsV1 endpoint, EndpointSubsetV1 subset, EndpointAddressV1 address)
{
var tags = base.GetServiceTags(configuration, endpoint, subset, address)
.ToList();
long gen = endpoint.Metadata.Generation ?? 0L;
tags.Add($"{nameof(endpoint.Metadata.Generation)}:{gen}");
return tags;
}
}