-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Discovery.KubernetesApi to support multi-config #2596
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
using k8s.Authentication; | ||
using k8s.Models; | ||
using Newtonsoft.Json; | ||
using Debug = System.Diagnostics.Debug; | ||
|
||
#if !NET6_0_OR_GREATER | ||
using Microsoft.Rest; | ||
|
@@ -40,6 +41,10 @@ public KubernetesApiException(string message, Exception innerException) : base(m | |
{ } | ||
} | ||
|
||
internal const string DefaultPath = "kubernetes-api"; | ||
internal const string DefaultConfigPath = "akka.discovery." + DefaultPath; | ||
internal static string FullPath(string path) => $"akka.discovery.{path}"; | ||
|
||
private readonly ILoggingAdapter _log; | ||
private readonly KubernetesDiscoverySettings _settings; | ||
private readonly Kubernetes? _client; | ||
|
@@ -50,10 +55,16 @@ public KubernetesApiException(string message, Exception innerException) : base(m | |
.DefaultIfNullOrWhitespace(ReadConfigVarFromFileSystem(_settings.PodNamespacePath, "pod-namespace")) | ||
.DefaultIfNullOrWhitespace("default")!; | ||
|
||
// Backward compatible constructor | ||
public KubernetesApiServiceDiscovery(ExtendedActorSystem system) | ||
: this(system, system.Settings.Config.GetConfig(FullPath(DefaultPath))) | ||
{ | ||
} | ||
|
||
public KubernetesApiServiceDiscovery(ExtendedActorSystem system, Configuration.Config config) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM |
||
{ | ||
_log = Logging.GetLogger(system, this); | ||
_settings = KubernetesDiscovery.Get(system).Settings; | ||
_settings = KubernetesDiscoverySettings.Create(config); | ||
|
||
if(_log.IsDebugEnabled) | ||
_log.Debug("Settings {0}", _settings); | ||
|
@@ -69,12 +80,12 @@ public KubernetesApiServiceDiscovery(ExtendedActorSystem system) | |
} | ||
else | ||
{ | ||
var config = KubernetesClientConfiguration.BuildDefaultConfig(); | ||
config.TokenProvider = new TokenFileAuth(_settings.ApiTokenPath); | ||
config.ClientCertificateFilePath = _settings.ApiCaPath; | ||
config.Namespace = PodNamespace; | ||
_host = config.Host = $"https://{host}:{port}"; | ||
_client = new Kubernetes(config); | ||
var clientConfig = KubernetesClientConfiguration.BuildDefaultConfig(); | ||
clientConfig.TokenProvider = new TokenFileAuth(_settings.ApiTokenPath); | ||
clientConfig.ClientCertificateFilePath = _settings.ApiCaPath; | ||
clientConfig.Namespace = PodNamespace; | ||
_host = clientConfig.Host = $"https://{host}:{port}"; | ||
_client = new Kubernetes(clientConfig); | ||
} | ||
} | ||
|
||
|
@@ -172,7 +183,8 @@ private async Task<V1PodList> ListNamespacedPod(string labelSelector, Cancellati | |
{ | ||
V1PodList podList; | ||
#if !NET6_0_OR_GREATER | ||
var result = await _client.ListNamespacedPodWithHttpMessagesAsync( | ||
Debug.Assert(_client != null, nameof(_client) + " != null"); | ||
var result = await _client!.ListNamespacedPodWithHttpMessagesAsync( | ||
namespaceParameter: PodNamespace, | ||
labelSelector: labelSelector, | ||
cancellationToken: cts.Token) | ||
|
@@ -192,7 +204,8 @@ private async Task<V1PodList> ListPodForAllNamespaces(string labelSelector, Canc | |
{ | ||
V1PodList podList; | ||
#if !NET6_0_OR_GREATER | ||
var result = await _client.ListPodForAllNamespacesWithHttpMessagesAsync( | ||
Debug.Assert(_client != null, nameof(_client) + " != null"); | ||
var result = await _client!.ListPodForAllNamespacesWithHttpMessagesAsync( | ||
labelSelector: labelSelector, | ||
cancellationToken: cts.Token) | ||
.ConfigureAwait(false); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,11 +15,10 @@ namespace Akka.Discovery.KubernetesApi; | |
|
||
public class KubernetesDiscoveryOptions: IHoconOption | ||
{ | ||
private const string FullPath = "akka.discovery.kubernetes-api"; | ||
|
||
public string ConfigPath { get; } = "kubernetes-api"; | ||
public string ConfigPath { get; set; } = KubernetesApiServiceDiscovery.DefaultPath; | ||
public Type Class { get; } = typeof(KubernetesApiServiceDiscovery); | ||
|
||
public bool IsDefaultPlugin { get; set; } = true; | ||
public string? ApiCaPath { get; set; } | ||
public string? ApiTokenPath { get; set; } | ||
public string? ApiServiceHostEnvName { get; set; } | ||
|
@@ -35,7 +34,7 @@ public class KubernetesDiscoveryOptions: IHoconOption | |
public void Apply(AkkaConfigurationBuilder builder, Setup? setup = null) | ||
{ | ||
var sb = new StringBuilder(); | ||
sb.AppendLine($"{FullPath} {{"); | ||
sb.AppendLine($"{KubernetesApiServiceDiscovery.FullPath(ConfigPath)} {{"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM - looks like we made the pathing more dynamic all-around here. |
||
sb.AppendLine($"class = {Class.AssemblyQualifiedName!.ToHocon()}"); | ||
|
||
if (ApiCaPath is { }) | ||
|
@@ -62,8 +61,15 @@ public void Apply(AkkaConfigurationBuilder builder, Setup? setup = null) | |
sb.AppendLine($"container-name = {ContainerName.ToHocon()}"); | ||
|
||
sb.AppendLine("}"); | ||
|
||
if(IsDefaultPlugin) | ||
sb.AppendLine($"akka.discovery.method = {ConfigPath}"); | ||
|
||
builder.AddHocon(sb.ToString(), HoconAddMode.Prepend); | ||
builder.AddHocon(KubernetesDiscovery.DefaultConfiguration(), HoconAddMode.Append); | ||
|
||
var fallback = KubernetesDiscovery.DefaultConfiguration() | ||
.GetConfig(KubernetesApiServiceDiscovery.FullPath(KubernetesApiServiceDiscovery.DefaultPath)) | ||
.MoveTo(KubernetesApiServiceDiscovery.FullPath(ConfigPath)); | ||
builder.AddHocon(fallback, HoconAddMode.Append); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the same method from the Azure plugin you were working on?