Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ public sealed class ConsulDiscoveryOptions

/// <summary>
/// Gets or sets a value indicating whether to register with the port number ASP.NET Core is listening on. Default value: true.
/// <para />
/// This property is ignored when <see cref="Port" /> or <see cref="Scheme" /> is explicitly configured, or when <see cref="UseNetworkInterfaces" /> is
/// <c>true</c>.
/// </summary>
public bool UseAspNetCoreUrls { get; set; } = true;
}
2 changes: 1 addition & 1 deletion src/Discovery/src/Consul/ConfigurationSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
},
"UseAspNetCoreUrls": {
"type": "boolean",
"description": "Gets or sets a value indicating whether to register with the port number ASP.NET Core is listening on. Default value: true."
"description": "Gets or sets a value indicating whether to register with the port number ASP.NET Core is listening on. Default value: true.\n\nThis property is ignored when 'Steeltoe.Discovery.Consul.Configuration.ConsulDiscoveryOptions.Port' or 'Steeltoe.Discovery.Consul.Configuration.ConsulDiscoveryOptions.Scheme' is explicitly configured, or when 'Steeltoe.Discovery.Consul.Configuration.ConsulDiscoveryOptions.UseNetworkInterfaces' is true."
},
"UseNetworkInterfaces": {
"type": "boolean",
Expand Down
47 changes: 21 additions & 26 deletions src/Discovery/src/Consul/PostConfigureConsulDiscoveryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void PostConfigure(string? name, ConsulDiscoveryOptions options)
options.HostName = options.IPAddress;
}

