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

Hide the Jsonnet Go implementation behind an interface #914

Merged
merged 3 commits into from
Aug 24, 2023

Conversation

julienduchesne
Copy link
Member

@julienduchesne julienduchesne commented Aug 24, 2023

Tanka currently evals jsonnet using the Go native code.
However, some other implementations have come up in the past years that could be worth using (ex: https://github.com/CertainLach/jrsonnet, which is much faster)

In this PR is the first step: I create an interface where all the jsonnet eval code happens. The Go Jsonnet implementation is now hidden behind this interface.

The setting can either be passed as a global flag or as an env spec attribute to be used when exporting (spec.exportJsonnetImplementation).

@julienduchesne julienduchesne force-pushed the julienduchesne/eval-interface branch 3 times, most recently from ef9265d to 806ca42 Compare August 24, 2023 00:46
@github-actions
Copy link

github-actions bot commented Aug 24, 2023

PR Preview Action v1.4.4
Preview removed because the pull request was closed.
2023-08-24 17:58 UTC

@julienduchesne julienduchesne marked this pull request as ready for review August 24, 2023 00:50
@julienduchesne julienduchesne requested a review from a team August 24, 2023 00:50
Tanka currently evals jsonnet using the Go native code.
However, some other implementations have come up in the past years that could be worth using (ex: https://github.com/CertainLach/jrsonnet, which is much faster)

In this PR is the first step: I create an interface where all the jsonnet eval code happens. The Go Jsonnet implementation is now hidden behind this interface.

The setting can either be passed as a global flag or as an env spec attribute to be used when exporting (`spec.exportJsonnetImplementation`)
Copy link
Member

@iainlane iainlane left a comment

Choose a reason for hiding this comment

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

Cool idea, it'll be nice to be able to try alternative implementations. 👍

I did have a couple of comments (and some inline ones)

naming nit: The term "VM" feels a bit implementation-specific even though both the current ones do use it, maybe something like "Evaluator" would be more generic?

compatibility: you are shuffling public modules around and making some things private. Since Tanka follows semver, that would require a new major version. Would it be possible to keep compatibility, defaulting to the go implementation, and have this functionality be transparent unless you want to use it?

pkg/jsonnet/implementation/implementation.go Outdated Show resolved Hide resolved
package types

type JsonnetVM interface {
EvaluateAnonymousSnippet(filename, snippet string) (string, error)
Copy link
Member

Choose a reason for hiding this comment

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

It doesn't seem like it's really necessary for this method to know the filename since it's "anonymous" - this is something that go-jsonnet uses to show you nicer errors. I checked the binary implementation from #915 and it ignores this too.

Would suggest this type:

Suggested change
EvaluateAnonymousSnippet(filename, snippet string) (string, error)
EvaluateAnonymousSnippet(snippet string) (string, error)

And arranging for the Go implementation to have the filename as a member on the wrapper struct.

Copy link
Member Author

Choose a reason for hiding this comment

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

Filename can't be on the wrapper. The same evaluator could be used to eval multiple snippets of different files

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd leave it there

Copy link
Member

Choose a reason for hiding this comment

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

Where in the code can that happen?

Could definitely be missing something but the only caller I can find is this one which calls evaluateSnippet, also with the path, and that always calls implementation.Get which returns a new instance, not a singleton, and then we call MakeVM on that to make a new VM.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the "how it's being used currently" is not entirely relevant. A Go Jsonnet VM can be re-used if the import paths or ext code do not change. So, for our concerns, a VM/Evaluator is defined by those parameters. If we add the path to the MakeEvaluator command, then the interface gets weird

Also, I'm thinking I may add the filename to the anonymous eval error message for the binary implementation as well

Copy link
Member

Choose a reason for hiding this comment

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

Also, I'm thinking I may add the filename to the anonymous eval error message for the binary implementation as well

This was a good comment that made me realise how it should be done: since the thing you care about this path for is error messages, print the filename at the call site when it makes sense to do so (i.e.the place where Evaluate() is called from).

diff --git a/pkg/tanka/evaluators.go b/pkg/tanka/evaluators.go
index 64c761cb..6f95230f 100644
--- a/pkg/tanka/evaluators.go
+++ b/pkg/tanka/evaluators.go
@@ -39,7 +39,7 @@ function(%s)
 
                raw, err = jsonnet.Evaluate(path, evalScript, opts)
                if err != nil {
-                       return "", errors.Wrap(err, "evaluating jsonnet")
+                       return "", fmt.Errorf("evaluating jsonnet for '%s': %w", path, err)
                }
                return raw, nil
        }

Or something

Copy link
Member Author

Choose a reason for hiding this comment

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

The Go implementation still needs the filepath to print out its stack traces though. How would you pass it?

Copy link
Member

Choose a reason for hiding this comment

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

I would move the selection of the implementation outwards a bit, and then it becomes more natural to pass the path when you select the Go one.

See 82ae5ae for something like what I mean, in case that's clearer.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, that works for me. The only thing that is weird is that vm.path is ignored when you EvaluateFile, so the path does nothing in that case and you have to specify it twice. But it's not any weirder than what I was doing

Also, I like that the loaders will use the specified implementation for all operations, including listing envs

Can you merge your PR into this one, or make yours ready to review and we can close this one?

Copy link
Member

Choose a reason for hiding this comment

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

Alright, cheers. I pushed that commit here, I'll give a ✔️ for your bits, guess we can consider this a cross review heh

pkg/jsonnet/eval.go Outdated Show resolved Hide resolved
`VM` -> `Evaluator`
Add some docstrings
Add some comments
@julienduchesne
Copy link
Member Author

compatibility: you are shuffling public modules around and making some things private. Since Tanka follows semver, that would require a new major version. Would it be possible to keep compatibility, defaulting to the go implementation, and have this functionality be transparent unless you want to use it?

We're on 0.x. There are no retro-compatibility guarantees on minors. Also, the main Tanka APIs, jsonnet.Evaluate... and the CLI, stay retro-compatible so, I'd say it's all good for this to go on a minor

@julienduchesne julienduchesne requested a review from a team August 24, 2023 11:59
@iainlane
Copy link
Member

We're on 0.x

Good point, that makes sense.

Add the `path` as a struct member of the Go-jsonnet implementation. We
have this available when we construct the loader. This lets us then drop
the same argument from `EvaluateAnonymousSnippet()` in the interface -
as not all implementations need it.
@julienduchesne julienduchesne merged commit 6a4f83f into main Aug 24, 2023
3 checks passed
@julienduchesne julienduchesne deleted the julienduchesne/eval-interface branch August 24, 2023 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants