-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1967 Customize K8s services creation in
Kube
service discovery pro…
…vider (#2052) * Initial refactoring * Interfaces namespace * `IKubeServiceBuilder` interface vs `KubeServiceBuilder` class * `IKubeServiceCreator` interface vs `KubeServiceCreator` class * Customize K8s services creation * Add logger * namespace Ocelot.AcceptanceTests.ServiceDiscovery * Add `KubernetesServiceDiscoveryTests` * Unit tests * AAA pattern * Acceptance tests * Update kubernetes.rst * Check docs
- Loading branch information
Showing
22 changed files
with
1,982 additions
and
1,311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ot.Provider.Kubernetes/IEndPointClient.cs → ....Kubernetes/Interfaces/IEndPointClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/Ocelot.Provider.Kubernetes/Interfaces/IKubeServiceBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using KubeClient.Models; | ||
using Ocelot.Values; | ||
|
||
namespace Ocelot.Provider.Kubernetes.Interfaces; | ||
|
||
public interface IKubeServiceBuilder | ||
{ | ||
IEnumerable<Service> BuildServices(KubeRegistryConfiguration configuration, EndpointsV1 endpoint); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Ocelot.Provider.Kubernetes/Interfaces/IKubeServiceCreator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using KubeClient.Models; | ||
using Ocelot.Values; | ||
|
||
namespace Ocelot.Provider.Kubernetes.Interfaces; | ||
|
||
public interface IKubeServiceCreator | ||
{ | ||
IEnumerable<Service> Create(KubeRegistryConfiguration configuration, EndpointsV1 endpoint, EndpointSubsetV1 subset); | ||
IEnumerable<Service> CreateInstance(KubeRegistryConfiguration configuration, EndpointsV1 endpoint, EndpointSubsetV1 subset, EndpointAddressV1 address); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
src/Ocelot.Provider.Kubernetes/KubeRegistryConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
namespace Ocelot.Provider.Kubernetes | ||
namespace Ocelot.Provider.Kubernetes; | ||
|
||
public class KubeRegistryConfiguration | ||
{ | ||
public class KubeRegistryConfiguration | ||
{ | ||
public string KubeNamespace { get; set; } | ||
public string KeyOfServiceInK8s { get; set; } | ||
} | ||
public string KubeNamespace { get; set; } | ||
public string KeyOfServiceInK8s { get; set; } | ||
public string Scheme { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using KubeClient.Models; | ||
using Ocelot.Logging; | ||
using Ocelot.Provider.Kubernetes.Interfaces; | ||
using Ocelot.Values; | ||
|
||
namespace Ocelot.Provider.Kubernetes; | ||
|
||
public class KubeServiceBuilder : IKubeServiceBuilder | ||
{ | ||
private readonly IOcelotLogger _logger; | ||
private readonly IKubeServiceCreator _serviceCreator; | ||
|
||
public KubeServiceBuilder(IOcelotLoggerFactory factory, IKubeServiceCreator serviceCreator) | ||
{ | ||
ArgumentNullException.ThrowIfNull(factory); | ||
_logger = factory.CreateLogger<KubeServiceBuilder>(); | ||
|
||
ArgumentNullException.ThrowIfNull(serviceCreator); | ||
_serviceCreator = serviceCreator; | ||
} | ||
|
||
public virtual IEnumerable<Service> BuildServices(KubeRegistryConfiguration configuration, EndpointsV1 endpoint) | ||
{ | ||
ArgumentNullException.ThrowIfNull(configuration); | ||
ArgumentNullException.ThrowIfNull(endpoint); | ||
|
||
var services = endpoint.Subsets | ||
.SelectMany(subset => _serviceCreator.Create(configuration, endpoint, subset)) | ||
.ToArray(); | ||
|
||
_logger.LogDebug(() => $"K8s '{Check(endpoint.Kind)}:{Check(endpoint.ApiVersion)}:{Check(endpoint.Metadata?.Name)}' endpoint: Total built {services.Length} services."); | ||
return services; | ||
} | ||
|
||
private static string Check(string str) => string.IsNullOrEmpty(str) ? "?" : str; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using KubeClient.Models; | ||
using Ocelot.Logging; | ||
using Ocelot.Provider.Kubernetes.Interfaces; | ||
using Ocelot.Values; | ||
|
||
namespace Ocelot.Provider.Kubernetes; | ||
|
||
public class KubeServiceCreator : IKubeServiceCreator | ||
{ | ||
private readonly IOcelotLogger _logger; | ||
|
||
public KubeServiceCreator(IOcelotLoggerFactory factory) | ||
{ | ||
ArgumentNullException.ThrowIfNull(factory); | ||
_logger = factory.CreateLogger<KubeServiceCreator>(); | ||
} | ||
|
||
public virtual IEnumerable<Service> Create(KubeRegistryConfiguration configuration, EndpointsV1 endpoint, EndpointSubsetV1 subset) | ||
=> (configuration == null || endpoint == null || subset == null) | ||
? Array.Empty<Service>() | ||
: subset.Addresses | ||
.SelectMany(address => CreateInstance(configuration, endpoint, subset, address)) | ||
.ToArray(); | ||
|
||
public virtual IEnumerable<Service> CreateInstance(KubeRegistryConfiguration configuration, EndpointsV1 endpoint, EndpointSubsetV1 subset, EndpointAddressV1 address) | ||
{ | ||
var instance = new Service( | ||
GetServiceName(configuration, endpoint, subset, address), | ||
GetServiceHostAndPort(configuration, endpoint, subset, address), | ||
GetServiceId(configuration, endpoint, subset, address), | ||
GetServiceVersion(configuration, endpoint, subset, address), | ||
GetServiceTags(configuration, endpoint, subset, address) | ||
); | ||
return new Service[] { instance }; | ||
} | ||
|
||
protected virtual string GetServiceName(KubeRegistryConfiguration configuration, EndpointsV1 endpoint, EndpointSubsetV1 subset, EndpointAddressV1 address) | ||
=> endpoint.Metadata?.Name; | ||
|
||
protected virtual ServiceHostAndPort GetServiceHostAndPort(KubeRegistryConfiguration configuration, EndpointsV1 endpoint, EndpointSubsetV1 subset, EndpointAddressV1 address) | ||
{ | ||
var ports = subset.Ports; | ||
bool portNameToScheme(EndpointPortV1 p) => string.Equals(p.Name, configuration.Scheme, StringComparison.InvariantCultureIgnoreCase); | ||
var portV1 = string.IsNullOrEmpty(configuration.Scheme) || !ports.Any(portNameToScheme) | ||
? ports.FirstOrDefault() | ||
: ports.FirstOrDefault(portNameToScheme); | ||
portV1 ??= new(); | ||
portV1.Name ??= configuration.Scheme ?? string.Empty; | ||
_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 virtual string GetServiceId(KubeRegistryConfiguration configuration, EndpointsV1 endpoint, EndpointSubsetV1 subset, EndpointAddressV1 address) | ||
=> endpoint.Metadata?.Uid; | ||
protected virtual string GetServiceVersion(KubeRegistryConfiguration configuration, EndpointsV1 endpoint, EndpointSubsetV1 subset, EndpointAddressV1 address) | ||
=> endpoint.ApiVersion; | ||
protected virtual IEnumerable<string> GetServiceTags(KubeRegistryConfiguration configuration, EndpointsV1 endpoint, EndpointSubsetV1 subset, EndpointAddressV1 address) | ||
=> Enumerable.Empty<string>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Ocelot.DependencyInjection; | ||
using Ocelot.Provider.Kubernetes.Interfaces; | ||
|
||
namespace Ocelot.Provider.Kubernetes | ||
namespace Ocelot.Provider.Kubernetes; | ||
|
||
public static class OcelotBuilderExtensions | ||
{ | ||
public static class OcelotBuilderExtensions | ||
public static IOcelotBuilder AddKubernetes(this IOcelotBuilder builder, bool usePodServiceAccount = true) | ||
{ | ||
public static IOcelotBuilder AddKubernetes(this IOcelotBuilder builder, bool usePodServiceAccount = true) | ||
{ | ||
builder.Services | ||
.AddSingleton(KubernetesProviderFactory.Get) | ||
.AddKubeClient(usePodServiceAccount); | ||
return builder; | ||
} | ||
builder.Services | ||
.AddKubeClient(usePodServiceAccount) | ||
.AddSingleton(KubernetesProviderFactory.Get) | ||
.AddSingleton<IKubeServiceBuilder, KubeServiceBuilder>() | ||
.AddSingleton<IKubeServiceCreator, KubeServiceCreator>(); | ||
return builder; | ||
} | ||
} |
7 changes: 3 additions & 4 deletions
7
src/Ocelot/ServiceDiscovery/ServiceDiscoveryFinderDelegate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
using Ocelot.Configuration; | ||
using Ocelot.ServiceDiscovery.Providers; | ||
|
||
namespace Ocelot.ServiceDiscovery | ||
{ | ||
public delegate IServiceDiscoveryProvider ServiceDiscoveryFinderDelegate(IServiceProvider provider, ServiceProviderConfiguration config, DownstreamRoute route); | ||
} | ||
namespace Ocelot.ServiceDiscovery; | ||
|
||
public delegate IServiceDiscoveryProvider ServiceDiscoveryFinderDelegate(IServiceProvider provider, ServiceProviderConfiguration config, DownstreamRoute route); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.