From 1d17fbc68147c6c785fedb95fac8dc709a32b584 Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Sat, 2 Feb 2019 12:19:16 -0800 Subject: [PATCH] simplify hcl2 parsing helper No need to pass in the entire eval context --- client/allocrunner/taskrunner/task_runner.go | 8 +------- helper/pluginutils/hclutils/util.go | 12 +++++++++--- helper/pluginutils/loader/init.go | 11 +---------- plugins/shared/cmd/launcher/command/device.go | 7 +------ 4 files changed, 12 insertions(+), 26 deletions(-) diff --git a/client/allocrunner/taskrunner/task_runner.go b/client/allocrunner/taskrunner/task_runner.go index 153999069..38fb488db 100644 --- a/client/allocrunner/taskrunner/task_runner.go +++ b/client/allocrunner/taskrunner/task_runner.go @@ -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()...) } diff --git a/helper/pluginutils/hclutils/util.go b/helper/pluginutils/hclutils/util.go index 86a8d2e6c..6e39a5a6c 100644 --- a/helper/pluginutils/hclutils/util.go +++ b/helper/pluginutils/hclutils/util.go @@ -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 diff --git a/helper/pluginutils/loader/init.go b/helper/pluginutils/loader/init.go index 774156c8d..2bf714ecc 100644 --- a/helper/pluginutils/loader/init.go +++ b/helper/pluginutils/loader/init.go @@ -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:") diff --git a/plugins/shared/cmd/launcher/command/device.go b/plugins/shared/cmd/launcher/command/device.go index 5b5d4855c..031137e1e 100644 --- a/plugins/shared/cmd/launcher/command/device.go +++ b/plugins/shared/cmd/launcher/command/device.go @@ -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() {