improve error message on service length (#12012)

This commit is contained in:
Karthick Ramachandran 2022-02-04 16:39:34 -08:00 committed by GitHub
parent 420fd17459
commit 0600bc32e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 4 deletions

3
.changelog/12012.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
consul: improve service name validation message to include maximum length requirement
```

View File

@ -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))
}
}

View File

@ -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
}