Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing YAML in jsonnet

Tags:

jsonnet

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;
like image 575
Aleš Avatar asked May 22 '18 18:05

Aleš


People also ask

What is the difference between JSON and Jsonnet?

JSON is used to describe data literally. Jsonnet extends JSON. Jsonnet has relaxed syntax and comments to make it easier for humans to write. Jsonnet also adds constructs for generating, translating and refining data.

What is Jsonnet used for?

Jsonnet is a new domain specific configuration language from Google which allows you to define data templates. These data templates are transformed into JSON objects using Jsonnet library or command line tool. As a language, Jsonnet is extension of JSON - a valid JSON object is always valid Jsonnet template.

What is a Libsonnet file?

libsonnet is a library Jsonnet file, containing the base configuration for a service. Suppose that test_config. jsonnet is a test configuration file that is used to test base_config. jsonnet , and test_config. json is the expected JSON output from compiling test_config.


1 Answers

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
like image 134
jjo Avatar answered Oct 08 '22 21:10

jjo