From d62213a13536d9790720793dfbb83a204ea8bec0 Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Wed, 1 Nov 2023 10:11:54 -0400 Subject: [PATCH] consul: fix lookups of default cluster across upgrades (#18945) Allocations that were created before Nomad 1.7 will not have the cluster field set for their Consul blocks. While this can be corrected server-side, that doesn't help allocations already on clients. --- client/allocrunner/consul_hook.go | 7 ++++++- client/allocrunner/group_service_hook.go | 6 +++++- client/allocrunner/taskrunner/service_hook.go | 6 +++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/client/allocrunner/consul_hook.go b/client/allocrunner/consul_hook.go index 4c3696983..92670ea9a 100644 --- a/client/allocrunner/consul_hook.go +++ b/client/allocrunner/consul_hook.go @@ -180,7 +180,12 @@ func (h *consulHook) prepareConsulTokensForServices(services []*structs.Service, continue } - if err := h.getConsulTokens(service.Cluster, service.Identity.Name, tokens, req); err != nil { + cluster := service.Cluster + if cluster == "" { + cluster = structs.ConsulDefaultCluster + } + + if err := h.getConsulTokens(cluster, service.Identity.Name, tokens, req); err != nil { mErr.Errors = append(mErr.Errors, err) continue } diff --git a/client/allocrunner/group_service_hook.go b/client/allocrunner/group_service_hook.go index 75e7ddd5b..603cd9a3a 100644 --- a/client/allocrunner/group_service_hook.go +++ b/client/allocrunner/group_service_hook.go @@ -267,7 +267,11 @@ func (h *groupServiceHook) getWorkloadServicesLocked() *serviceregistration.Work tokens := map[string]string{} for _, service := range h.services { - if token, ok := allocTokens[service.Cluster][service.MakeUniqueIdentityName()]; ok { + cluster := service.Cluster + if cluster == "" { + cluster = structs.ConsulDefaultCluster + } + if token, ok := allocTokens[cluster][service.MakeUniqueIdentityName()]; ok { tokens[service.Name] = token } } diff --git a/client/allocrunner/taskrunner/service_hook.go b/client/allocrunner/taskrunner/service_hook.go index 7987a0383..2aaa15151 100644 --- a/client/allocrunner/taskrunner/service_hook.go +++ b/client/allocrunner/taskrunner/service_hook.go @@ -234,7 +234,11 @@ func (h *serviceHook) getWorkloadServices() *serviceregistration.WorkloadService tokens := map[string]string{} for _, service := range h.services { - if token, ok := allocTokens[service.Cluster][service.MakeUniqueIdentityName()]; ok { + cluster := service.Cluster + if cluster == "" { + cluster = structs.ConsulDefaultCluster + } + if token, ok := allocTokens[cluster][service.MakeUniqueIdentityName()]; ok { tokens[service.Name] = token } }