@@ -232,11 +232,17 @@ type ServiceClient struct {
232
232
233
233
// checkWatcher restarts checks that are unhealthy.
234
234
checkWatcher * checkWatcher
235
+
236
+ // isClientAgent specifies whether this Consul client is being used
237
+ // by a Nomad client.
238
+ isClientAgent bool
235
239
}
236
240
237
241
// 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 {
240
246
return & ServiceClient {
241
247
client : consulClient ,
242
248
logger : logger ,
@@ -255,6 +261,7 @@ func NewServiceClient(consulClient AgentAPI, logger *log.Logger) *ServiceClient
255
261
agentServices : make (map [string ]struct {}),
256
262
agentChecks : make (map [string ]struct {}),
257
263
checkWatcher : newCheckWatcher (logger , consulClient ),
264
+ isClientAgent : isNomadClient ,
258
265
}
259
266
}
260
267
@@ -433,7 +440,12 @@ func (c *ServiceClient) sync() error {
433
440
// Known service, skip
434
441
continue
435
442
}
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 {
437
449
// Not managed by Nomad, skip
438
450
continue
439
451
}
@@ -470,7 +482,12 @@ func (c *ServiceClient) sync() error {
470
482
// Known check, leave it
471
483
continue
472
484
}
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 {
474
491
// Service not managed by Nomad, skip
475
492
continue
476
493
}
0 commit comments