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

[Kubernetes Lease] Add Akka.Hosting support #784

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
@@ -0,0 +1,86 @@
// -----------------------------------------------------------------------
// <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.Coordination.KubernetesApi.Tests
{
public class AkkaHostingSpec
{
[Fact(DisplayName = "Hosting extension should add default hocon settings")]
public void HostingExtension1Test()
{
var builder = new AkkaConfigurationBuilder(new ServiceCollection(), "test");

builder.WithKubernetesLease();

builder.Configuration.HasValue.Should().BeTrue();
builder.Configuration.Value.GetConfig("akka.coordination.lease.kubernetes")
.Should().NotBeNull();
}

[Fact(DisplayName = "Hosting Action<Setup> extension should add Setup class and default hocon settings")]
public void HostingExtension2Test()
{
var builder = new AkkaConfigurationBuilder(new ServiceCollection(), "test");

builder.WithKubernetesLease(lease =>
{
lease.Namespace = "underTest";
});

builder.Configuration.HasValue.Should().BeTrue();
builder.Configuration.Value.GetConfig("akka.coordination.lease.kubernetes")
.Should().NotBeNull();
var setup = ExtractSetup(builder);
setup.Should().NotBeNull();
setup.Namespace.Should().Be("underTest");
}

[Fact(DisplayName = "Hosting Setup extension should add Setup class and default hocon settings")]
public void HostingExtension3Test()
{
var builder = new AkkaConfigurationBuilder(new ServiceCollection(), "test");

builder.WithKubernetesLease(new KubernetesLeaseSetup
{
Namespace = "underTest"
});

builder.Configuration.HasValue.Should().BeTrue();
builder.Configuration.Value.GetConfig("akka.coordination.lease.kubernetes")
.Should().NotBeNull();
var setup = ExtractSetup(builder);
setup.Should().NotBeNull();
setup.Namespace.Should().Be("underTest");
}

private KubernetesLeaseSetup ExtractSetup(AkkaConfigurationBuilder builder)
{
var type = builder.GetType();
var fieldInfo = type.GetField("Setups", BindingFlags.NonPublic | BindingFlags.Instance);
var setups = (HashSet<Setup>) fieldInfo?.GetValue(builder);

if (setups == null)
throw new XunitException("Could not found 'Setups' field, AkkaConfigurationBuilder internal API changed");

setups.Should().NotBeNull();
return (KubernetesLeaseSetup) setups.FirstOrDefault(s => s is KubernetesLeaseSetup);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="Akka.Coordination" Version="$(AkkaVersion)" />
<PackageReference Include="Akka.Hosting" Version="$(AkkaHostingVersion)" />
<PackageReference Include="KubernetesClient" Version="$(KubernetesClientVersion)" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// -----------------------------------------------------------------------
// <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.Coordination.KubernetesApi
{
public static class AkkaHostingExtensions
{
/// <summary>
/// Adds Akka.Coordination.KubernetesApi <see cref="Lease"/> support to the <see cref="ActorSystem"/>.
/// Note that this only adds the lease plugin, you will still need to add the services that depends on
/// <see cref="Lease"/> to use this.
/// </summary>
/// <param name="builder">
/// The builder instance being configured.
/// </param>
/// <returns>
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.
/// </returns>
public static AkkaConfigurationBuilder WithKubernetesLease(this AkkaConfigurationBuilder builder)
=> WithKubernetesLease(builder, (KubernetesLeaseSetup)null);

/// <summary>
/// Adds Akka.Coordination.KubernetesApi <see cref="Lease"/> support to the <see cref="ActorSystem"/>.
/// Note that this only adds the lease plugin, you will still need to add the services that depends on
/// <see cref="Lease"/> to use this.
/// </summary>
/// <param name="builder">
/// The builder instance being configured.
/// </param>
/// <param name="configure">
/// An action that modifies an <see cref="KubernetesLeaseSetup"/> instance, used
/// to configure Akka.Coordination.KubernetesApi.
/// </param>
/// <returns>
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.
/// </returns>
public static AkkaConfigurationBuilder WithKubernetesLease(
this AkkaConfigurationBuilder builder,
Action<KubernetesLeaseSetup> configure)
{
if (configure == null)
throw new ArgumentNullException(nameof(configure));

var setup = new KubernetesLeaseSetup();
configure(setup);
return WithKubernetesLease(builder, setup);
}

/// <summary>
/// Adds Akka.Coordination.KubernetesApi <see cref="Lease"/> support to the <see cref="ActorSystem"/>.
/// Note that this only adds the lease plugin, you will still need to add the services that depends on
/// <see cref="Lease"/> to use this.
/// </summary>
/// <param name="builder">
/// The builder instance being configured.
/// </param>
/// <param name="setup">
/// The <see cref="KubernetesLeaseSetup"/> instance used to configure Akka.Discovery.Azure.
/// </param>
/// <returns>
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.
/// </returns>
public static AkkaConfigurationBuilder WithKubernetesLease(
this AkkaConfigurationBuilder builder,
KubernetesLeaseSetup setup)
{
builder.AddHocon(KubernetesLease.DefaultConfiguration);
if (setup != null)
builder.AddSetup(setup);

return builder;
}

}
}