Skip to content

Commit 52247d3

Browse files
author
Preetha
authored
Merge pull request #4365 from hashicorp/b-consul-server-resync
Fix consul server resync when server and client point to the same Consul agent
2 parents acb9d12 + a7668cd commit 52247d3

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

client/task_runner_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func testTaskRunnerFromAlloc(t *testing.T, restarts bool, alloc *structs.Allocat
143143

144144
vclient := vaultclient.NewMockVaultClient()
145145
cclient := consul.NewMockAgent()
146-
serviceClient := consul.NewServiceClient(cclient, logger)
146+
serviceClient := consul.NewServiceClient(cclient, logger, true)
147147
go serviceClient.Run()
148148
tr := NewTaskRunner(logger, conf, db, upd.Update, taskDir, alloc, task, vclient, serviceClient)
149149
if !restarts {
@@ -1860,7 +1860,7 @@ func TestTaskRunner_CheckWatcher_Restart(t *testing.T) {
18601860
// backed by a mock consul whose checks are always unhealthy.
18611861
consulAgent := consul.NewMockAgent()
18621862
consulAgent.SetStatus("critical")
1863-
consulClient := consul.NewServiceClient(consulAgent, ctx.tr.logger)
1863+
consulClient := consul.NewServiceClient(consulAgent, ctx.tr.logger, true)
18641864
go consulClient.Run()
18651865
defer consulClient.Shutdown()
18661866

command/agent/agent.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ func (a *Agent) setupConsul(consulConfig *config.ConsulConfig) error {
958958
a.consulCatalog = client.Catalog()
959959

960960
// Create Consul Service client for service advertisement and checks.
961-
a.consulService = consul.NewServiceClient(client.Agent(), a.logger)
961+
a.consulService = consul.NewServiceClient(client.Agent(), a.logger, a.Client() != nil)
962962

963963
// Run the Consul service client's sync'ing main loop
964964
go a.consulService.Run()

command/agent/consul/client.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,17 @@ type ServiceClient struct {
232232

233233
// checkWatcher restarts checks that are unhealthy.
234234
checkWatcher *checkWatcher
235+
236+
// isClientAgent specifies whether this Consul client is being used
237+
// by a Nomad client.
238+
isClientAgent bool
235239
}
236240

237241
// NewServiceClient creates a new Consul ServiceClient from an existing Consul API
238-
// Client and logger.
239-
func NewServiceClient(consulClient AgentAPI, logger *log.Logger) *ServiceClient {
242+
// Client, logger and takes whether the client is being used by a Nomad Client agent.
243+
// When being used by a Nomad client, this Consul client reconciles all services and
244+
// checks created by Nomad on behalf of running tasks.
245+
func NewServiceClient(consulClient AgentAPI, logger *log.Logger, isNomadClient bool) *ServiceClient {
240246
return &ServiceClient{
241247
client: consulClient,
242248
logger: logger,
@@ -255,6 +261,7 @@ func NewServiceClient(consulClient AgentAPI, logger *log.Logger) *ServiceClient
255261
agentServices: make(map[string]struct{}),
256262
agentChecks: make(map[string]struct{}),
257263
checkWatcher: newCheckWatcher(logger, consulClient),
264+
isClientAgent: isNomadClient,
258265
}
259266
}
260267

@@ -433,7 +440,12 @@ func (c *ServiceClient) sync() error {
433440
// Known service, skip
434441
continue
435442
}
436-
if !isNomadService(id) {
443+
444+
// Ignore if this is not a Nomad managed service. Also ignore
445+
// Nomad managed services if this is not a client agent.
446+
// This is to prevent server agents from removing services
447+
// registered by client agents
448+
if !isNomadService(id) || !c.isClientAgent {
437449
// Not managed by Nomad, skip
438450
continue
439451
}
@@ -470,7 +482,12 @@ func (c *ServiceClient) sync() error {
470482
// Known check, leave it
471483
continue
472484
}
473-
if !isNomadService(check.ServiceID) {
485+
486+
// Ignore if this is not a Nomad managed check. Also ignore
487+
// Nomad managed checks if this is not a client agent.
488+
// This is to prevent server agents from removing checks
489+
// registered by client agents
490+
if !isNomadService(check.ServiceID) || !c.isClientAgent {
474491
// Service not managed by Nomad, skip
475492
continue
476493
}

command/agent/consul/int_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func TestConsul_Integration(t *testing.T) {
142142
consulClient, err := consulapi.NewClient(consulConfig)
143143
assert.Nil(err)
144144

145-
serviceClient := consul.NewServiceClient(consulClient.Agent(), logger)
145+
serviceClient := consul.NewServiceClient(consulClient.Agent(), logger, true)
146146
defer serviceClient.Shutdown() // just-in-case cleanup
147147
consulRan := make(chan struct{})
148148
go func() {

command/agent/consul/unit_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func setupFake(t *testing.T) *testFakeCtx {
117117
fc := NewMockAgent()
118118
tt := testTask()
119119
return &testFakeCtx{
120-
ServiceClient: NewServiceClient(fc, testlog.Logger(t)),
120+
ServiceClient: NewServiceClient(fc, testlog.Logger(t), true),
121121
FakeConsul: fc,
122122
Task: tt,
123123
MockExec: tt.DriverExec.(*mockExec),

0 commit comments

Comments
 (0)