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

[Discovery.KubernetesApi] Add Akka.Hosting extension methods #822

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,73 @@
// -----------------------------------------------------------------------
// <copyright file="AkkaHostingSpec.cs" company="Akka.NET Project">
// Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
// -----------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Actor.Setup;
using Akka.Hosting;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;
using Xunit.Sdk;

namespace Akka.Discovery.KubernetesApi.Tests
{
public class AkkaHostingSpec
{
[Fact(DisplayName = "AkkaConfigurationBuilder extension should inject proper HOCON and Setup")]
public async Task AkkaConfigurationBuilderTest()
{
var hostBuilder = new HostBuilder()
.ConfigureServices((context, services) =>
{
services.AddAkka("test", (builder, provider) =>
{
builder
.WithKubernetesDiscovery(new KubernetesDiscoverySetup
{
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");
});
});

using (var host = hostBuilder.Build())
{
await host.StartAsync();

var system = host.Services.GetRequiredService<ActorSystem>();

var settings = KubernetesDiscovery.Get(system).Settings;
settings.PodNamespace.Should().Be("underTest");

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

private static T ExtractSetup<T>(AkkaConfigurationBuilder builder) where T : Setup
{
var type = builder.GetType();
var fieldInfo = type.GetField("Setups", BindingFlags.NonPublic | BindingFlags.Instance);
if (fieldInfo == null)
throw new XunitException("Could not found 'AkkaConfigurationBuilder.Setups' field, AkkaConfigurationBuilder internal API has changed");

var setups = (HashSet<Setup>) fieldInfo.GetValue(builder);
setups.Should().NotBeNull("'AkkaConfigurationBuilder.Setups' should exist and contains a value");
return (T) setups!.FirstOrDefault(s => s is KubernetesDiscoverySetup);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,6 @@ public void EmptySettingsTest()
empty.ContainerName.Should().Be(settings.ContainerName);
}

[Fact(DisplayName = "Illegal pod-label-selector must throw")]
public void IllegalPodLabelSelectorTest()
{
var settings = KubernetesDiscoverySettings.Empty;

Invoking(() => settings.WithPodLabelSelector("={0}"))
.Should().ThrowExactly<ConfigurationException>();

Invoking(() => settings.WithPodLabelSelector("a="))
.Should().ThrowExactly<ConfigurationException>();

Invoking(() => settings.WithPodLabelSelector("a=={0}"))
.Should().ThrowExactly<ConfigurationException>();

Invoking(() => settings.WithPodLabelSelector("a{1}={0}"))
.Should().ThrowExactly<ConfigurationException>();

Invoking(() => settings.WithPodLabelSelector("a={0}b"))
.Should().ThrowExactly<ConfigurationException>();
}

[Fact(DisplayName = "Settings With override must work")]
public void WithOverrideTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="Akka.Discovery" Version="$(AkkaVersion)" />
<PackageReference Include="Akka.Hosting" Version="0.4.2" />
Aaronontheweb marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="KubernetesClient" Version="$(KubernetesClientVersion)" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// -----------------------------------------------------------------------
// <copyright file="AkkaHostingExtensions.cs" company="Akka.NET Project">
// Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
// -----------------------------------------------------------------------

using System;
using Akka.Actor;
using Akka.Hosting;

namespace Akka.Discovery.KubernetesApi
{
public static class AkkaHostingExtensions
{
/// <summary>
/// Adds Akka.Discovery.KubernetesApi support to the <see cref="ActorSystem"/>.
/// Note that this only adds the discovery plugin, you will still need to add ClusterBootstrap for
/// a complete solution.
/// </summary>
/// <param name="builder">
/// The builder instance being configured.
/// </param>
/// <param name="podLabelSelector">
/// <para>
/// Optional. Pod label selector value to query pod API with. See the official Kubernetes documentation on
/// <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors">
/// label selectors</a> for more information.
/// </para>
/// <b>Default</b>: "app={0}", where "{0}" will be replaced by
/// <c>ClusterBootstrapSetup.ContactPointDiscovery.ServiceName</c> value.
/// </param>
/// <returns>
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.
/// </returns>
/// <example>
/// <code>
/// // In this code sample, the final label selector would be "app=testService".
/// services.AddAkka("mySystem", builder => {
/// builder
/// .WithClustering()
/// .WithClusterBootstrap(setup =>
/// {
/// setup.ContactPointDiscovery.ServiceName = "testService";
/// }, autoStart: true)
/// .WithKubernetesDiscovery();
/// }
/// </code>
/// </example>
public static AkkaConfigurationBuilder WithKubernetesDiscovery(
this AkkaConfigurationBuilder builder,
string podLabelSelector = null)
{
return builder.WithKubernetesDiscovery(new KubernetesDiscoverySetup
{
PodLabelSelector = podLabelSelector
});
}

/// <summary>
/// Adds Akka.Discovery.KubernetesApi support to the <see cref="ActorSystem"/>.
/// Note that this only adds the discovery plugin, you will still need to add ClusterBootstrap for
/// a complete solution.
/// </summary>
/// <param name="builder">
/// The builder instance being configured.
/// </param>
/// <param name="configure">
/// An action that modifies an <see cref="KubernetesDiscoverySetup"/> instance, used
/// to configure Akka.Discovery.KubernetesApi.
/// </param>
/// <returns>
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.
/// </returns>
/// <example>
/// <code>
/// services.AddAkka("mySystem", builder => {
/// builder
/// .WithClustering()
/// .WithClusterBootstrap(setup =>
/// {
/// setup.ContactPointDiscovery.ServiceName = "testService";
/// }, autoStart: true)
/// .WithKubernetesDiscovery(setup => {
/// setup.PodNamespace = "my-cluster-namespace";
/// });
/// }
/// </code>
/// </example>
public static AkkaConfigurationBuilder WithKubernetesDiscovery(
this AkkaConfigurationBuilder builder,
Action<KubernetesDiscoverySetup> configure)
{
var setup = new KubernetesDiscoverySetup();
configure(setup);
return builder.WithKubernetesDiscovery(setup);
}

/// <summary>
/// Adds Akka.Discovery.KubernetesApi support to the <see cref="ActorSystem"/>.
/// Note that this only adds the discovery plugin, you will still need to add ClusterBootstrap for
/// a complete solution.
/// </summary>
/// <param name="builder">
/// The builder instance being configured.
/// </param>
/// <param name="setup">
/// The <see cref="KubernetesDiscoverySetup"/> instance used to configure Akka.Discovery.KubernetesApi.
/// </param>
/// <returns>
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.
/// </returns>
/// <example>
/// <code>
/// services.AddAkka("mySystem", builder => {
/// builder
/// .WithClustering()
/// .WithClusterBootstrap(setup =>
/// {
/// setup.ContactPointDiscovery.ServiceName = "testService";
/// }, autoStart: true)
/// .WithKubernetesDiscovery(new KubernetesDiscoverySetup {
/// PodNamespace = "my-cluster-namespace"
/// });
/// }
/// </code>
/// </example>
public static AkkaConfigurationBuilder WithKubernetesDiscovery(
this AkkaConfigurationBuilder builder,
KubernetesDiscoverySetup setup)
{
builder.AddHocon("akka.discovery.method=kubernetes-api", HoconAddMode.Prepend);
builder.AddSetup(setup);

// Force start the module
builder.AddStartup((system, registry) =>
{
KubernetesDiscovery.Get(system);
});
return builder;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
// </copyright>
//-----------------------------------------------------------------------

using System.Linq;
using Akka.Actor;
using Akka.Configuration;

#nullable enable
namespace Akka.Discovery.KubernetesApi
Expand Down Expand Up @@ -55,14 +53,6 @@ public static KubernetesDiscoverySettings Create(Configuration.Config config)
PodNamespacePath = podNamespacePath;
PodNamespace = podNamespace;
PodDomain = podDomain;
if (!podLabelSelector.EndsWith("={0}") ||
podLabelSelector.StartsWith("=") ||
podLabelSelector.Count(c => c == '{') != 1 ||
podLabelSelector.Count(c => c == '}') != 1 ||
podLabelSelector.Count(c => c == '=') != 1)
{
throw new ConfigurationException("pod-label-selector value must be in the form of \"podLabel={0}\"");
}
_podLabelSelector = podLabelSelector;
RawIp = rawIp;
ContainerName = string.IsNullOrWhiteSpace(containerName) ? null : containerName;
Expand Down