github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/nomad/state/events.go (about) 1 package state 2 3 import ( 4 memdb "github.com/hashicorp/go-memdb" 5 "github.com/hashicorp/nomad/nomad/structs" 6 ) 7 8 var MsgTypeEvents = map[structs.MessageType]string{ 9 structs.NodeRegisterRequestType: structs.TypeNodeRegistration, 10 structs.NodeDeregisterRequestType: structs.TypeNodeDeregistration, 11 structs.UpsertNodeEventsType: structs.TypeNodeEvent, 12 structs.EvalUpdateRequestType: structs.TypeEvalUpdated, 13 structs.AllocClientUpdateRequestType: structs.TypeAllocationUpdated, 14 structs.JobRegisterRequestType: structs.TypeJobRegistered, 15 structs.AllocUpdateRequestType: structs.TypeAllocationUpdated, 16 structs.NodeUpdateStatusRequestType: structs.TypeNodeEvent, 17 structs.JobDeregisterRequestType: structs.TypeJobDeregistered, 18 structs.JobBatchDeregisterRequestType: structs.TypeJobBatchDeregistered, 19 structs.AllocUpdateDesiredTransitionRequestType: structs.TypeAllocationUpdateDesiredStatus, 20 structs.NodeUpdateEligibilityRequestType: structs.TypeNodeDrain, 21 structs.NodeUpdateDrainRequestType: structs.TypeNodeDrain, 22 structs.BatchNodeUpdateDrainRequestType: structs.TypeNodeDrain, 23 structs.DeploymentStatusUpdateRequestType: structs.TypeDeploymentUpdate, 24 structs.DeploymentPromoteRequestType: structs.TypeDeploymentPromotion, 25 structs.DeploymentAllocHealthRequestType: structs.TypeDeploymentAllocHealth, 26 structs.ApplyPlanResultsRequestType: structs.TypePlanResult, 27 structs.ACLTokenDeleteRequestType: structs.TypeACLTokenDeleted, 28 structs.ACLTokenUpsertRequestType: structs.TypeACLTokenUpserted, 29 structs.ACLPolicyDeleteRequestType: structs.TypeACLPolicyDeleted, 30 structs.ACLPolicyUpsertRequestType: structs.TypeACLPolicyUpserted, 31 } 32 33 func eventsFromChanges(tx ReadTxn, changes Changes) *structs.Events { 34 eventType, ok := MsgTypeEvents[changes.MsgType] 35 if !ok { 36 return nil 37 } 38 39 var events []structs.Event 40 for _, change := range changes.Changes { 41 if event, ok := eventFromChange(change); ok { 42 event.Type = eventType 43 event.Index = changes.Index 44 events = append(events, event) 45 } 46 } 47 48 return &structs.Events{Index: changes.Index, Events: events} 49 } 50 51 func eventFromChange(change memdb.Change) (structs.Event, bool) { 52 if change.Deleted() { 53 switch change.Table { 54 case "acl_token": 55 before, ok := change.Before.(*structs.ACLToken) 56 if !ok { 57 return structs.Event{}, false 58 } 59 60 return structs.Event{ 61 Topic: structs.TopicACLToken, 62 Key: before.AccessorID, 63 Payload: structs.NewACLTokenEvent(before), 64 }, true 65 case "acl_policy": 66 before, ok := change.Before.(*structs.ACLPolicy) 67 if !ok { 68 return structs.Event{}, false 69 } 70 return structs.Event{ 71 Topic: structs.TopicACLPolicy, 72 Key: before.Name, 73 Payload: &structs.ACLPolicyEvent{ 74 ACLPolicy: before, 75 }, 76 }, true 77 case "nodes": 78 before, ok := change.Before.(*structs.Node) 79 if !ok { 80 return structs.Event{}, false 81 } 82 83 // Node secret ID should not be included 84 node := before.Copy() 85 node.SecretID = "" 86 87 return structs.Event{ 88 Topic: structs.TopicNode, 89 Key: node.ID, 90 Payload: &structs.NodeStreamEvent{ 91 Node: node, 92 }, 93 }, true 94 } 95 return structs.Event{}, false 96 } 97 98 switch change.Table { 99 case "acl_token": 100 after, ok := change.After.(*structs.ACLToken) 101 if !ok { 102 return structs.Event{}, false 103 } 104 105 return structs.Event{ 106 Topic: structs.TopicACLToken, 107 Key: after.AccessorID, 108 Payload: structs.NewACLTokenEvent(after), 109 }, true 110 case "acl_policy": 111 after, ok := change.After.(*structs.ACLPolicy) 112 if !ok { 113 return structs.Event{}, false 114 } 115 return structs.Event{ 116 Topic: structs.TopicACLPolicy, 117 Key: after.Name, 118 Payload: &structs.ACLPolicyEvent{ 119 ACLPolicy: after, 120 }, 121 }, true 122 case "evals": 123 after, ok := change.After.(*structs.Evaluation) 124 if !ok { 125 return structs.Event{}, false 126 } 127 return structs.Event{ 128 Topic: structs.TopicEvaluation, 129 Key: after.ID, 130 FilterKeys: []string{ 131 after.JobID, 132 after.DeploymentID, 133 }, 134 Namespace: after.Namespace, 135 Payload: &structs.EvaluationEvent{ 136 Evaluation: after, 137 }, 138 }, true 139 case "allocs": 140 after, ok := change.After.(*structs.Allocation) 141 if !ok { 142 return structs.Event{}, false 143 } 144 alloc := after.Copy() 145 146 filterKeys := []string{ 147 alloc.JobID, 148 alloc.DeploymentID, 149 } 150 151 // remove job info to help keep size of alloc event down 152 alloc.Job = nil 153 154 return structs.Event{ 155 Topic: structs.TopicAllocation, 156 Key: after.ID, 157 FilterKeys: filterKeys, 158 Namespace: after.Namespace, 159 Payload: &structs.AllocationEvent{ 160 Allocation: alloc, 161 }, 162 }, true 163 case "jobs": 164 after, ok := change.After.(*structs.Job) 165 if !ok { 166 return structs.Event{}, false 167 } 168 return structs.Event{ 169 Topic: structs.TopicJob, 170 Key: after.ID, 171 Namespace: after.Namespace, 172 Payload: &structs.JobEvent{ 173 Job: after, 174 }, 175 }, true 176 case "nodes": 177 after, ok := change.After.(*structs.Node) 178 if !ok { 179 return structs.Event{}, false 180 } 181 182 // Node secret ID should not be included 183 node := after.Copy() 184 node.SecretID = "" 185 186 return structs.Event{ 187 Topic: structs.TopicNode, 188 Key: node.ID, 189 Payload: &structs.NodeStreamEvent{ 190 Node: node, 191 }, 192 }, true 193 case "deployment": 194 after, ok := change.After.(*structs.Deployment) 195 if !ok { 196 return structs.Event{}, false 197 } 198 return structs.Event{ 199 Topic: structs.TopicDeployment, 200 Key: after.ID, 201 Namespace: after.Namespace, 202 FilterKeys: []string{after.JobID}, 203 Payload: &structs.DeploymentEvent{ 204 Deployment: after, 205 }, 206 }, true 207 } 208 209 return structs.Event{}, false 210 }