Skip to content

Commit 1fabefd

Browse files
authored
interpolate network.dns block on client (#12021)
1 parent b775a73 commit 1fabefd

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

.changelog/12021.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
client: Allow interpolation of the network.dns block
3+
```

client/taskenv/network.go

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
//
1010
// Current interoperable fields:
1111
// - Hostname
12+
// - DNS
1213
func InterpolateNetworks(taskEnv *TaskEnv, networks structs.Networks) structs.Networks {
1314

1415
// 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
2324
// Iterate the copy and perform the interpolation.
2425
for i := range interpolated {
2526
interpolated[i].Hostname = taskEnv.ReplaceEnv(interpolated[i].Hostname)
27+
if interpolated[i].DNS != nil {
28+
interpolated[i].DNS.Servers = taskEnv.ParseAndReplace(interpolated[i].DNS.Servers)
29+
interpolated[i].DNS.Searches = taskEnv.ParseAndReplace(interpolated[i].DNS.Searches)
30+
interpolated[i].DNS.Options = taskEnv.ParseAndReplace(interpolated[i].DNS.Options)
31+
}
2632
}
2733

2834
return interpolated

client/taskenv/network_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,50 @@ func Test_InterpolateNetworks(t *testing.T) {
3434
},
3535
name: "interpolated hostname",
3636
},
37+
{
38+
inputTaskEnv: testEnv,
39+
inputNetworks: structs.Networks{
40+
{
41+
DNS: &structs.DNSConfig{
42+
Servers: []string{"127.0.0.1"},
43+
Options: []string{"some-opt"},
44+
Searches: []string{"example.com"},
45+
},
46+
},
47+
},
48+
expectedOutputNetworks: structs.Networks{
49+
{
50+
DNS: &structs.DNSConfig{
51+
Servers: []string{"127.0.0.1"},
52+
Options: []string{"some-opt"},
53+
Searches: []string{"example.com"},
54+
},
55+
},
56+
},
57+
name: "non-interpolated dns servers",
58+
},
59+
{
60+
inputTaskEnv: testEnv,
61+
inputNetworks: structs.Networks{
62+
{
63+
DNS: &structs.DNSConfig{
64+
Servers: []string{"${foo}"},
65+
Options: []string{"${foo}-opt"},
66+
Searches: []string{"${foo}.example.com"},
67+
},
68+
},
69+
},
70+
expectedOutputNetworks: structs.Networks{
71+
{
72+
DNS: &structs.DNSConfig{
73+
Servers: []string{"bar"},
74+
Options: []string{"bar-opt"},
75+
Searches: []string{"bar.example.com"},
76+
},
77+
},
78+
},
79+
name: "interpolated dns servers",
80+
},
3781
}
3882

3983
for _, tc := range testCases {

website/content/docs/job-specification/network.mdx

+2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ The label of the port is just text - it has no special meaning to Nomad.
120120
- `searches` `(array<string>: nil)` - Sets the search list for hostname lookup
121121
- `options` `(array<string>: nil)` - Sets internal resolver variables.
122122

123+
These parameters support [interpolation](/docs/runtime/interpolation).
124+
123125
## `network` Examples
124126

125127
The following examples only show the `network` stanzas. Remember that the

0 commit comments

Comments
 (0)