https://github.com/mark-rushakoff/yaml-jsonnet-bootstrap
Convert a YAML document stream into a valid Jsonnet program
https://github.com/mark-rushakoff/yaml-jsonnet-bootstrap
jsonnet yaml
Last synced: 2 months ago
JSON representation
Convert a YAML document stream into a valid Jsonnet program
- Host: GitHub
- URL: https://github.com/mark-rushakoff/yaml-jsonnet-bootstrap
- Owner: mark-rushakoff
- License: unlicense
- Created: 2020-01-14T07:16:06.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-01-14T07:18:28.000Z (over 6 years ago)
- Last Synced: 2025-04-12T14:30:02.386Z (about 1 year ago)
- Topics: jsonnet, yaml
- Language: Go
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# yaml-jsonnet-bootstrap
This utility parses a YAML document stream and generates Jsonnet boilerplate to generate the same YAML.
yaml-jsonnet-bootstrap reads a YAML document stream from stdin and writes a valid Jsonnet program to stdout.
I wrote this because I couldn't find an online converter from a YAML stream to a JSON array.
## Usage
Given a YAML document like this:
```yaml
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: myacct
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: myrole
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: mybinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: myrole
subjects:
- kind: ServiceAccount
name: myacct
...
```
then if you run `yaml-jsonnet-bootstrap < /path/to/example.yml > /path/to/example.jsonnet`,
you will generate an unformatted Jsonnet file:
```jsonnet
local var0 = {
"apiVersion": "v1",
"kind": "ServiceAccount",
"metadata": {
"name": "myacct"
}
};
local var1 = {
"apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "Role",
"metadata": {
"name": "myrole"
},
"rules": [
{
"apiGroups": [
""
],
"resources": [
"pods"
],
"verbs": [
"get",
"list",
"watch"
]
}
]
};
local var2 = {
"apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "RoleBinding",
"metadata": {
"name": "mybinding"
},
"roleRef": {
"apiGroup": "rbac.authorization.k8s.io",
"kind": "Role",
"name": "myrole"
},
"subjects": [
{
"kind": "ServiceAccount",
"name": "myacct"
}
]
};
{
Objects(conf):: [
var0,
var1,
var2,
],
}
```
Then, you can run jsonnetfmt on the file to get something nicely formatted like:
```jsonnet
local var0 = {
apiVersion: 'v1',
kind: 'ServiceAccount',
metadata: {
name: 'myacct',
},
};
local var1 = {
apiVersion: 'rbac.authorization.k8s.io/v1',
kind: 'Role',
metadata: {
name: 'myrole',
},
rules: [
{
apiGroups: [
'',
],
resources: [
'pods',
],
verbs: [
'get',
'list',
'watch',
],
},
],
};
local var2 = {
apiVersion: 'rbac.authorization.k8s.io/v1',
kind: 'RoleBinding',
metadata: {
name: 'mybinding',
},
roleRef: {
apiGroup: 'rbac.authorization.k8s.io',
kind: 'Role',
name: 'myrole',
},
subjects: [
{
kind: 'ServiceAccount',
name: 'myacct',
},
],
};
{
Objects(conf):: [
var0,
var1,
var2,
],
}
```
So the normal pipeline is typically `yaml-jsonnet-bootstrap < file.yml | jsonnetfmt - > out.jsonnet`.
From there, you can start editing out.jsonnet to give meaningful variable names and parameterize as needed.