openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
1.97k stars 290 forks source link

YAML Parser support for Helm Charts #3782

Open tylervangorder-8451 opened 7 months ago

tylervangorder-8451 commented 7 months ago

What problem are you trying to solve?

For those organizations that are using Helm, it is common for helm charts to include templating directives.

As an example:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{.Release.Name }}-configmap
data:
  JAVA_OPTS: "{{.Values.javaOpts }}"

https://helm.sh/docs/chart_template_guide/values_files/

Describe the solution you'd like

It would be nice to have a specialized version of the YAML parser that is aware of the template syntax.

Have you considered any alternatives or workarounds?

Currently, any Helm chart that has template directives will generate a parsing exception from the YAML parser.

The above example results in the follow warning during parsing:

There were problems parsing helm/service-configmap.yaml

Additional context

This would likely have to be a specialized parser that is aware of the template directives.

timtebeek commented 6 months ago

hi @tylervangorder-8451 ; definitely agree it would be helpful to both have a parser that handles Helm (it's come up before) and be able to target such files with recipes to do something about the inevitable copy, paste & drift there.

As you'll know we use SnakeYaml to parse our yaml files, which is the likely source of the parser errors you're seeing. I don't know if it's possible to make Snakeyaml tolerate template directives, that would be a first option to explore.

I also looked to see if there's any recently maintained Helm specific parsers, but there's not a lot that I could find

Something to explore further...

traceyyoshima commented 6 months ago

Note: The YAML spec is well-defined, so an option would be to add an ANTLR-based YAML parser that allows for {{}} to be a part of names. Most of the code already exists as models, visitors, etc, so I suspect this would be about a day or two of work.

tylervangorder-8451 commented 6 months ago

Yep, a YAML parser that is tolerant of {{}} would be a great step in the right direction. As most of the changes we want to make to these helm charts are "normal" yaml recipes....add a property here, drop a property there, etc. But because of the parsing errors, I don't think we can make any automated changes to these files.

wesboe commented 4 weeks ago

We have the same using but then with Ansible and Jinja2. Jinja uses {{}} for variables. See https://jinja.palletsprojects.com/en/3.1.x/templates/

In the yaml files of our Spring Boot applications we use variables to replace environment specific properties. Like connection to DB etc. Openrewrite will ignore the whole file because of the brackets. Currently we must ignore all spring boot properties recipes.

Invalid yaml

spring:
  cassandra:
    contact-points: {{ cassandra_contactPoints }}

Stacktrace [WARNING] There were problems parsing conf/application.yaml [WARNING] java.lang.NullPointerException: Cannot invoke "org.openrewrite.yaml.tree.YamlKey.getPrefix()" because "this.key" is null org.openrewrite.yaml.YamlParser$MappingBuilder.push(YamlParser.java:481)