From d080eed9ae6a1a6cc1d5ae8108808347714af787 Mon Sep 17 00:00:00 2001 From: James Rasell Date: Tue, 28 Jun 2022 09:47:28 +0200 Subject: [PATCH] client: fixed a problem calculating a service namespace. (#13493) When calculating a services namespace for registration, the code assumed the first task within the task array would include a service block. This is incorrect as it is possible only a latter task within the array contains a service definition. This change fixes the logic, so we correctly search for a service definition before identifying the namespace. --- .changelog/13493.txt | 3 +++ nomad/structs/alloc.go | 6 +++--- nomad/structs/alloc_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 .changelog/13493.txt diff --git a/.changelog/13493.txt b/.changelog/13493.txt new file mode 100644 index 000000000..0d19a9938 --- /dev/null +++ b/.changelog/13493.txt @@ -0,0 +1,3 @@ +```release-note:bug +client: Fixed a problem calculating a services namespace +``` diff --git a/nomad/structs/alloc.go b/nomad/structs/alloc.go index a02774773..2f5c0cfa9 100644 --- a/nomad/structs/alloc.go +++ b/nomad/structs/alloc.go @@ -43,9 +43,9 @@ func (a *Allocation) ServiceProviderNamespace() string { } } - if len(tg.Tasks) > 0 { - if len(tg.Tasks[0].Services) > 0 { - switch tg.Tasks[0].Services[0].Provider { + for _, task := range tg.Tasks { + if len(task.Services) > 0 { + switch task.Services[0].Provider { case ServiceProviderNomad: return a.Job.Namespace default: diff --git a/nomad/structs/alloc_test.go b/nomad/structs/alloc_test.go index 92bc61615..bbb874a39 100644 --- a/nomad/structs/alloc_test.go +++ b/nomad/structs/alloc_test.go @@ -103,6 +103,34 @@ func Test_Allocation_ServiceProviderNamespace(t *testing.T) { expectedOutput: "platform", name: "nomad task service", }, + { + inputAllocation: &Allocation{ + Job: &Job{ + Namespace: "platform", + TaskGroups: []*TaskGroup{ + { + Name: "test-group", + Tasks: []*Task{ + { + Name: "task1", + }, + { + Name: "task2", + Services: []*Service{ + { + Provider: ServiceProviderNomad, + }, + }, + }, + }, + }, + }, + }, + TaskGroup: "test-group", + }, + expectedOutput: "platform", + name: "multiple tasks with service not in first", + }, } for _, tc := range testCases {