If prerendering the helmfile is not an option then you have 2 options:
- Escape the curly brackets before parsing the chart, process it and then bring back the curly brackets like this:
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)
- Utilize ruamel.yaml and jinja2 templates
Construct your representer that will ignore the curly brackets when loading the yaml file:
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