Skip to content

Commit

Permalink
Add Discovery.AwsApi multi-config support (#2651)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron Stannard <[email protected]>
  • Loading branch information
Arkatufus and Aaronontheweb authored Jul 15, 2024
1 parent ff66309 commit 9c88f33
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ public static AkkaConfigurationBuilder WithAwsEc2Discovery(
this AkkaConfigurationBuilder builder,
Ec2ServiceDiscoveryOptions options)
{
builder.AddHocon($"akka.discovery.method = {options.ConfigPath}", HoconAddMode.Prepend);
options.Apply(builder);
builder.AddHocon(AwsEc2Discovery.DefaultConfiguration(), HoconAddMode.Append);

// force start the module
builder.AddStartup((system, registry) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace Akka.Discovery.AwsApi.Ec2
{
public class AwsEc2Discovery : IExtension
{
internal const string DefaultPath = "aws-api-ec2-tag-based";
internal const string DefaultConfigPath = "akka.discovery." + DefaultPath;

public static Configuration.Config DefaultConfiguration()
=> ConfigurationFactory.FromResource<AwsEc2Discovery>("Akka.Discovery.AwsApi.reference.conf");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ namespace Akka.Discovery.AwsApi.Ec2;

public class Ec2ServiceDiscoveryOptions: IHoconOption
{
private const string FullPath = "akka.discovery.aws-api-ec2-tag-based";

public string ConfigPath { get; } = "aws-api-ec2-tag-based";
public string ConfigPath { get; set; } = AwsEc2Discovery.DefaultPath;
internal static string FullPath(string path) => $"akka.discovery.{path}";
public Type Class { get; } = typeof(Ec2TagBasedServiceDiscovery);

/// <summary>
/// Mark this plugin as the default plugin to be used by ClusterBootstrap
/// </summary>
public bool IsDefaultPlugin { get; set; } = true;

/// <summary>
/// A class <see cref="Type"/> that extends <see cref="AmazonEC2Config"/> with either
/// a no arguments constructor or a single argument constructor that takes an ExtendedActorSystem
Expand Down Expand Up @@ -88,7 +92,7 @@ private static void ValidateType<T>(Type type, string paramName)
public void Apply(AkkaConfigurationBuilder builder, Setup? setup = null)
{
var sb = new StringBuilder();
sb.AppendLine($"{FullPath} {{");
sb.AppendLine($"{FullPath(ConfigPath)} {{");
sb.AppendLine($"class = {Class.AssemblyQualifiedName!.ToHocon()}");

if(ClientConfig is { })
Expand Down Expand Up @@ -126,5 +130,13 @@ public void Apply(AkkaConfigurationBuilder builder, Setup? setup = null)
sb.AppendLine("}");

builder.AddHocon(sb.ToString(), HoconAddMode.Prepend);

if(IsDefaultPlugin)
builder.AddHocon($"akka.discovery.method = {ConfigPath}", HoconAddMode.Prepend);

var fallback = AwsEc2Discovery.DefaultConfiguration()
.GetConfig(AwsEc2Discovery.DefaultConfigPath)
.MoveTo(FullPath(ConfigPath));
builder.AddHocon(fallback, HoconAddMode.Append);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,19 @@ private AmazonEC2Client Ec2Client
}
}

// Backward compatibility constructor
public Ec2TagBasedServiceDiscovery(ExtendedActorSystem system)
: this(system, system.Settings.Config.GetConfig(AwsEc2Discovery.DefaultConfigPath))
{
}

public Ec2TagBasedServiceDiscovery(ExtendedActorSystem system, Configuration.Config config)
{
_system = system;
_config = _system.Settings.Config.GetConfig("akka.discovery.aws-api-ec2-tag-based");
_config = config;
_log = Logging.GetLogger(system, typeof(Ec2TagBasedServiceDiscovery));
_settings = AwsEc2Discovery.Get(system).Settings;

_settings = Ec2ServiceDiscoverySettings.Create(_config);

_runningInstancesFilter = new Filter("instance-state-name", new List<string> {"running"});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ public static AkkaConfigurationBuilder WithAwsEcsDiscovery(
this AkkaConfigurationBuilder builder,
EcsServiceDiscoveryOptions options)
{
builder.AddHocon($"akka.discovery.method = {options.ConfigPath}", HoconAddMode.Prepend);
options.Apply(builder);
builder.AddHocon(AwsEcsDiscovery.DefaultConfiguration(), HoconAddMode.Append);

// force start the module
builder.AddStartup((system, registry) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ namespace Akka.Discovery.AwsApi.Ecs
{
public class AwsEcsDiscovery: IExtension
{
internal const string DefaultPath = "aws-api-ecs";
internal const string DefaultConfigPath = "akka.discovery." + DefaultPath;

public static Either<string, IPAddress> GetContainerAddress()
{
var addresses = NetworkInterface.GetAllNetworkInterfaces()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ private AmazonECSClient EcsClient
}
}

public EcsServiceDiscovery(ActorSystem system)
// Backward compatibility constructor
public EcsServiceDiscovery(ExtendedActorSystem system)
: this(system, system.Settings.Config.GetConfig(AwsEcsDiscovery.DefaultConfigPath))
{
_settings = AwsEcsDiscovery.Get(system).Settings;
}

public EcsServiceDiscovery(ExtendedActorSystem system, Configuration.Config config)
{
_settings = EcsServiceDiscoverySettings.Create(config);
}

public override async Task<Resolved> Lookup(Lookup lookup, TimeSpan resolveTimeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ namespace Akka.Discovery.AwsApi.Ecs;

public class EcsServiceDiscoveryOptions: IHoconOption
{
private const string FullPath = "akka.discovery.aws-api-ecs";
public string ConfigPath { get; } = "aws-api-ecs";
public string ConfigPath { get; } = AwsEcsDiscovery.DefaultPath;
internal static string FullPath(string path) => $"akka.discovery.{path}";
public Type Class { get; } = typeof(EcsServiceDiscovery);

/// <summary>
/// Mark this plugin as the default plugin to be used by ClusterBootstrap
/// </summary>
public bool IsDefaultPlugin { get; set; } = true;

/// <summary>
/// Optional. The name of the AWS ECS cluster.
/// </summary>
Expand All @@ -35,7 +40,7 @@ public class EcsServiceDiscoveryOptions: IHoconOption
public void Apply(AkkaConfigurationBuilder builder, Setup? setup = null)
{
var sb = new StringBuilder();
sb.AppendLine($"{FullPath} {{");
sb.AppendLine($"{FullPath(ConfigPath)} {{");
sb.AppendLine($"class = {Class.AssemblyQualifiedName!.ToHocon()}");

if (Cluster is { })
Expand All @@ -50,5 +55,13 @@ public void Apply(AkkaConfigurationBuilder builder, Setup? setup = null)
sb.AppendLine("}");

builder.AddHocon(sb.ToString(), HoconAddMode.Prepend);

if(IsDefaultPlugin)
builder.AddHocon($"akka.discovery.method = {ConfigPath}", HoconAddMode.Prepend);

var fallback = AwsEcsDiscovery.DefaultConfiguration()
.GetConfig(AwsEcsDiscovery.DefaultConfigPath)
.MoveTo(FullPath(ConfigPath));
builder.AddHocon(fallback, HoconAddMode.Append);
}
}

0 comments on commit 9c88f33

Please sign in to comment.