interpolate network.dns block on client (#12021)

This commit is contained in:
Tiernan 2022-02-16 23:39:44 +10:00 committed by GitHub
parent 27bb2da5ee
commit c30b4617aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 0 deletions

3
.changelog/12021.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
client: Allow interpolation of the network.dns block
```

View File

@ -9,6 +9,7 @@ import (
//
// Current interoperable fields:
// - Hostname
// - DNS
func InterpolateNetworks(taskEnv *TaskEnv, networks structs.Networks) structs.Networks {
// Guard against not having a valid taskEnv. This can be the case if the
@ -23,6 +24,11 @@ func InterpolateNetworks(taskEnv *TaskEnv, networks structs.Networks) structs.Ne
// Iterate the copy and perform the interpolation.
for i := range interpolated {
interpolated[i].Hostname = taskEnv.ReplaceEnv(interpolated[i].Hostname)
if interpolated[i].DNS != nil {
interpolated[i].DNS.Servers = taskEnv.ParseAndReplace(interpolated[i].DNS.Servers)
interpolated[i].DNS.Searches = taskEnv.ParseAndReplace(interpolated[i].DNS.Searches)
interpolated[i].DNS.Options = taskEnv.ParseAndReplace(interpolated[i].DNS.Options)
}
}
return interpolated

View File

@ -34,6 +34,50 @@ func Test_InterpolateNetworks(t *testing.T) {
},
name: "interpolated hostname",
},
{
inputTaskEnv: testEnv,
inputNetworks: structs.Networks{
{
DNS: &structs.DNSConfig{
Servers: []string{"127.0.0.1"},
Options: []string{"some-opt"},
Searches: []string{"example.com"},
},
},
},
expectedOutputNetworks: structs.Networks{
{
DNS: &structs.DNSConfig{
Servers: []string{"127.0.0.1"},
Options: []string{"some-opt"},
Searches: []string{"example.com"},
},
},
},
name: "non-interpolated dns servers",
},
{
inputTaskEnv: testEnv,
inputNetworks: structs.Networks{
{
DNS: &structs.DNSConfig{
Servers: []string{"${foo}"},
Options: []string{"${foo}-opt"},
Searches: []string{"${foo}.example.com"},
},
},
},
expectedOutputNetworks: structs.Networks{
{
DNS: &structs.DNSConfig{
Servers: []string{"bar"},
Options: []string{"bar-opt"},
Searches: []string{"bar.example.com"},
},
},
},
name: "interpolated dns servers",
},
}
for _, tc := range testCases {

View File

@ -120,6 +120,8 @@ The label of the port is just text - it has no special meaning to Nomad.
- `searches` `(array<string>: nil)` - Sets the search list for hostname lookup
- `options` `(array<string>: nil)` - Sets internal resolver variables.
These parameters support [interpolation](/docs/runtime/interpolation).
## `network` Examples
The following examples only show the `network` stanzas. Remember that the