Skip to content

Commit

Permalink
interpolate network.dns block on client (#12021)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvx committed Feb 16, 2022
1 parent b775a73 commit 1fabefd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/12021.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
client: Allow interpolation of the network.dns block
```
6 changes: 6 additions & 0 deletions client/taskenv/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
44 changes: 44 additions & 0 deletions client/taskenv/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions website/content/docs/job-specification/network.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1fabefd

Please sign in to comment.