open-nomad/nomad/state/node_events.go
Drew Bailey c463479848
filter on additional filter keys, remove switch statement duplication
properly wire up durable event count

move newline responsibility

moves newline creation from NDJson to the http handler, json stream only encodes and sends now

ignore snapshot restore if broker is disabled

enable dev mode to access event steam without acl

use mapping instead of switch

use pointers for config sizes, remove unused ttl, simplify closed conn logic
2020-10-14 14:14:33 -04:00

83 lines
2.1 KiB
Go

package state
import (
"fmt"
"github.com/hashicorp/nomad/nomad/structs"
)
// NodeDeregisterEventFromChanges generates a NodeDeregistrationEvent from a set
// of transaction changes.
func NodeDeregisterEventFromChanges(tx ReadTxn, changes Changes) (*structs.Events, error) {
var events []structs.Event
for _, change := range changes.Changes {
switch change.Table {
case "nodes":
before, ok := change.Before.(*structs.Node)
if !ok {
return nil, fmt.Errorf("transaction change was not a Node")
}
event := structs.Event{
Topic: structs.TopicNode,
Type: TypeNodeDeregistration,
Index: changes.Index,
Key: before.ID,
Payload: &NodeEvent{
Node: before,
},
}
events = append(events, event)
}
}
return &structs.Events{Index: changes.Index, Events: events}, nil
}
func NodeDrainEventFromChanges(tx ReadTxn, changes Changes) (*structs.Events, error) {
var events []structs.Event
for _, change := range changes.Changes {
switch change.Table {
case "nodes":
after, ok := change.After.(*structs.Node)
if !ok {
return nil, fmt.Errorf("transaction change was not a Node")
}
// retrieve allocations currently on node
allocs, err := allocsByNodeTxn(tx, nil, after.ID)
if err != nil {
return nil, fmt.Errorf("retrieving allocations for node drain event: %w", err)
}
// build job/alloc details for node drain
jobAllocs := make(map[string]*JobDrainDetails)
for _, a := range allocs {
if _, ok := jobAllocs[a.Job.Name]; !ok {
jobAllocs[a.Job.Name] = &JobDrainDetails{
AllocDetails: make(map[string]NodeDrainAllocDetails),
Type: a.Job.Type,
}
}
jobAllocs[a.Job.Name].AllocDetails[a.ID] = NodeDrainAllocDetails{
Migrate: a.MigrateStrategy(),
ID: a.ID,
}
}
event := structs.Event{
Topic: structs.TopicNode,
Type: TypeNodeDrain,
Index: changes.Index,
Key: after.ID,
Payload: &NodeDrainEvent{
Node: after,
JobAllocs: jobAllocs,
},
}
events = append(events, event)
}
}
return &structs.Events{Index: changes.Index, Events: events}, nil
}