2020-10-01 18:43:28 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2020-10-02 18:23:30 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-10-01 18:43:28 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-10-08 18:27:52 +00:00
|
|
|
TypeNodeRegistration = "NodeRegistration"
|
|
|
|
TypeNodeDeregistration = "NodeDeregistration"
|
|
|
|
TypeNodeEligibilityUpdate = "NodeEligibility"
|
|
|
|
TypeNodeDrain = "NodeDrain"
|
|
|
|
TypeNodeEvent = "NodeEvent"
|
2020-10-06 20:21:58 +00:00
|
|
|
TypeDeploymentUpdate = "DeploymentStatusUpdate"
|
|
|
|
TypeDeploymentPromotion = "DeploymentPromotion"
|
|
|
|
TypeDeploymentAllocHealth = "DeploymentAllocHealth"
|
|
|
|
TypeAllocCreated = "AllocCreated"
|
|
|
|
TypeAllocUpdated = "AllocUpdated"
|
|
|
|
TypeAllocUpdateDesiredStatus = "AllocUpdateDesiredStatus"
|
|
|
|
TypeEvalUpdated = "EvalUpdated"
|
|
|
|
TypeJobRegistered = "JobRegistered"
|
|
|
|
TypeJobDeregistered = "JobDeregistered"
|
|
|
|
TypeJobBatchDeregistered = "JobBatchDeregistered"
|
2020-10-08 15:57:21 +00:00
|
|
|
TypePlanResult = "PlanResult"
|
2020-10-01 18:43:28 +00:00
|
|
|
)
|
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
// JobEvent holds a newly updated Job.
|
2020-10-01 18:43:28 +00:00
|
|
|
type JobEvent struct {
|
|
|
|
Job *structs.Job
|
|
|
|
}
|
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
// EvalEvent holds a newly updated Eval.
|
2020-10-01 18:43:28 +00:00
|
|
|
type EvalEvent struct {
|
|
|
|
Eval *structs.Evaluation
|
|
|
|
}
|
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
// AllocEvent holds a newly updated Allocation. The
|
|
|
|
// Allocs embedded Job has been removed to reduce size.
|
2020-10-01 18:43:28 +00:00
|
|
|
type AllocEvent struct {
|
|
|
|
Alloc *structs.Allocation
|
|
|
|
}
|
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
// DeploymentEvent holds a newly updated Deployment.
|
2020-10-01 18:43:28 +00:00
|
|
|
type DeploymentEvent struct {
|
|
|
|
Deployment *structs.Deployment
|
|
|
|
}
|
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
// NodeEvent holds a newly updated Node
|
2020-10-01 18:43:28 +00:00
|
|
|
type NodeEvent struct {
|
|
|
|
Node *structs.Node
|
|
|
|
}
|
|
|
|
|
|
|
|
type NodeDrainAllocDetails struct {
|
|
|
|
ID string
|
|
|
|
Migrate *structs.MigrateStrategy
|
|
|
|
}
|
|
|
|
|
|
|
|
type JobDrainDetails struct {
|
|
|
|
Type string
|
|
|
|
AllocDetails map[string]NodeDrainAllocDetails
|
|
|
|
}
|
2020-10-02 18:23:30 +00:00
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
var MsgTypeEvents = map[structs.MessageType]string{
|
|
|
|
structs.NodeRegisterRequestType: TypeNodeRegistration,
|
|
|
|
structs.UpsertNodeEventsType: TypeNodeEvent,
|
|
|
|
structs.EvalUpdateRequestType: TypeEvalUpdated,
|
|
|
|
structs.AllocClientUpdateRequestType: TypeAllocUpdated,
|
|
|
|
structs.JobRegisterRequestType: TypeJobRegistered,
|
|
|
|
structs.AllocUpdateRequestType: TypeAllocUpdated,
|
|
|
|
structs.NodeUpdateStatusRequestType: TypeNodeEvent,
|
|
|
|
structs.JobDeregisterRequestType: TypeJobDeregistered,
|
|
|
|
structs.JobBatchDeregisterRequestType: TypeJobBatchDeregistered,
|
|
|
|
structs.AllocUpdateDesiredTransitionRequestType: TypeAllocUpdateDesiredStatus,
|
|
|
|
structs.NodeUpdateEligibilityRequestType: TypeNodeDrain,
|
2020-10-15 20:44:36 +00:00
|
|
|
structs.NodeUpdateDrainRequestType: TypeNodeDrain,
|
2020-10-08 18:27:52 +00:00
|
|
|
structs.BatchNodeUpdateDrainRequestType: TypeNodeDrain,
|
|
|
|
structs.DeploymentStatusUpdateRequestType: TypeDeploymentUpdate,
|
|
|
|
structs.DeploymentPromoteRequestType: TypeDeploymentPromotion,
|
|
|
|
structs.DeploymentAllocHealthRequestType: TypeDeploymentAllocHealth,
|
|
|
|
structs.ApplyPlanResultsRequestType: TypePlanResult,
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenericEventsFromChanges returns a set of events for a given set of
|
|
|
|
// transaction changes. It currently ignores Delete operations.
|
2020-10-06 20:21:58 +00:00
|
|
|
func GenericEventsFromChanges(tx ReadTxn, changes Changes) (*structs.Events, error) {
|
2020-10-08 18:27:52 +00:00
|
|
|
eventType, ok := MsgTypeEvents[changes.MsgType]
|
|
|
|
if !ok {
|
2020-10-06 20:21:58 +00:00
|
|
|
return nil, nil
|
2020-10-02 18:23:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 19:12:35 +00:00
|
|
|
var events []structs.Event
|
2020-10-02 18:23:30 +00:00
|
|
|
for _, change := range changes.Changes {
|
|
|
|
switch change.Table {
|
|
|
|
case "evals":
|
2020-10-06 20:21:58 +00:00
|
|
|
if change.Deleted() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-10-02 18:23:30 +00:00
|
|
|
after, ok := change.After.(*structs.Evaluation)
|
|
|
|
if !ok {
|
2020-10-06 20:21:58 +00:00
|
|
|
return nil, fmt.Errorf("transaction change was not an Evaluation")
|
2020-10-02 18:23:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 19:12:35 +00:00
|
|
|
event := structs.Event{
|
2020-10-20 12:26:21 +00:00
|
|
|
Topic: structs.TopicEval,
|
|
|
|
Type: eventType,
|
|
|
|
Index: changes.Index,
|
|
|
|
Key: after.ID,
|
|
|
|
FilterKeys: []string{
|
|
|
|
after.JobID,
|
|
|
|
after.DeploymentID,
|
|
|
|
},
|
2020-10-08 15:57:21 +00:00
|
|
|
Namespace: after.Namespace,
|
2020-10-02 18:23:30 +00:00
|
|
|
Payload: &EvalEvent{
|
|
|
|
Eval: after,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
events = append(events, event)
|
|
|
|
|
|
|
|
case "allocs":
|
2020-10-06 20:21:58 +00:00
|
|
|
if change.Deleted() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-10-02 18:23:30 +00:00
|
|
|
after, ok := change.After.(*structs.Allocation)
|
|
|
|
if !ok {
|
2020-10-06 20:21:58 +00:00
|
|
|
return nil, fmt.Errorf("transaction change was not an Allocation")
|
2020-10-02 18:23:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 20:21:58 +00:00
|
|
|
alloc := after.Copy()
|
2020-10-08 18:27:52 +00:00
|
|
|
|
|
|
|
filterKeys := []string{
|
|
|
|
alloc.JobID,
|
|
|
|
alloc.DeploymentID,
|
|
|
|
}
|
|
|
|
|
2020-10-06 20:21:58 +00:00
|
|
|
// remove job info to help keep size of alloc event down
|
|
|
|
alloc.Job = nil
|
|
|
|
|
2020-10-04 19:12:35 +00:00
|
|
|
event := structs.Event{
|
2020-10-08 18:27:52 +00:00
|
|
|
Topic: structs.TopicAlloc,
|
|
|
|
Type: eventType,
|
|
|
|
Index: changes.Index,
|
|
|
|
Key: after.ID,
|
|
|
|
FilterKeys: filterKeys,
|
|
|
|
Namespace: after.Namespace,
|
2020-10-02 18:23:30 +00:00
|
|
|
Payload: &AllocEvent{
|
2020-10-06 20:21:58 +00:00
|
|
|
Alloc: alloc,
|
2020-10-02 18:23:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-10-02 20:13:49 +00:00
|
|
|
events = append(events, event)
|
|
|
|
case "jobs":
|
2020-10-06 20:21:58 +00:00
|
|
|
if change.Deleted() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-10-02 20:13:49 +00:00
|
|
|
after, ok := change.After.(*structs.Job)
|
|
|
|
if !ok {
|
2020-10-06 20:21:58 +00:00
|
|
|
return nil, fmt.Errorf("transaction change was not an Allocation")
|
2020-10-02 20:13:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 19:12:35 +00:00
|
|
|
event := structs.Event{
|
2020-10-08 18:27:52 +00:00
|
|
|
Topic: structs.TopicJob,
|
2020-10-08 15:57:21 +00:00
|
|
|
Type: eventType,
|
|
|
|
Index: changes.Index,
|
|
|
|
Key: after.ID,
|
|
|
|
Namespace: after.Namespace,
|
2020-10-02 20:13:49 +00:00
|
|
|
Payload: &JobEvent{
|
|
|
|
Job: after,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
events = append(events, event)
|
|
|
|
case "nodes":
|
2020-10-06 20:21:58 +00:00
|
|
|
if change.Deleted() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-10-02 20:13:49 +00:00
|
|
|
after, ok := change.After.(*structs.Node)
|
|
|
|
if !ok {
|
2020-10-06 20:21:58 +00:00
|
|
|
return nil, fmt.Errorf("transaction change was not a Node")
|
2020-10-02 20:13:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 19:12:35 +00:00
|
|
|
event := structs.Event{
|
2020-10-08 18:27:52 +00:00
|
|
|
Topic: structs.TopicNode,
|
2020-10-02 20:13:49 +00:00
|
|
|
Type: eventType,
|
|
|
|
Index: changes.Index,
|
|
|
|
Key: after.ID,
|
|
|
|
Payload: &NodeEvent{
|
|
|
|
Node: after,
|
|
|
|
},
|
|
|
|
}
|
2020-10-02 18:23:30 +00:00
|
|
|
events = append(events, event)
|
2020-10-08 15:57:21 +00:00
|
|
|
case "deployment":
|
|
|
|
if change.Deleted() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
after, ok := change.After.(*structs.Deployment)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("transaction change was not a Node")
|
|
|
|
}
|
|
|
|
|
|
|
|
event := structs.Event{
|
2020-10-08 18:27:52 +00:00
|
|
|
Topic: structs.TopicDeployment,
|
2020-10-08 15:57:21 +00:00
|
|
|
Type: eventType,
|
|
|
|
Index: changes.Index,
|
|
|
|
Key: after.ID,
|
|
|
|
Namespace: after.Namespace,
|
|
|
|
FilterKeys: []string{after.JobID},
|
|
|
|
Payload: &DeploymentEvent{
|
|
|
|
Deployment: after,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
events = append(events, event)
|
2020-10-02 18:23:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 20:21:58 +00:00
|
|
|
return &structs.Events{Index: changes.Index, Events: events}, nil
|
2020-10-02 18:23:30 +00:00
|
|
|
}
|