-
Notifications
You must be signed in to change notification settings - Fork 440
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
Relax computed importstr ? #196
Comments
Any technical reason for |
Widening it to a variable is just as "bad" as widening it to an arbitrary expression, so if we were going to do anything it would be that. The property that we gain by having the restriction is that you can do a simple static analysis and determine which files are dependencies, and then determine whether or not the code needs re-executing. This is potentially useful although I'm not sure anyone has made use of it yet. However if we lift the restriction and lose the property, we won't be getting it back again. So it needs careful thought. There is one difference between Jsonnet and other programming languages, and that's that everybody writes programs that terminate in a relatively short period of time, so one answer is that if you want to know what files affect the evaluation of a config, you should execute the config. Or, in the case of deciding whether or not a cached JSON value needs recomputing, save not just the JSON output but the list of files used to compute it. |
Thank you for the detailed explanation .
What about an explicit keyword or function to express it? |
I'm closing it, Thank you. |
I suppose native extensions broke static analysis already :) Same as they do in Java etc. |
@sparkprime The stdlib is so limited and so opinionated that, basically, we're forced to write native extensions, which kind of defeats the purpose of using Jsonnet. |
@nikolay This guarantees that it will work the same on every machine and makes it much easier to reason about the code. But of course it restricts the structure of the solution - any input data must be prepared beforehand and passed to Jsonnet. So it's sort of a "it's not a bug it's a feature" situation. That said, it would always be nice to make passing data to Jsonnet easier (and more obvious).
Native extensions are not a proper solution here. It's a way to bypass the restrictions of the language. But there are reasons for these restrictions. Also native functions were supposed to be pure functions - for cases in which Jsonnet is not fast enough or a native library is used. Using them to perform IO is an abuse of the mechanism, IMO. It's good it worked for @ant31, but I don't think it's a recommended way. (Related #422) |
If you were forced to write native functions to do things that were 1) pure and 2) relatively simple, like not regex matching or gzip compression or whatever, then there's a case for adding it to the stdlib instead. Things that are pure, complex, but well-specified with pure implementations available in all languages (like gzip) then we could add them as builtins. Things that have "writing" side effects should not be added as native functions by anyone. Things that have "reading" side effects could be added, as long as you're OK with it maybe reading them more times than you expect (or not at all). Typically this is OK for things like reading from the same dir (you will want to do an import cache to make it completely robust), but less OK when reading remotely. Not having computed imports is somewhat orthogonal to all of this. It's essentially about knowing what production systems may be impacted by config refactorings. You can see this in Bazel as well - you have to predeclare what you might read. The idea is then it can always know what to rebuild. When people ask for computed imports, I wonder what they're trying to do. We could definitely strengthen the way Jsonnet can take "input" data from outside. There was a proposal at one point to make it easier to do pipe-like functionality, e.g.
could be a syntax sugar for
That gives you a jq-like functionality but it's also not that big a difference from the status quo. |
@sparkprime Well, that above is nice, but can't be done with |
So you have more than one dynamic config file, and you also need their filenames to be parameterized from outside of Jsonnet? |
Yes, @sparkprime. There are many legit use cases for it. |
What if there was a way to give a list of files on the commandline, and have them all accessible internally as a dict keyed by filename? You can do this now using --code-str but it'd be a clunky bash one-liner. |
@sparkprime That would definitely be an improvement over the status quo if you want to preserve the immutability at any price. What I'm trying to accomplish is something along the lines of Kapitan and ksonnet, but using as much Jsonnet as possible and as little Bash as possible, too. This approach would also allow the Bash script to concatenate/template-expand those configs and pass them to the Jsonnet compiler. |
@ant31 would that have helped for your original issue? @hausdorff what do you think? @dgarstang you once said you were continually bitten by this, what do you think? |
@sparkprime the approach of giving the files in the command line ? I'm packaging all together kubernetes resources, import the json/yaml and perfom transformations with jsonnet:
I want to abstract as much jsonnet internal as possible to end-users to feel like 'plain' json. expand(
resources: {
file: 'resources/deployment.yaml',
name: 'toto-app'
},
{
file: 'resources/svc.yaml',
name: 'toto-app'
}];
configs: [{
file: 'data/config.ini'
}]) The structure for the end-user is easy and doesn't requires any knowledge about jsonnet expect the call to the 'expand' function.
|
@ant31 In your case only a complete computed import string would do then. |
Unless I'm missing something providing an object with directory contents (possibly recursive), and have imported directories specified in the commandline would also work and be a bit easier to control. It would require handling the paths 100% on the jsonnet side (to get This is as strong as computed imports, but doesn't let jsonnet jump around the whole filesystem, only the specified part, hides the absolute paths and discourages libraries from silently depending on Maybe that could even be integrated with the current extVar/tla mechanism. What do you think? |
How this would be different in term of Hermiticity ? |
The caller of jsonnet command has control over what data gets passed to jsonnet. This changes a lot. Consequences somewhat related to hermicity:
It's fairly safe feature to add:
Proof of conceptIn the previous section I said that:
So let's do just that. I wrote a little Go program https://github.com/sbarzowski/dir2json You can use it to easily provide directory contents to jsonnet:
The downside is that it reads the whole directory eagerly and stores it in a temporary file. And it's not exactly convenient. More precise propositionLet's add So the example from PoC would become:
EDIT: probably |
I mentioned a use for this in #479, as automatically noted. Another use that I found recently was importing a set of variables. Consider:
(The reason I specify Without a computed |
Adds a new builtin function (importstrDyn) that accept a computed string. I did not modified the importstr logic itself as I don't understand well enough how the parser is working.
I like the idea of "hermeticity" and "explicitness" of imports too. Did you consider a variation of "dir2json" or a "builtin" to make
Maybe there could be a "builtin" to Import a static list or a tree like above |
I agree. I would be on board with So you could do something like: This would require extending the imports abstractions slightly to allow producing directory listings and it could be a bit annoying to implement. Also opens questions about handling of hidden files.
Hmm.... Not sure I understand. What would this tool do exactly? Is it the same as |
An easy/standard way to create "data structures" eventually like the one Yes, I think you just described with "importdir" the kind of builtin most commenters would be happy with, including me. And yes, without such "importdir" many will likely end up with ad-hoc tools creating files/structures like in the example above. I am undecided which is better when I think of Disclaimer: I am a happy user of jsonnet since about just a few months. Thanks you for caring about it, btw! |
importdir could be as simple as producing a map that's just the filename -> content.
|
@Jezza Yes, that's basically the idea. There are still some complications, though:
Because of that, a file generation approach would take at least 10x less work IMO. This can be done with very little commitment and as a single, separate tool. And it doesn't require any knowledge of the internals. Ofc. it will work only with the actual file systems, not with the abstract importers. |
Any further interest in carrying this issue forward? It seems like a desirable and one without many alternatives other than creating a mapping of all file paths yourself. |
How do people feel about a separate commandline tool for turning directory contents into jsonnet import manifests? We could provide this like jsonnetfmt -- a separate tool but part of the release and maintained. |
Another possibility to keep it out of the core language would be a command line flag like
or a variation thereof making
I must add the whole issue does not seem a big deal to me anymore --- |
@alexei-matveev I'd actually be very fascinated to know why your attitude changed as it would give insight into how important this feature is |
Not much to report, actually. The isssue did not prove to be a "cost center" Off-topic: i spent much more time converting YAML with comments to Jsonnet and preserving comments. |
If there's a YAML parser out there that preserves comments, it should be pretty easy (and there must be, because YAML reformatters exist). |
A tool like this already exists: https://github.com/waisbrot/yaml2jsonnet. There was a discussion about it here: #765 and @waisbrot created such a tool. |
Still not entirely sure, if the solution proposed by @sbarzowski with |
I am also bogged down by this issue. Can we update something on this @sparkprime ? |
* Apply golint recommendations
Do you think it will be possible to use
importstr
with variable?e.g:
The text was updated successfully, but these errors were encountered: