service: remove duplicate name check during validation (#10868)

When a task group with `service` block(s) is validated, we validate that there
are no duplicates, but this validation doesn't have access to the task environment
because it hasn't been created yet. Services and checks with interpolation can
be flagged incorrectly as conflicting. Name conflicts in services are not
actually an error in Consul and users have reported wanting to use the same
service name for task groups differentiated by tags.
This commit is contained in:
Tim Gross 2021-07-08 09:43:38 -04:00 committed by GitHub
parent e88e1e5001
commit 9f128a28ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 11 deletions

3
.changelog/10868.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
consul: Fixed a bug where services may incorrectly fail conflicting name validation
```

View File

@ -6331,13 +6331,11 @@ func (tg *TaskGroup) validateNetworks() error {
return mErr.ErrorOrNil()
}
// validateServices runs Service.Validate() on group-level services,
// checks that group services do not conflict with task services and that
// validateServices runs Service.Validate() on group-level services, checks
// group service checks that refer to tasks only refer to tasks that exist.
func (tg *TaskGroup) validateServices() error {
var mErr multierror.Error
knownTasks := make(map[string]struct{})
knownServices := make(map[string]struct{})
// Create a map of known tasks and their services so we can compare
// vs the group-level services and checks
@ -6347,15 +6345,11 @@ func (tg *TaskGroup) validateServices() error {
continue
}
for _, service := range task.Services {
if _, ok := knownServices[service.Name+service.PortLabel]; ok {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Service %s is duplicate", service.Name))
}
for _, check := range service.Checks {
if check.TaskName != "" {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Check %s is invalid: only task group service checks can be assigned tasks", check.Name))
}
}
knownServices[service.Name+service.PortLabel] = struct{}{}
}
}
for i, service := range tg.Services {
@ -6370,10 +6364,7 @@ func (tg *TaskGroup) validateServices() error {
if service.AddressMode == AddressModeDriver {
mErr.Errors = append(mErr.Errors, fmt.Errorf("service %q cannot use address_mode=\"driver\", only services defined in a \"task\" block can use this mode", service.Name))
}
if _, ok := knownServices[service.Name+service.PortLabel]; ok {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Service %s is duplicate", service.Name))
}
knownServices[service.Name+service.PortLabel] = struct{}{}
for _, check := range service.Checks {
if check.TaskName != "" {
if check.Type != ServiceCheckScript && check.Type != ServiceCheckGRPC {