Updated KubeProvider to use pod endpoints instead of service

This commit is contained in:
WebMed 2020-02-10 23:52:55 +01:00
parent fc3b6fdb8b
commit 88034a5f9f
4 changed files with 69 additions and 24 deletions

View File

@ -0,0 +1,41 @@
using HTTPlease;
using KubeClient;
using KubeClient.Models;
using KubeClient.ResourceClients;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Ocelot.Provider.Kubernetes.KubeApiClientExtensions
{
public class EndPointClientV1 : KubeResourceClient
{
public EndPointClientV1(IKubeApiClient client) : base(client)
{
}
public async Task<EndpointsV1> Get(string serviceName, string kubeNamespace = null, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(serviceName)) throw new ArgumentNullException(nameof(serviceName));
var response = await Http.GetAsync(
Requests.Collection.WithTemplateParameters(new
{
Namespace = kubeNamespace ?? KubeClient.DefaultNamespace,
ServiceName = serviceName
}),
cancellationToken: cancellationToken
);
if (response.IsSuccessStatusCode)
return await response.ReadContentAsAsync<EndpointsV1>();
return null;
}
public static class Requests
{
public static readonly HttpRequest Collection = KubeRequest.Create("api/v1/namespaces/{Namespace}/endpoints/{ServiceName}");
}
}
}

View File

@ -0,0 +1,12 @@
using KubeClient;
namespace Ocelot.Provider.Kubernetes.KubeApiClientExtensions
{
public static class KubeClientExtensions
{
public static EndPointClientV1 EndPointsV1(this IKubeApiClient kubeClient)
{
return kubeClient.ResourceClient(client => new EndPointClientV1(client));
}
}
}

View File

@ -6,14 +6,15 @@ using Ocelot.Values;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Ocelot.Provider.Kubernetes.KubeApiClientExtensions;
namespace Ocelot.Provider.Kubernetes namespace Ocelot.Provider.Kubernetes
{ {
public class Kube : IServiceDiscoveryProvider public class Kube : IServiceDiscoveryProvider
{ {
private KubeRegistryConfiguration kubeRegistryConfiguration; private KubeRegistryConfiguration kubeRegistryConfiguration;
private IOcelotLogger logger; private readonly IOcelotLogger logger;
private IKubeApiClient kubeApi; private readonly IKubeApiClient kubeApi;
public Kube(KubeRegistryConfiguration kubeRegistryConfiguration, IOcelotLoggerFactory factory, IKubeApiClient kubeApi) public Kube(KubeRegistryConfiguration kubeRegistryConfiguration, IOcelotLoggerFactory factory, IKubeApiClient kubeApi)
{ {
@ -22,14 +23,13 @@ namespace Ocelot.Provider.Kubernetes
this.kubeApi = kubeApi; this.kubeApi = kubeApi;
} }
public async Task<List<Service>> Get() public async Task<List<Service>> Get()
{ {
var service = await kubeApi.ServicesV1().Get(kubeRegistryConfiguration.KeyOfServiceInK8s, kubeRegistryConfiguration.KubeNamespace); var endpoint = await kubeApi.EndPointsV1().Get(kubeRegistryConfiguration.KeyOfServiceInK8s, kubeRegistryConfiguration.KubeNamespace);
var services = new List<Service>(); var services = new List<Service>();
if (IsValid(service)) if (endpoint != null && endpoint.Subsets.Any())
{ {
services.Add(BuildService(service)); services.AddRange(BuildServices(endpoint));
} }
else else
{ {
@ -38,25 +38,17 @@ namespace Ocelot.Provider.Kubernetes
return services; return services;
} }
private bool IsValid(ServiceV1 service) private List<Service> BuildServices(EndpointsV1 endpoint)
{ {
if (string.IsNullOrEmpty(service.Spec.ClusterIP) || service.Spec.Ports.Count <= 0) var services = new List<Service>();
{
return false;
}
return true; foreach (var subset in endpoint.Subsets)
}
private Service BuildService(ServiceV1 serviceEntry)
{ {
var servicePort = serviceEntry.Spec.Ports.FirstOrDefault(); services.AddRange(subset.Addresses.Select(address => new Service(endpoint.Metadata.Name,
return new Service( new ServiceHostAndPort(address.Ip, subset.Ports.First().Port),
serviceEntry.Metadata.Name, endpoint.Metadata.Uid, string.Empty, Enumerable.Empty<string>())));
new ServiceHostAndPort(serviceEntry.Spec.ClusterIP, servicePort.Port), }
serviceEntry.Metadata.Uid, return services;
string.Empty,
Enumerable.Empty<string>());
} }
} }
} }

View File

@ -28,8 +28,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="KubeClient" Version="2.3.4" /> <PackageReference Include="KubeClient" Version="2.3.11" />
<PackageReference Include="KubeClient.Extensions.DependencyInjection" Version="2.3.4" /> <PackageReference Include="KubeClient.Extensions.DependencyInjection" Version="2.3.11" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>