Make system jobs fail validation if they contain a reschedule stanza

This commit is contained in:
Preetha Appan 2018-04-11 14:56:20 -05:00
parent 9ab32ab7e6
commit a7b7b662ed
No known key found for this signature in database
GPG Key ID: 9F7C19990A50EAFC
4 changed files with 56 additions and 10 deletions

View File

@ -430,14 +430,12 @@ func (g *TaskGroup) Canonicalize(job *Job) {
MaxDelay: helper.TimeToPtr(structs.DefaultBatchJobReschedulePolicy.MaxDelay),
Unlimited: helper.BoolToPtr(structs.DefaultBatchJobReschedulePolicy.Unlimited),
}
default:
defaultReschedulePolicy = nil
}
if defaultReschedulePolicy != nil && g.ReschedulePolicy != nil {
defaultReschedulePolicy.Merge(g.ReschedulePolicy)
g.ReschedulePolicy = defaultReschedulePolicy
}
g.ReschedulePolicy = defaultReschedulePolicy
// Merge the migrate strategy from the job
if jm, tm := job.Migrate != nil, g.Migrate != nil; jm && tm {

View File

@ -6,6 +6,8 @@ import (
"strconv"
"strings"
"time"
"github.com/golang/snappy"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/nomad/structs"
@ -639,13 +641,35 @@ func ApiTgToStructsTG(taskGroup *api.TaskGroup, tg *structs.TaskGroup) {
}
if taskGroup.ReschedulePolicy != nil {
attempts := 0
if taskGroup.ReschedulePolicy.Attempts != nil {
attempts = *taskGroup.ReschedulePolicy.Attempts
}
var interval, delay, maxDelay time.Duration
if taskGroup.ReschedulePolicy.Interval != nil {
interval = *taskGroup.ReschedulePolicy.Interval
}
if taskGroup.ReschedulePolicy.Delay != nil {
delay = *taskGroup.ReschedulePolicy.Delay
}
if taskGroup.ReschedulePolicy.MaxDelay != nil {
maxDelay = *taskGroup.ReschedulePolicy.MaxDelay
}
delayFunction := ""
if taskGroup.ReschedulePolicy.DelayFunction != nil {
delayFunction = *taskGroup.ReschedulePolicy.DelayFunction
}
unlimited := false
if taskGroup.ReschedulePolicy.Unlimited != nil {
unlimited = *taskGroup.ReschedulePolicy.Unlimited
}
tg.ReschedulePolicy = &structs.ReschedulePolicy{
Attempts: *taskGroup.ReschedulePolicy.Attempts,
Interval: *taskGroup.ReschedulePolicy.Interval,
Delay: *taskGroup.ReschedulePolicy.Delay,
DelayFunction: *taskGroup.ReschedulePolicy.DelayFunction,
MaxDelay: *taskGroup.ReschedulePolicy.MaxDelay,
Unlimited: *taskGroup.ReschedulePolicy.Unlimited,
Attempts: attempts,
Interval: interval,
Delay: delay,
DelayFunction: delayFunction,
MaxDelay: maxDelay,
Unlimited: unlimited,
}
}

View File

@ -3301,7 +3301,11 @@ func (tg *TaskGroup) Validate(j *Job) error {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Task Group %v should have a restart policy", tg.Name))
}
if j.Type != JobTypeSystem {
if j.Type == JobTypeSystem {
if tg.ReschedulePolicy != nil {
mErr.Errors = append(mErr.Errors, fmt.Errorf("System jobs should not have a reschedule policy but got %+v", tg.ReschedulePolicy))
}
} else {
if tg.ReschedulePolicy != nil {
if err := tg.ReschedulePolicy.Validate(); err != nil {
mErr.Errors = append(mErr.Errors, err)

View File

@ -713,6 +713,26 @@ func TestTaskGroup_Validate(t *testing.T) {
if !strings.Contains(err.Error(), "does not allow update block") {
t.Fatalf("err: %s", err)
}
tg = &TaskGroup{
Count: -1,
RestartPolicy: &RestartPolicy{
Interval: 5 * time.Minute,
Delay: 10 * time.Second,
Attempts: 10,
Mode: RestartPolicyModeDelay,
},
ReschedulePolicy: &ReschedulePolicy{
Interval: 5 * time.Minute,
Attempts: 5,
Delay: 5 * time.Second,
},
}
j.Type = JobTypeSystem
err = tg.Validate(j)
if !strings.Contains(err.Error(), "System jobs should not have a reschedule policy") {
t.Fatalf("err: %s", err)
}
}
func TestTask_Validate(t *testing.T) {