Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error message on service length #12012

Merged
merged 4 commits into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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