Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion chartvalidator/checker/engine_chart_rendering.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"context"
"fmt"
"math/rand"
Expand Down Expand Up @@ -89,8 +90,8 @@ func (engine *ChartRenderingEngine) renderSingleChart(chart ChartRenderParams, w

args := []string{
"template",
chart.ChartName,
resolveChartReference(chart),
"--release-name", chart.ChartName,
"-f", chart.BaseValuesFile,
"-f", chart.ValuesOverride,
"--version", chart.ChartVersion,
Expand Down Expand Up @@ -124,6 +125,8 @@ func (engine *ChartRenderingEngine) renderSingleChart(chart ChartRenderParams, w
return nil, fmt.Errorf("helm command failed: %w", err)
}

output = sanitizeManifestOutput(output)

logEngineDebug(engine.name, workerId, fmt.Sprintf("helm %s\t\tCOMPLETED", strings.Join(args, " ")))

// Create output file path using release name (use absolute path for output)
Expand Down Expand Up @@ -159,6 +162,29 @@ func resolveChartReference(chart ChartRenderParams) string {
return chart.ChartName
}

func sanitizeManifestOutput(output []byte) []byte {
trimmed := bytes.TrimLeft(output, "\n\r\t ")
manifestMarkers := [][]byte{
[]byte("---\n"),
[]byte("apiVersion:"),
[]byte("# Source:"),
}

firstIndex := -1
for _, marker := range manifestMarkers {
idx := bytes.Index(trimmed, marker)
if idx >= 0 && (firstIndex == -1 || idx < firstIndex) {
firstIndex = idx
}
}

if firstIndex <= 0 {
return trimmed
}

return trimmed[firstIndex:]
}

// Suffix the files just in case two charts end up having the same name
func generateRandomString(length int) string {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Expand Down
10 changes: 8 additions & 2 deletions chartvalidator/checker/engine_chart_rendering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -44,13 +45,14 @@ func TestRenderBasics(t *testing.T) {
assertChartFieldsMatch(t, testChart, result.Chart)

// Verify the command that was executed
expectedCommand := "helm template test-chart --release-name test-chart -f values.yaml -f override.yaml --version 1.0.0 --include-crds --kube-version 1.33.0 --repo https://example.com/charts --api-versions something --api-versions something-else"
expectedCommand := "helm template test-chart test-chart -f values.yaml -f override.yaml --version 1.0.0 --include-crds --kube-version 1.33.0 --repo https://example.com/charts --api-versions something --api-versions something-else"
actualCommand := mockExecutor.GetFullCommand()
assert.Equal(t, expectedCommand, actualCommand)
}

func TestRenderOCIRepo(t *testing.T) {
mockExecutor := createMockExecutor()
mockExecutor.Output = []byte("Pulled: europe-west4-docker.pkg.dev/wallet-dev-462809/interledger-helm-charts/test-chart:1.0.0\nDigest: sha256:abc123\n---\napiVersion: v1\nkind: ConfigMap\nmetadata:\n name: test\n")
engine := createEngine(mockExecutor, false)
defer cleanupEngine(engine)

Expand All @@ -62,9 +64,13 @@ func TestRenderOCIRepo(t *testing.T) {
assertChartFieldsMatch(t, testChart, result.Chart)

// OCI charts should be templated using the full OCI chart reference, not --repo.
expectedCommand := "helm template oci://europe-west4-docker.pkg.dev/wallet-dev-462809/interledger-helm-charts/test-chart --release-name test-chart -f values.yaml -f override.yaml --version 1.0.0 --include-crds --kube-version 1.33.0 --api-versions something --api-versions something-else"
expectedCommand := "helm template test-chart oci://europe-west4-docker.pkg.dev/wallet-dev-462809/interledger-helm-charts/test-chart -f values.yaml -f override.yaml --version 1.0.0 --include-crds --kube-version 1.33.0 --api-versions something --api-versions something-else"
actualCommand := mockExecutor.GetFullCommand()
assert.Equal(t, expectedCommand, actualCommand)

renderedManifest, err := os.ReadFile(result.ManifestPath)
assert.NoError(t, err)
assert.Equal(t, "---\napiVersion: v1\nkind: ConfigMap\nmetadata:\n name: test\n", string(renderedManifest))
}

func TestRenderBaseFileNotExist(t *testing.T) {
Expand Down
Loading