-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for common YAML templates (specifically Helm) #16
Comments
What would be great here would be a mode (i.e. perhaps enabled by a boolean, if we don't want it as default behavior) that is capable of handling Jinja expressions:
by checking space around the 2-character tokens, together with allowing Helm's use of
|
Also running into this issue here -> +1 |
Actually this would make a bigger issue. I built a little script to "fix" the error we both had - see below The issue is - it just moves on to another syntax error right after as this linter is not designed to lint helm templates. This would require more logic to be implemented. Basically "the whole helm templating". ScriptRun for Example in this container: quay.io/helmpack/chart-testing:v3.0.0 (must run #!/bin/bash
echo "Stashing workDirectory"
git stash
yamlFiles=(`find | grep -e ".ya\?ml"`)
function replaceHelmSyntax() {
sed -i "s!{{-!{{!g" ${1}
}
s="charts/my-service/templates/deployment.yaml"
#for s in ${yamlFiles[@]}; do
replaceHelmSyntax ${s}
cat -n ${s}
yamllint ${s}
#done
echo "Resetting workDirectory"
git checkout .
git stash pop
|
Hi, I don't know how to tackle this need. It's a kind of chicken and egg thing: what is true YAML and that must be linted? The not yet processed, templated YAML or the YAML document that is the result of the template resolution process? My first reflex was to have a kind of "escaper" just like @H3rby7 did in Bash but this may not be we expect from such a feature: we can imagine "ugly" Jinja templates that would put together odd parts of YAML fragments to produce a valid YAML document: is fragment, if linted, would be full of errors but once processed, the complete YAML document could be syntactically correct. The problem is that embedding all templating engines is not possible, so what to do? |
Having the linter to sort of ignore (or recognize) the template strings is feasible but at an important cost: it will require hacking (not to say reimplementing) SnakeYAML, which is something I'm not very likely to do. So help is wanted! |
Perhaps ignoring templated YAML files altogether is the most feasible thing here? My main concern when I raised the ticket was that we have lots of repos that contain a large number of Helm charts, and the number of false positives resulting from these renders enabling yamllint in SonarQube impractical. I also do occasionally see bugs that result from incorrect indentation within these templates, but without running the templating engine I suspect finding these would be next to impossible. For Helm charts specifically, we could potentially identify a chart via the directory structure, to save complex parsing changes:
I know this isn't a general solution to the templated YAML problem, but it may be sufficient for what I imagine to be a common use case. I wonder if there are other widely-used templates that still bear the .yaml extension: I suspect this is the most prevalent. Dropping the second requirement (so just adding the means to say "if the path matches |
Thanks for the feedback. So yes, you would need to call yamllint after the template engines. However I can work on an exclusion mechanism to counterbalance the recursive analysis. BTW if you use the sonar-yaml plugin, you have the possibility to exclude paths from the analysis if I remember well. So this raises the question: how do you actually call yamllint? |
The tool already allows you to ignore some files with the |
…env variables to give path to yamllint conf file
So as I saw several posts regarding this issue I wanted to share a simple fix for your pipeline scans:
Hope this helps others coming here after me. Also could be a good addition to the general yamllint wiki as helm is a veeeeery wide spread use-case. |
the above doesn't work when you have a malformed yaml to begin with. |
orefalo this will if you use the shell |
Hi, a bit confused by your answer. Apologies, I don't have your expertise (and may be missing context) helm can be quite cryptic about yaml parsing errors, the other day (when I found this post), I spend 2h to find the source of the problem - no surprise k8s is a pain to learn. Anyways, I might be out of context on this thread. |
I ran across this thread trying to use yamllint on my Helm charts. I think this command directly from Helm might be better suited: And maybe: If you are using GitHub Actions, the chart testing tool has an Action directly from the Helm org, and appears to use yamllint under the hood: |
Summary: We will use a postgres deployment for dev deployments and cloudSQL for testing/staging/prod. Unfortunately, yamllint doesnt handle templated YAMLs: sbaudoin/yamllint#16. Alternatives to disabling any YAMLs in the templates directory would be to name the files something other than .yaml (however that is the recommended for helm). Relevant Issues: N/A Type of change: /kind infra Test Plan: skaffold run with dev profile Signed-off-by: Michelle Nguyen <[email protected]> GitOrigin-RevId: 6c71b25abf82c6666cdc5cc626fafd12fbfa8581
Summary: We will use a postgres deployment for dev deployments and cloudSQL for testing/staging/prod. Unfortunately, yamllint doesnt handle templated YAMLs: sbaudoin/yamllint#16. Alternatives to disabling any YAMLs in the templates directory would be to name the files something other than .yaml (however that is the recommended for helm). Relevant Issues: N/A Type of change: /kind infra Test Plan: skaffold run with dev profile Signed-off-by: Michelle Nguyen <[email protected]> GitOrigin-RevId: 6c71b25abf82c6666cdc5cc626fafd12fbfa8581
Summary: We will use a postgres deployment for dev deployments and cloudSQL for testing/staging/prod. Unfortunately, yamllint doesnt handle templated YAMLs: sbaudoin/yamllint#16. Alternatives to disabling any YAMLs in the templates directory would be to name the files something other than .yaml (however that is the recommended for helm). Relevant Issues: N/A Type of change: /kind infra Test Plan: skaffold run with dev profile Signed-off-by: Michelle Nguyen <[email protected]> GitOrigin-RevId: 6c71b25abf82c6666cdc5cc626fafd12fbfa8581
Summary: We will use a postgres deployment for dev deployments and cloudSQL for testing/staging/prod. Unfortunately, yamllint doesnt handle templated YAMLs: sbaudoin/yamllint#16. Alternatives to disabling any YAMLs in the templates directory would be to name the files something other than .yaml (however that is the recommended for helm). Relevant Issues: N/A Type of change: /kind infra Test Plan: skaffold run with dev profile Signed-off-by: Michelle Nguyen <[email protected]> GitOrigin-RevId: 6c71b25abf82c6666cdc5cc626fafd12fbfa8581
One common form of YAML file found in applications is the Helm chart, which utilizes a template language that commonly results in two linting errors, even though the YAML is valid.
Firstly, I tend to set up the linter to enforce a single space after opening braces and before closing ones. Because
{{
is used as a templating expression, code such as this:results in "too few spaces inside braces (braces)". I could allow 0 or 1 space, which I may be forced to do, but it'd be great if there was a way to override this check if the next character is also a brace, allowing
{{
.In a related fashion, this line is flagged with "Parse error: syntax error: expected the node content, but found '-'":
{{-
is used in Helm to trim whitespace. Is there a way to tweak the rules to avoid flagging this construct as an error?The text was updated successfully, but these errors were encountered: