mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-01-18 05:18:24 +02:00
helmExecute: opt out from template parsing (#4511)
Add option to opt out from helm template parsing Co-authored-by: Linda Siebert <linda.siebert@sap.com> Co-authored-by: Alexander Link <33052602+alxsap@users.noreply.github.com>
This commit is contained in:
parent
b58bb87114
commit
e80adc5ab9
@ -2,6 +2,7 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
@ -70,20 +71,21 @@ func helmExecute(config helmExecuteOptions, telemetryData *telemetry.CustomData,
|
||||
helmConfig.PublishVersion = artifactInfo.Version
|
||||
}
|
||||
|
||||
err = parseAndRenderCPETemplate(config, GeneralConfig.EnvRootPath, utils)
|
||||
if err != nil {
|
||||
log.Entry().WithError(err).Fatalf("failed to parse/render template: %v", err)
|
||||
}
|
||||
|
||||
helmExecutor := kubernetes.NewHelmExecutor(helmConfig, utils, GeneralConfig.Verbose, log.Writer())
|
||||
|
||||
// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
|
||||
if err := runHelmExecute(config, helmExecutor, commonPipelineEnvironment); err != nil {
|
||||
if err := runHelmExecute(config, helmExecutor, utils, commonPipelineEnvironment); err != nil {
|
||||
log.Entry().WithError(err).Fatalf("step execution failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func runHelmExecute(config helmExecuteOptions, helmExecutor kubernetes.HelmExecutor, commonPipelineEnvironment *helmExecuteCommonPipelineEnvironment) error {
|
||||
func runHelmExecute(config helmExecuteOptions, helmExecutor kubernetes.HelmExecutor, utils fileHandler, commonPipelineEnvironment *helmExecuteCommonPipelineEnvironment) error {
|
||||
if config.RenderValuesTemplate {
|
||||
err := parseAndRenderCPETemplate(config, GeneralConfig.EnvRootPath, utils)
|
||||
if err != nil {
|
||||
log.Entry().WithError(err).Fatalf("failed to parse/render template: %v", err)
|
||||
}
|
||||
}
|
||||
switch config.HelmCommand {
|
||||
case "upgrade":
|
||||
if err := helmExecutor.RunHelmUpgrade(); err != nil {
|
||||
@ -147,7 +149,7 @@ func runHelmExecuteDefault(config helmExecuteOptions, helmExecutor kubernetes.He
|
||||
}
|
||||
|
||||
// parseAndRenderCPETemplate allows to parse and render a template which contains references to the CPE
|
||||
func parseAndRenderCPETemplate(config helmExecuteOptions, rootPath string, utils kubernetes.DeployUtils) error {
|
||||
func parseAndRenderCPETemplate(config helmExecuteOptions, rootPath string, utils fileHandler) error {
|
||||
cpe := piperenv.CPEMap{}
|
||||
err := cpe.LoadFromDisk(path.Join(rootPath, "commonPipelineEnvironment"))
|
||||
if err != nil {
|
||||
@ -187,3 +189,9 @@ func parseAndRenderCPETemplate(config helmExecuteOptions, rootPath string, utils
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type fileHandler interface {
|
||||
FileExists(string) (bool, error)
|
||||
FileRead(string) ([]byte, error)
|
||||
FileWrite(string, []byte, os.FileMode) error
|
||||
}
|
||||
|
@ -48,6 +48,7 @@ type helmExecuteOptions struct {
|
||||
RenderSubchartNotes bool `json:"renderSubchartNotes,omitempty"`
|
||||
TemplateStartDelimiter string `json:"templateStartDelimiter,omitempty"`
|
||||
TemplateEndDelimiter string `json:"templateEndDelimiter,omitempty"`
|
||||
RenderValuesTemplate bool `json:"renderValuesTemplate,omitempty"`
|
||||
}
|
||||
|
||||
type helmExecuteCommonPipelineEnvironment struct {
|
||||
@ -236,6 +237,7 @@ func addHelmExecuteFlags(cmd *cobra.Command, stepConfig *helmExecuteOptions) {
|
||||
cmd.Flags().BoolVar(&stepConfig.RenderSubchartNotes, "renderSubchartNotes", true, "If set, render subchart notes along with the parent.")
|
||||
cmd.Flags().StringVar(&stepConfig.TemplateStartDelimiter, "templateStartDelimiter", `{{`, "When templating value files, use this start delimiter.")
|
||||
cmd.Flags().StringVar(&stepConfig.TemplateEndDelimiter, "templateEndDelimiter", `}}`, "When templating value files, use this end delimiter.")
|
||||
cmd.Flags().BoolVar(&stepConfig.RenderValuesTemplate, "renderValuesTemplate", true, "A flag to turn templating value files on or off.")
|
||||
|
||||
cmd.MarkFlagRequired("image")
|
||||
}
|
||||
@ -634,6 +636,15 @@ func helmExecuteMetadata() config.StepData {
|
||||
Aliases: []config.Alias{},
|
||||
Default: `}}`,
|
||||
},
|
||||
{
|
||||
Name: "renderValuesTemplate",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"STEPS", "PARAMETERS"},
|
||||
Type: "bool",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
Containers: []config.Container{
|
||||
|
@ -63,7 +63,7 @@ func TestRunHelmUpgrade(t *testing.T) {
|
||||
helmExecute := &mocks.HelmExecutor{}
|
||||
helmExecute.On("RunHelmUpgrade").Return(testCase.methodError)
|
||||
|
||||
err := runHelmExecute(testCase.config, helmExecute, &cpe)
|
||||
err := runHelmExecute(testCase.config, helmExecute, &fileHandlerMock{}, &cpe)
|
||||
if err != nil {
|
||||
assert.Equal(t, testCase.expectedErrStr, err.Error())
|
||||
}
|
||||
@ -102,7 +102,7 @@ func TestRunHelmLint(t *testing.T) {
|
||||
helmExecute := &mocks.HelmExecutor{}
|
||||
helmExecute.On("RunHelmLint").Return(testCase.methodError)
|
||||
|
||||
err := runHelmExecute(testCase.config, helmExecute, &cpe)
|
||||
err := runHelmExecute(testCase.config, helmExecute, &fileHandlerMock{}, &cpe)
|
||||
if err != nil {
|
||||
assert.Equal(t, testCase.expectedErrStr, err.Error())
|
||||
}
|
||||
@ -141,7 +141,7 @@ func TestRunHelmInstall(t *testing.T) {
|
||||
helmExecute := &mocks.HelmExecutor{}
|
||||
helmExecute.On("RunHelmInstall").Return(testCase.methodError)
|
||||
|
||||
err := runHelmExecute(testCase.config, helmExecute, &cpe)
|
||||
err := runHelmExecute(testCase.config, helmExecute, &fileHandlerMock{}, &cpe)
|
||||
if err != nil {
|
||||
assert.Equal(t, testCase.expectedErrStr, err.Error())
|
||||
}
|
||||
@ -179,7 +179,7 @@ func TestRunHelmTest(t *testing.T) {
|
||||
helmExecute := &mocks.HelmExecutor{}
|
||||
helmExecute.On("RunHelmTest").Return(testCase.methodError)
|
||||
|
||||
err := runHelmExecute(testCase.config, helmExecute, &cpe)
|
||||
err := runHelmExecute(testCase.config, helmExecute, &fileHandlerMock{}, &cpe)
|
||||
if err != nil {
|
||||
assert.Equal(t, testCase.expectedErrStr, err.Error())
|
||||
}
|
||||
@ -217,7 +217,7 @@ func TestRunHelmUninstall(t *testing.T) {
|
||||
helmExecute := &mocks.HelmExecutor{}
|
||||
helmExecute.On("RunHelmUninstall").Return(testCase.methodError)
|
||||
|
||||
err := runHelmExecute(testCase.config, helmExecute, &cpe)
|
||||
err := runHelmExecute(testCase.config, helmExecute, &fileHandlerMock{}, &cpe)
|
||||
if err != nil {
|
||||
assert.Equal(t, testCase.expectedErrStr, err.Error())
|
||||
}
|
||||
@ -255,7 +255,7 @@ func TestRunHelmDependency(t *testing.T) {
|
||||
helmExecute := &mocks.HelmExecutor{}
|
||||
helmExecute.On("RunHelmDependency").Return(testCase.methodError)
|
||||
|
||||
err := runHelmExecute(testCase.config, helmExecute, &cpe)
|
||||
err := runHelmExecute(testCase.config, helmExecute, &fileHandlerMock{}, &cpe)
|
||||
if err != nil {
|
||||
assert.Equal(t, testCase.expectedErrStr, err.Error())
|
||||
}
|
||||
@ -295,7 +295,7 @@ func TestRunHelmPush(t *testing.T) {
|
||||
helmExecute := &mocks.HelmExecutor{}
|
||||
helmExecute.On("RunHelmPublish").Return(testCase.methodString, testCase.methodError)
|
||||
|
||||
err := runHelmExecute(testCase.config, helmExecute, &cpe)
|
||||
err := runHelmExecute(testCase.config, helmExecute, &fileHandlerMock{}, &cpe)
|
||||
if err != nil {
|
||||
assert.Equal(t, testCase.expectedErrStr, err.Error())
|
||||
}
|
||||
@ -314,6 +314,8 @@ func TestRunHelmDefaultCommand(t *testing.T) {
|
||||
methodPackageError error
|
||||
methodPublishError error
|
||||
expectedErrStr string
|
||||
fileUtils fileHandlerMock
|
||||
assertFunc func(fileHandlerMock) error
|
||||
}{
|
||||
{
|
||||
config: helmExecuteOptions{
|
||||
@ -322,6 +324,45 @@ func TestRunHelmDefaultCommand(t *testing.T) {
|
||||
methodLintError: nil,
|
||||
methodPackageError: nil,
|
||||
methodPublishError: nil,
|
||||
fileUtils: fileHandlerMock{},
|
||||
},
|
||||
{
|
||||
// this test checks if parseAndRenderCPETemplate is called properly
|
||||
// when config.RenderValuesTemplate is true
|
||||
config: helmExecuteOptions{
|
||||
HelmCommand: "",
|
||||
RenderValuesTemplate: true,
|
||||
},
|
||||
methodLintError: nil,
|
||||
methodPackageError: nil,
|
||||
methodPublishError: nil,
|
||||
fileUtils: fileHandlerMock{},
|
||||
// we expect the values file is traversed since parsing and rendering according to cpe template is active
|
||||
assertFunc: func(f fileHandlerMock) error {
|
||||
if len(f.fileExistsCalled) == 1 && f.fileExistsCalled[0] == "/values.yaml" {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("expected FileExists called for ['/values.yaml'] but was: %+v", f.fileExistsCalled)
|
||||
},
|
||||
},
|
||||
{
|
||||
// this test checks if parseAndRenderCPETemplate is NOT called
|
||||
// when config.RenderValuesTemplate is false
|
||||
config: helmExecuteOptions{
|
||||
HelmCommand: "",
|
||||
RenderValuesTemplate: false,
|
||||
},
|
||||
methodLintError: nil,
|
||||
methodPackageError: nil,
|
||||
methodPublishError: nil,
|
||||
fileUtils: fileHandlerMock{},
|
||||
// we expect the values file is not traversed since parsing and rendering according to cpe template is not active
|
||||
assertFunc: func(f fileHandlerMock) error {
|
||||
if len(f.fileExistsCalled) > 0 {
|
||||
return fmt.Errorf("expected FileExists not called, but was for: %+v", f.fileExistsCalled)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
config: helmExecuteOptions{
|
||||
@ -329,6 +370,7 @@ func TestRunHelmDefaultCommand(t *testing.T) {
|
||||
},
|
||||
methodLintError: errors.New("some error"),
|
||||
expectedErrStr: "failed to execute helm lint: some error",
|
||||
fileUtils: fileHandlerMock{},
|
||||
},
|
||||
{
|
||||
config: helmExecuteOptions{
|
||||
@ -336,6 +378,7 @@ func TestRunHelmDefaultCommand(t *testing.T) {
|
||||
},
|
||||
methodPackageError: errors.New("some error"),
|
||||
expectedErrStr: "failed to execute helm dependency: some error",
|
||||
fileUtils: fileHandlerMock{},
|
||||
},
|
||||
{
|
||||
config: helmExecuteOptions{
|
||||
@ -343,6 +386,7 @@ func TestRunHelmDefaultCommand(t *testing.T) {
|
||||
},
|
||||
methodPublishError: errors.New("some error"),
|
||||
expectedErrStr: "failed to execute helm publish: some error",
|
||||
fileUtils: fileHandlerMock{},
|
||||
},
|
||||
}
|
||||
|
||||
@ -353,10 +397,13 @@ func TestRunHelmDefaultCommand(t *testing.T) {
|
||||
helmExecute.On("RunHelmDependency").Return(testCase.methodPackageError)
|
||||
helmExecute.On("RunHelmPublish").Return(testCase.methodPublishError)
|
||||
|
||||
err := runHelmExecute(testCase.config, helmExecute, &cpe)
|
||||
err := runHelmExecute(testCase.config, helmExecute, &testCase.fileUtils, &cpe)
|
||||
if err != nil {
|
||||
assert.Equal(t, testCase.expectedErrStr, err.Error())
|
||||
}
|
||||
if testCase.assertFunc != nil {
|
||||
assert.NoError(t, testCase.assertFunc(testCase.fileUtils))
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
@ -485,3 +532,24 @@ tag: {{ imageTag "test-image" }}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type fileHandlerMock struct {
|
||||
fileExistsCalled []string
|
||||
fileReadCalled []string
|
||||
fileWriteCalled []string
|
||||
}
|
||||
|
||||
func (f *fileHandlerMock) FileWrite(name string, content []byte, mode os.FileMode) error {
|
||||
f.fileWriteCalled = append(f.fileWriteCalled, name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fileHandlerMock) FileRead(name string) ([]byte, error) {
|
||||
f.fileReadCalled = append(f.fileReadCalled, name)
|
||||
return []byte{}, nil
|
||||
}
|
||||
|
||||
func (f *fileHandlerMock) FileExists(name string) (bool, error) {
|
||||
f.fileExistsCalled = append(f.fileExistsCalled, name)
|
||||
return true, nil
|
||||
}
|
||||
|
@ -360,6 +360,13 @@ spec:
|
||||
scope:
|
||||
- STEPS
|
||||
- PARAMETERS
|
||||
- name: renderValuesTemplate
|
||||
type: bool
|
||||
description: A flag to turn templating value files on or off.
|
||||
default: true
|
||||
scope:
|
||||
- STEPS
|
||||
- PARAMETERS
|
||||
containers:
|
||||
- image: dtzar/helm-kubectl:3
|
||||
workingDir: /config
|
||||
|
Loading…
x
Reference in New Issue
Block a user