7

Is there a way how I can import a .yaml file in jsonnet?

I have found that jsonnet supports importing .json and also has a native importstr() function but looks like no support for .yaml?

I would like to do:

local foo = import "foo.yaml";
local bar = foo.bar;
Aleš
  • 8,896
  • 8
  • 62
  • 107

1 Answers1

5

Not at the moment (May/2018), there's an open issue for it at https://github.com/google/jsonnet/issues/460, if you're working with Kubernetes manifests (importing + massaging w/jsonnet) you could use https://github.com/ksonnet/kubecfg which contains a superset of jsonnet, including std.parseYaml().

Update(2018-05-23): added ksonnet example

Using ksonnet's embedded parseYaml, at app folder:

$ cat assets/foo.yaml 
foo: value1
bar: value2

$ cat components/cm.jsonnet 
local env = std.extVar("__ksonnet/environments");
local params = std.extVar("__ksonnet/params").components.cm;
local k = import "k.libsonnet";
local configMap = k.core.v1.configMap;

local parseYaml = std.native("parseYaml");

configMap.new(params.name, params.data) {
  data+: parseYaml(importstr "../assets/foo.yaml")[0] {
    foo: "my own value",
  },
}

$ ks show default
---
apiVersion: v1
data:
  bar: value2
  foo: my own value
kind: ConfigMap
metadata:
  name: cm
jjo
  • 2,595
  • 1
  • 8
  • 16
  • I am using ksonnet and its not there - yet. – Aleš May 22 '18 at 19:09
  • 1
    I think in `ksonnet` it is not `std.parseYaml`, but something like `local kubecfg = import "kubecfg.libsonnet"; kubecfg.parseYaml(yaml_string)`. – sbarzowski May 23 '18 at 07:04
  • 1
    as `ksonnet` has common roots with `kubecfg` it's as @sbarzowski said (tho I had to directly use it via std.native()), I've updated the answer with an example. – jjo May 23 '18 at 12:49
  • 1
    ksonnet is no longer maintained and GH repos will be archived: https://blogs.vmware.com/cloudnative/2019/02/05/welcoming-heptio-open-source-projects-to-vmware/ – danielrvt Nov 10 '20 at 10:52