From 8ad8e1095ac521f11410be8bb3d1760394ded0b5 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Thu, 8 Jul 2021 09:18:59 -0500 Subject: [PATCH] consul/connect: remove sidecar proxy before removing parent service This PR will have Nomad de-register a sidecar proxy service before attempting to de-register the parent service. Otherwise, Consul will emit a warning and an error. Fixes #10845 --- .changelog/10873.txt | 3 +++ command/agent/consul/service_client.go | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 .changelog/10873.txt diff --git a/.changelog/10873.txt b/.changelog/10873.txt new file mode 100644 index 000000000000..edafe0d7e755 --- /dev/null +++ b/.changelog/10873.txt @@ -0,0 +1,3 @@ +```release-note:bug +consul/connect: Fixed a bug where service deregistered before connect sidecar +``` diff --git a/command/agent/consul/service_client.go b/command/agent/consul/service_client.go index 3a22848f0984..246f38234d56 100644 --- a/command/agent/consul/service_client.go +++ b/command/agent/consul/service_client.go @@ -821,8 +821,23 @@ func (c *ServiceClient) sync(reason syncReason) error { continue } - // Unknown Nomad managed service; kill + // Get the Consul namespace this service is in. ns := servicesInConsul[id].Namespace + + // If this service has a sidecar, we need to remove the sidecar first, + // otherwise Consul will produce a warning and an error when removing + // the parent service. + // + // The sidecar is not tracked on the Nomad side; it was registered + // implicitly through the parent service. + if sidecar := getNomadSidecar(id, servicesInConsul); sidecar != nil { + if err := c.agentAPI.ServiceDeregisterOpts(sidecar.ID, &api.QueryOptions{Namespace: ns}); err != nil { + metrics.IncrCounter([]string{"client", "consul", "sync_failure"}, 1) + return err + } + } + + // Remove the unwanted service. if err := c.agentAPI.ServiceDeregisterOpts(id, &api.QueryOptions{Namespace: ns}); err != nil { if isOldNomadService(id) { // Don't hard-fail on old entries. See #3620