-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DNS recursor strategy option (#10611)
This change adds a new `dns_config.recursor_strategy` option which controls how Consul queries DNS resolvers listed in the `recursors` config option. The supported options are `sequential` (default), and `random`. Closes #8807 Co-authored-by: Blake Covarrubias <blake@covarrubi.as> Co-authored-by: Priyanka Sengupta <psengupta@flatiron.com>
- Loading branch information
Showing
11 changed files
with
127 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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,3 @@ | ||
```release-note:improvement | ||
config: add `dns_config.recursor_strategy` flag to control the order which DNS recursors are queried | ||
``` |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,10 +1,34 @@ | ||
package dns | ||
|
||
import "regexp" | ||
import ( | ||
"math/rand" | ||
"regexp" | ||
) | ||
|
||
// MaxLabelLength is the maximum length for a name that can be used in DNS. | ||
const MaxLabelLength = 63 | ||
|
||
// InvalidNameRe is a regex that matches characters which can not be included in | ||
// a DNS name. | ||
var InvalidNameRe = regexp.MustCompile(`[^A-Za-z0-9\\-]+`) | ||
|
||
type RecursorStrategy string | ||
|
||
const ( | ||
RecursorStrategySequential RecursorStrategy = "sequential" | ||
RecursorStrategyRandom RecursorStrategy = "random" | ||
) | ||
|
||
func (s RecursorStrategy) Indexes(max int) []int { | ||
switch s { | ||
case RecursorStrategyRandom: | ||
return rand.Perm(max) | ||
default: | ||
idxs := make([]int, max) | ||
for i := range idxs { | ||
idxs[i] = i | ||
} | ||
return idxs | ||
|
||
} | ||
} |
This file contains 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,40 @@ | ||
package dns | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/consul/sdk/testutil/retry" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDNS_Recursor_StrategyRandom(t *testing.T) { | ||
configuredRecursors := []string{"1.1.1.1", "8.8.4.4", "8.8.8.8"} | ||
recursorStrategy := RecursorStrategy("random") | ||
|
||
retry.RunWith(&retry.Counter{Count: 5}, t, func(r *retry.R) { | ||
recursorsToQuery := make([]string, 0) | ||
for _, idx := range recursorStrategy.Indexes(len(configuredRecursors)) { | ||
recursorsToQuery = append(recursorsToQuery, configuredRecursors[idx]) | ||
} | ||
|
||
// Ensure the slices contain the same elements | ||
require.ElementsMatch(t, configuredRecursors, recursorsToQuery) | ||
|
||
// Ensure the elements are not in the same order | ||
require.NotEqual(r, configuredRecursors, recursorsToQuery) | ||
}) | ||
} | ||
|
||
func TestDNS_Recursor_StrategySequential(t *testing.T) { | ||
expectedRecursors := []string{"1.1.1.1", "8.8.4.4", "8.8.8.8"} | ||
recursorStrategy := RecursorStrategy("sequential") | ||
|
||
recursorsToQuery := make([]string, 0) | ||
for _, idx := range recursorStrategy.Indexes(len(expectedRecursors)) { | ||
recursorsToQuery = append(recursorsToQuery, expectedRecursors[idx]) | ||
} | ||
|
||
// The list of recursors should match the order in which they were defined | ||
// in the configuration | ||
require.Equal(t, recursorsToQuery, expectedRecursors) | ||
} |
This file contains 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
This file contains 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