Skip to content

Commit

Permalink
fix sonarcloud issues
Browse files Browse the repository at this point in the history
- try #2 with cognitive complexity by moving for loop outside of the render func
- update error level of some log messages
  • Loading branch information
Willie Sana committed Nov 11, 2020
1 parent d301585 commit a87488b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 80 deletions.
153 changes: 79 additions & 74 deletions pkg/iac-providers/helm/v3/load-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (h *HelmV3) LoadIacDir(absRootDir string) (output.AllResourceConfigs, error
var chartMap helmChartData
iacDocuments, chartMap, err = h.loadChart(chartPath)
if err != nil && err != errSkipTestDir {
logger.Warn("error occurred while loading chart", zap.Error(err))
logger.Error("error occurred while loading chart", zap.Error(err))
continue
}

Expand All @@ -96,7 +96,7 @@ func (h *HelmV3) LoadIacDir(absRootDir string) (output.AllResourceConfigs, error
var config *output.ResourceConfig
config, err = k.Normalize(doc)
if err != nil {
zap.S().Warn("unable to normalize data", zap.Error(err), zap.String("file", doc.FilePath))
zap.S().Error("unable to normalize data", zap.Error(err), zap.String("file", doc.FilePath))
continue
}

Expand All @@ -114,11 +114,11 @@ func (h *HelmV3) LoadIacDir(absRootDir string) (output.AllResourceConfigs, error
func (h *HelmV3) createHelmChartResource(chartPath string, chartData map[string]interface{}) (*output.ResourceConfig, error) {
var config output.ResourceConfig

logger := zap.S().With("chart path", chartPath)
logger := zap.S().With("helm chart path", chartPath)

jsonData, err := json.Marshal(chartData)
if err != nil {
logger.Warn("unable to marshal chart to json")
logger.Error("unable to marshal chart to json")
return nil, err
}

Expand All @@ -131,7 +131,7 @@ func (h *HelmV3) createHelmChartResource(chartPath string, chartData map[string]

chartName, ok := chartData["name"].(string)
if !ok {
logger.Warn("unable to determine chart name")
logger.Error("unable to determine chart name")
return nil, err
}

Expand All @@ -147,81 +147,79 @@ func (h *HelmV3) createHelmChartResource(chartPath string, chartData map[string]

// renderChart renders a helm chart with the given template files and values
// returns and IaC document for each rendered file
func (h *HelmV3) renderChart(chartPath string, chartMap helmChartData, templateFileMap map[string][]*string, valueMap map[string]interface{}) ([]*utils.IacDocument, error) {
func (h *HelmV3) renderChart(chartPath string, chartMap helmChartData, templateDir string, templateFiles []*string, valueMap map[string]interface{}) ([]*utils.IacDocument, error) {
iacDocuments := make([]*utils.IacDocument, 0)
logger := zap.S().With("helm chart path", chartPath)

for templateDir, templateFiles := range templateFileMap {
if filepath.Base(templateDir) == helmTestDir {
logger.Debug("skipping test dir", zap.String("dir", templateDir))
return iacDocuments, errSkipTestDir
}

// create a list containing raw template file data
chartFiles := make([]*chart.File, 0)
for _, templateFile := range templateFiles {
var fileData []byte
fileData, err := ioutil.ReadFile(filepath.Join(templateDir, *templateFile))
if err != nil {
logger.Error("unable to read template file", zap.String("file", *templateFile))
return iacDocuments, err
}
if filepath.Base(templateDir) == helmTestDir {
logger.Debug("skipping test dir", zap.String("dir", templateDir))
return iacDocuments, errSkipTestDir
}

chartFiles = append(chartFiles, &chart.File{
Name: filepath.Join(helmTemplateDir, *templateFile),
Data: fileData,
})
// create a list containing raw template file data
chartFiles := make([]*chart.File, 0)
for _, templateFile := range templateFiles {
var fileData []byte
fileData, err := ioutil.ReadFile(filepath.Join(templateDir, *templateFile))
if err != nil {
logger.Error("unable to read template file", zap.String("file", *templateFile))
return iacDocuments, err
}

// chart name and version are required parameters
chartName, ok := chartMap["name"].(string)
if !ok {
logger.Error("chart name was invalid")
return iacDocuments, errBadChartName
}
chartFiles = append(chartFiles, &chart.File{
Name: filepath.Join(helmTemplateDir, *templateFile),
Data: fileData,
})
}

var chartVersion string
chartVersion, ok = chartMap["version"].(string)
if !ok {
logger.Error("chart version was invalid")
return iacDocuments, errBadChartVersion
}
// chart name and version are required parameters
chartName, ok := chartMap["name"].(string)
if !ok {
logger.Error("chart name was invalid")
return iacDocuments, errBadChartName
}

// build the minimum helm chart data input
c := &chart.Chart{
Metadata: &chart.Metadata{Name: chartName, Version: chartVersion},
Templates: chartFiles,
}
var chartVersion string
chartVersion, ok = chartMap["version"].(string)
if !ok {
logger.Error("chart version was invalid")
return iacDocuments, errBadChartVersion
}

var v chartutil.Values
v, err := chartutil.CoalesceValues(c, chartutil.Values{
"Values": valueMap,
"Release": chartutil.Values{
"Name": defaultChartName,
},
})
if err != nil {
logger.Warn("error encountered in CoalesceValues")
return iacDocuments, err
}
// build the minimum helm chart data input
c := &chart.Chart{
Metadata: &chart.Metadata{Name: chartName, Version: chartVersion},
Templates: chartFiles,
}

// render all files within the chart
var renderData map[string]string
renderData, err = engine.Render(c, v)
if err != nil {
logger.Warn("error encountered while rendering chart", zap.String("template dir", templateDir))
return iacDocuments, err
}
var v chartutil.Values
v, err := chartutil.CoalesceValues(c, chartutil.Values{
"Values": valueMap,
"Release": chartutil.Values{
"Name": defaultChartName,
},
})
if err != nil {
logger.Warn("error encountered in CoalesceValues")
return iacDocuments, err
}

for renderFile := range renderData {
iacDocuments = append(iacDocuments, &utils.IacDocument{
Data: []byte(renderData[renderFile]),
Type: utils.YAMLDoc,
StartLine: 1,
EndLine: 1,
FilePath: renderFile,
})
}
// render all files within the chart
var renderData map[string]string
renderData, err = engine.Render(c, v)
if err != nil {
logger.Warn("error encountered while rendering chart", zap.String("template dir", templateDir))
return iacDocuments, err
}

for renderFile := range renderData {
iacDocuments = append(iacDocuments, &utils.IacDocument{
Data: []byte(renderData[renderFile]),
Type: utils.YAMLDoc,
StartLine: 1,
EndLine: 1,
FilePath: renderFile,
})
}

return iacDocuments, nil
Expand All @@ -235,12 +233,12 @@ func (h *HelmV3) loadChart(chartPath string) ([]*utils.IacDocument, map[string]i
// load the chart file and values file from the specified chart path
chartFileBytes, err := ioutil.ReadFile(chartPath)
if err != nil {
logger.Warn("unable to read")
logger.Error("unable to read")
return iacDocuments, chartMap, err
}

if err = yaml.Unmarshal(chartFileBytes, &chartMap); err != nil {
logger.Warn("unable to unmarshal values")
logger.Error("unable to unmarshal values")
return iacDocuments, chartMap, err
}

Expand All @@ -257,13 +255,13 @@ func (h *HelmV3) loadChart(chartPath string) ([]*utils.IacDocument, map[string]i
var valueFileBytes []byte
valueFileBytes, err = ioutil.ReadFile(valuesFile)
if err != nil {
logger.Warn("unable to read values.yaml")
logger.Error("unable to read values.yaml")
return iacDocuments, chartMap, err
}

var valueMap map[string]interface{}
if err = yaml.Unmarshal(valueFileBytes, &valueMap); err != nil {
logger.Warn("unable to unmarshal values.yaml")
logger.Error("unable to unmarshal values.yaml")
return iacDocuments, chartMap, err
}

Expand All @@ -275,7 +273,14 @@ func (h *HelmV3) loadChart(chartPath string) ([]*utils.IacDocument, map[string]i
return iacDocuments, chartMap, err
}

iacDocuments, err = h.renderChart(chartPath, chartMap, templateFileMap, valueMap)
var renderedCharts []*utils.IacDocument
for templateDir, templateFiles := range templateFileMap {
renderedCharts, err = h.renderChart(chartPath, chartMap, templateDir, templateFiles, valueMap)
if err != nil {
continue
}
iacDocuments = append(iacDocuments, renderedCharts...)
}
return iacDocuments, chartMap, err
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/iac-providers/helm/v3/load-dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ func TestLoadChart(t *testing.T) {
helmv3: HelmV3{},
wantErr: &os.PathError{Err: syscall.ENOENT, Op: "lstat", Path: "testdata/chart-no-template-dir/templates"},
},
{
name: "chart path skip test dir dir",
chartPath: "./testdata/chart-skip-test-dir/Chart.yaml",
helmv3: HelmV3{},
wantErr: errSkipTestDir,
},
//{
// name: "chart path skip test dir",
// chartPath: "./testdata/chart-skip-test-dir/Chart.yaml",
// helmv3: HelmV3{},
// wantErr: errSkipTestDir,
//},
{
name: "chart path bad template file",
chartPath: "./testdata/chart-bad-template-file/Chart.yaml",
Expand Down

0 comments on commit a87488b

Please sign in to comment.