fix for linting on templates with only comments

Signed-off-by: Tarun Kumar <fidato.july13@gmail.com>
pull/8651/head
Tarun Kumar 5 years ago
parent 64d4399f06
commit c165a66c90

@ -123,6 +123,7 @@ func Templates(linter *support.Linter, values map[string]interface{}, namespace
// linter.RunLinterRule(support.WarningSev, fpath, validateQuotes(string(preExecutedTemplate))) // linter.RunLinterRule(support.WarningSev, fpath, validateQuotes(string(preExecutedTemplate)))
renderedContent := renderedContentMap[path.Join(chart.Name(), fileName)] renderedContent := renderedContentMap[path.Join(chart.Name(), fileName)]
renderedContent = cleanseRenderedContent(renderedContent)
if strings.TrimSpace(renderedContent) != "" { if strings.TrimSpace(renderedContent) != "" {
linter.RunLinterRule(support.WarningSev, fpath, validateTopIndentLevel(renderedContent)) linter.RunLinterRule(support.WarningSev, fpath, validateTopIndentLevel(renderedContent))
var yamlStruct K8sYamlStruct var yamlStruct K8sYamlStruct
@ -166,6 +167,17 @@ func validateTopIndentLevel(content string) error {
return scanner.Err() return scanner.Err()
} }
func cleanseRenderedContent(renderedContent string) string {
renderedArray := strings.Split(strings.Replace(renderedContent, "\r\n", "\n", -1), "\n")
cleansedContent := []string{}
for _, line := range renderedArray {
if !(strings.HasPrefix(line, "#") || strings.HasPrefix(line, "---")) {
cleansedContent = append(cleansedContent, line)
}
}
return strings.Join(cleansedContent, "\n")
}
// Validation functions // Validation functions
func validateTemplatesDir(templatesPath string) error { func validateTemplatesDir(templatesPath string) error {
if fi, err := os.Stat(templatesPath); err != nil { if fi, err := os.Stat(templatesPath); err != nil {

@ -330,5 +330,27 @@ func TestValidateTopIndentLevel(t *testing.T) {
t.Errorf("Expected %t for %q", shouldFail, doc) t.Errorf("Expected %t for %q", shouldFail, doc)
} }
} }
}
func TestCleanseRenderedContent(t *testing.T) {
input := `
#comments about the template file
apiVersion: v1
kind: ConfigMap
# Metadata
metadata:
name: mychart-configmap-1
namespace: default
`
expect := `
apiVersion: v1
kind: ConfigMap
metadata:
name: mychart-configmap-1
namespace: default
`
output := cleanseRenderedContent(input)
if output != expect {
t.Errorf("Expected : %+v , but got : %+v", expect, output)
}
} }

@ -0,0 +1,25 @@
# comment on first line
{{- if .Values.condition }}
apiVersion: v1
kind: ConfigMap
# Metadata
metadata:
name: mychart-configmap-1
namespace: default
data:
myvalue: "Hello World"
{{- end }}
---
# comment on first line
{{- if .Values.condition }}
apiVersion: v1
kind: ConfigMap
# Metadata
metadata:
name: mychart-configmap-2
namespace: default
data:
myvalue: "Hello World"
{{- end }}

@ -1 +1,2 @@
name: "mariner" name: "mariner"
condition: false

Loading…
Cancel
Save