diff --git a/cmd/helm/template.go b/cmd/helm/template.go index e504bc774..d760fb87b 100644 --- a/cmd/helm/template.go +++ b/cmd/helm/template.go @@ -27,6 +27,8 @@ import ( "sort" "strings" + "helm.sh/helm/v3/pkg/release" + "github.com/spf13/cobra" "helm.sh/helm/v3/cmd/helm/require" @@ -68,7 +70,6 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { client.ClientOnly = !validate client.APIVersions = chartutil.VersionSet(extraAPIs) client.IncludeCRDs = includeCrds - client.SkipTests = skipTests rel, err := runInstall(args, client, valueOpts, out) if err != nil && !settings.Debug { @@ -86,6 +87,9 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { if !client.DisableHooks { fileWritten := make(map[string]bool) for _, m := range rel.Hooks { + if skipTests && isTestHook(m) { + continue + } if client.OutputDir == "" { fmt.Fprintf(&manifests, "---\n# Source: %s\n%s\n", m.Path, m.Manifest) } else { @@ -165,7 +169,7 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { f.StringVar(&client.OutputDir, "output-dir", "", "writes the executed templates to files in output-dir instead of stdout") f.BoolVar(&validate, "validate", false, "validate your manifests against the Kubernetes cluster you are currently pointing at. This is the same validation performed on an install") f.BoolVar(&includeCrds, "include-crds", false, "include CRDs in the templated output") - f.BoolVar(&skipTests, "skip-tests", false, "skip tests and manifests in tests directories from templated output") + f.BoolVar(&skipTests, "skip-tests", false, "skip tests from templated output") f.BoolVar(&client.IsUpgrade, "is-upgrade", false, "set .Release.IsUpgrade instead of .Release.IsInstall") f.StringArrayVarP(&extraAPIs, "api-versions", "a", []string{}, "Kubernetes api versions used for Capabilities.APIVersions") f.BoolVar(&client.UseReleaseName, "release-name", false, "use release name in the output-dir path.") @@ -174,6 +178,15 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { return cmd } +func isTestHook(h *release.Hook) bool { + for _, e := range h.Events { + if e == release.HookTest { + return true + } + } + return false +} + // The following functions (writeToFile, createOrOpenFile, and ensureDirectoryForFile) // are coppied from the actions package. This is part of a change to correct a // bug introduced by #8156. As part of the todo to refactor renderResources diff --git a/cmd/helm/template_test.go b/cmd/helm/template_test.go index dd30b3836..9e6a0c434 100644 --- a/cmd/helm/template_test.go +++ b/cmd/helm/template_test.go @@ -122,9 +122,9 @@ func TestTemplateCmd(t *testing.T) { golden: "output/template-with-invalid-yaml-debug.txt", }, { - name: "template with skip-tests", + name: "template skip-tests", cmd: fmt.Sprintf(`template '%s' --skip-tests`, chartPath), - golden: "output/template-no-tests.txt", + golden: "output/template-skip-tests.txt", }, } runTestCmd(t, tests) diff --git a/cmd/helm/testdata/output/template-name-template.txt b/cmd/helm/testdata/output/template-name-template.txt index 741630922..5e4478937 100644 --- a/cmd/helm/testdata/output/template-name-template.txt +++ b/cmd/helm/testdata/output/template-name-template.txt @@ -5,14 +5,6 @@ kind: ServiceAccount metadata: name: subchart-sa --- -# Source: subchart/templates/tests/test-config.yaml -apiVersion: v1 -kind: ConfigMap -metadata: - name: "foobar-YWJj-baz-testconfig" -data: - message: Hello World ---- # Source: subchart/templates/subdir/role.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: Role @@ -92,6 +84,16 @@ spec: selector: app.kubernetes.io/name: subchart --- +# Source: subchart/templates/tests/test-config.yaml +apiVersion: v1 +kind: ConfigMap +metadata: + name: "foobar-YWJj-baz-testconfig" + annotations: + "helm.sh/hook": test +data: + message: Hello World +--- # Source: subchart/templates/tests/test-nothing.yaml apiVersion: v1 kind: Pod diff --git a/cmd/helm/testdata/output/template-set.txt b/cmd/helm/testdata/output/template-set.txt index 42a08c391..0db9a9b74 100644 --- a/cmd/helm/testdata/output/template-set.txt +++ b/cmd/helm/testdata/output/template-set.txt @@ -5,14 +5,6 @@ kind: ServiceAccount metadata: name: subchart-sa --- -# Source: subchart/templates/tests/test-config.yaml -apiVersion: v1 -kind: ConfigMap -metadata: - name: "RELEASE-NAME-testconfig" -data: - message: Hello World ---- # Source: subchart/templates/subdir/role.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: Role @@ -92,6 +84,16 @@ spec: selector: app.kubernetes.io/name: subchart --- +# Source: subchart/templates/tests/test-config.yaml +apiVersion: v1 +kind: ConfigMap +metadata: + name: "RELEASE-NAME-testconfig" + annotations: + "helm.sh/hook": test +data: + message: Hello World +--- # Source: subchart/templates/tests/test-nothing.yaml apiVersion: v1 kind: Pod diff --git a/cmd/helm/testdata/output/template-no-tests.txt b/cmd/helm/testdata/output/template-skip-tests.txt similarity index 82% rename from cmd/helm/testdata/output/template-no-tests.txt rename to cmd/helm/testdata/output/template-skip-tests.txt index de537c214..16808ede3 100644 --- a/cmd/helm/testdata/output/template-no-tests.txt +++ b/cmd/helm/testdata/output/template-skip-tests.txt @@ -84,22 +84,3 @@ spec: name: nginx selector: app.kubernetes.io/name: subchart ---- -# Source: subchart/templates/tests/test-nothing.yaml -apiVersion: v1 -kind: Pod -metadata: - name: "RELEASE-NAME-test" - annotations: - "helm.sh/hook": test -spec: - containers: - - name: test - image: "alpine:latest" - envFrom: - - configMapRef: - name: "RELEASE-NAME-testconfig" - command: - - echo - - "$message" - restartPolicy: Never diff --git a/cmd/helm/testdata/output/template-values-files.txt b/cmd/helm/testdata/output/template-values-files.txt index 42a08c391..0db9a9b74 100644 --- a/cmd/helm/testdata/output/template-values-files.txt +++ b/cmd/helm/testdata/output/template-values-files.txt @@ -5,14 +5,6 @@ kind: ServiceAccount metadata: name: subchart-sa --- -# Source: subchart/templates/tests/test-config.yaml -apiVersion: v1 -kind: ConfigMap -metadata: - name: "RELEASE-NAME-testconfig" -data: - message: Hello World ---- # Source: subchart/templates/subdir/role.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: Role @@ -92,6 +84,16 @@ spec: selector: app.kubernetes.io/name: subchart --- +# Source: subchart/templates/tests/test-config.yaml +apiVersion: v1 +kind: ConfigMap +metadata: + name: "RELEASE-NAME-testconfig" + annotations: + "helm.sh/hook": test +data: + message: Hello World +--- # Source: subchart/templates/tests/test-nothing.yaml apiVersion: v1 kind: Pod diff --git a/cmd/helm/testdata/output/template-with-api-version.txt b/cmd/helm/testdata/output/template-with-api-version.txt index da77c51c0..3e488f0d2 100644 --- a/cmd/helm/testdata/output/template-with-api-version.txt +++ b/cmd/helm/testdata/output/template-with-api-version.txt @@ -5,14 +5,6 @@ kind: ServiceAccount metadata: name: subchart-sa --- -# Source: subchart/templates/tests/test-config.yaml -apiVersion: v1 -kind: ConfigMap -metadata: - name: "RELEASE-NAME-testconfig" -data: - message: Hello World ---- # Source: subchart/templates/subdir/role.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: Role @@ -93,6 +85,16 @@ spec: selector: app.kubernetes.io/name: subchart --- +# Source: subchart/templates/tests/test-config.yaml +apiVersion: v1 +kind: ConfigMap +metadata: + name: "RELEASE-NAME-testconfig" + annotations: + "helm.sh/hook": test +data: + message: Hello World +--- # Source: subchart/templates/tests/test-nothing.yaml apiVersion: v1 kind: Pod diff --git a/cmd/helm/testdata/output/template-with-crds.txt b/cmd/helm/testdata/output/template-with-crds.txt index 57e770176..4bd5d2e29 100644 --- a/cmd/helm/testdata/output/template-with-crds.txt +++ b/cmd/helm/testdata/output/template-with-crds.txt @@ -22,14 +22,6 @@ kind: ServiceAccount metadata: name: subchart-sa --- -# Source: subchart/templates/tests/test-config.yaml -apiVersion: v1 -kind: ConfigMap -metadata: - name: "RELEASE-NAME-testconfig" -data: - message: Hello World ---- # Source: subchart/templates/subdir/role.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: Role @@ -110,6 +102,16 @@ spec: selector: app.kubernetes.io/name: subchart --- +# Source: subchart/templates/tests/test-config.yaml +apiVersion: v1 +kind: ConfigMap +metadata: + name: "RELEASE-NAME-testconfig" + annotations: + "helm.sh/hook": test +data: + message: Hello World +--- # Source: subchart/templates/tests/test-nothing.yaml apiVersion: v1 kind: Pod diff --git a/cmd/helm/testdata/output/template.txt b/cmd/helm/testdata/output/template.txt index b2c65a4e1..a81934b20 100644 --- a/cmd/helm/testdata/output/template.txt +++ b/cmd/helm/testdata/output/template.txt @@ -5,14 +5,6 @@ kind: ServiceAccount metadata: name: subchart-sa --- -# Source: subchart/templates/tests/test-config.yaml -apiVersion: v1 -kind: ConfigMap -metadata: - name: "RELEASE-NAME-testconfig" -data: - message: Hello World ---- # Source: subchart/templates/subdir/role.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: Role @@ -92,6 +84,16 @@ spec: selector: app.kubernetes.io/name: subchart --- +# Source: subchart/templates/tests/test-config.yaml +apiVersion: v1 +kind: ConfigMap +metadata: + name: "RELEASE-NAME-testconfig" + annotations: + "helm.sh/hook": test +data: + message: Hello World +--- # Source: subchart/templates/tests/test-nothing.yaml apiVersion: v1 kind: Pod diff --git a/cmd/helm/testdata/testcharts/subchart/templates/tests/test-config.yaml b/cmd/helm/testdata/testcharts/subchart/templates/tests/test-config.yaml index de639e03b..0aa3eea29 100644 --- a/cmd/helm/testdata/testcharts/subchart/templates/tests/test-config.yaml +++ b/cmd/helm/testdata/testcharts/subchart/templates/tests/test-config.yaml @@ -2,5 +2,7 @@ apiVersion: v1 kind: ConfigMap metadata: name: "{{ .Release.Name }}-testconfig" + annotations: + "helm.sh/hook": test data: message: Hello World diff --git a/pkg/action/action.go b/pkg/action/action.go index 2fc452b7f..071db709b 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -101,7 +101,7 @@ type Configuration struct { // TODO: This function is badly in need of a refactor. // 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 (c *Configuration) renderResources(ch *chart.Chart, values chartutil.Values, releaseName, outputDir string, subNotes, useReleaseName, includeCrds bool, skipTests bool, pr postrender.PostRenderer, dryRun bool) ([]*release.Hook, *bytes.Buffer, string, error) { +func (c *Configuration) renderResources(ch *chart.Chart, values chartutil.Values, releaseName, outputDir string, subNotes, useReleaseName, includeCrds bool, pr postrender.PostRenderer, dryRun bool) ([]*release.Hook, *bytes.Buffer, string, error) { hs := []*release.Hook{} b := bytes.NewBuffer(nil) @@ -194,7 +194,7 @@ func (c *Configuration) renderResources(ch *chart.Chart, values chartutil.Values } } - for _, m := range filterManifests(manifests, skipTests) { + for _, m := range manifests { if outputDir == "" { fmt.Fprintf(b, "---\n# Source: %s\n%s\n", m.Name, m.Content) } else { @@ -224,19 +224,6 @@ func (c *Configuration) renderResources(ch *chart.Chart, values chartutil.Values return hs, b, notes, nil } -func filterManifests(manifests []releaseutil.Manifest, skipTests bool) []releaseutil.Manifest { - if skipTests { - var manifestsWithoutTests []releaseutil.Manifest - for _, m := range manifests { - if !strings.Contains(m.Name, "tests/") { - manifestsWithoutTests = append(manifestsWithoutTests, m) - } - } - return manifestsWithoutTests - } - return manifests -} - // RESTClientGetter gets the rest client type RESTClientGetter interface { ToRESTConfig() (*rest.Config, error) diff --git a/pkg/action/install.go b/pkg/action/install.go index f065e818c..00fb208b0 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -91,7 +91,6 @@ type Install struct { SubNotes bool DisableOpenAPIValidation bool IncludeCRDs bool - SkipTests bool // APIVersions allows a manual set of supported API Versions to be passed // (for things like templating). These are ignored if ClientOnly is false APIVersions chartutil.VersionSet @@ -237,7 +236,7 @@ func (i *Install) Run(chrt *chart.Chart, vals map[string]interface{}) (*release. rel := i.createRelease(chrt, vals) var manifestDoc *bytes.Buffer - rel.Hooks, manifestDoc, rel.Info.Notes, err = i.cfg.renderResources(chrt, valuesToRender, i.ReleaseName, i.OutputDir, i.SubNotes, i.UseReleaseName, i.IncludeCRDs, i.SkipTests, i.PostRenderer, i.DryRun) + rel.Hooks, manifestDoc, rel.Info.Notes, err = i.cfg.renderResources(chrt, valuesToRender, i.ReleaseName, i.OutputDir, i.SubNotes, i.UseReleaseName, i.IncludeCRDs, i.PostRenderer, i.DryRun) // Even for errors, attach this if available if manifestDoc != nil { rel.Manifest = manifestDoc.String() diff --git a/pkg/action/upgrade.go b/pkg/action/upgrade.go index f4110f6af..b707e7e69 100644 --- a/pkg/action/upgrade.go +++ b/pkg/action/upgrade.go @@ -223,7 +223,7 @@ func (u *Upgrade) prepareUpgrade(name string, chart *chart.Chart, vals map[strin return nil, nil, err } - hooks, manifestDoc, notesTxt, err := u.cfg.renderResources(chart, valuesToRender, "", "", u.SubNotes, false, false, false, u.PostRenderer, u.DryRun) + hooks, manifestDoc, notesTxt, err := u.cfg.renderResources(chart, valuesToRender, "", "", u.SubNotes, false, false, u.PostRenderer, u.DryRun) if err != nil { return nil, nil, err }