From c68ed09e2f49455ef21c90ff42b49ef5e6848d0a Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Thu, 7 Dec 2017 22:09:37 -0800 Subject: [PATCH] Improve port label validation and diff testing --- nomad/structs/diff_test.go | 6 ++++++ nomad/structs/structs.go | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/nomad/structs/diff_test.go b/nomad/structs/diff_test.go index 379f1a23995a..4574bcb64c30 100644 --- a/nomad/structs/diff_test.go +++ b/nomad/structs/diff_test.go @@ -3507,6 +3507,12 @@ func TestTaskDiff(t *testing.T) { Type: DiffTypeEdited, Name: "Check", Fields: []*FieldDiff{ + { + Type: DiffTypeNone, + Name: "AddressMode", + Old: "", + New: "", + }, { Type: DiffTypeNone, Name: "Command", diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index fa29071aa1c9..7fdc06fdbbe8 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -3473,10 +3473,14 @@ func validateServices(t *Task) error { knownServices[service.Name+service.PortLabel] = struct{}{} if service.PortLabel != "" { - if _, err := strconv.Atoi(service.PortLabel); service.AddressMode == "driver" && err == nil { - // Numeric ports are valid when AddressMode=driver + if service.AddressMode == "driver" { + // Numeric port labels are valid for address_mode=driver + _, err := strconv.Atoi(service.PortLabel) + if err != nil { + // Not a numeric port label, add it to list to check + servicePorts[service.PortLabel] = append(servicePorts[service.PortLabel], service.Name) + } } else { - servicePorts[service.PortLabel] = append(servicePorts[service.PortLabel], service.Name) } }