Skip to content

Commit

Permalink
Light refactorings to parse_yaml.go (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
msugakov authored Sep 28, 2021
1 parent 64a1f66 commit 15bff8c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 65 deletions.
9 changes: 2 additions & 7 deletions pkg/lintcontext/create_contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ func CreateContextsWithOptions(options Options, filesOrDirs ...string) ([]LintCo
if !info.IsDir() {
if strings.HasSuffix(strings.ToLower(currentPath), ".tgz") {
ctx := newCtx(options)
if err := ctx.loadObjectsFromTgzHelmChart(currentPath); err != nil {
return err
}

ctx.loadObjectsFromTgzHelmChart(currentPath)
contextsByDir[currentPath] = ctx
return nil
}
Expand All @@ -91,9 +88,7 @@ func CreateContextsWithOptions(options Options, filesOrDirs ...string) ([]LintCo
}
ctx := newCtx(options)
contextsByDir[currentPath] = ctx
if err := ctx.loadObjectsFromHelmChart(currentPath); err != nil {
return err
}
ctx.loadObjectsFromHelmChart(currentPath)
return filepath.SkipDir
}
return nil
Expand Down
78 changes: 20 additions & 58 deletions pkg/lintcontext/parse_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,78 +119,37 @@ func (l *lintContextImpl) renderValues(chrt *chart.Chart, values map[string]inte
return rendered, nil
}

func (l *lintContextImpl) loadObjectsFromHelmChart(dir string) error {
metadata := ObjectMetadata{FilePath: dir}
func (l *lintContextImpl) loadObjectsFromHelmChart(dir string) {
renderedFiles, err := l.renderHelmChart(dir)
if err != nil {
l.addInvalidObjects(InvalidObject{Metadata: metadata, LoadErr: err})
return nil
l.addInvalidObjects(InvalidObject{Metadata: ObjectMetadata{FilePath: dir}, LoadErr: err})
return
}
// Paths returned by helm include redundant directory in front, therefore we strip it out.
for path, contents := range normalizeDirectoryPaths(renderedFiles) {
pathToTemplate := filepath.Join(dir, path)

// Skip NOTES.txt file that may be present among templates but is not a kubernetes resource.
if strings.HasSuffix(pathToTemplate, string(filepath.Separator)+chartutil.NotesName) {
continue
}

if err := l.loadObjectsFromReader(pathToTemplate, strings.NewReader(contents)); err != nil {
loadErr := errors.Wrapf(err, "loading object %s from rendered helm chart %s", pathToTemplate, dir)
l.addInvalidObjects(InvalidObject{Metadata: ObjectMetadata{FilePath: pathToTemplate}, LoadErr: loadErr})
}
}
return nil
l.loadHelmRenderedTemplates(dir, normalizeDirectoryPaths(renderedFiles))
}

func (l *lintContextImpl) loadObjectsFromTgzHelmChart(tgzFile string) error {
metadata := ObjectMetadata{FilePath: tgzFile}
func (l *lintContextImpl) loadObjectsFromTgzHelmChart(tgzFile string) {
renderedFiles, err := l.renderTgzHelmChart(tgzFile)
if err != nil {
l.invalidObjects = append(l.invalidObjects, InvalidObject{Metadata: metadata, LoadErr: err})
return nil
}
for path, contents := range renderedFiles {
pathToTemplate := filepath.Join(tgzFile, path)

// Skip NOTES.txt file that may be present among templates but is not a kubernetes resource.
if strings.HasSuffix(pathToTemplate, string(filepath.Separator)+chartutil.NotesName) {
continue
}

if err := l.loadObjectsFromReader(pathToTemplate, strings.NewReader(contents)); err != nil {
loadErr := errors.Wrapf(err, "loading object %s from rendered helm chart %s", pathToTemplate, tgzFile)
l.addInvalidObjects(InvalidObject{Metadata: ObjectMetadata{FilePath: pathToTemplate}, LoadErr: loadErr})
}
l.addInvalidObjects(InvalidObject{Metadata: ObjectMetadata{FilePath: tgzFile}, LoadErr: err})
return
}
return nil
l.loadHelmRenderedTemplates(tgzFile, renderedFiles)
}

func (l *lintContextImpl) renderTgzHelmChart(tgzFile string) (map[string]string, error) {
log.SetOutput(nopWriter{})
defer log.SetOutput(os.Stderr)
chrt, err := loader.LoadFile(tgzFile)

chrt, err := loader.LoadFile(tgzFile)
if err != nil {
return nil, err
}
if err := chrt.Validate(); err != nil {
return nil, err
}

return l.renderChart(tgzFile, chrt)
}

func (l *lintContextImpl) parseValues(filePath string, bytes []byte) (map[string]interface{}, error) {
currentMap := map[string]interface{}{}

if err := y.Unmarshal(bytes, &currentMap); err != nil {
return nil, errors.Wrapf(err, "failed to parse %s", filePath)
}

return currentMap, nil
}

func (l *lintContextImpl) loadObjectFromYAMLReader(filePath string, r *yaml.YAMLReader) error {
doc, err := r.Read()
if err != nil {
Expand Down Expand Up @@ -268,9 +227,9 @@ func (l *lintContextImpl) renderChart(fileName string, chart *chart.Chart) (map[
return nil, errors.Errorf("%s not found", indexName)
}

values, err := l.parseValues(indexName, chart.Raw[valuesIndex].Data)
if err != nil {
return nil, errors.Wrap(err, "loading values.yaml file")
values := map[string]interface{}{}
if err := y.Unmarshal(chart.Raw[valuesIndex].Data, &values); err != nil {
return nil, errors.Wrapf(err, "failed to parse values file %s", indexName)
}

return l.renderValues(chart, values)
Expand All @@ -280,8 +239,8 @@ func (l *lintContextImpl) renderTgzHelmChartReader(fileName string, tgzReader io
// Helm doesn't have great logging behaviour, and can spam stderr, so silence their logging.
log.SetOutput(nopWriter{})
defer log.SetOutput(os.Stderr)
chrt, err := loader.LoadArchive(tgzReader)

chrt, err := loader.LoadArchive(tgzReader)
if err != nil {
return nil, err
}
Expand All @@ -290,22 +249,25 @@ func (l *lintContextImpl) renderTgzHelmChartReader(fileName string, tgzReader io
}

func (l *lintContextImpl) readObjectsFromTgzHelmChart(fileName string, tgzReader io.Reader) {
metadata := ObjectMetadata{FilePath: fileName}
renderedFiles, err := l.renderTgzHelmChartReader(fileName, tgzReader)
if err != nil {
l.invalidObjects = append(l.invalidObjects, InvalidObject{Metadata: metadata, LoadErr: err})
l.addInvalidObjects(InvalidObject{Metadata: ObjectMetadata{FilePath: fileName}, LoadErr: err})
return
}
l.loadHelmRenderedTemplates(fileName, renderedFiles)
}

func (l *lintContextImpl) loadHelmRenderedTemplates(chartPath string, renderedFiles map[string]string) {
for path, contents := range renderedFiles {
pathToTemplate := filepath.Join(fileName, path)
pathToTemplate := filepath.Join(chartPath, path)

// Skip NOTES.txt file that may be present among templates but is not a kubernetes resource.
if strings.HasSuffix(pathToTemplate, string(filepath.Separator)+chartutil.NotesName) {
continue
}

if err := l.loadObjectsFromReader(pathToTemplate, strings.NewReader(contents)); err != nil {
loadErr := errors.Wrapf(err, "loading object %s from rendered helm chart %s", pathToTemplate, fileName)
loadErr := errors.Wrapf(err, "loading object %s from rendered helm chart %s", pathToTemplate, chartPath)
l.addInvalidObjects(InvalidObject{Metadata: ObjectMetadata{FilePath: pathToTemplate}, LoadErr: loadErr})
}
}
Expand Down

0 comments on commit 15bff8c

Please sign in to comment.