diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index d06eafbc6..18fa5f93b 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -1789,6 +1789,15 @@ func validateServices(t *Task) error { if service.PortLabel != "" { servicePorts[service.PortLabel] = append(servicePorts[service.PortLabel], service.Name) } + + // Ensure that check names are unique. + knownChecks := make(map[string]struct{}) + for _, check := range service.Checks { + if _, ok := knownChecks[check.Name]; ok { + mErr.Errors = append(mErr.Errors, fmt.Errorf("check %q is duplicate", check.Name)) + } + knownChecks[check.Name] = struct{}{} + } } // Get the set of port labels. diff --git a/nomad/structs/structs_test.go b/nomad/structs/structs_test.go index 84e136ad7..01b828ae8 100644 --- a/nomad/structs/structs_test.go +++ b/nomad/structs/structs_test.go @@ -284,6 +284,10 @@ func TestTask_Validate_Services(t *testing.T) { Name: "check-name", Type: ServiceCheckTCP, }, + { + Name: "check-name", + Type: ServiceCheckTCP, + }, }, } @@ -313,6 +317,10 @@ func TestTask_Validate_Services(t *testing.T) { if !strings.Contains(err.Error(), "service \"service-name\" is duplicate") { t.Fatalf("err: %v", err) } + + if !strings.Contains(err.Error(), "check \"check-name\" is duplicate") { + t.Fatalf("err: %v", err) + } } func TestTask_Validate_LogConfig(t *testing.T) {