diff --git a/pkg/action/action.go b/pkg/action/action.go index e93d6181f..015fa2736 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -276,9 +276,9 @@ func splitAndDeannotate(postrendered, fallbackPrefix string) (map[string]string, // TODO: As part of the refactor the duplicate code in cmd/helm/template.go should be removed // // This code has to do with writing files to disk. -func (cfg *Configuration) renderResources(ctx context.Context, ch *chart.Chart, values common.Values, releaseName, outputDir string, subNotes, useReleaseName, includeCrds bool, pr postrenderer.PostRenderer, interactWithRemote, enableDNS, hideSecret bool, postRenderStrategy PostRenderStrategy) ([]*release.Hook, *bytes.Buffer, string, error) { +func (cfg *Configuration) renderResources(ctx context.Context, ch *chart.Chart, values common.Values, releaseName, outputDir string, subNotes, useReleaseName, includeCrds bool, pr postrenderer.PostRenderer, interactWithRemote, enableDNS, hideSecret bool, postRenderStrategy PostRenderStrategy) ([]*release.Hook, []byte, string, error) { var hs []*release.Hook - b := bytes.NewBuffer(nil) + var b []byte caps, err := cfg.getCapabilities() if err != nil { @@ -355,7 +355,7 @@ func (cfg *Configuration) renderResources(ctx context.Context, ch *chart.Chart, if strings.TrimSpace(content) == "" { continue } - fmt.Fprintf(b, "---\n# Source: %s\n%s\n", name, content) + b = fmt.Appendf(b, "---\n# Source: %s\n%s\n", name, content) } return hs, b, "", err } @@ -473,7 +473,7 @@ func (cfg *Configuration) renderResources(ctx context.Context, ch *chart.Chart, if strings.TrimSpace(content) == "" { continue } - fmt.Fprintf(b, "---\n# Source: %s\n%s\n", name, content) + b = fmt.Appendf(b, "---\n# Source: %s\n%s\n", name, content) } return hs, b, "", err } @@ -484,7 +484,7 @@ func (cfg *Configuration) renderResources(ctx context.Context, ch *chart.Chart, if includeCrds { for _, crd := range ch.CRDObjects() { if outputDir == "" { - fmt.Fprintf(b, "---\n# Source: %s\n%s\n", crd.Filename, string(crd.File.Data)) + b = fmt.Appendf(b, "---\n# Source: %s\n%s\n", crd.Filename, string(crd.File.Data)) } else { err = writeToFile(outputDir, crd.Filename, string(crd.File.Data), fileWritten[crd.Filename]) if err != nil { @@ -498,9 +498,9 @@ func (cfg *Configuration) renderResources(ctx context.Context, ch *chart.Chart, for _, m := range manifests { if outputDir == "" { if hideSecret && m.Head.Kind == "Secret" && m.Head.Version == "v1" { - fmt.Fprintf(b, "---\n# Source: %s\n# HIDDEN: The Secret output has been suppressed\n", m.Name) + b = fmt.Appendf(b, "---\n# Source: %s\n# HIDDEN: The Secret output has been suppressed\n", m.Name) } else { - fmt.Fprintf(b, "---\n# Source: %s\n%s\n", m.Name, m.Content) + b = fmt.Appendf(b, "---\n# Source: %s\n%s\n", m.Name, m.Content) } } else { newDir := outputDir diff --git a/pkg/action/action_test.go b/pkg/action/action_test.go index 056c539a5..d4294f00a 100644 --- a/pkg/action/action_test.go +++ b/pkg/action/action_test.go @@ -1845,7 +1845,7 @@ data: name: value ` - assert.Equal(t, expectedBuf, buf.String()) + assert.Equal(t, expectedBuf, string(buf)) assert.Len(t, hooks, 1) assert.Equal(t, expectedHook, hooks[0].Manifest) } @@ -1941,7 +1941,7 @@ func TestRenderResources_PostRenderer_Integration(t *testing.T) { assert.Empty(t, notes) // Notes should be empty for this test // Verify that the post-renderer modifications are present in the output - output := buf.String() + output := string(buf) expected := `--- # Source: hello/templates/goodbye goodbye: world @@ -2036,8 +2036,8 @@ spec: require.NoError(t, err) assert.Len(t, hooks, 1) assert.Equal(t, "my-app", hooks[0].Name) - assert.Contains(t, buf.String(), "kind: Deployment") - assert.Contains(t, buf.String(), "kind: ServiceAccount") + assert.Contains(t, string(buf), "kind: Deployment") + assert.Contains(t, string(buf), "kind: ServiceAccount") } func TestRenderResources_PostRenderer_CombinedInvokesOnceWithEverything(t *testing.T) { @@ -2221,7 +2221,7 @@ metadata: // Hooks still round-trip through the release so they can execute. require.Len(t, hooks, 1) assert.Contains(t, hooks[0].Manifest, "hook-cm") - assert.Contains(t, manifestDoc.String(), "template-cm") + assert.Contains(t, string(manifestDoc), "template-cm") } func TestRenderResources_PostRenderer_NoHooksWithOnlyHooks(t *testing.T) { diff --git a/pkg/action/install.go b/pkg/action/install.go index 6fc919366..c2c054968 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -374,12 +374,10 @@ func (i *Install) RunWithContext(ctx context.Context, ch ci.Charter, vals map[st rel := i.createRelease(chrt, vals, i.Labels) - var manifestDoc *bytes.Buffer - rel.Hooks, manifestDoc, rel.Info.Notes, err = i.cfg.renderResources(ctx, chrt, valuesToRender, i.ReleaseName, i.OutputDir, i.SubNotes, i.UseReleaseName, i.IncludeCRDs, i.PostRenderer, interactWithServer(i.DryRunStrategy), i.EnableDNS, i.HideSecret, i.PostRenderStrategy) + var manifest []byte + rel.Hooks, manifest, rel.Info.Notes, err = i.cfg.renderResources(ctx, chrt, valuesToRender, i.ReleaseName, i.OutputDir, i.SubNotes, i.UseReleaseName, i.IncludeCRDs, i.PostRenderer, interactWithServer(i.DryRunStrategy), i.EnableDNS, i.HideSecret, i.PostRenderStrategy) // Even for errors, attach this if available - if manifestDoc != nil { - rel.Manifest = manifestDoc.String() - } + rel.Manifest = string(manifest) // Check error from render if err != nil { rel.SetStatus(rcommon.StatusFailed, "failed to render resource: "+err.Error()) @@ -391,7 +389,7 @@ func (i *Install) RunWithContext(ctx context.Context, ch ci.Charter, vals map[st rel.SetStatus(rcommon.StatusPendingInstall, "Initial install underway") var toBeAdopted kube.ResourceList - resources, err := i.cfg.KubeClient.Build(bytes.NewBufferString(rel.Manifest), !i.DisableOpenAPIValidation) + resources, err := i.cfg.KubeClient.Build(bytes.NewReader(manifest), !i.DisableOpenAPIValidation) if err != nil { return nil, fmt.Errorf("unable to build kubernetes objects from release manifest: %w", err) } diff --git a/pkg/action/upgrade.go b/pkg/action/upgrade.go index 85904124b..ffc013306 100644 --- a/pkg/action/upgrade.go +++ b/pkg/action/upgrade.go @@ -299,7 +299,7 @@ func (u *Upgrade) prepareUpgrade(ctx context.Context, name string, chart *chartv return nil, nil, false, err } - hooks, manifestDoc, notesTxt, err := u.cfg.renderResources(ctx, chart, valuesToRender, "", "", u.SubNotes, false, false, u.PostRenderer, interactWithServer(u.DryRunStrategy), u.EnableDNS, u.HideSecret, u.PostRenderStrategy) + hooks, manifest, notesTxt, err := u.cfg.renderResources(ctx, chart, valuesToRender, "", "", u.SubNotes, false, false, u.PostRenderer, interactWithServer(u.DryRunStrategy), u.EnableDNS, u.HideSecret, u.PostRenderStrategy) if err != nil { return nil, nil, false, err } @@ -328,7 +328,7 @@ func (u *Upgrade) prepareUpgrade(ctx context.Context, name string, chart *chartv Description: "Preparing upgrade", // This should be overwritten later. }, Version: revision, - Manifest: manifestDoc.String(), + Manifest: string(manifest), Hooks: hooks, Labels: mergeCustomLabels(lastRelease.Labels, u.Labels), ApplyMethod: string(determineReleaseSSApplyMethod(serverSideApply)), @@ -337,7 +337,7 @@ func (u *Upgrade) prepareUpgrade(ctx context.Context, name string, chart *chartv if notesTxt != "" { upgradedRelease.Info.Notes = notesTxt } - err = validateManifest(u.cfg.KubeClient, manifestDoc.Bytes(), !u.DisableOpenAPIValidation) + err = validateManifest(u.cfg.KubeClient, manifest, !u.DisableOpenAPIValidation) return currentRelease, upgradedRelease, serverSideApply, err }