moved JSON handlers and extension code around a bit for proper order of

initialization
This commit is contained in:
Chris Baker
2021-03-22 14:12:42 +00:00
parent ca51bd91bd
commit 0cd707e3a9
12 changed files with 86 additions and 57 deletions

View File

@@ -8,14 +8,17 @@ import (
"time"
"github.com/hashicorp/go-msgpack/codec"
"github.com/hashicorp/nomad/command/agent/host"
"github.com/hashicorp/nomad/command/agent/monitor"
"github.com/hashicorp/nomad/command/agent/pprof"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/nomad/json/handlers"
"github.com/hashicorp/nomad/nomad/structs"
metrics "github.com/armon/go-metrics"
log "github.com/hashicorp/go-hclog"
sframer "github.com/hashicorp/nomad/client/lib/streamframer"
cstructs "github.com/hashicorp/nomad/client/structs"
)
@@ -121,7 +124,7 @@ func (a *Agent) monitor(conn io.ReadWriteCloser) {
frames := make(chan *sframer.StreamFrame, streamFramesBuffer)
errCh := make(chan error)
var buf bytes.Buffer
frameCodec := codec.NewEncoder(&buf, structs.JsonHandle)
frameCodec := codec.NewEncoder(&buf, handlers.JsonHandle)
framer := sframer.NewStreamFramer(frames, 1*time.Second, 200*time.Millisecond, 1024)
framer.Run()

View File

@@ -10,10 +10,12 @@ import (
metrics "github.com/armon/go-metrics"
"github.com/hashicorp/go-msgpack/codec"
"github.com/hashicorp/nomad/acl"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/nomad/json/handlers"
"github.com/hashicorp/nomad/nomad/structs"
nstructs "github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
@@ -279,7 +281,7 @@ func newExecStream(decoder *codec.Decoder, encoder *codec.Encoder) drivers.ExecT
buf: buf,
encoder: encoder,
frameCodec: codec.NewEncoder(buf, structs.JsonHandle),
frameCodec: codec.NewEncoder(buf, handlers.JsonHandle),
}
}

View File

