diff --git a/nomad/structs/diff.go b/nomad/structs/diff.go index 24d1f242e14b..8919760d4435 100644 --- a/nomad/structs/diff.go +++ b/nomad/structs/diff.go @@ -646,10 +646,10 @@ func serviceDiffs(old, new []*Service, contextual bool) []*ObjectDiff { oldMap := make(map[string]*Service, len(old)) newMap := make(map[string]*Service, len(new)) for _, o := range old { - oldMap[o.Name] = o + oldMap[o.Name+o.PortLabel] = o } for _, n := range new { - newMap[n.Name] = n + newMap[n.Name+n.PortLabel] = n } var diffs []*ObjectDiff diff --git a/nomad/structs/diff_test.go b/nomad/structs/diff_test.go index 6eabcdc8b004..83132d8bc1ea 100644 --- a/nomad/structs/diff_test.go +++ b/nomad/structs/diff_test.go @@ -3885,6 +3885,74 @@ func TestTaskGroupDiff(t *testing.T) { }, }, }, + + { + TestCase: "TaskGroup service added with same name", + Contextual: true, + Old: &TaskGroup{ + Services: []*Service{ + { + Name: "foo", + PortLabel: "label1", + }, + }, + }, + + New: &TaskGroup{ + Services: []*Service{ + { + Name: "foo", + PortLabel: "label1", + }, + { + Name: "foo", + PortLabel: "label2", + }, + }, + }, + Expected: &TaskGroupDiff{ + Type: DiffTypeEdited, + Objects: []*ObjectDiff{ + { + Type: DiffTypeAdded, + Name: "Service", + Fields: []*FieldDiff{ + { + Type: DiffTypeNone, + Name: "AddressMode", + }, + { + Type: DiffTypeAdded, + Name: "EnableTagOverride", + New: "false", + }, + { + Type: DiffTypeAdded, + Name: "Name", + New: "foo", + }, + { + Type: DiffTypeNone, + Name: "Namespace", + }, + { + Type: DiffTypeNone, + Name: "OnUpdate", + }, + { + Type: DiffTypeAdded, + Name: "PortLabel", + New: "label2", + }, + { + Type: DiffTypeNone, + Name: "TaskName", + }, + }, + }, + }, + }, + }, } for i, c := range cases {