Last active
May 23, 2016 15:41
-
-
Save ant31/58d43cbf1e4469d62eed778c76394829 to your computer and use it in GitHub Desktop.
Convert kpm manifest.yaml to jsonnet to better manifest.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
package: | |
name: kubespray/etcd | |
author: Antoine Legrand | |
version: 1.0.0 | |
description: etcd | |
license: MIT | |
variables: | |
cluster_token: etcd-cluster-1 | |
cluster_state: 'new' | |
data_volumes: | |
- name: varetcd | |
emptyDir: | |
medium: "" | |
service: svc.cluster.local | |
initial_cluster: etcd-1=http://etcd-1.{{namespace}}.{{service}}:2380,etcd-2=http://etcd-2.{{namespace}}.{{service}}:2380,etcd-3=http://etcd-3.{{namespace}}.{{service}}:2380 | |
resources: | |
- file: etcd-member-dp.yaml | |
name: etcd | |
type: deployment | |
sharded: yes | |
- file: etcd-member-svc.yaml | |
name: etcd | |
type: svc | |
sharded: yes | |
- file: etcd-svc.yaml | |
name: etcd | |
type: svc | |
shards: | |
- name: 1 | |
- name: 2 | |
- name: 3 | |
deploy: | |
- name: $self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function( | |
data_volumes=[{'emptyDir': {'medium': ''}, 'name': 'varetcd'}], | |
cluster_token="etcd-cluster-1", | |
initial_cluster="etcd-1=http://etcd-1.{{namespace}}.{{service}}:2380,etcd-2=http://etcd-2.{{namespace}}.{{service}}:2380,etcd-3=http://etcd-3.{{namespace}}.{{service}}:2380", | |
cluster_state="new", | |
service="svc.cluster.local", | |
) | |
{ | |
package: {'author': 'Antoine Legrand', 'version': '1.0.0', 'description': 'etcd', 'license': 'MIT', 'name': 'kubespray/etcd'}, | |
variables: {'data_volumes': [{'emptyDir': {'medium': ''}, 'name': 'varetcd'}], 'cluster_token': 'etcd-cluster-1', 'initial_cluster': 'etcd-1=http://etcd-1.{{namespace}}.{{service}}:2380,etcd-2=http://etcd-2.{{namespace}}.{{service}}:2380,etcd-3=http://etcd-3.{{namespace}}.{{service}}:2380', 'cluster_state': 'new', 'service': 'svc.cluster.local'}, | |
shards: [{'name': 1}, {'name': 2}, {'name': 3}], | |
resources: [] | |
+ [ {"type": "deployment", "sharded": true, "name": "etcd", "file": "etcd-member-dp.yaml"} + shard + {name: "etcd-%s" % shard.name} | |
for shard in $.shards | |
] | |
+ [ {"type": "svc", "sharded": true, "name": "etcd", "file": "etcd-member-svc.yaml"} + shard + {name: "etcd-%s" % shard.name} | |
for shard in $.shards | |
] | |
+ [ {"type": "svc", "name": "etcd", "file": "etcd-svc.yaml"} ], | |
deploy: [{'name': '$self'}] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
deploy: | |
- {name: $self} | |
package: {author: Antoine Legrand, description: etcd, license: MIT, name: kubespray/etcd, | |
version: 1.0.0} | |
resources: | |
- {file: etcd-member-dp.yaml, name: etcd-1, sharded: true, type: deployment} | |
- {file: etcd-member-dp.yaml, name: etcd-2, sharded: true, type: deployment} | |
- {file: etcd-member-dp.yaml, name: etcd-3, sharded: true, type: deployment} | |
- {file: etcd-member-svc.yaml, name: etcd-1, sharded: true, type: svc} | |
- {file: etcd-member-svc.yaml, name: etcd-2, sharded: true, type: svc} | |
- {file: etcd-member-svc.yaml, name: etcd-3, sharded: true, type: svc} | |
- {file: etcd-svc.yaml, name: etcd, type: svc} | |
shards: | |
- {name: 1} | |
- {name: 2} | |
- {name: 3} | |
variables: | |
cluster_state: new | |
cluster_token: etcd-cluster-1 | |
data_volumes: | |
- emptyDir: {medium: ''} | |
name: varetcd | |
initial_cluster: etcd-1=http://etcd-1.{{namespace}}.{{service}}:2380,etcd-2=http://etcd-2.{{namespace}}.{{service}}:2380,etcd-3=http://etcd-3.{{namespace}}.{{service}}:2380 | |
service: svc.cluster.local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import kpm.kub | |
import json | |
import _jsonnet | |
import yaml | |
import jinja2 | |
TEMPLATE = """ | |
function( | |
{% for key,value in manifest.variables.iteritems() -%} | |
{% if value is string -%} | |
{{key}}="{{value}}", | |
{% else -%} | |
{{key}}={{value}}, | |
{% endif -%} | |
{%- endfor -%} | |
) | |
{ | |
package: {{manifest.package}}, | |
variables: {{manifest.variables}}, | |
shards: {{manifest.shards}}, | |
{% if manifest.resources|length > 0 %} | |
resources: [] {% for item in manifest.resources %} | |
+ [ {{item|json}} {% if 'sharded' in item -%} + shard + {name: "{{item.name}}-%s" % shard.name} | |
for shard in $.shards | |
{% endif -%} | |
] | |
{%- endfor %}, | |
{% endif %} | |
deploy: {{manifest.deploy}} | |
} | |
""" | |
def do_json(value, **kwargs): | |
""" | |
Serializes an object as JSON. Optionally given keyword arguments | |
are passed to json.dumps(), ensure_ascii however defaults to False. | |
""" | |
import json | |
kwargs.setdefault('ensure_ascii', False) | |
return json.dumps(value, **kwargs) | |
filters = {"json": do_json} | |
def render(template, package="rabbitmq/rabbitmq"): | |
jinja_env = jinja2.Environment() | |
jinja_env.filters.update(filters) | |
template = jinja_env.from_string(template) | |
k = kpm.kub.Kub(package) | |
v = {"manifest": k.manifest} | |
jsonnet_str = template.render(v) | |
print jsonnet_str | |
json_str = _jsonnet.evaluate_snippet("snippet", jsonnet_str) | |
print json_str | |
yaml_str = yaml.safe_dump(json.loads(json_str)) | |
return yaml_str | |
if __name__ == "__main__": | |
# usage: ./kpm_jsonnet.py -p kubespray/etcd | |
# ./kpm_jsonnet.py -p rabbitmq/rabbitmq | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-p", "--package", default="rabbitmq/rabbitmq") | |
args = parser.parse_args() | |
print render(TEMPLATE, args.package) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment