Skip to content

Commit

Permalink
improve error message on service length (#12012)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkarthick committed Feb 5, 2022
1 parent e9ef2c0 commit 16485f4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/12012.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
consul: improve service name validation message to include maximum length requirement
```
4 changes: 2 additions & 2 deletions client/allocrunner/taskrunner/validate_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
5 changes: 3 additions & 2 deletions nomad/structs/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 16485f4

Please sign in to comment.