mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:22:50 +08:00
kubernetes use in cluster (#882)
* refactor :kubernetes use in cluster * feat:delete KubeClient
This commit is contained in:
parent
f1f9f4a54e
commit
d147910e8e
@ -15,13 +15,14 @@ namespace Ocelot.Provider.Kubernetes
|
|||||||
private IOcelotLogger logger;
|
private IOcelotLogger logger;
|
||||||
private IKubeApiClient kubeApi;
|
private IKubeApiClient kubeApi;
|
||||||
|
|
||||||
public Kube(KubeRegistryConfiguration kubeRegistryConfiguration, IOcelotLoggerFactory factory, IKubeApiClientFactory kubeClientFactory)
|
public Kube(KubeRegistryConfiguration kubeRegistryConfiguration, IOcelotLoggerFactory factory, IKubeApiClient kubeApi)
|
||||||
{
|
{
|
||||||
this.kubeRegistryConfiguration = kubeRegistryConfiguration;
|
this.kubeRegistryConfiguration = kubeRegistryConfiguration;
|
||||||
this.logger = factory.CreateLogger<Kube>();
|
this.logger = factory.CreateLogger<Kube>();
|
||||||
this.kubeApi = kubeClientFactory.Get(kubeRegistryConfiguration);
|
this.kubeApi = kubeApi;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task<List<Service>> Get()
|
public async Task<List<Service>> Get()
|
||||||
{
|
{
|
||||||
var service = await kubeApi.ServicesV1()
|
var service = await kubeApi.ServicesV1()
|
||||||
|
@ -5,16 +5,8 @@ namespace Ocelot.Provider.Kubernetes
|
|||||||
{
|
{
|
||||||
public class KubeRegistryConfiguration
|
public class KubeRegistryConfiguration
|
||||||
{
|
{
|
||||||
public Uri ApiEndPoint { get; set; }
|
|
||||||
|
|
||||||
public string KubeNamespace { get; set; }
|
public string KubeNamespace { get; set; }
|
||||||
|
|
||||||
public string KeyOfServiceInK8s { get; set; }
|
public string KeyOfServiceInK8s { get; set; }
|
||||||
|
|
||||||
public KubeAuthStrategy AuthStrategy { get; set; }
|
|
||||||
|
|
||||||
public string AccessToken { get; set; }
|
|
||||||
|
|
||||||
public bool AllowInsecure { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,18 +16,14 @@ namespace Ocelot.Provider.Kubernetes
|
|||||||
|
|
||||||
private static ServiceDiscovery.Providers.IServiceDiscoveryProvider GetkubeProvider(IServiceProvider provider, Configuration.ServiceProviderConfiguration config, string name, IOcelotLoggerFactory factory)
|
private static ServiceDiscovery.Providers.IServiceDiscoveryProvider GetkubeProvider(IServiceProvider provider, Configuration.ServiceProviderConfiguration config, string name, IOcelotLoggerFactory factory)
|
||||||
{
|
{
|
||||||
var kubeClientFactory = provider.GetService<IKubeApiClientFactory>();
|
var kubeClient = provider.GetService<IKubeApiClient>();
|
||||||
var k8sRegistryConfiguration = new KubeRegistryConfiguration()
|
var k8sRegistryConfiguration = new KubeRegistryConfiguration()
|
||||||
{
|
{
|
||||||
ApiEndPoint = new Uri($"https://{config.Host}:{config.Port}"),
|
|
||||||
KeyOfServiceInK8s = name,
|
KeyOfServiceInK8s = name,
|
||||||
KubeNamespace = config.Namespace,
|
KubeNamespace = config.Namespace,
|
||||||
AuthStrategy = KubeAuthStrategy.BearerToken,
|
|
||||||
AccessToken = config.Token,
|
|
||||||
AllowInsecure = true // Don't validate server certificate
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var k8sServiceDiscoveryProvider = new Kube(k8sRegistryConfiguration, factory, kubeClientFactory);
|
var k8sServiceDiscoveryProvider = new Kube(k8sRegistryConfiguration, factory, kubeClient);
|
||||||
if (config.Type?.ToLower() == "pollkube")
|
if (config.Type?.ToLower() == "pollkube")
|
||||||
{
|
{
|
||||||
return new PollKube(config.PollingInterval, factory, k8sServiceDiscoveryProvider);
|
return new PollKube(config.PollingInterval, factory, k8sServiceDiscoveryProvider);
|
||||||
|
@ -25,7 +25,13 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="KubeClient" Version="2.2.4" />
|
<Compile Remove="IKubeApiClientFactory.cs" />
|
||||||
|
<Compile Remove="KubeApiClientFactory.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="KubeClient" Version="2.2.11" />
|
||||||
|
<PackageReference Include="KubeClient.Extensions.DependencyInjection" Version="2.2.11" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using KubeClient;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Ocelot.DependencyInjection;
|
using Ocelot.DependencyInjection;
|
||||||
|
|
||||||
namespace Ocelot.Provider.Kubernetes
|
namespace Ocelot.Provider.Kubernetes
|
||||||
{
|
{
|
||||||
public static class OcelotBuilderExtensions
|
public static class OcelotBuilderExtensions
|
||||||
{
|
{
|
||||||
public static IOcelotBuilder AddKubernetes(this IOcelotBuilder builder)
|
public static IOcelotBuilder AddKubernetes(this IOcelotBuilder builder, bool usePodServiceAccount = true)
|
||||||
{
|
{
|
||||||
builder.Services.AddSingleton(KubernetesProviderFactory.Get);
|
builder.Services.AddSingleton(KubernetesProviderFactory.Get);
|
||||||
builder.Services.AddSingleton<IKubeApiClientFactory, KubeApiClientFactory>();
|
builder.Services.AddKubeClient(usePodServiceAccount);
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using KubeClient.Models;
|
using KubeClient;
|
||||||
|
using KubeClient.Models;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
@ -31,7 +32,7 @@ namespace Ocelot.UnitTests.Kubernetes
|
|||||||
private readonly Mock<IOcelotLoggerFactory> _factory;
|
private readonly Mock<IOcelotLoggerFactory> _factory;
|
||||||
private readonly Mock<IOcelotLogger> _logger;
|
private readonly Mock<IOcelotLogger> _logger;
|
||||||
private string _receivedToken;
|
private string _receivedToken;
|
||||||
private readonly IKubeApiClientFactory _clientFactory;
|
private readonly IKubeApiClient _clientFactory;
|
||||||
|
|
||||||
public KubeServiceDiscoveryProviderTests()
|
public KubeServiceDiscoveryProviderTests()
|
||||||
{
|
{
|
||||||
@ -42,15 +43,20 @@ namespace Ocelot.UnitTests.Kubernetes
|
|||||||
_fakekubeServiceDiscoveryUrl = $"http://{_kubeHost}:{_port}";
|
_fakekubeServiceDiscoveryUrl = $"http://{_kubeHost}:{_port}";
|
||||||
_serviceEntries = new ServiceV1();
|
_serviceEntries = new ServiceV1();
|
||||||
_factory = new Mock<IOcelotLoggerFactory>();
|
_factory = new Mock<IOcelotLoggerFactory>();
|
||||||
_clientFactory = new KubeApiClientFactory();
|
|
||||||
|
var option = new KubeClientOptions
|
||||||
|
{
|
||||||
|
ApiEndPoint = new Uri(_fakekubeServiceDiscoveryUrl),
|
||||||
|
AccessToken = "txpc696iUhbVoudg164r93CxDTrKRVWG",
|
||||||
|
AuthStrategy = KubeClient.KubeAuthStrategy.BearerToken,
|
||||||
|
AllowInsecure = true
|
||||||
|
};
|
||||||
|
|
||||||
|
_clientFactory = KubeApiClient.Create(option);
|
||||||
_logger = new Mock<IOcelotLogger>();
|
_logger = new Mock<IOcelotLogger>();
|
||||||
_factory.Setup(x => x.CreateLogger<Kube>()).Returns(_logger.Object);
|
_factory.Setup(x => x.CreateLogger<Kube>()).Returns(_logger.Object);
|
||||||
var config = new KubeRegistryConfiguration()
|
var config = new KubeRegistryConfiguration()
|
||||||
{
|
{
|
||||||
ApiEndPoint = new Uri(_fakekubeServiceDiscoveryUrl),
|
|
||||||
AccessToken = "txpc696iUhbVoudg164r93CxDTrKRVWG",
|
|
||||||
AllowInsecure = true,
|
|
||||||
AuthStrategy = KubeClient.KubeAuthStrategy.BearerToken,
|
|
||||||
KeyOfServiceInK8s = _serviceName,
|
KeyOfServiceInK8s = _serviceName,
|
||||||
KubeNamespace = _namespaces
|
KubeNamespace = _namespaces
|
||||||
};
|
};
|
||||||
|
@ -19,6 +19,10 @@
|
|||||||
<DebugSymbols>True</DebugSymbols>
|
<DebugSymbols>True</DebugSymbols>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Remove="Kubernetes\KubeProviderFactoryTests.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\src\Ocelot.Provider.Kubernetes\Ocelot.Provider.Kubernetes.csproj" />
|
<ProjectReference Include="..\..\src\Ocelot.Provider.Kubernetes\Ocelot.Provider.Kubernetes.csproj" />
|
||||||
<ProjectReference Include="..\..\src\Ocelot\Ocelot.csproj" />
|
<ProjectReference Include="..\..\src\Ocelot\Ocelot.csproj" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user