Skip to content

Commit

Permalink
cleanup, adding comments
Browse files Browse the repository at this point in the history
Signed-off-by: manasachi <manasachinta7@gmail.com>
  • Loading branch information
manasachi committed Jun 26, 2024
1 parent d0d82bf commit dd0cc0d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 5 additions & 3 deletions pkg/safeguards/preprocessing_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ import (
"helm.sh/helm/v3/pkg/engine"
)

// Given a Helm chart directory or file, render all templates and write them to a temporary directory
// Given a Helm chart directory or file, renders all templates and writes them to the specified directory
func RenderHelmChart(isFile bool, mainChartPath, tempDir string) error {
if isFile { //get the directory that the chart lives in
if isFile { // Get the directory that the Chart.yaml lives in
mainChartPath = filepath.Dir(mainChartPath)
}
loadedCharts := make(map[string]*chart.Chart) // map of chart path to chart object

mainChart, err := loader.Load(mainChartPath)
if err != nil {
return fmt.Errorf("failed to load main chart: %s", err)
}

loadedCharts := make(map[string]*chart.Chart) // map of chart path to chart object
loadedCharts[mainChartPath] = mainChart

// Load subcharts and dependencies
Expand Down Expand Up @@ -67,6 +68,7 @@ func RenderHelmChart(isFile bool, mainChartPath, tempDir string) error {
return nil
}

// Returns values from values.yaml and release options specified in values.yaml
func getValues(chart *chart.Chart, valuesPath string) (chartutil.Values, error) {
// Load values file
valuesFile, err := os.ReadFile(valuesPath)
Expand Down
6 changes: 5 additions & 1 deletion pkg/safeguards/preprocessing_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func setup(t *testing.T) {
}
}

// Test rendering a valid Helm chart with no subcharts and three templates
func TestRenderHelmChart_Valid(t *testing.T) {
setup(t)
t.Cleanup(func() { cleanupDir(t, tempDir) })
Expand Down Expand Up @@ -79,6 +80,7 @@ func TestRenderHelmChart_Valid(t *testing.T) {
assert.Equal(t, parseYAML(t, getManifestAsString(t, "expectedingress.yaml")), parseYAML(t, readFile(t, filepath.Join(tempDir, "ingress.yaml"))))
}

// Should successfully render a Helm chart with sub charts and be able to render subchart separately within a helm chart
func TestSubCharts(t *testing.T) {
setup(t)
t.Cleanup(func() { cleanupDir(t, tempDir) })
Expand Down Expand Up @@ -124,7 +126,8 @@ func TestSubCharts(t *testing.T) {
//expect mainchart.yaml to not exist
outputFilePath := filepath.Join(tempDir, "maindeployment.yaml")
assert.NoFileExists(t, outputFilePath, "Unexpected file was created: %s", outputFilePath)

assert.Equal(t, parseYAML(t, getManifestAsString(t, "expected-subchart1.yaml")), parseYAML(t, readFile(t, filepath.Join(tempDir, "deployment1.yaml"))))
assert.Equal(t, parseYAML(t, getManifestAsString(t, "expected-subchart2.yaml")), parseYAML(t, readFile(t, filepath.Join(tempDir, "deployment2.yaml"))))
}

/**
Expand Down Expand Up @@ -163,6 +166,7 @@ func TestInvalidDeployments(t *testing.T) {
assert.Contains(t, err.Error(), "map has no entry for key")
}

/** Test different helm folder structures */
func TestDifferentFolderStructures(t *testing.T) {
setup(t)
t.Cleanup(func() { cleanupDir(t, tempDir) })
Expand Down

0 comments on commit dd0cc0d

Please sign in to comment.