update structs with lifecycle

This commit is contained in:
Mahmood Ali 2019-10-11 13:25:32 -04:00
parent 3b5786ddb3
commit 4ebeac721a
3 changed files with 61 additions and 3 deletions

View file

@ -924,6 +924,13 @@ func ApiTaskToStructsTask(apiTask *api.Task, structsTask *structs.Task) {
File: apiTask.DispatchPayload.File, File: apiTask.DispatchPayload.File,
} }
} }
if apiTask.Lifecycle != nil {
structsTask.Lifecycle = &structs.TaskLifecycleConfig{
RunLevel: apiTask.Lifecycle.RunLevel,
BlockUntil: apiTask.Lifecycle.BlockUntil,
}
}
} }
func ApiResourcesToStructs(in *api.Resources) *structs.Resources { func ApiResourcesToStructs(in *api.Resources) *structs.Resources {

View file

@ -340,7 +340,7 @@ func TestParse(t *testing.T) {
Driver: "docker", Driver: "docker",
User: "", User: "",
Lifecycle: &api.TaskLifecycle{ Lifecycle: &api.TaskLifecycle{
Runlevel: "prestart", RunLevel: "prestart",
BlockUntil: "completed", BlockUntil: "completed",
}, },
Config: map[string]interface{}{ Config: map[string]interface{}{

View file

@ -25,8 +25,8 @@ import (
"github.com/gorhill/cronexpr" "github.com/gorhill/cronexpr"
hcodec "github.com/hashicorp/go-msgpack/codec" hcodec "github.com/hashicorp/go-msgpack/codec"
"github.com/hashicorp/go-multierror" multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-version" version "github.com/hashicorp/go-version"
"github.com/hashicorp/nomad/acl" "github.com/hashicorp/nomad/acl"
"github.com/hashicorp/nomad/command/agent/pprof" "github.com/hashicorp/nomad/command/agent/pprof"
"github.com/hashicorp/nomad/helper" "github.com/hashicorp/nomad/helper"
@ -4385,6 +4385,46 @@ func (d *DispatchPayloadConfig) Validate() error {
return nil return nil
} }
const (
TaskLifecycleRunLevelPrestart = "prestart"
TaskLifecycleBlockUntilStarted = "started"
TaskLifecycleBlockUntilCompleted = "completed"
)
type TaskLifecycleConfig struct {
RunLevel string
BlockUntil string
}
func (d *TaskLifecycleConfig) Copy() *TaskLifecycleConfig {
if d == nil {
return nil
}
nd := new(TaskLifecycleConfig)
*nd = *d
return nd
}
func (d *TaskLifecycleConfig) Validate() error {
if d == nil {
return nil
}
switch d.RunLevel {
case TaskLifecycleRunLevelPrestart:
default:
return fmt.Errorf("invalid run_level: %v", d.RunLevel)
}
switch d.BlockUntil {
case TaskLifecycleBlockUntilStarted, TaskLifecycleBlockUntilCompleted:
default:
return fmt.Errorf("invalid block_until: %v", d.BlockUntil)
}
return nil
}
var ( var (
// These default restart policies needs to be in sync with // These default restart policies needs to be in sync with
// Canonicalize in api/tasks.go // Canonicalize in api/tasks.go
@ -5407,6 +5447,8 @@ type Task struct {
// DispatchPayload configures how the task retrieves its input from a dispatch // DispatchPayload configures how the task retrieves its input from a dispatch
DispatchPayload *DispatchPayloadConfig DispatchPayload *DispatchPayloadConfig
Lifecycle *TaskLifecycleConfig
// Meta is used to associate arbitrary metadata with this // Meta is used to associate arbitrary metadata with this
// task. This is opaque to Nomad. // task. This is opaque to Nomad.
Meta map[string]string Meta map[string]string
@ -5486,6 +5528,7 @@ func (t *Task) Copy() *Task {
nt.LogConfig = nt.LogConfig.Copy() nt.LogConfig = nt.LogConfig.Copy()
nt.Meta = helper.CopyMapStringString(nt.Meta) nt.Meta = helper.CopyMapStringString(nt.Meta)
nt.DispatchPayload = nt.DispatchPayload.Copy() nt.DispatchPayload = nt.DispatchPayload.Copy()
nt.Lifecycle = nt.Lifecycle.Copy()
if t.Artifacts != nil { if t.Artifacts != nil {
artifacts := make([]*TaskArtifact, 0, len(t.Artifacts)) artifacts := make([]*TaskArtifact, 0, len(t.Artifacts))
@ -5665,6 +5708,14 @@ func (t *Task) Validate(ephemeralDisk *EphemeralDisk, jobType string, tgServices
} }
} }
// Validate the Lifecycle block if there
if t.Lifecycle != nil {
if err := t.Lifecycle.Validate(); err != nil {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Lifecycle validation failed: %v", err))
}
}
// Validation for TaskKind field which is used for Consul Connect integration // Validation for TaskKind field which is used for Consul Connect integration
if t.Kind.IsConnectProxy() { if t.Kind.IsConnectProxy() {
// This task is a Connect proxy so it should not have service stanzas // This task is a Connect proxy so it should not have service stanzas