Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: evaluator v2 #909

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 53 additions & 40 deletions cmd/terramate/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/terramate-io/terramate/hcl/info"
"github.com/terramate-io/terramate/modvendor/download"
"github.com/terramate-io/terramate/printer"
"github.com/terramate-io/terramate/runtime"
"github.com/terramate-io/terramate/safeguard"
"github.com/terramate-io/terramate/tg"
"github.com/terramate-io/terramate/versions"
Expand Down Expand Up @@ -112,6 +113,7 @@ type cliSpec struct {
LogDestination string `optional:"true" default:"stderr" enum:"stderr,stdout" help:"Destination channel of log messages: 'stderr' or 'stdout'."`
Quiet bool `optional:"false" help:"Disable outputs."`
Verbose int `short:"v" optional:"true" default:"0" type:"counter" help:"Increase verboseness of output"`
CPUProfiling bool `optional:"true" default:"false" help:"Create a CPU profile file when running"`

deprecatedGlobalSafeguardsCliSpec

Expand Down Expand Up @@ -590,6 +592,8 @@ Please see https://terramate.io/docs/cli/configuration/project-setup for details
fatal("flag --changed requires a repository with at least two commits")
}

globalsResolver := globals.NewResolver(&prj.root)
prj.globals = globalsResolver
uimode := HumanMode
if val := os.Getenv("CI"); envVarIsSet(val) {
uimode = AutomationMode
Expand Down Expand Up @@ -1056,7 +1060,7 @@ func (c *cli) gencodeWithVendor() (generate.Report, download.Report) {

log.Debug().Msg("generating code")

report := generate.Do(c.cfg(), c.vendorDir(), vendorRequestEvents)
report := generate.Do(c.cfg(), c.globals(), c.vendorDir(), vendorRequestEvents)

log.Debug().Msg("code generation finished, waiting for vendor requests to be handled")

Expand Down Expand Up @@ -1887,7 +1891,7 @@ func (c *cli) generateDebug() {
selectedStacks[stack.Dir()] = struct{}{}
}

results, err := generate.Load(c.cfg(), c.vendorDir())
results, err := generate.Load(c.cfg(), c.globals(), c.vendorDir())
if err != nil {
fatalWithDetails(err, "generate debug: loading generated code")
}
Expand Down Expand Up @@ -1926,12 +1930,21 @@ func (c *cli) printStacksGlobals() {

for _, stackEntry := range c.filterStacks(report.Stacks) {
stack := stackEntry.Stack
report := globals.ForStack(c.cfg(), stack)
if err := report.AsError(); err != nil {
tree := stackEntry.Stack.Tree()
evalctx := eval.New(
stack.Dir,
runtime.NewResolver(c.cfg(), stack),
c.globals(),
)
evalctx.SetFunctions(stdlib.Functions(evalctx, tree.HostDir()))

expr, _ := ast.ParseExpression(`global`, `<print-globals>`)
globals, err := evalctx.Eval(expr)
if err != nil {
fatalWithDetails(err, "listing stacks globals: loading stack at %s", stack.Dir)
}

globalsStrRepr := report.Globals.String()
globalsStrRepr := fmt.FormatAttributes(globals.AsValueMap())
if globalsStrRepr == "" {
continue
}
Expand Down Expand Up @@ -2056,6 +2069,18 @@ func (c *cli) partialEval() {
}
}

func (c *cli) detectEvalContext(overrideGlobals map[string]string) *eval.Context {
var st *config.Stack
if config.IsStack(c.cfg(), c.wd()) {
var err error
st, err = config.LoadStack(c.cfg(), prj.PrjAbsPath(c.rootdir(), c.wd()))
if err != nil {
fatalWithDetails(err, "setup eval context: loading stack config")
}
}
return c.setupEvalContext(st, overrideGlobals)
}

func (c *cli) evalRunArgs(st *config.Stack, cmd []string) ([]string, error) {
ctx := c.setupEvalContext(st, map[string]string{})
var newargs []string
Expand Down Expand Up @@ -2125,42 +2150,23 @@ func (c *cli) outputEvalResult(val cty.Value, asJSON bool) {
c.output.MsgStdOut("%s", string(data))
}

func (c *cli) detectEvalContext(overrideGlobals map[string]string) *eval.Context {
var st *config.Stack
if config.IsStack(c.cfg(), c.wd()) {
var err error
st, err = config.LoadStack(c.cfg(), prj.PrjAbsPath(c.rootdir(), c.wd()))
if err != nil {
fatalWithDetails(err, "setup eval context: loading stack config")
}
}
return c.setupEvalContext(st, overrideGlobals)
}

func (c *cli) setupEvalContext(st *config.Stack, overrideGlobals map[string]string) *eval.Context {
runtime := c.cfg().Runtime()

var pdir prj.Path
var tdir string
if st != nil {
tdir = st.HostDir(c.cfg())
runtime.Merge(st.RuntimeValues(c.cfg()))
pdir = st.Dir
} else {
pdir = prj.PrjAbsPath(c.rootdir(), c.wd())
tdir = c.wd()
}

ctx := eval.NewContext(stdlib.NoFS(tdir))
ctx.SetNamespace("terramate", runtime)

wdPath := prj.PrjAbsPath(c.rootdir(), tdir)
tree, ok := c.cfg().Lookup(wdPath)
if !ok {
fatalWithDetails(errors.E("configuration at %s not found", wdPath), "Missing configuration")
}
exprs, err := globals.LoadExprs(tree)
if err != nil {
fatalWithDetails(err, "loading globals expressions")
}

var overrideStmts eval.Stmts
for name, exprStr := range overrideGlobals {
expr, err := ast.ParseExpression(exprStr, "<cmdline>")
if err != nil {
Expand All @@ -2170,20 +2176,26 @@ func (c *cli) setupEvalContext(st *config.Stack, overrideGlobals map[string]stri
)
}
parts := strings.Split(name, ".")
length := len(parts)
globalPath := globals.NewGlobalAttrPath(parts[0:length-1], parts[length-1])
exprs.SetOverride(
wdPath,
globalPath,
expr,
info.NewRange(c.rootdir(), hhcl.Range{
Filename: "<eval argument>",
ref := eval.NewRef("global", parts...)
overrideStmts = append(overrideStmts, eval.Stmt{
Origin: ref,
LHS: ref,
Info: eval.NewInfo(pdir, info.NewRange(c.rootdir(), hhcl.Range{
Start: hhcl.InitialPos,
End: hhcl.InitialPos,
}),
)
Filename: `<cmdline>`,
})),
RHS: eval.NewExprRHS(expr),
})
}
_ = exprs.Eval(ctx)

ctx := eval.New(
tree.Dir(),
runtime.NewResolver(c.cfg(), st),
globals.NewResolver(c.cfg(), overrideStmts...),
)

ctx.SetFunctions(stdlib.NoFS(ctx, c.wd()))
return ctx
}

Expand All @@ -2200,7 +2212,7 @@ func (c *cli) checkOutdatedGeneratedCode() {
return
}

outdatedFiles, err := generate.DetectOutdated(c.cfg(), c.vendorDir())
outdatedFiles, err := generate.DetectOutdated(c.cfg(), c.globals(), c.vendorDir())
if err != nil {
fatalWithDetails(err, "failed to check outdated code on project")
}
Expand Down Expand Up @@ -2248,6 +2260,7 @@ func (c *cli) gitSafeguardRemoteEnabled() bool {
func (c *cli) wd() string { return c.prj.wd }
func (c *cli) rootdir() string { return c.prj.rootdir }
func (c *cli) cfg() *config.Root { return &c.prj.root }
func (c *cli) globals() *globals.Resolver { return c.prj.globals }
func (c *cli) baseRef() string { return c.prj.baseRef }
func (c *cli) stackManager() *stack.Manager { return c.prj.stackManager }
func (c *cli) rootNode() hcl.Config { return c.prj.root.Tree().Node }
Expand Down
2 changes: 2 additions & 0 deletions cmd/terramate/cli/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/terramate-io/terramate/config"
"github.com/terramate-io/terramate/errors"
"github.com/terramate-io/terramate/git"
"github.com/terramate-io/terramate/globals"
"github.com/terramate-io/terramate/hcl"
"github.com/terramate-io/terramate/stack"
)
Expand All @@ -21,6 +22,7 @@ type project struct {
isRepo bool
root config.Root
baseRef string
globals *globals.Resolver
normalizedRepo string
stackManager *stack.Manager

Expand Down
18 changes: 6 additions & 12 deletions cmd/terramate/cli/script_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/terramate-io/terramate/hcl/eval"
"github.com/terramate-io/terramate/printer"
prj "github.com/terramate-io/terramate/project"
"github.com/terramate-io/terramate/runtime"
"github.com/terramate-io/terramate/stdlib"
)

Expand Down Expand Up @@ -83,7 +84,7 @@ func (c *cli) runScript() {
for _, st := range result.Stacks {
run := stackRun{Stack: st.Stack}

ectx, err := scriptEvalContext(c.cfg(), st.Stack)
ectx, err := scriptEvalContext(c.cfg(), st.Stack, c.globals())
if err != nil {
fatalWithDetails(err, "failed to get context")
}
Expand Down Expand Up @@ -218,18 +219,11 @@ func printScriptCommand(w io.Writer, stack *config.Stack, run stackRunTask) {
fmt.Fprintln(w, prompt, color.YellowString(strings.Join(run.Cmd, " ")))
}

func scriptEvalContext(root *config.Root, st *config.Stack) (*eval.Context, error) {
globalsReport := globals.ForStack(root, st)
if err := globalsReport.AsError(); err != nil {
return nil, err
}
func scriptEvalContext(root *config.Root, st *config.Stack, globalsResolver *globals.Resolver) (*eval.Context, error) {

evalctx := eval.NewContext(stdlib.Functions(st.HostDir(root)))
runtime := root.Runtime()
runtime.Merge(st.RuntimeValues(root))
evalctx.SetNamespace("terramate", runtime)
evalctx.SetNamespace("global", globalsReport.Globals.AsValueMap())
evalctx.SetEnv(os.Environ())
evalctx := eval.New(st.Dir, globalsResolver, runtime.NewResolver(root, st))
evalctx.SetFunctions(stdlib.Functions(evalctx, st.HostDir(root)))
//evalctx.SetEnv(os.Environ())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be fixed later


return evalctx, nil
}
Expand Down
9 changes: 6 additions & 3 deletions config/assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/terramate-io/terramate/errors"
"github.com/terramate-io/terramate/hcl"
"github.com/terramate-io/terramate/hcl/eval"
"github.com/terramate-io/terramate/project"
"github.com/terramate-io/terramate/stdlib"
"github.com/terramate-io/terramate/test"
"github.com/zclconf/go-cty/cty"
Expand Down Expand Up @@ -175,13 +176,15 @@ func TestAssertConfigEval(t *testing.T) {
tcase := tcase
t.Run(tcase.name, func(t *testing.T) {
t.Parallel()
hclctx := eval.NewContext(stdlib.Functions(test.TempDir(t)))
evalctx := eval.New(project.RootPath)
funcs := stdlib.Functions(evalctx, t.TempDir())
evalctx.SetFunctions(funcs)

for k, v := range tcase.namespaces {
hclctx.SetNamespace(k, v.asCtyMap())
evalctx.SetNamespace(k, v.asCtyMap())
}

got, err := config.EvalAssert(hclctx, tcase.assert)
got, err := config.EvalAssert(evalctx, tcase.assert)
assert.IsError(t, err, tcase.wantErr)
if !equalAsserts(tcase.want, got) {
t.Fatalf("got %#v != want %#v", got, tcase.want)
Expand Down
14 changes: 11 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ const (
type Root struct {
tree Tree

lookupCache map[string]*Tree

// hasTerragruntStacks tells if the repository has any Terragrunt stack.
hasTerragruntStacks *bool

runtime project.Runtime
runtime project.Runtime
}

// Tree is the configuration tree.
Expand Down Expand Up @@ -124,6 +125,7 @@ func NewRoot(tree *Tree) *Root {
r := &Root{}
tree.root = r
r.tree = *tree
r.lookupCache = make(map[string]*Tree)

r.initRuntime()
return r
Expand All @@ -146,7 +148,13 @@ func (root *Root) HostDir() string { return root.tree.RootDir() }

// Lookup a node from the root using a filesystem query path.
func (root *Root) Lookup(path project.Path) (*Tree, bool) {
return root.tree.lookup(path)
tree, ok := root.lookupCache[path.String()]
if ok {
return tree, tree != nil
}
tree, ok = root.tree.lookup(path)
root.lookupCache[path.String()] = tree
return tree, ok
}

// StacksByPaths returns the stacks from the provided relative paths.
Expand Down
5 changes: 4 additions & 1 deletion config/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/terramate-io/terramate/hcl/ast"
"github.com/terramate-io/terramate/hcl/eval"
"github.com/terramate-io/terramate/hcl/info"
"github.com/terramate-io/terramate/project"
"github.com/terramate-io/terramate/stdlib"
"github.com/terramate-io/terramate/test"

Expand Down Expand Up @@ -785,7 +786,9 @@ func TestScriptEval(t *testing.T) {
tcase := tcase
t.Run(tcase.name, func(t *testing.T) {
t.Parallel()
hclctx := eval.NewContext(stdlib.Functions(test.TempDir(t)))
scopedir := test.TempDir(t)
hclctx := eval.New(project.NewPath("/"))
hclctx.SetFunctions(stdlib.Functions(hclctx, scopedir))
hclctx.SetNamespace("global", tcase.globals)

got, err := config.EvalScript(hclctx, tcase.script)
Expand Down
Loading
Loading