Skip to content

Commit

Permalink
core: refactor task validation
Browse files Browse the repository at this point in the history
Move all validations related to task fields to Task.Validate(). Prior to
this, some task validations were being done inside TaskGroup.Validate()
because they required access to some group values.

But similarly to how TaskGroup.Validate() tasks the job as parameter,
it's fair to expect the task to receive its group.
  • Loading branch information
lgfa29 committed May 29, 2023
1 parent d2a6340 commit a5f3e0d
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 88 deletions.
79 changes: 39 additions & 40 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6807,37 +6807,12 @@ func (tg *TaskGroup) Validate(j *Job) error {
mErr.Errors = append(mErr.Errors, outer)
}

isTypeService := j.Type == JobTypeService

// Validate the tasks
for _, task := range tg.Tasks {
// Validate the task does not reference undefined volume mounts
for i, mnt := range task.VolumeMounts {
if mnt.Volume == "" {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Task %s has a volume mount (%d) referencing an empty volume", task.Name, i))
continue
}

if _, ok := tg.Volumes[mnt.Volume]; !ok {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Task %s has a volume mount (%d) referencing undefined volume %s", task.Name, i, mnt.Volume))
continue
}
}

if err := task.Validate(tg.EphemeralDisk, j.Type, tg.Services, tg.Networks); err != nil {
if err := task.Validate(j.Type, tg); err != nil {
outer := fmt.Errorf("Task %s validation failed: %v", task.Name, err)
mErr.Errors = append(mErr.Errors, outer)
}

// Validate the group's Update Strategy does not conflict with the Task's kill_timeout for service type jobs
if isTypeService && tg.Update != nil {
// progress_deadline = 0 has a special meaning show it should not
// validated against the task's kill_timeout.
if tg.Update.ProgressDeadline > 0 && task.KillTimeout > tg.Update.ProgressDeadline {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Task %s has a kill timout (%s) longer than the group's progress deadline (%s)",
task.Name, task.KillTimeout.String(), tg.Update.ProgressDeadline.String()))
}
}
}

