mirror of
https://github.com/kemko/nomad.git
synced 2026-01-04 17:35:43 +03:00
simplify hcl2 parsing helper
No need to pass in the entire eval context
This commit is contained in:
@@ -11,7 +11,6 @@ import (
|
||||
metrics "github.com/armon/go-metrics"
|
||||
log "github.com/hashicorp/go-hclog"
|
||||
multierror "github.com/hashicorp/go-multierror"
|
||||
"github.com/hashicorp/hcl2/hcl"
|
||||
"github.com/hashicorp/hcl2/hcldec"
|
||||
"github.com/hashicorp/nomad/client/allocdir"
|
||||
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
||||
@@ -620,12 +619,7 @@ func (tr *TaskRunner) runDriver() error {
|
||||
tr.logger.Warn("some environment variables not available for rendering", "keys", strings.Join(keys, ", "))
|
||||
}
|
||||
|
||||
evalCtx := &hcl.EvalContext{
|
||||
Variables: vars,
|
||||
Functions: hclutils.GetStdlibFuncs(),
|
||||
}
|
||||
|
||||
val, diag := hclutils.ParseHclInterface(tr.task.Config, tr.taskSchema, evalCtx)
|
||||
val, diag := hclutils.ParseHclInterface(tr.task.Config, tr.taskSchema, vars)
|
||||
if diag.HasErrors() {
|
||||
return multierror.Append(errors.New("failed to parse config"), diag.Errs()...)
|
||||
}
|
||||
|
||||
@@ -15,8 +15,14 @@ import (
|
||||
)
|
||||
|
||||
// ParseHclInterface is used to convert an interface value representing a hcl2
|
||||
// body and return the interpolated value.
|
||||
func ParseHclInterface(val interface{}, spec hcldec.Spec, ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
|
||||
// body and return the interpolated value. Vars may be nil if there are no
|
||||
// variables to interpolate.
|
||||
func ParseHclInterface(val interface{}, spec hcldec.Spec, vars map[string]cty.Value) (cty.Value, hcl.Diagnostics) {
|
||||
evalCtx := &hcl.EvalContext{
|
||||
Variables: vars,
|
||||
Functions: GetStdlibFuncs(),
|
||||
}
|
||||
|
||||
// Encode to json
|
||||
var buf bytes.Buffer
|
||||
enc := codec.NewEncoder(&buf, structs.JsonHandle)
|
||||
@@ -37,7 +43,7 @@ func ParseHclInterface(val interface{}, spec hcldec.Spec, ctx *hcl.EvalContext)
|
||||
return cty.NilVal, diag
|
||||
}
|
||||
|
||||
value, decDiag := hcldec.Decode(hclFile.Body, spec, ctx)
|
||||
value, decDiag := hcldec.Decode(hclFile.Body, spec, evalCtx)
|
||||
diag = diag.Extend(decDiag)
|
||||
if diag.HasErrors() {
|
||||
return cty.NilVal, diag
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
multierror "github.com/hashicorp/go-multierror"
|
||||
plugin "github.com/hashicorp/go-plugin"
|
||||
version "github.com/hashicorp/go-version"
|
||||
hcl2 "github.com/hashicorp/hcl2/hcl"
|
||||
"github.com/hashicorp/nomad/helper/pluginutils/hclspecutils"
|
||||
"github.com/hashicorp/nomad/helper/pluginutils/hclutils"
|
||||
"github.com/hashicorp/nomad/nomad/structs/config"
|
||||
@@ -18,14 +17,6 @@ import (
|
||||
"github.com/zclconf/go-cty/cty/msgpack"
|
||||
)
|
||||
|
||||
var (
|
||||
// configParseCtx is the context used to parse a plugin's configuration
|
||||
// stanza
|
||||
configParseCtx = &hcl2.EvalContext{
|
||||
Functions: hclutils.GetStdlibFuncs(),
|
||||
}
|
||||
)
|
||||
|
||||
// validateConfig returns whether or not the configuration is valid
|
||||
func validateConfig(config *PluginLoaderConfig) error {
|
||||
var mErr multierror.Error
|
||||
@@ -466,7 +457,7 @@ func (l *PluginLoader) validatePluginConfig(id PluginID, info *pluginInfo) error
|
||||
}
|
||||
|
||||
// Parse the config using the spec
|
||||
val, diag := hclutils.ParseHclInterface(info.config, spec, configParseCtx)
|
||||
val, diag := hclutils.ParseHclInterface(info.config, spec, nil)
|
||||
if diag.HasErrors() {
|
||||
multierror.Append(&mErr, diag.Errs()...)
|
||||
return multierror.Prefix(&mErr, "failed parsing config:")
|
||||
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
plugin "github.com/hashicorp/go-plugin"
|
||||
"github.com/hashicorp/hcl"
|
||||
"github.com/hashicorp/hcl/hcl/ast"
|
||||
hcl2 "github.com/hashicorp/hcl2/hcl"
|
||||
"github.com/hashicorp/hcl2/hcldec"
|
||||
"github.com/hashicorp/nomad/helper/pluginutils/hclspecutils"
|
||||
"github.com/hashicorp/nomad/helper/pluginutils/hclutils"
|
||||
@@ -197,11 +196,7 @@ func (c *Device) setConfig(spec hcldec.Spec, apiVersion string, config []byte, n
|
||||
|
||||
c.logger.Trace("raw hcl config", "config", hclog.Fmt("% #v", pretty.Formatter(configVal)))
|
||||
|
||||
ctx := &hcl2.EvalContext{
|
||||
Functions: hclutils.GetStdlibFuncs(),
|
||||
}
|
||||
|
||||
val, diag := hclutils.ParseHclInterface(configVal, spec, ctx)
|
||||
val, diag := hclutils.ParseHclInterface(configVal, spec, nil)
|
||||
if diag.HasErrors() {
|
||||
errStr := "failed to parse config"
|
||||
for _, err := range diag.Errs() {
|
||||
|
||||
Reference in New Issue
Block a user