Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse helm chart yaml file using python

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.

like image 888
Manoj Kumar Maharana Avatar asked May 24 '26 16:05

Manoj Kumar Maharana


2 Answers

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.

like image 167
coderanger Avatar answered May 26 '26 06:05

coderanger


If prerendering the helmfile is not an option then you have 2 options:

  1. 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)
  1. Utilize ruamel.yaml and jinja2 templates

Construct 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

like image 40
Ilias Antoniadis Avatar answered May 26 '26 05:05

Ilias Antoniadis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!