From 6791147254f72e5c9bb14f32881f3d547fdf65b8 Mon Sep 17 00:00:00 2001 From: Derek Strickland <1111455+DerekStrickland@users.noreply.github.com> Date: Thu, 31 Mar 2022 13:34:16 -0400 Subject: [PATCH] disconnected clients: `TaskGroup` validation (#12418) * TaskGroup: Validate that max_client_disconnect and stop_after_client_disconnect are mutually exclusive. --- nomad/structs/structs.go | 4 ++++ nomad/structs/structs_test.go | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 83a50300994..7271559e654 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -6294,6 +6294,10 @@ func (tg *TaskGroup) Validate(j *Job) error { mErr.Errors = append(mErr.Errors, errors.New("Missing tasks for task group")) } + if tg.MaxClientDisconnect != nil && tg.StopAfterClientDisconnect != nil { + mErr.Errors = append(mErr.Errors, errors.New("Task group cannot be configured with both max_client_disconnect and stop_after_client_disconnect")) + } + if tg.MaxClientDisconnect != nil && *tg.MaxClientDisconnect < 0 { mErr.Errors = append(mErr.Errors, errors.New("max_client_disconnect cannot be negative")) } diff --git a/nomad/structs/structs_test.go b/nomad/structs/structs_test.go index ee6afecceb7..1c83b5f17d5 100644 --- a/nomad/structs/structs_test.go +++ b/nomad/structs/structs_test.go @@ -415,7 +415,6 @@ func testJob() *Job { "elb_check_interval": "30s", "elb_check_min": "3", }, - MaxClientDisconnect: helper.TimeToPtr(1 * time.Hour), }, }, Meta: map[string]string{ @@ -5880,7 +5879,6 @@ func TestParameterizedJobConfig_Validate_NonBatch(t *testing.T) { func TestJobConfig_Validate_StopAferClientDisconnect(t *testing.T) { ci.Parallel(t) - // Setup a system Job with stop_after_client_disconnect set, which is invalid job := testJob() job.Type = JobTypeSystem @@ -5912,14 +5910,17 @@ func TestJobConfig_Validate_MaxClientDisconnect(t *testing.T) { job := testJob() timeout := -1 * time.Minute job.TaskGroups[0].MaxClientDisconnect = &timeout + job.TaskGroups[0].StopAfterClientDisconnect = &timeout err := job.Validate() require.Error(t, err) require.Contains(t, err.Error(), "max_client_disconnect cannot be negative") + require.Contains(t, err.Error(), "Task group cannot be configured with both max_client_disconnect and stop_after_client_disconnect") // Modify the job with a valid max_client_disconnect value timeout = 1 * time.Minute job.TaskGroups[0].MaxClientDisconnect = &timeout + job.TaskGroups[0].StopAfterClientDisconnect = nil err = job.Validate() require.NoError(t, err) }