if (options.Port == 0)
if (options is { UseAspNetCoreUrls: true, Port: 0, Scheme: null, UseNetworkInterfaces: false })
{
ICollection<string> addresses = _configuration.GetListenAddresses();
SetSchemeWithPortFromListenAddresses(options, addresses);
Expand All @@ -79,38 +79,33 @@ private string GetServiceName(ConsulDiscoveryOptions options)

private void SetSchemeWithPortFromListenAddresses(ConsulDiscoveryOptions options, IEnumerable<string> listenOnAddresses)
{
// Try to pull some values out of server configuration to override defaults, but only if not using NetUtils.
// If NetUtils are configured, the user probably wants to define their own behavior.
if (options is { UseAspNetCoreUrls: true, Port: 0, Scheme: null })
{
int? listenHttpPort = null;
int? listenHttpsPort = null;
int? listenHttpPort = null;
int? listenHttpsPort = null;

foreach (string address in listenOnAddresses)
{
BindingAddress bindingAddress = BindingAddress.Parse(address);

if (bindingAddress is { Scheme: "http", Port: > 0 } && listenHttpPort == null)
{
listenHttpPort = bindingAddress.Port;
}
else if (bindingAddress is { Scheme: "https", Port: > 0 } && listenHttpsPort == null)
{
listenHttpsPort = bindingAddress.Port;
}
}
foreach (string address in listenOnAddresses)
{
BindingAddress bindingAddress = BindingAddress.Parse(address);

if (listenHttpsPort != null)
if (bindingAddress is { Scheme: "http", Port: > 0 } && listenHttpPort == null)
{
options.Port = listenHttpsPort.Value;
options.Scheme = "https";
listenHttpPort = bindingAddress.Port;
}
else if (listenHttpPort != null)
else if (bindingAddress is { Scheme: "https", Port: > 0 } && listenHttpsPort == null)
{
options.Port = listenHttpPort.Value;
options.Scheme = "http";
listenHttpsPort = bindingAddress.Port;
}
}

if (listenHttpsPort != null)
{
options.Port = listenHttpsPort.Value;
options.Scheme = "https";
}
else if (listenHttpPort != null)
{
options.Port = listenHttpPort.Value;
options.Scheme = "http";
}
}

private string GetInstanceId(ConsulDiscoveryOptions options)
Expand Down
106 changes: 57 additions & 49 deletions src/Discovery/src/Eureka/Configuration/EurekaInstanceOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ public sealed partial class EurekaInstanceOptions
internal const string DefaultStatusPageUrlPath = "/info";
internal const string DefaultHealthCheckUrlPath = "/health";

private bool UseAspNetCoreUrls => !Platform.IsCloudFoundry || IsContainerToContainerMethod() || IsForceHostNameMethod();
private bool IsAnyPortConfigured =>
this is { IsNonSecurePortEnabled: true, NonSecurePort: not null } or { IsSecurePortEnabled: true, SecurePort: not null };

private bool IsSuitableRegistrationMethod => !Platform.IsCloudFoundry || IsContainerToContainerMethod() || IsForceHostNameMethod();
internal bool ShouldSetPortsFromListenAddresses => UseAspNetCoreUrls && IsSuitableRegistrationMethod && !IsAnyPortConfigured;

internal TimeSpan LeaseRenewalInterval => TimeSpan.FromSeconds(LeaseRenewalIntervalInSeconds);
internal TimeSpan LeaseExpirationDuration => TimeSpan.FromSeconds(LeaseExpirationDurationInSeconds);
Expand Down Expand Up @@ -205,6 +209,13 @@ public sealed partial class EurekaInstanceOptions
Name = DataCenterName.MyOwn
};

/// <summary>
/// Gets or sets a value indicating whether to register with the port number(s) ASP.NET Core is listening on. Default value: true.
Comment thread
bart-vmware marked this conversation as resolved.
/// <para />
/// This property is ignored when <see cref="NonSecurePort" /> or <see cref="SecurePort" /> is explicitly configured.
/// </summary>
public bool UseAspNetCoreUrls { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether <see cref="NetworkInterface.GetAllNetworkInterfaces" /> is used to determine <see cref="IPAddress" /> and
/// <see cref="HostName" />. Default value: false.
Expand All @@ -231,72 +242,69 @@ internal bool IsForceHostNameMethod()

internal void SetPortsFromListenAddresses(IEnumerable<string> listenOnAddresses, string source, ILogger<EurekaInstanceOptions> logger)
{
if (UseAspNetCoreUrls)
int? listenHttpPort = null;
int? listenHttpsPort = null;

foreach (string address in listenOnAddresses)
{
int? listenHttpPort = null;
int? listenHttpsPort = null;
BindingAddress bindingAddress = BindingAddress.Parse(address);

foreach (string address in listenOnAddresses)
if (bindingAddress is { Scheme: "http", Port: > 0 } && listenHttpPort == null)
{
BindingAddress bindingAddress = BindingAddress.Parse(address);

if (bindingAddress is { Scheme: "http", Port: > 0 } && listenHttpPort == null)
{
listenHttpPort = bindingAddress.Port;
}
else if (bindingAddress is { Scheme: "https", Port: > 0 } && listenHttpsPort == null)
{
listenHttpsPort = bindingAddress.Port;
}
listenHttpPort = bindingAddress.Port;
}
else if (bindingAddress is { Scheme: "https", Port: > 0 } && listenHttpsPort == null)
{
listenHttpsPort = bindingAddress.Port;
}
}

int? nonSecurePort = IsNonSecurePortEnabled ? NonSecurePort : null;
int? securePort = IsSecurePortEnabled ? SecurePort : null;
int? nonSecurePort = IsNonSecurePortEnabled ? NonSecurePort : null;
int? securePort = IsSecurePortEnabled ? SecurePort : null;

if (nonSecurePort != listenHttpPort)
if (nonSecurePort != listenHttpPort)
{
if (listenHttpPort != null)
{
if (listenHttpPort != null)
if (nonSecurePort == null)
{
if (nonSecurePort == null)
{
LogActivatingNonSecurePort(logger, listenHttpPort, source);
}
else
{
LogChangingNonSecurePort(logger, listenHttpPort, source);
}

NonSecurePort = listenHttpPort.Value;
IsNonSecurePortEnabled = true;
LogActivatingNonSecurePort(logger, listenHttpPort, source);
}
else if (nonSecurePort != null)
else
{
LogDeactivatingNonSecurePort(logger, source);
IsNonSecurePortEnabled = false;
LogChangingNonSecurePort(logger, listenHttpPort, source);
}

NonSecurePort = listenHttpPort.Value;
IsNonSecurePortEnabled = true;
}
else if (nonSecurePort != null)
{
LogDeactivatingNonSecurePort(logger, source);
IsNonSecurePortEnabled = false;
}
}

if (securePort != listenHttpsPort)
if (securePort != listenHttpsPort)
{
if (listenHttpsPort != null)
{
if (listenHttpsPort != null)
if (securePort == null)
{
if (securePort == null)
{
LogActivatingSecurePort(logger, listenHttpsPort, source);
}
else
{
LogChangingSecurePort(logger, listenHttpsPort, source);
}

SecurePort = listenHttpsPort.Value;
IsSecurePortEnabled = true;
LogActivatingSecurePort(logger, listenHttpsPort, source);
}
else if (securePort != null)
else
{
LogDeactivatingSecurePort(logger, source);
IsSecurePortEnabled = false;
LogChangingSecurePort(logger, listenHttpsPort, source);
}

SecurePort = listenHttpsPort.Value;
IsSecurePortEnabled = true;
}
else if (securePort != null)
{
LogDeactivatingSecurePort(logger, source);
IsSecurePortEnabled = false;
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/Discovery/src/Eureka/ConfigurationSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@
"type": "string",
"description": "Gets or sets the relative path to the status page for the instance. The status page URL is then constructed out of the 'Steeltoe.Discovery.Eureka.Configuration.EurekaInstanceOptions.HostName' and the type of communication - secure or non-secure, as specified in 'Steeltoe.Discovery.Eureka.Configuration.EurekaInstanceOptions.SecurePort' and 'Steeltoe.Discovery.Eureka.Configuration.EurekaInstanceOptions.NonSecurePort'. It is normally used for informational purposes for other services to find out about the status of the instance. Users can provide a simple HTML page indicating what the current status of the instance is. Default value: /info."
},
"UseAspNetCoreUrls": {
"type": "boolean",
"description": "Gets or sets a value indicating whether to register with the port number(s) ASP.NET Core is listening on. Default value: true.\n\nThis property is ignored when 'Steeltoe.Discovery.Eureka.Configuration.EurekaInstanceOptions.NonSecurePort' or 'Steeltoe.Discovery.Eureka.Configuration.EurekaInstanceOptions.SecurePort' is explicitly configured."
},
"UseNetworkInterfaces": {
"type": "boolean",
"description": "Gets or sets a value indicating whether 'System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces' is used to determine 'Steeltoe.Discovery.Eureka.Configuration.EurekaInstanceOptions.IPAddress' and 'Steeltoe.Discovery.Eureka.Configuration.EurekaInstanceOptions.HostName'. Default value: false."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void PostConfigure(string? name, EurekaInstanceOptions options)
{
ArgumentNullException.ThrowIfNull(options);

if (_listenState.ListenOnAddresses != null)
if (_listenState.ListenOnAddresses != null && options.ShouldSetPortsFromListenAddresses)
{
options.SetPortsFromListenAddresses(_listenState.ListenOnAddresses, "address features", _optionsLogger);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Discovery/src/Eureka/EurekaServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ private static void ConfigureEurekaClientOptions(IServiceCollection services)

private static void ConfigureEurekaInstanceOptions(IServiceCollection services)
{
DynamicPortAssignmentHostedService.Wire(services);

services.AddOptions<EurekaInstanceOptions>().BindConfiguration(EurekaInstanceOptions.ConfigurationPrefix);
services.AddOptions<InetOptions>().BindConfiguration(InetOptions.ConfigurationPrefix);
services.AddSingleton<IPostConfigureOptions<EurekaInstanceOptions>, PostConfigureEurekaInstanceOptions>();

DynamicPortAssignmentHostedService.Wire(services);
}

private static void AddEurekaServices(IServiceCollection services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private void SetPorts(EurekaInstanceOptions options)
options.IsSecurePortEnabled = true;
}

if (options.NonSecurePort == null && options.SecurePort == null)
if (options.ShouldSetPortsFromListenAddresses)
{
var optionsLogger = _serviceProvider.GetRequiredService<ILogger<EurekaInstanceOptions>>();
ICollection<string> addresses = _configuration.GetListenAddresses();
Expand Down
2 changes: 2 additions & 0 deletions src/Discovery/src/Eureka/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
Steeltoe.Discovery.Eureka.Configuration.EurekaInstanceOptions.UseAspNetCoreUrls.get -> bool
Steeltoe.Discovery.Eureka.Configuration.EurekaInstanceOptions.UseAspNetCoreUrls.set -> void
Loading
Loading