-
Notifications
You must be signed in to change notification settings - Fork 172
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(jsonnet): add support for importing all files in a directory #1374
base: main
Are you sure you want to change the base?
Conversation
If accepted, I will add support for this function to jsonnet-libs Additionally, I noticed that the |
d0afe89
to
a73c5d1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@partcyborg 👋
Thanks for your contribution! Would you also be able to add some tests please, for our better understanding and ease of reviewing?
Hey @dsotirakis, thank you for your reply. I have added a unit test for the Update: I noticed i was missing a test for the exclude functionality, so i updated the test to ensure that is working too |
Add native function `importFiles` which reads and evaluates all jsonnet files in a directory. Returns an object of with key/value pairs of `<file name>: <evaluated object>` Directory path is found using a `calledFrom` option, similar to `helmTemplate`. Includes support for: - Choosing files by extension using the `extension` option (default is `.libsonnet`) - Excluding files using the `exclude` option, which takes an array of file names to skip
4c46530
to
0f7d83d
Compare
0f7d83d
to
af47706
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for proposing this 🙂 I've left you a comment also in the issue with an idea for a different approach 🙂
pkg/jsonnet/native/funcs_test.go
Outdated
@@ -208,3 +225,41 @@ func TestRegexSubstInvalid(t *testing.T) { | |||
assert.Empty(t, ret) | |||
assert.NotEmpty(t, err) | |||
} | |||
|
|||
func TestImportFiles(t *testing.T) { | |||
tempDir, err := os.MkdirTemp("", "importFilesTest") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Please use T.TempDir
here. With that you don't need to do the os.RemoveAll
manually later on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced this with a call to T.TempDir()
pkg/jsonnet/native/funcs_test.go
Outdated
importDir := filepath.Join(tempDir, importDirName) | ||
err = os.Mkdir(importDir, 0750) | ||
assert.Nil(t, err) | ||
importFiles := []string{"test1.libsonnet", "test2.libsonnet"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: I think it would make the test easier to read if the files were explicitly filled with certain content and the result was compared to an "expected" JSON string 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have refactored the test to
- Use static jsonnet content in the imported and excluded files
- Compare the result in json format with the expected json
Please let me know if this is what you were thinking.
} | ||
|
||
// importFiles imports and evaluates all matching jsonnet files in the given relative directory | ||
func importFiles(vm *jsonnet.VM) *jsonnet.NativeFunction { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo: Please add documentation about this new function to https://github.com/grafana/tanka/blob/main/docs/src/content/docs/jsonnet/native.md 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zerok I am happy to do this, but held off because I don't see documents for helmTemplate
here either. My original plan was to add jsonnet library code similar to the helm functions in tanka-util and document those.
That being said, I am happy to also add documentation for the raw native function here, but I would like to get some clarity on whether this will be accepted before I do the work to add it.
This breaks the hermitic nature of jsonnet, I'd vouch heavily against doing this in Tanka as we'd be creating a parallel ecosystem that is different from upstream. See upstream discussion here: https://groups.google.com/g/jsonnet/c/JdvlXDAdvq0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding a request changes as I would love to have a more in depth discusssion about this, see comments in #1375
While I see your point, I don't think has the impact you think it does for a few reasons:
|
Why not render the JSON beforehand then? Seems like this could be handled by the tooling pipeline rather than the jsonnet code. The fact that we break hermiticity in another feature does not warrant that we should do it regardless. These features have different tradeoffs. |
Add native function
importFiles
which reads and evaluates all jsonnet files in a directory.Returns an object of with key/value pairs of
<file name>: <evaluated object>
Directory path is found using a
calledFrom
option, similar tohelmTemplate
.Includes support for:
extension
option (default is.libsonnet
)exclude
option, which takes an array of file names to skipFixes #1375