return mErr.ErrorOrNil()
Expand Down Expand Up @@ -7304,14 +7279,22 @@ func DefaultLogConfig() *LogConfig {
// Validate returns an error if the log config specified are less than the
// minimum allowed. Note that because we have a non-zero default MaxFiles and
// MaxFileSizeMB, we can't validate that they're unset if Disabled=true
func (l *LogConfig) Validate() error {
func (l *LogConfig) Validate(disk *EphemeralDisk) error {
var mErr multierror.Error
if l.MaxFiles < 1 {
mErr.Errors = append(mErr.Errors, fmt.Errorf("minimum number of files is 1; got %d", l.MaxFiles))
}
if l.MaxFileSizeMB < 1 {
mErr.Errors = append(mErr.Errors, fmt.Errorf("minimum file size is 1MB; got %d", l.MaxFileSizeMB))
}
if disk != nil {
logUsage := (l.MaxFiles * l.MaxFileSizeMB)
if disk.SizeMB <= logUsage {
mErr.Errors = append(mErr.Errors,
fmt.Errorf("log storage (%d MB) must be less than requested disk capacity (%d MB)",
logUsage, disk.SizeMB))
}
}
return mErr.ErrorOrNil()
}

Expand Down Expand Up @@ -7541,7 +7524,7 @@ func (t *Task) GoString() string {
}

// Validate is used to check a task for reasonable configuration
func (t *Task) Validate(ephemeralDisk *EphemeralDisk, jobType string, tgServices []*Service, tgNetworks Networks) error {
func (t *Task) Validate(jobType string, tg *TaskGroup) error {
var mErr multierror.Error
if t.Name == "" {
mErr.Errors = append(mErr.Errors, errors.New("Missing task name"))
Expand All @@ -7558,6 +7541,20 @@ func (t *Task) Validate(ephemeralDisk *EphemeralDisk, jobType string, tgServices
}
if t.KillTimeout < 0 {
mErr.Errors = append(mErr.Errors, errors.New("KillTimeout must be a positive value"))
} else {
// Validate the group's update strategy does not conflict with the
// task's kill_timeout for service jobs.
//
// progress_deadline = 0 has a special meaning show it should not
// validated against the task's kill_timeout.
conflictsWithProgressDeadline := jobType == JobTypeService &&

This comment has been minimized.

Copy link
@Juanadelacuesta

Juanadelacuesta May 30, 2023

Member

great! There was a bug with the previous refactor, because the default for KillTimeout is 5s and it conflicted with a ProgressDeadline set to 0

tg.Update != nil &&
tg.Update.ProgressDeadline > 0 &&
t.KillTimeout > tg.Update.ProgressDeadline
if conflictsWithProgressDeadline {
mErr.Errors = append(mErr.Errors, fmt.Errorf("KillTimout (%s) longer than the group's ProgressDeadline (%s)",
t.KillTimeout, tg.Update.ProgressDeadline))
}
}
if t.ShutdownDelay < 0 {
mErr.Errors = append(mErr.Errors, errors.New("ShutdownDelay must be a positive value"))
Expand All @@ -7573,10 +7570,11 @@ func (t *Task) Validate(ephemeralDisk *EphemeralDisk, jobType string, tgServices
// Validate the log config
if t.LogConfig == nil {
mErr.Errors = append(mErr.Errors, errors.New("Missing Log Config"))
} else if err := t.LogConfig.Validate(); err != nil {
} else if err := t.LogConfig.Validate(tg.EphemeralDisk); err != nil {
mErr.Errors = append(mErr.Errors, err)
}

// Validate constraints and affinities.
for idx, constr := range t.Constraints {
if err := constr.Validate(); err != nil {
outer := fmt.Errorf("Constraint %d validation failed: %s", idx+1, err)
Expand Down Expand Up @@ -7604,32 +7602,26 @@ func (t *Task) Validate(ephemeralDisk *EphemeralDisk, jobType string, tgServices
}

// Validate Services
if err := validateServices(t, tgNetworks); err != nil {
if err := validateServices(t, tg.Networks); err != nil {
mErr.Errors = append(mErr.Errors, err)
}

if t.LogConfig != nil && ephemeralDisk != nil {
logUsage := (t.LogConfig.MaxFiles * t.LogConfig.MaxFileSizeMB)
if ephemeralDisk.SizeMB <= logUsage {
mErr.Errors = append(mErr.Errors,
fmt.Errorf("log storage (%d MB) must be less than requested disk capacity (%d MB)",
logUsage, ephemeralDisk.SizeMB))
}
}

// Validate artifacts.
for idx, artifact := range t.Artifacts {
if err := artifact.Validate(); err != nil {
outer := fmt.Errorf("Artifact %d validation failed: %v", idx+1, err)
mErr.Errors = append(mErr.Errors, outer)
}
}

// Validate Vault.
if t.Vault != nil {
if err := t.Vault.Validate(); err != nil {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Vault validation failed: %v", err))
}
}

// Validate templates.
destinations := make(map[string]int, len(t.Templates))
for idx, tmpl := range t.Templates {
if err := tmpl.Validate(); err != nil {
Expand Down Expand Up @@ -7671,7 +7663,7 @@ func (t *Task) Validate(ephemeralDisk *EphemeralDisk, jobType string, tgServices
}

// Ensure the proxy task has a corresponding service entry
serviceErr := ValidateConnectProxyService(t.Kind.Value(), tgServices)
serviceErr := ValidateConnectProxyService(t.Kind.Value(), tg.Services)
if serviceErr != nil {
mErr.Errors = append(mErr.Errors, serviceErr)
}
Expand All @@ -7682,6 +7674,13 @@ func (t *Task) Validate(ephemeralDisk *EphemeralDisk, jobType string, tgServices
if !MountPropagationModeIsValid(vm.PropagationMode) {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Volume Mount (%d) has an invalid propagation mode: \"%s\"", idx, vm.PropagationMode))
}

// Validate the task does not reference undefined volume mounts
if vm.Volume == "" {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Volume Mount (%d) references an empty volume", idx))
} else if _, ok := tg.Volumes[vm.Volume]; !ok {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Volume Mount (%d) references undefined volume %s", idx, vm.Volume))
}
}

// Validate CSI Plugin Config
Expand Down
Loading

0 comments on commit a5f3e0d

Please sign in to comment.