Skip to content
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

Change Discovery.Kubernetes extension method to take options instead of setup #1200

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,10 @@ public async Task AkkaConfigurationBuilderTest()
services.AddAkka("test", (builder, _) =>
{
builder
.WithKubernetesDiscovery(new KubernetesDiscoverySetup
.WithKubernetesDiscovery(new KubernetesDiscoveryOptions
{
PodNamespace = "underTest",
});

var setup = ExtractSetup<KubernetesDiscoverySetup>(builder);
setup.Should().NotBeNull();
setup!.PodNamespace.Should().Be("underTest");
builder.Configuration.HasValue.Should().BeTrue();
builder.Configuration.Value.GetString("akka.discovery.method").Should().Be("kubernetes-api");
});
});

Expand All @@ -49,10 +43,9 @@ public async Task AkkaConfigurationBuilderTest()
settings.PodNamespace.Should().Be("underTest");

system.Settings.Config.GetString("akka.discovery.method").Should().Be("kubernetes-api");

var config = system.Settings.Config.GetConfig("akka.discovery.kubernetes-api");
config.Should().NotBeNull();
}

private static T? ExtractSetup<T>(AkkaConfigurationBuilder builder) where T : Setup
=> builder.Setups.FirstOrDefault(s => s is T) as T;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public static class AkkaHostingExtensions
/// services.AddAkka("mySystem", builder => {
/// builder
/// .WithClustering()
/// .WithClusterBootstrap(setup =>
/// .WithClusterBootstrap(options =>
/// {
/// setup.ContactPointDiscovery.ServiceName = "testService";
/// options.ContactPointDiscovery.ServiceName = "testService";
/// }, autoStart: true)
/// .WithKubernetesDiscovery();
/// }
Expand All @@ -51,7 +51,7 @@ public static class AkkaHostingExtensions
this AkkaConfigurationBuilder builder,
string? podLabelSelector = null)
{
return builder.WithKubernetesDiscovery(new KubernetesDiscoverySetup
return builder.WithKubernetesDiscovery(new KubernetesDiscoveryOptions
{
PodLabelSelector = podLabelSelector
});
Expand All @@ -66,7 +66,7 @@ public static class AkkaHostingExtensions
/// The builder instance being configured.
/// </param>
/// <param name="configure">
/// An action that modifies an <see cref="KubernetesDiscoverySetup"/> instance, used
/// An action that modifies an <see cref="KubernetesDiscoveryOptions"/> instance, used
/// to configure Akka.Discovery.KubernetesApi.
/// </param>
/// <returns>
Expand All @@ -77,23 +77,23 @@ public static class AkkaHostingExtensions
/// services.AddAkka("mySystem", builder => {
/// builder
/// .WithClustering()
/// .WithClusterBootstrap(setup =>
/// .WithClusterBootstrap(options =>
/// {
/// setup.ContactPointDiscovery.ServiceName = "testService";
/// options.ContactPointDiscovery.ServiceName = "testService";
/// }, autoStart: true)
/// .WithKubernetesDiscovery(setup => {
/// setup.PodNamespace = "my-cluster-namespace";
/// .WithKubernetesDiscovery(options => {
/// options.PodNamespace = "my-cluster-namespace";
/// });
/// }
/// </code>
/// </example>
public static AkkaConfigurationBuilder WithKubernetesDiscovery(
this AkkaConfigurationBuilder builder,
Action<KubernetesDiscoverySetup> configure)
Action<KubernetesDiscoveryOptions> configure)
{
var setup = new KubernetesDiscoverySetup();
configure(setup);
return builder.WithKubernetesDiscovery(setup);
var options = new KubernetesDiscoveryOptions();
configure(options);
return builder.WithKubernetesDiscovery(options);
}

/// <summary>
Expand All @@ -104,8 +104,8 @@ public static class AkkaHostingExtensions
/// <param name="builder">
/// The builder instance being configured.
/// </param>
/// <param name="setup">
/// The <see cref="KubernetesDiscoverySetup"/> instance used to configure Akka.Discovery.KubernetesApi.
/// <param name="options">
/// The <see cref="KubernetesDiscoveryOptions"/> instance used to configure Akka.Discovery.KubernetesApi.
/// </param>
/// <returns>
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.
Expand All @@ -115,22 +115,23 @@ public static class AkkaHostingExtensions
/// services.AddAkka("mySystem", builder => {
/// builder
/// .WithClustering()
/// .WithClusterBootstrap(setup =>
/// .WithClusterBootstrap(options =>
/// {
/// setup.ContactPointDiscovery.ServiceName = "testService";
/// options.ContactPointDiscovery.ServiceName = "testService";
/// }, autoStart: true)
/// .WithKubernetesDiscovery(new KubernetesDiscoverySetup {
/// .WithKubernetesDiscovery(new KubernetesDiscoveryOptions {
/// PodNamespace = "my-cluster-namespace"
/// });
/// }
/// </code>
/// </example>
public static AkkaConfigurationBuilder WithKubernetesDiscovery(
this AkkaConfigurationBuilder builder,
KubernetesDiscoverySetup setup)
KubernetesDiscoveryOptions options)
{
builder.AddHocon("akka.discovery.method=kubernetes-api", HoconAddMode.Prepend);
builder.AddSetup(setup);
options.Apply(builder);
builder.AddHocon($"akka.discovery.method = {options.ConfigPath}", HoconAddMode.Prepend);
builder.AddHocon(KubernetesDiscovery.DefaultConfiguration(), HoconAddMode.Append);

// Force start the module
builder.AddStartup((system, _) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// -----------------------------------------------------------------------
// <copyright file="KubernetesDiscoveryOptions.cs" company="Akka.NET Project">
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Linq;
using System.Text;
using Akka.Actor.Setup;
using Akka.Hosting;

namespace Akka.Discovery.KubernetesApi;

public class KubernetesDiscoveryOptions: IHoconOption
{
private const string FullPath = "akka.discovery.kubernetes-api";

public string ConfigPath { get; } = "kubernetes-api";
public Type Class { get; } = typeof(KubernetesApiServiceDiscovery);

public string? ApiCaPath { get; set; }
public string? ApiTokenPath { get; set; }
public string? ApiServiceHostEnvName { get; set; }
public string? ApiServicePortEnvName { get; set; }
public string? PodNamespacePath { get; set; }
public string? PodNamespace { get; set; }
public string? PodDomain { get; set; }
public string? PodLabelSelector { get; set; }
public bool? RawIp { get; set; }
public string? ContainerName { get; set; }

public void Apply(AkkaConfigurationBuilder builder, Setup? setup = null)
{
var sb = new StringBuilder();
sb.AppendLine($"{FullPath} {{");
sb.AppendLine($"class = {Class.AssemblyQualifiedName!.ToHocon()}");

if (ApiCaPath is { })
sb.AppendLine($"api-ca-path = {ApiCaPath.ToHocon()}");
if (ApiTokenPath is { })
sb.AppendLine($"api-token-path = {ApiTokenPath.ToHocon()}");
if (ApiServiceHostEnvName is { })
sb.AppendLine($"api-service-host-env-name = {ApiServiceHostEnvName.ToHocon()}");
if (ApiServicePortEnvName is { })
sb.AppendLine($"api-service-port-env-name = {ApiServicePortEnvName.ToHocon()}");
if (PodNamespacePath is { })
sb.AppendLine($"pod-namespace-path = {PodNamespacePath.ToHocon()}");
if (PodNamespace is { })
sb.AppendLine($"pod-namespace = {PodNamespace.ToHocon()}");
if (PodDomain is { })
sb.AppendLine($"pod-domain = {PodDomain.ToHocon()}");
if (PodLabelSelector is { })
sb.AppendLine($"pod-label-selector = {PodLabelSelector.ToHocon()}");
if (RawIp is { })
sb.AppendLine($"use-raw-ip = {RawIp.ToHocon()}");
if (ContainerName is { })
sb.AppendLine($"container-name = {ContainerName.ToHocon()}");

sb.AppendLine("}");

builder.AddHocon(sb.ToString(), HoconAddMode.Prepend);
builder.AddHocon(KubernetesDiscovery.DefaultConfiguration(), HoconAddMode.Append);
}
}