Skip to content

Commit

Permalink
jobspec: ensure service uniqueness in job validation
Browse files Browse the repository at this point in the history
  • Loading branch information
shoenig committed Jul 20, 2022
1 parent 87ef517 commit 0cc1833
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6628,6 +6628,19 @@ func (tg *TaskGroup) validateServices() error {
// Accumulate task names in this group
taskSet := set.New[string](len(tg.Tasks))

// each service in a group must be unique (i.e. used in MakeAllocServiceID)
type unique struct {
name string
task string
port string
}

// Accumulate service IDs in this group
idSet := set.New[unique](0)

// Accumulate IDs that are duplicates
idDuplicateSet := set.New[unique](0)

// Accumulate the providers used for this task group. Currently, Nomad only
// allows the use of a single service provider within a task group.
providerSet := set.New[string](1)
Expand All @@ -6642,20 +6655,35 @@ func (tg *TaskGroup) validateServices() error {
}

for _, service := range task.Services {

// Ensure no task-level checks specify a task
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))
}
}

// Track that we have seen this service id
id := unique{service.Name, task.Name, service.PortLabel}
if !idSet.Insert(id) {
// accumulate duplicates for a single error later on
idDuplicateSet.Insert(id)
}

// Track that we have seen this service provider
providerSet.Insert(service.Provider)
}
}

for i, service := range tg.Services {

// Track that we have seen this service id
id := unique{service.Name, "group", service.PortLabel}
if !idSet.Insert(id) {
// accumulate duplicates for a single error later on
idDuplicateSet.Insert(id)
}

// Track that we have seen this service provider
providerSet.Insert(service.Provider)

Expand Down Expand Up @@ -6688,6 +6716,25 @@ func (tg *TaskGroup) validateServices() error {
}
}

// Produce an error of any services which are not unique enough in the group
// i.e. have same <task, name, port>
if idDuplicateSet.Size() > 0 {
mErr.Errors = append(mErr.Errors,
fmt.Errorf(
"Services are not unique: %s",
idDuplicateSet.String(
func(u unique) string {
s := u.task + "->" + u.name
if u.port != "" {
s += ":" + u.port
}
return s
},
),
),
)
}

// The initial feature release of native service discovery only allows for
// a single service provider to be used across all services in a task
// group.
Expand Down

0 comments on commit 0cc1833

Please sign in to comment.