mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 17:08:15 +08:00
kubernetes use in cluster (#882)
* refactor :kubernetes use in cluster * feat:delete KubeClient
This commit is contained in:
@ -15,13 +15,14 @@ namespace Ocelot.Provider.Kubernetes
|
||||
private IOcelotLogger logger;
|
||||
private IKubeApiClient kubeApi;
|
||||
|
||||
public Kube(KubeRegistryConfiguration kubeRegistryConfiguration, IOcelotLoggerFactory factory, IKubeApiClientFactory kubeClientFactory)
|
||||
public Kube(KubeRegistryConfiguration kubeRegistryConfiguration, IOcelotLoggerFactory factory, IKubeApiClient kubeApi)
|
||||
{
|
||||
this.kubeRegistryConfiguration = kubeRegistryConfiguration;
|
||||
this.logger = factory.CreateLogger<Kube>();
|
||||
this.kubeApi = kubeClientFactory.Get(kubeRegistryConfiguration);
|
||||
this.kubeApi = kubeApi;
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<Service>> Get()
|
||||
{
|
||||
var service = await kubeApi.ServicesV1()
|
||||
|
@ -5,16 +5,8 @@ namespace Ocelot.Provider.Kubernetes
|
||||
{
|
||||
public class KubeRegistryConfiguration
|
||||
{
|
||||
public Uri ApiEndPoint { get; set; }
|
||||
|
||||
public string KubeNamespace { 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)
|
||||
{
|
||||
var kubeClientFactory = provider.GetService<IKubeApiClientFactory>();
|
||||
var kubeClient = provider.GetService<IKubeApiClient>();
|
||||
var k8sRegistryConfiguration = new KubeRegistryConfiguration()
|
||||
{
|
||||
ApiEndPoint = new Uri($"https://{config.Host}:{config.Port}"),
|
||||
KeyOfServiceInK8s = name,
|
||||
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")
|
||||
{
|
||||
return new PollKube(config.PollingInterval, factory, k8sServiceDiscoveryProvider);
|
||||
|
@ -25,7 +25,13 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<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>
|
||||
|
@ -1,14 +1,15 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using KubeClient;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.DependencyInjection;
|
||||
|
||||
namespace Ocelot.Provider.Kubernetes
|
||||
{
|
||||
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<IKubeApiClientFactory, KubeApiClientFactory>();
|
||||
builder.Services.AddKubeClient(usePodServiceAccount);
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user