1package state 2 3import ( 4 "time" 5 6 "github.com/hashicorp/nomad/nomad/structs" 7) 8 9// State captures the state of the allocation runner. 10type State struct { 11 // ClientStatus captures the overall state of the allocation 12 ClientStatus string 13 14 // ClientDescription is an optional human readable description of the 15 // allocations client state 16 ClientDescription string 17 18 // DeploymentStatus captures the status of the deployment 19 DeploymentStatus *structs.AllocDeploymentStatus 20 21 // TaskStates is a snapshot of task states. 22 TaskStates map[string]*structs.TaskState 23 24 // NetworkStatus captures network details not known until runtime 25 NetworkStatus *structs.AllocNetworkStatus 26} 27 28// SetDeploymentStatus is a helper for updating the client-controlled 29// DeploymentStatus fields: Healthy and Timestamp. The Canary and ModifyIndex 30// fields should only be updated by the server. 31func (s *State) SetDeploymentStatus(timestamp time.Time, healthy bool) { 32 if s.DeploymentStatus == nil { 33 s.DeploymentStatus = &structs.AllocDeploymentStatus{} 34 } 35 36 s.DeploymentStatus.Healthy = &healthy 37 s.DeploymentStatus.Timestamp = timestamp 38} 39 40// ClearDeploymentStatus is a helper to clear the client-controlled 41// DeploymentStatus fields: Healthy and Timestamp. The Canary and ModifyIndex 42// fields should only be updated by the server. 43func (s *State) ClearDeploymentStatus() { 44 if s.DeploymentStatus == nil { 45 return 46 } 47 48 s.DeploymentStatus.Healthy = nil 49 s.DeploymentStatus.Timestamp = time.Time{} 50} 51 52// Copy returns a deep copy of State. 53func (s *State) Copy() *State { 54 taskStates := make(map[string]*structs.TaskState, len(s.TaskStates)) 55 for k, v := range s.TaskStates { 56 taskStates[k] = v.Copy() 57 } 58 return &State{ 59 ClientStatus: s.ClientStatus, 60 ClientDescription: s.ClientDescription, 61 DeploymentStatus: s.DeploymentStatus.Copy(), 62 TaskStates: taskStates, 63 NetworkStatus: s.NetworkStatus.Copy(), 64 } 65} 66 67// ClientTerminalStatus returns if the client status is terminal and will no longer transition 68func (a *State) ClientTerminalStatus() bool { 69 switch a.ClientStatus { 70 case structs.AllocClientStatusComplete, structs.AllocClientStatusFailed, structs.AllocClientStatusLost: 71 return true 72 default: 73 return false 74 } 75} 76