-
Notifications
You must be signed in to change notification settings - Fork 756
Fallback to previous DNS service discovery #10140
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/Microsoft.Extensions.ServiceDiscovery.Dns/FallbackDnsResolver.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using DnsClient; | ||
| using DnsClient.Protocol; | ||
| using Microsoft.Extensions.Options; | ||
| using Microsoft.Extensions.ServiceDiscovery.Dns.Resolver; | ||
|
|
||
| namespace Microsoft.Extensions.ServiceDiscovery.Dns; | ||
|
|
||
| internal sealed class FallbackDnsResolver : IDnsResolver | ||
| { | ||
| private readonly LookupClient _lookupClient; | ||
| private readonly IOptionsMonitor<DnsServiceEndpointProviderOptions> _options; | ||
| private readonly TimeProvider _timeProvider; | ||
|
|
||
| public FallbackDnsResolver(LookupClient lookupClient, IOptionsMonitor<DnsServiceEndpointProviderOptions> options, TimeProvider timeProvider) | ||
| { | ||
| _lookupClient = lookupClient; | ||
| _options = options; | ||
| _timeProvider = timeProvider; | ||
| } | ||
|
|
||
| private TimeSpan DefaultRefreshPeriod => _options.CurrentValue.DefaultRefreshPeriod; | ||
|
|
||
| public async ValueTask<AddressResult[]> ResolveIPAddressesAsync(string name, CancellationToken cancellationToken = default) | ||
| { | ||
| DateTime expiresAt = _timeProvider.GetUtcNow().DateTime.Add(DefaultRefreshPeriod); | ||
| var addresses = await System.Net.Dns.GetHostAddressesAsync(name, cancellationToken).ConfigureAwait(false); | ||
|
|
||
| var results = new AddressResult[addresses.Length]; | ||
|
|
||
| for (int i = 0; i < addresses.Length; i++) | ||
| { | ||
| results[i] = new AddressResult | ||
| { | ||
| Address = addresses[i], | ||
| ExpiresAt = expiresAt | ||
| }; | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| public async ValueTask<ServiceResult[]> ResolveServiceAsync(string name, CancellationToken cancellationToken = default) | ||
| { | ||
| DateTime now = _timeProvider.GetUtcNow().DateTime; | ||
| var queryResult = await _lookupClient.QueryAsync(name, DnsClient.QueryType.SRV, cancellationToken: cancellationToken).ConfigureAwait(false); | ||
| if (queryResult.HasError) | ||
| { | ||
| throw CreateException(name, queryResult.ErrorMessage); | ||
| } | ||
|
|
||
| var lookupMapping = new Dictionary<string, List<AddressResult>>(); | ||
| foreach (var record in queryResult.Additionals.OfType<AddressRecord>()) | ||
| { | ||
| if (!lookupMapping.TryGetValue(record.DomainName, out var addresses)) | ||
| { | ||
| addresses = new List<AddressResult>(); | ||
| lookupMapping[record.DomainName] = addresses; | ||
| } | ||
|
|
||
| addresses.Add(new AddressResult | ||
| { | ||
| Address = record.Address, | ||
| ExpiresAt = now.Add(TimeSpan.FromSeconds(record.TimeToLive)) | ||
| }); | ||
| } | ||
|
|
||
| var srvRecords = queryResult.Answers.OfType<SrvRecord>().ToList(); | ||
|
|
||
| var results = new ServiceResult[srvRecords.Count]; | ||
| for (int i = 0; i < srvRecords.Count; i++) | ||
| { | ||
| var record = srvRecords[i]; | ||
|
|
||
| results[i] = new ServiceResult | ||
| { | ||
| ExpiresAt = now.Add(TimeSpan.FromSeconds(record.TimeToLive)), | ||
| Priority = record.Priority, | ||
| Weight = record.Weight, | ||
| Port = record.Port, | ||
| Target = record.Target, | ||
| Addresses = lookupMapping.TryGetValue(record.Target, out var addresses) | ||
| ? addresses.ToArray() | ||
| : Array.Empty<AddressResult>() | ||
| }; | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| private static InvalidOperationException CreateException(string dnsName, string errorMessage) | ||
| { | ||
| var msg = errorMessage switch | ||
| { | ||
| { Length: > 0 } => $"No DNS SRV records were found for DNS name '{dnsName}': {errorMessage}.", | ||
| _ => $"No DNS SRV records were found for DNS name '{dnsName}'", | ||
| }; | ||
| return new InvalidOperationException(msg); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
src/Microsoft.Extensions.ServiceDiscovery.Dns/Resolver/IDnsResolver.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,10 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Net.Sockets; | ||
|
|
||
| namespace Microsoft.Extensions.ServiceDiscovery.Dns.Resolver; | ||
|
|
||
| internal interface IDnsResolver | ||
| { | ||
| ValueTask<AddressResult[]> ResolveIPAddressesAsync(string name, AddressFamily addressFamily, CancellationToken cancellationToken = default); | ||
BrennanConroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ValueTask<AddressResult[]> ResolveIPAddressesAsync(string name, CancellationToken cancellationToken = default); | ||
| ValueTask<ServiceResult[]> ResolveServiceAsync(string name, CancellationToken cancellationToken = default); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,11 +46,38 @@ public static IServiceCollection AddDnsSrvServiceEndpointProvider(this IServiceC | |
| ArgumentNullException.ThrowIfNull(configureOptions); | ||
|
|
||
| services.AddServiceDiscoveryCore(); | ||
|
|
||
| if (!GetDnsClientFallbackFlag()) | ||
| { | ||
| services.TryAddSingleton<IDnsResolver, DnsResolver>(); | ||
| } | ||
| else | ||
| { | ||
| services.TryAddSingleton<IDnsResolver, FallbackDnsResolver>(); | ||
| services.TryAddSingleton<DnsClient.LookupClient>(); | ||
| } | ||
|
|
||
| services.TryAddSingleton<IDnsResolver, DnsResolver>(); | ||
BrennanConroy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| services.AddSingleton<IServiceEndpointProviderFactory, DnsSrvServiceEndpointProviderFactory>(); | ||
| var options = services.AddOptions<DnsSrvServiceEndpointProviderOptions>(); | ||
| options.Configure(o => configureOptions?.Invoke(o)); | ||
| return services; | ||
|
|
||
| static bool GetDnsClientFallbackFlag() | ||
| { | ||
| if (AppContext.TryGetSwitch("Microsoft.Extensions.ServiceDiscovery.Dns.UseDnsClientFallback", out var value)) | ||
| { | ||
| return value; | ||
| } | ||
|
|
||
| var envVar = Environment.GetEnvironmentVariable("MICROSOFT_EXTENSIONS_SERVICE_DISCOVERY_DNS_USE_DNSCLIENT_FALLBACK"); | ||
| if (envVar is not null && (envVar.Equals("true", StringComparison.OrdinalIgnoreCase) || envVar.Equals("1"))) | ||
|
Comment on lines
+67
to
+73
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't find an example how you do feature flags in Aspire, so I used the same mechanism as we use in .NET runtime. I am open to name suggestions, I honestly could not think of a better name. |
||
| { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We checked
CNameRecordbefore:foreach (var record in result.Additionals.Where(x => x is AddressRecord or CNameRecord))Is that something we still should handle here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RFC explicitly prohibits aliases in the target of the SRV record
So we shouldn't need to look for CNAME records.