diff --git a/.changelog/12012.txt b/.changelog/12012.txt new file mode 100644 index 000000000..c0ed0f788 --- /dev/null +++ b/.changelog/12012.txt @@ -0,0 +1,3 @@ +```release-note:improvement +consul: improve service name validation message to include maximum length requirement +``` diff --git a/client/allocrunner/taskrunner/validate_hook.go b/client/allocrunner/taskrunner/validate_hook.go index a2fb1d220..0a22f90e9 100644 --- a/client/allocrunner/taskrunner/validate_hook.go +++ b/client/allocrunner/taskrunner/validate_hook.go @@ -56,10 +56,10 @@ func validateTask(task *structs.Task, taskEnv *taskenv.TaskEnv, conf *config.Con } // Validate the Service names once they're interpolated - for i, service := range task.Services { + for _, service := range task.Services { name := taskEnv.ReplaceEnv(service.Name) if err := service.ValidateName(name); err != nil { - mErr.Errors = append(mErr.Errors, fmt.Errorf("service (%d) failed validation: %v", i, err)) + mErr.Errors = append(mErr.Errors, fmt.Errorf("service (%s) failed validation: %v", name, err)) } } diff --git a/nomad/structs/services.go b/nomad/structs/services.go index d4760fe52..7c6a1c4e5 100644 --- a/nomad/structs/services.go +++ b/nomad/structs/services.go @@ -545,7 +545,8 @@ func (s *Service) Validate() error { serviceNameStripped := args.ReplaceEnvWithPlaceHolder(s.Name, "ENV-VAR") if err := s.ValidateName(serviceNameStripped); err != nil { - mErr.Errors = append(mErr.Errors, fmt.Errorf("Service name must be valid per RFC 1123 and can contain only alphanumeric characters or dashes: %q", s.Name)) + // Log actual service name, not the stripped version. + mErr.Errors = append(mErr.Errors, fmt.Errorf("%v: %q", err, s.Name)) } switch s.AddressMode { @@ -607,7 +608,7 @@ func (s *Service) ValidateName(name string) error { // (https://tools.ietf.org/html/rfc2782). re := regexp.MustCompile(`^(?i:[a-z0-9]|[a-z0-9][a-z0-9\-]{0,61}[a-z0-9])$`) if !re.MatchString(name) { - return fmt.Errorf("Service name must be valid per RFC 1123 and can contain only alphanumeric characters or dashes and must be no longer than 63 characters: %q", name) + return fmt.Errorf("Service name must be valid per RFC 1123 and can contain only alphanumeric characters or dashes and must be no longer than 63 characters") } return nil }