mirror of https://github.com/helm/helm
commit
11eeb4a6b1
@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
Copyright The Helm Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"runtime/pprof"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
cpuProfileFile *os.File
|
||||||
|
cpuProfilePath string
|
||||||
|
memProfilePath string
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
cpuProfilePath = os.Getenv("HELM_PPROF_CPU_PROFILE")
|
||||||
|
memProfilePath = os.Getenv("HELM_PPROF_MEM_PROFILE")
|
||||||
|
}
|
||||||
|
|
||||||
|
// startProfiling starts profiling CPU usage if HELM_PPROF_CPU_PROFILE is set
|
||||||
|
// to a file path. It returns an error if the file could not be created or
|
||||||
|
// CPU profiling could not be started.
|
||||||
|
func startProfiling() error {
|
||||||
|
if cpuProfilePath != "" {
|
||||||
|
var err error
|
||||||
|
cpuProfileFile, err = os.Create(cpuProfilePath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not create CPU profile: %w", err)
|
||||||
|
}
|
||||||
|
if err := pprof.StartCPUProfile(cpuProfileFile); err != nil {
|
||||||
|
cpuProfileFile.Close()
|
||||||
|
cpuProfileFile = nil
|
||||||
|
return fmt.Errorf("could not start CPU profile: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// stopProfiling stops profiling CPU and memory usage.
|
||||||
|
// It writes memory profile to the file path specified in HELM_PPROF_MEM_PROFILE
|
||||||
|
// environment variable.
|
||||||
|
func stopProfiling() error {
|
||||||
|
errs := []error{}
|
||||||
|
|
||||||
|
// Stop CPU profiling if it was started
|
||||||
|
if cpuProfileFile != nil {
|
||||||
|
pprof.StopCPUProfile()
|
||||||
|
err := cpuProfileFile.Close()
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
cpuProfileFile = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if memProfilePath != "" {
|
||||||
|
f, err := os.Create(memProfilePath)
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
runtime.GC() // get up-to-date statistics
|
||||||
|
if err := pprof.WriteHeapProfile(f); err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := errors.Join(errs...); err != nil {
|
||||||
|
return fmt.Errorf("error(s) while stopping profiling: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,208 @@
|
|||||||
|
/*
|
||||||
|
Copyright The Helm Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package action
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"helm.sh/helm/v4/pkg/chart"
|
||||||
|
kubefake "helm.sh/helm/v4/pkg/kube/fake"
|
||||||
|
"helm.sh/helm/v4/pkg/release"
|
||||||
|
)
|
||||||
|
|
||||||
|
func podManifestWithOutputLogs(hookDefinitions []release.HookOutputLogPolicy) string {
|
||||||
|
hookDefinitionString := convertHooksToCommaSeparated(hookDefinitions)
|
||||||
|
return fmt.Sprintf(`kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: finding-sharky,
|
||||||
|
annotations:
|
||||||
|
"helm.sh/hook": pre-install
|
||||||
|
"helm.sh/hook-output-log-policy": %s
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: sharky-test
|
||||||
|
image: fake-image
|
||||||
|
cmd: fake-command`, hookDefinitionString)
|
||||||
|
}
|
||||||
|
|
||||||
|
func podManifestWithOutputLogWithNamespace(hookDefinitions []release.HookOutputLogPolicy) string {
|
||||||
|
hookDefinitionString := convertHooksToCommaSeparated(hookDefinitions)
|
||||||
|
return fmt.Sprintf(`kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: finding-george
|
||||||
|
namespace: sneaky-namespace
|
||||||
|
annotations:
|
||||||
|
"helm.sh/hook": pre-install
|
||||||
|
"helm.sh/hook-output-log-policy": %s
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: george-test
|
||||||
|
image: fake-image
|
||||||
|
cmd: fake-command`, hookDefinitionString)
|
||||||
|
}
|
||||||
|
|
||||||
|
func jobManifestWithOutputLog(hookDefinitions []release.HookOutputLogPolicy) string {
|
||||||
|
hookDefinitionString := convertHooksToCommaSeparated(hookDefinitions)
|
||||||
|
return fmt.Sprintf(`kind: Job
|
||||||
|
apiVersion: batch/v1
|
||||||
|
metadata:
|
||||||
|
name: losing-religion
|
||||||
|
annotations:
|
||||||
|
"helm.sh/hook": pre-install
|
||||||
|
"helm.sh/hook-output-log-policy": %s
|
||||||
|
spec:
|
||||||
|
completions: 1
|
||||||
|
parallelism: 1
|
||||||
|
activeDeadlineSeconds: 30
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: religion-container
|
||||||
|
image: religion-image
|
||||||
|
cmd: religion-command`, hookDefinitionString)
|
||||||
|
}
|
||||||
|
|
||||||
|
func jobManifestWithOutputLogWithNamespace(hookDefinitions []release.HookOutputLogPolicy) string {
|
||||||
|
hookDefinitionString := convertHooksToCommaSeparated(hookDefinitions)
|
||||||
|
return fmt.Sprintf(`kind: Job
|
||||||
|
apiVersion: batch/v1
|
||||||
|
metadata:
|
||||||
|
name: losing-religion
|
||||||
|
namespace: rem-namespace
|
||||||
|
annotations:
|
||||||
|
"helm.sh/hook": pre-install
|
||||||
|
"helm.sh/hook-output-log-policy": %s
|
||||||
|
spec:
|
||||||
|
completions: 1
|
||||||
|
parallelism: 1
|
||||||
|
activeDeadlineSeconds: 30
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: religion-container
|
||||||
|
image: religion-image
|
||||||
|
cmd: religion-command`, hookDefinitionString)
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertHooksToCommaSeparated(hookDefinitions []release.HookOutputLogPolicy) string {
|
||||||
|
var commaSeparated string
|
||||||
|
for i, policy := range hookDefinitions {
|
||||||
|
if i+1 == len(hookDefinitions) {
|
||||||
|
commaSeparated += policy.String()
|
||||||
|
} else {
|
||||||
|
commaSeparated += policy.String() + ","
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return commaSeparated
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInstallRelease_HookOutputLogsOnFailure(t *testing.T) {
|
||||||
|
// Should output on failure with expected namespace if hook-failed is set
|
||||||
|
runInstallForHooksWithFailure(t, podManifestWithOutputLogs([]release.HookOutputLogPolicy{release.HookOutputOnFailed}), "spaced", true)
|
||||||
|
runInstallForHooksWithFailure(t, podManifestWithOutputLogWithNamespace([]release.HookOutputLogPolicy{release.HookOutputOnFailed}), "sneaky-namespace", true)
|
||||||
|
runInstallForHooksWithFailure(t, jobManifestWithOutputLog([]release.HookOutputLogPolicy{release.HookOutputOnFailed}), "spaced", true)
|
||||||
|
runInstallForHooksWithFailure(t, jobManifestWithOutputLogWithNamespace([]release.HookOutputLogPolicy{release.HookOutputOnFailed}), "rem-namespace", true)
|
||||||
|
|
||||||
|
// Should not output on failure with expected namespace if hook-succeed is set
|
||||||
|
runInstallForHooksWithFailure(t, podManifestWithOutputLogs([]release.HookOutputLogPolicy{release.HookOutputOnSucceeded}), "", false)
|
||||||
|
runInstallForHooksWithFailure(t, podManifestWithOutputLogWithNamespace([]release.HookOutputLogPolicy{release.HookOutputOnSucceeded}), "", false)
|
||||||
|
runInstallForHooksWithFailure(t, jobManifestWithOutputLog([]release.HookOutputLogPolicy{release.HookOutputOnSucceeded}), "", false)
|
||||||
|
runInstallForHooksWithFailure(t, jobManifestWithOutputLogWithNamespace([]release.HookOutputLogPolicy{release.HookOutputOnSucceeded}), "", false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInstallRelease_HookOutputLogsOnSuccess(t *testing.T) {
|
||||||
|
// Should output on success with expected namespace if hook-succeeded is set
|
||||||
|
runInstallForHooksWithSuccess(t, podManifestWithOutputLogs([]release.HookOutputLogPolicy{release.HookOutputOnSucceeded}), "spaced", true)
|
||||||
|
runInstallForHooksWithSuccess(t, podManifestWithOutputLogWithNamespace([]release.HookOutputLogPolicy{release.HookOutputOnSucceeded}), "sneaky-namespace", true)
|
||||||
|
runInstallForHooksWithSuccess(t, jobManifestWithOutputLog([]release.HookOutputLogPolicy{release.HookOutputOnSucceeded}), "spaced", true)
|
||||||
|
runInstallForHooksWithSuccess(t, jobManifestWithOutputLogWithNamespace([]release.HookOutputLogPolicy{release.HookOutputOnSucceeded}), "rem-namespace", true)
|
||||||
|
|
||||||
|
// Should not output on success if hook-failed is set
|
||||||
|
runInstallForHooksWithSuccess(t, podManifestWithOutputLogs([]release.HookOutputLogPolicy{release.HookOutputOnFailed}), "", false)
|
||||||
|
runInstallForHooksWithSuccess(t, podManifestWithOutputLogWithNamespace([]release.HookOutputLogPolicy{release.HookOutputOnFailed}), "", false)
|
||||||
|
runInstallForHooksWithSuccess(t, jobManifestWithOutputLog([]release.HookOutputLogPolicy{release.HookOutputOnFailed}), "", false)
|
||||||
|
runInstallForHooksWithSuccess(t, jobManifestWithOutputLogWithNamespace([]release.HookOutputLogPolicy{release.HookOutputOnFailed}), "", false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInstallRelease_HooksOutputLogsOnSuccessAndFailure(t *testing.T) {
|
||||||
|
// Should output on success with expected namespace if hook-succeeded and hook-failed is set
|
||||||
|
runInstallForHooksWithSuccess(t, podManifestWithOutputLogs([]release.HookOutputLogPolicy{release.HookOutputOnSucceeded, release.HookOutputOnFailed}), "spaced", true)
|
||||||
|
runInstallForHooksWithSuccess(t, podManifestWithOutputLogWithNamespace([]release.HookOutputLogPolicy{release.HookOutputOnSucceeded, release.HookOutputOnFailed}), "sneaky-namespace", true)
|
||||||
|
runInstallForHooksWithSuccess(t, jobManifestWithOutputLog([]release.HookOutputLogPolicy{release.HookOutputOnSucceeded, release.HookOutputOnFailed}), "spaced", true)
|
||||||
|
runInstallForHooksWithSuccess(t, jobManifestWithOutputLogWithNamespace([]release.HookOutputLogPolicy{release.HookOutputOnSucceeded, release.HookOutputOnFailed}), "rem-namespace", true)
|
||||||
|
|
||||||
|
// Should output on failure if hook-succeeded and hook-failed is set
|
||||||
|
runInstallForHooksWithFailure(t, podManifestWithOutputLogs([]release.HookOutputLogPolicy{release.HookOutputOnSucceeded, release.HookOutputOnFailed}), "spaced", true)
|
||||||
|
runInstallForHooksWithFailure(t, podManifestWithOutputLogWithNamespace([]release.HookOutputLogPolicy{release.HookOutputOnSucceeded, release.HookOutputOnFailed}), "sneaky-namespace", true)
|
||||||
|
runInstallForHooksWithFailure(t, jobManifestWithOutputLog([]release.HookOutputLogPolicy{release.HookOutputOnSucceeded, release.HookOutputOnFailed}), "spaced", true)
|
||||||
|
runInstallForHooksWithFailure(t, jobManifestWithOutputLogWithNamespace([]release.HookOutputLogPolicy{release.HookOutputOnSucceeded, release.HookOutputOnFailed}), "rem-namespace", true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runInstallForHooksWithSuccess(t *testing.T, manifest, expectedNamespace string, shouldOutput bool) {
|
||||||
|
var expectedOutput string
|
||||||
|
if shouldOutput {
|
||||||
|
expectedOutput = fmt.Sprintf("attempted to output logs for namespace: %s", expectedNamespace)
|
||||||
|
}
|
||||||
|
is := assert.New(t)
|
||||||
|
instAction := installAction(t)
|
||||||
|
instAction.ReleaseName = "failed-hooks"
|
||||||
|
outBuffer := &bytes.Buffer{}
|
||||||
|
instAction.cfg.KubeClient = &kubefake.PrintingKubeClient{Out: io.Discard, LogOutput: outBuffer}
|
||||||
|
|
||||||
|
templates := []*chart.File{
|
||||||
|
{Name: "templates/hello", Data: []byte("hello: world")},
|
||||||
|
{Name: "templates/hooks", Data: []byte(manifest)},
|
||||||
|
}
|
||||||
|
vals := map[string]interface{}{}
|
||||||
|
|
||||||
|
res, err := instAction.Run(buildChartWithTemplates(templates), vals)
|
||||||
|
is.NoError(err)
|
||||||
|
is.Equal(expectedOutput, outBuffer.String())
|
||||||
|
is.Equal(release.StatusDeployed, res.Info.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runInstallForHooksWithFailure(t *testing.T, manifest, expectedNamespace string, shouldOutput bool) {
|
||||||
|
var expectedOutput string
|
||||||
|
if shouldOutput {
|
||||||
|
expectedOutput = fmt.Sprintf("attempted to output logs for namespace: %s", expectedNamespace)
|
||||||
|
}
|
||||||
|
is := assert.New(t)
|
||||||
|
instAction := installAction(t)
|
||||||
|
instAction.ReleaseName = "failed-hooks"
|
||||||
|
failingClient := instAction.cfg.KubeClient.(*kubefake.FailingKubeClient)
|
||||||
|
failingClient.WatchUntilReadyError = fmt.Errorf("failed watch")
|
||||||
|
instAction.cfg.KubeClient = failingClient
|
||||||
|
outBuffer := &bytes.Buffer{}
|
||||||
|
failingClient.PrintingKubeClient = kubefake.PrintingKubeClient{Out: io.Discard, LogOutput: outBuffer}
|
||||||
|
|
||||||
|
templates := []*chart.File{
|
||||||
|
{Name: "templates/hello", Data: []byte("hello: world")},
|
||||||
|
{Name: "templates/hooks", Data: []byte(manifest)},
|
||||||
|
}
|
||||||
|
vals := map[string]interface{}{}
|
||||||
|
|
||||||
|
res, err := instAction.Run(buildChartWithTemplates(templates), vals)
|
||||||
|
is.Error(err)
|
||||||
|
is.Contains(res.Info.Description, "failed pre-install")
|
||||||
|
is.Equal(expectedOutput, outBuffer.String())
|
||||||
|
is.Equal(release.StatusFailed, res.Info.Status)
|
||||||
|
}
|
Before Width: | Height: | Size: 374 B After Width: | Height: | Size: 374 B |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue