open-nomad/nomad/structs/json_encoding.go
Chris Baker dd291e69f4 removed deprecated fields from Drain structs and API
node drain: use msgtype on txn so that events are emitted
wip: encoding extension to add Node.Drain field back to API responses

new approach for hiding Node.SecretID in the API, using `json` tag
documented this approach in the contributing guide
refactored the JSON handlers with extensions
modified event stream encoding to use the go-msgpack encoders with the extensions
2021-03-21 15:30:11 +00:00

37 lines
819 B
Go

package structs
import (
"reflect"
"github.com/hashicorp/go-msgpack/codec"
)
// Special encoding for structs.Node, to perform the following:
// 1. provide backwards compatibility for the following fields:
// * Node.Drain
type nodeExt struct{}
// ConvertExt converts a structs.Node to a struct with the extra field, Drain
func (n nodeExt) ConvertExt(v interface{}) interface{} {
node := v.(*Node)
if node == nil {
return nil
}
type NodeAlias Node
return &struct {
*NodeAlias
Drain bool
}{
NodeAlias: (*NodeAlias)(node),
Drain: node.DrainStrategy != nil,
}
}
// UpdateExt is not used
func (n nodeExt) UpdateExt(_ interface{}, _ interface{}) {}
func RegisterJSONEncodingExtensions(h *codec.JsonHandle) *codec.JsonHandle {
h.SetInterfaceExt(reflect.TypeOf(Node{}), 1, nodeExt{})
return h
}