Skip to content

Commit fa3a2b7

Browse files
committed
consul: fixup expected consul tagged_addresses when using ipv6
This PR is a continuation of #14917, where we missed the ipv6 cases. Consul auto-inserts tagged_addresses for keys - lan_ipv4 - wan_ipv4 - lan_ipv6 - wan_ipv6 even though the service registration coming from Nomad does not contain such elements. When doing the differential between services Nomad expects to be registered vs. the services actually registered into Consul, we must first purge these automatically inserted tagged_addresses if they do not exist in the Nomad view of the Consul service.
1 parent 79afeee commit fa3a2b7

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

.changelog/15411.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
consul: Fixed a bug where services would continuously re-register when using ipv6
3+
```

command/agent/consul/service_client.go

+6
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ func maybeTweakTaggedAddresses(wanted *api.AgentServiceRegistration, existing *a
225225
if _, exists := wanted.TaggedAddresses["wan_ipv4"]; !exists {
226226
delete(existing.TaggedAddresses, "wan_ipv4")
227227
}
228+
if _, exists := wanted.TaggedAddresses["lan_ipv6"]; !exists {
229+
delete(existing.TaggedAddresses, "lan_ipv6")
230+
}
231+
if _, exists := wanted.TaggedAddresses["wan_ipv6"]; !exists {
232+
delete(existing.TaggedAddresses, "wan_ipv6")
233+
}
228234
}
229235
}
230236

command/agent/consul/service_client_test.go

+107
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,115 @@ import (
1313
"github.com/hashicorp/nomad/nomad/structs"
1414
"github.com/shoenig/test/must"
1515
"github.com/stretchr/testify/require"
16+
"golang.org/x/exp/maps"
1617
)
1718

19+
func TestSyncLogic_maybeTweakTaggedAddresses(t *testing.T) {
20+
ci.Parallel(t)
21+
22+
cases := []struct {
23+
name string
24+
wanted map[string]api.ServiceAddress
25+
existing map[string]api.ServiceAddress
26+
id string
27+
exp []string
28+
}{
29+
{
30+
name: "not managed by nomad",
31+
id: "_nomad-other-hello",
32+
wanted: map[string]api.ServiceAddress{
33+
// empty
34+
},
35+
existing: map[string]api.ServiceAddress{
36+
"lan_ipv4": {},
37+
"wan_ipv4": {},
38+
"custom": {},
39+
},
40+
exp: []string{"lan_ipv4", "wan_ipv4", "custom"},
41+
},
42+
{
43+
name: "remove defaults",
44+
id: "_nomad-task-hello",
45+
wanted: map[string]api.ServiceAddress{
46+
"lan_custom": {},
47+
"wan_custom": {},
48+
},
49+
existing: map[string]api.ServiceAddress{
50+
"lan_ipv4": {},
51+
"wan_ipv4": {},
52+
"lan_ipv6": {},
53+
"wan_ipv6": {},
54+
"lan_custom": {},
55+
"wan_custom": {},
56+
},
57+
exp: []string{"lan_custom", "wan_custom"},
58+
},
59+
{
60+
name: "overridden defaults",
61+
id: "_nomad-task-hello",
62+
wanted: map[string]api.ServiceAddress{
63+
"lan_ipv4": {},
64+
"wan_ipv4": {},
65+
"lan_ipv6": {},
66+
"wan_ipv6": {},
67+
"custom": {},
68+
},
69+
existing: map[string]api.ServiceAddress{
70+
"lan_ipv4": {},
71+
"wan_ipv4": {},
72+
"lan_ipv6": {},
73+
"wan_ipv6": {},
74+
"custom": {},
75+
},
76+
exp: []string{"lan_ipv4", "wan_ipv4", "lan_ipv6", "wan_ipv6", "custom"},
77+
},
78+
{
79+
name: "applies to nomad client",
80+
id: "_nomad-client-12345",
81+
wanted: map[string]api.ServiceAddress{
82+
"custom": {},
83+
},
84+
existing: map[string]api.ServiceAddress{
85+
"lan_ipv4": {},
86+
"wan_ipv4": {},
87+
"lan_ipv6": {},
88+
"wan_ipv6": {},
89+
"custom": {},
90+
},
91+
exp: []string{"custom"},
92+
},
93+
{
94+
name: "applies to nomad server",
95+
id: "_nomad-server-12345",
96+
wanted: map[string]api.ServiceAddress{
97+
"custom": {},
98+
},
99+
existing: map[string]api.ServiceAddress{
100+
"lan_ipv4": {},
101+
"wan_ipv4": {},
102+
"lan_ipv6": {},
103+
"wan_ipv6": {},
104+
"custom": {},
105+
},
106+
exp: []string{"custom"},
107+
},
108+
}
109+
110+
for _, tc := range cases {
111+
t.Run(tc.name, func(t *testing.T) {
112+
asr := &api.AgentServiceRegistration{
113+
ID: tc.id,
114+
TaggedAddresses: maps.Clone(tc.wanted),
115+
}
116+
as := &api.AgentService{
117+
TaggedAddresses: maps.Clone(tc.existing),
118+
}
119+
maybeTweakTaggedAddresses(asr, as)
120+
must.MapContainsKeys(t, as.TaggedAddresses, tc.exp)
121+
})
122+
}
123+
}
124+
18125
func TestSyncLogic_agentServiceUpdateRequired(t *testing.T) {
19126
ci.Parallel(t)
20127

0 commit comments

Comments
 (0)