Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task pause schedule (CE part) #20476

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions api/task_sched.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package api

type TaskSchedule struct {
Cron *TaskScheduleCron `hcl:"cron,block"`
}

type TaskScheduleCron struct {
Start string `hcl:"start,optional"`
Stop string `hcl:"stop,optional"`
Timezone string `hcl:"timezone,optional"`
}
2 changes: 2 additions & 0 deletions api/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,8 @@ type Task struct {
Identities []*WorkloadIdentity `hcl:"identity,block"`

Actions []*Action `hcl:"action,block"`

Schedule *TaskSchedule `hcl:"schedule,block"`
}

func (t *Task) Canonicalize(tg *TaskGroup, job *Job) {
Expand Down
20 changes: 20 additions & 0 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,11 @@ func ApiTaskToStructsTask(job *structs.Job, group *structs.TaskGroup,
act := ApiActionToStructsAction(job, action)
structsTask.Actions = append(structsTask.Actions, act)
}

if apiTask.Schedule != nil {
sched := apiScheduleToStructsSchedule(apiTask.Schedule)
structsTask.Schedule = sched
}
}

// apiWaitConfigToStructsWaitConfig is a copy and type conversion between the API
Expand Down Expand Up @@ -1475,6 +1480,21 @@ func ApiActionToStructsAction(job *structs.Job, action *api.Action) *structs.Act
}
}

func apiScheduleToStructsSchedule(s *api.TaskSchedule) *structs.TaskSchedule {
if s.Cron == nil {
// Since cron is the only field, drop the whole schedule block if its empty
return nil
}

return &structs.TaskSchedule{
Cron: &structs.TaskScheduleCron{
Start: s.Cron.Start,
Stop: s.Cron.Stop,
Timezone: s.Cron.Timezone,
},
}
}

func ApiResourcesToStructs(in *api.Resources) *structs.Resources {
if in == nil {
return nil
Expand Down
1 change: 1 addition & 0 deletions nomad/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func NewJobEndpoints(s *Server, ctx *RPCContext) *Job {
&jobValidate{srv: s},
&memoryOversubscriptionValidate{srv: s},
jobNumaHook{},
jobSchedHook{},
},
}
}
Expand Down
13 changes: 13 additions & 0 deletions nomad/job_endpoint_hook_sched.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package nomad

// jobSchedHook implements a job Validating admission controller.
//
// The implementation of Validate are in the _ce/_ent files.
type jobSchedHook struct{}

func (jobSchedHook) Name() string {
return "schedule"
}
23 changes: 23 additions & 0 deletions nomad/job_endpoint_hook_sched_ce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

//go:build !ent

package nomad

import (
"errors"

"github.com/hashicorp/nomad/nomad/structs"
)

func (jobSchedHook) Validate(job *structs.Job) ([]error, error) {
for _, tg := range job.TaskGroups {
for _, task := range tg.Tasks {
if task.Schedule != nil {
return nil, errors.New("task schedules requires Nomad Enterprise")
}
}
}
return nil, nil
}
3 changes: 3 additions & 0 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7700,6 +7700,9 @@ type Task struct {

// Alloc-exec-like runnable commands
Actions []*Action

// Schedule for pausing tasks. Enterprise only.
Schedule *TaskSchedule
}

func (t *Task) UsesCores() bool {
Expand Down
14 changes: 14 additions & 0 deletions nomad/structs/task_sched.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package structs

type TaskSchedule struct {
Cron *TaskScheduleCron
}

type TaskScheduleCron struct {
Start string
Stop string
Timezone string
}
Loading