I am trying to parse a helm chart YAML file using python. The file contains some curly braces, that's why I am unable to parse the YAML file.
a sample YAML file
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Values.nginx.name }}-config-map
labels:
app: {{ .Values.nginx.name }}-config-map
data:
SERVER_NAME: 12.121.112.12
CLIENT_MAX_BODY: 500M
READ_TIME_OUT: '500000'
Basically, I could not figure out how to ignore the values present at right side.
You would have to write an implementation of Go's text/template library in Python. A better option is probably to push your content through helm template first and then parse it.
If prerendering the helmfile is not an option then you have 2 options:
def escape_go_templates(content: str) -> str:
return re.sub(r"\s{{(.*?)}}", r" __GO_TEMPLATE__\1__GO_TEMPLATE__", content)
def unescape_go_templates(content: str) -> str:
return re.sub(r"__GO_TEMPLATE__(.+?)__GO_TEMPLATE__", r"{{\1}}", content)
ruamel.yaml and jinja2 templatesConstruct your representer that will ignore the curly brackets when loading the yaml file:
def represent_str(representer: SafeRepresenter, data: str | None) -> ScalarNode:
if data and data.startswith("{{"):
return representer.represent_scalar("tag:yaml.org,2002:str", data, style="-")
return representer.represent_str(data)
init your yaml instance:
from ruamel.yaml import YAML
yaml = YAML(typ="jinja2")
yaml.width = 4096 # This is because lines in charts can get very long.
yaml.representer.add_representer(str, _represent_str)
this will allow to parse helm charts with python you can find more details in my blogpost
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With