@@ -17,13 +17,15 @@ import (
metrics "github.com/armon/go-metrics"
"github.com/hashicorp/go-msgpack/codec"
"github.com/hpcloud/tail/watch"
"github.com/hashicorp/nomad/acl"
"github.com/hashicorp/nomad/client/allocdir"
sframer "github.com/hashicorp/nomad/client/lib/streamframer"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/nomad/json/handlers"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hpcloud/tail/watch"
)
var (
@@ -237,7 +239,7 @@ func (f *FileSystem) stream(conn io.ReadWriteCloser) {
frames := make(chan *sframer.StreamFrame, streamFramesBuffer)
errCh := make(chan error)
var buf bytes.Buffer
frameCodec := codec.NewEncoder(&buf, structs.JsonHandle)
frameCodec := codec.NewEncoder(&buf, handlers.JsonHandle)
// Create the framer
framer := sframer.NewStreamFramer(frames, streamHeartbeatRate, streamBatchWindow, streamFrameSize)
@@ -468,7 +470,7 @@ func (f *FileSystem) logs(conn io.ReadWriteCloser) {
var streamErr error
buf := new(bytes.Buffer)
frameCodec := codec.NewEncoder(buf, structs.JsonHandle)
frameCodec := codec.NewEncoder(buf, handlers.JsonHandle)
OUTER:
for {
select {

View File

@@ -24,6 +24,7 @@ import (
"github.com/hashicorp/nomad/helper/noxssrw"
"github.com/hashicorp/nomad/helper/tlsutil"
"github.com/hashicorp/nomad/nomad/json/handlers"
"github.com/hashicorp/nomad/nomad/structs"
)
@@ -495,13 +496,13 @@ func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Reque
if obj != nil {
var buf bytes.Buffer
if prettyPrint {
enc := codec.NewEncoder(&buf, structs.JsonHandlePretty)
enc := codec.NewEncoder(&buf, handlers.JsonHandlePretty)
err = enc.Encode(obj)
if err == nil {
buf.Write([]byte("\n"))
}
} else {
enc := codec.NewEncoder(&buf, structs.JsonHandleWithExtensions)
enc := codec.NewEncoder(&buf, handlers.JsonHandleWithExtensions)
err = enc.Encode(obj)
}
if err != nil {

View File

@@ -6,13 +6,14 @@ import (
"github.com/hashicorp/go-msgpack/codec"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/nomad/helper/pluginutils/hclspecutils"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
"github.com/hashicorp/nomad/plugins/shared/hclspec"
"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/require"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/nomad/helper/pluginutils/hclspecutils"
"github.com/hashicorp/nomad/nomad/json/handlers"
"github.com/hashicorp/nomad/plugins/drivers"
"github.com/hashicorp/nomad/plugins/shared/hclspec"
)
type HCLParser struct {
@@ -121,7 +122,7 @@ func JsonConfigToInterface(t *testing.T, config string) interface{} {
t.Helper()
// Decode from json
dec := codec.NewDecoderBytes([]byte(config), structs.JsonHandle)
dec := codec.NewDecoderBytes([]byte(config), handlers.JsonHandle)
var m map[string]interface{}
err := dec.Decode(&m)

View File

@@ -9,7 +9,9 @@ import (
hcl "github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hcldec"
hjson "github.com/hashicorp/hcl/v2/json"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/nomad/json/handlers"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
"github.com/zclconf/go-cty/cty/function/stdlib"
@@ -26,7 +28,7 @@ func ParseHclInterface(val interface{}, spec hcldec.Spec, vars map[string]cty.Va
// Encode to json
var buf bytes.Buffer
enc := codec.NewEncoder(&buf, structs.JsonHandle)
enc := codec.NewEncoder(&buf, handlers.JsonHandle)
err := enc.Encode(val)
if err != nil {
// Convert to a hcl diagnostics message

View File

@@ -10,12 +10,14 @@ import (
"time"
log "github.com/hashicorp/go-hclog"
sframer "github.com/hashicorp/nomad/client/lib/streamframer"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/command/agent/host"
"github.com/hashicorp/nomad/command/agent/monitor"
"github.com/hashicorp/nomad/command/agent/pprof"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/nomad/json/handlers"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/go-msgpack/codec"
@@ -185,7 +187,7 @@ func (a *Agent) monitor(conn io.ReadWriteCloser) {
frames := make(chan *sframer.StreamFrame, 32)
errCh := make(chan error)
var buf bytes.Buffer
frameCodec := codec.NewEncoder(&buf, structs.JsonHandle)
frameCodec := codec.NewEncoder(&buf, handlers.JsonHandle)
framer := sframer.NewStreamFramer(frames, 1*time.Second, 200*time.Millisecond, 1024)
framer.Run()

View File

@@ -1,4 +1,4 @@
package structs
package json
import (
"reflect"
@@ -6,27 +6,6 @@ import (
"github.com/hashicorp/go-msgpack/codec"
)
func init() {
registerExtension(reflect.TypeOf(Node{}), nodeExt)
}
func nodeExt(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,
}
}
// BOILERPLATE GOES HERE
type extendFunc func(interface{}) interface{}
var (
@@ -44,7 +23,8 @@ func (n nomadJsonEncodingExtensions) ConvertExt(v interface{}) interface{} {
if fn, ok := extendedTypes[reflect.TypeOf(v)]; ok {
return fn(v)
} else {
return nil
// shouldn't get here
return v
}
}
@@ -52,7 +32,7 @@ func (n nomadJsonEncodingExtensions) ConvertExt(v interface{}) interface{} {
func (n nomadJsonEncodingExtensions) UpdateExt(_ interface{}, _ interface{}) {}
func NomadJsonEncodingExtensions(h *codec.JsonHandle) *codec.JsonHandle {
for tpe, _ := range extendedTypes {
for tpe := range extendedTypes {
h.SetInterfaceExt(tpe, 1, nomadJsonEncodingExtensions{})
}
return h

28
nomad/json/extensions.go Normal file
View File

@@ -0,0 +1,28 @@
package json
import (
"reflect"
"github.com/hashicorp/nomad/nomad/structs"
)
func init() {
// TODO: this could be simplified by looking up the base type in the case of a pointer type
registerExtension(reflect.TypeOf(structs.Node{}), nodeExt)
registerExtension(reflect.TypeOf(&structs.Node{}), nodeExt)
}
func nodeExt(v interface{}) interface{} {
node := v.(*structs.Node)
if node == nil {
return nil
}
type NodeAlias structs.Node
return &struct {
*NodeAlias
Drain bool
}{
NodeAlias: (*NodeAlias)(node),
Drain: node.DrainStrategy != nil,
}
}

View File

@@ -0,0 +1,24 @@
package handlers
import (
"github.com/hashicorp/go-msgpack/codec"
"github.com/hashicorp/nomad/nomad/json"
)
var (
// JsonHandle and JsonHandlePretty are the codec handles to JSON encode
// structs. The pretty handle will add indents for easier human consumption.
// JsonHandleWithExtensions and JsonHandlePretty include extensions for
// encoding structs objects with API-specific fields
JsonHandle = &codec.JsonHandle{
HTMLCharsAsIs: true,
}
JsonHandleWithExtensions = json.NomadJsonEncodingExtensions(&codec.JsonHandle{
HTMLCharsAsIs: true,
})
JsonHandlePretty = json.NomadJsonEncodingExtensions(&codec.JsonHandle{
HTMLCharsAsIs: true,
Indent: 4,
})
)

View File

@@ -8,6 +8,7 @@ import (
"github.com/hashicorp/go-msgpack/codec"
"github.com/hashicorp/nomad/nomad/json/handlers"
"github.com/hashicorp/nomad/nomad/structs"
)
@@ -74,7 +75,7 @@ func (n *JsonStream) Send(v interface{}) error {
}
var buf bytes.Buffer
enc := codec.NewEncoder(&buf, structs.JsonHandleWithExtensions)
enc := codec.NewEncoder(&buf, handlers.JsonHandleWithExtensions)
err := enc.Encode(v)
if err != nil {
return fmt.Errorf("error marshaling json for stream: %w", err)

View File

@@ -10581,23 +10581,6 @@ var MsgpackHandle = func() *codec.MsgpackHandle {
return h
}()
var (
// JsonHandle and JsonHandlePretty are the codec handles to JSON encode
// structs. The pretty handle will add indents for easier human consumption.
// JsonHandleWithExtensions and JsonHandlePretty include extensions for
// encoding structs objects with API-specific fields
JsonHandle = &codec.JsonHandle{
HTMLCharsAsIs: true,
}
JsonHandleWithExtensions = NomadJsonEncodingExtensions(&codec.JsonHandle{
HTMLCharsAsIs: true,
})
JsonHandlePretty = NomadJsonEncodingExtensions(&codec.JsonHandle{
HTMLCharsAsIs: true,
Indent: 4,
})
)
// Decode is used to decode a MsgPack encoded object
func Decode(buf []byte, out interface{}) error {
return codec.NewDecoder(bytes.NewReader(buf), MsgpackHandle).Decode(out)