Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Jsonnet Function Family for HCL #1360

Merged
merged 11 commits into from
Apr 20, 2021
Merged

Jsonnet Function Family for HCL #1360

merged 11 commits into from
Apr 20, 2021

Conversation

mitchellh
Copy link
Contributor

@mitchellh mitchellh commented Apr 19, 2021

This introduces two new HCL functions jsonnetfile and jsonnetdir which behave similarly to our template functions: they process a file or directory, respectively, and convert Jsonnet-formatted files to JSON. They return a path to the processed file or directory, which is copied rather than updated in-place (behaving the same as our templating functions).

These functions can be used anywhere, but are created particularly to pair with #1357 since Jsonnet is a popular way to manage K8S configurations in certain environments.

Features

  • Can process a single jsonnet file or a directory of files
  • For directory processing, only files ending in .jsonnet are processed
  • For both directory and single file processing, files in the result have a .json suffix added
  • Full Jsonnet parameterization is supported -- users can specify custom variables.
  • Unlike templating, HCL variables don't magically exist here. Users are encouraged to compose this with our templating functions to access that, or manually pass through the variables. We have to do this since type conversions are a mess.

Example

Building on #1357

app "example-nodejs" {
  # ... 

  deploy {
    use "kubernetes-apply" {
      // Note this complex case where we compose template and jsonnet, this lets us first 
      // template our ".jsonnet" files and then process them. The user could also just use
      // the `ext_vars` approach or something.
      path = jsonnetdir(templatedir("${path.app}/k8s"), {
        ext_vars = { 
          environment = "production"
        }
      })

      prune_label = "app=myapp"
    }
  }
}

@mitchellh mitchellh added this to the 0.4.0 milestone Apr 19, 2021
@mitchellh mitchellh requested a review from a team April 19, 2021 21:21
@github-actions github-actions bot added the core label Apr 19, 2021
cty.StringVal("testdata/filesystem/hello.txt"),
}),
false,
},
Copy link
Contributor Author

@mitchellh mitchellh Apr 19, 2021

Choose a reason for hiding this comment

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

I deleted these tests cause they were overly dependent on an exact file structure and the functionality was already tested by other cases. The "overly dependent" property was resulting in failures due to test files introduced for the jsonnet tests.

@mitchellh mitchellh changed the title Jsonnet Function Familiy for HCL Jsonnet Function Family for HCL Apr 19, 2021
Copy link
Contributor

@evanphx evanphx left a comment

Choose a reason for hiding this comment

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

Just one question about what deletes the tempdir. It doesn't look like anything does, which means users will accidentally create a ton of them?

},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
td, err := ioutil.TempDir("", "waypoint")
Copy link
Contributor

Choose a reason for hiding this comment

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

What deletes the tempdir?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nobody, this is behaving the same way as template* functions at the moment where we dangle the temp dirs. The hope is it goes into a system temp dir that gets cleared at some point currently (mine does on both macOS and Linux, at least). I think in the future we can try some process-wide cleanup if you wanted but I think that should be a separate PR.

@evanphx
Copy link
Contributor

evanphx commented Apr 20, 2021 via email

@mitchellh
Copy link
Contributor Author

Sounds good. My recommendation would be to maintain a global var of cleanup things and call a package-level cleanup at some point. We need to keep these directories around longer than expected because plugins may use them all the way until Waypoint exits.

@evanphx
Copy link
Contributor

evanphx commented Apr 20, 2021 via email

@mitchellh mitchellh merged commit 62e8cd9 into main Apr 20, 2021
@mitchellh mitchellh deleted the feature/jsonnet branch April 20, 2021 15:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants