You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-11-06 09:09:19 +02:00
helmExecute: Grab values from environment variables (#4374)
* Grab values from environment variables * use os.ExpandEnv in RunHelmUpgrade function * use os.ExpandEnv in RunHelmUpgrade function * use os.ExpandEnv in RunHelmUpgrade function * Extract new logic to separate func && update tests --------- Co-authored-by: Mao <bruce.mao@sap.com> Co-authored-by: Vyacheslav Starostin <32613074+vstarostin@users.noreply.github.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
@@ -174,7 +175,7 @@ func (h *HelmExecute) RunHelmUpgrade() error {
|
||||
}
|
||||
|
||||
if len(h.config.AdditionalParameters) > 0 {
|
||||
helmParams = append(helmParams, h.config.AdditionalParameters...)
|
||||
helmParams = append(helmParams, expandEnv(h.config.AdditionalParameters)...)
|
||||
}
|
||||
|
||||
if err := h.runHelmCommand(helmParams); err != nil {
|
||||
@@ -250,7 +251,7 @@ func (h *HelmExecute) RunHelmInstall() error {
|
||||
}
|
||||
|
||||
if len(h.config.AdditionalParameters) > 0 {
|
||||
helmParams = append(helmParams, h.config.AdditionalParameters...)
|
||||
helmParams = append(helmParams, expandEnv(h.config.AdditionalParameters)...)
|
||||
}
|
||||
|
||||
if h.verbose {
|
||||
@@ -475,3 +476,10 @@ func (h *HelmExecute) runHelmCommand(helmParams []string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// expandEnv replaces ${var} or $var in params according to the values of the current environment variables
|
||||
func expandEnv(params []string) []string {
|
||||
paramsRaw := strings.Join(params, " ")
|
||||
|
||||
return strings.Split(os.ExpandEnv(paramsRaw), " ")
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ package kubernetes
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/SAP/jenkins-library/pkg/log"
|
||||
@@ -115,6 +116,8 @@ func TestRunHelmAdd(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRunHelmUpgrade(t *testing.T) {
|
||||
os.Setenv("IMAGE", "image")
|
||||
|
||||
testTable := []struct {
|
||||
config HelmExecuteOptions
|
||||
generalVerbose bool
|
||||
@@ -127,7 +130,7 @@ func TestRunHelmUpgrade(t *testing.T) {
|
||||
Namespace: "test_namespace",
|
||||
ForceUpdates: true,
|
||||
HelmDeployWaitSeconds: 3456,
|
||||
AdditionalParameters: []string{"additional parameter"},
|
||||
AdditionalParameters: []string{"additional", "parameters"},
|
||||
Image: "dtzar/helm-kubectl:3.4.1",
|
||||
TargetRepositoryName: "test",
|
||||
TargetRepositoryURL: "https://charts.helm.sh/stable",
|
||||
@@ -136,7 +139,7 @@ func TestRunHelmUpgrade(t *testing.T) {
|
||||
generalVerbose: true,
|
||||
expectedExecCalls: []mock.ExecCall{
|
||||
{Exec: "helm", Params: []string{"repo", "add", "test", "https://charts.helm.sh/stable", "--debug"}},
|
||||
{Exec: "helm", Params: []string{"upgrade", "test_deployment", "test", "--debug", "--install", "--namespace", "test_namespace", "--force", "--wait", "--timeout", "3456s", "--atomic", "--render-subchart-notes", "additional parameter"}},
|
||||
{Exec: "helm", Params: []string{"upgrade", "test_deployment", "test", "--debug", "--install", "--namespace", "test_namespace", "--force", "--wait", "--timeout", "3456s", "--atomic", "--render-subchart-notes", "additional", "parameters"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -146,14 +149,31 @@ func TestRunHelmUpgrade(t *testing.T) {
|
||||
Namespace: "test_namespace",
|
||||
ForceUpdates: true,
|
||||
HelmDeployWaitSeconds: 3456,
|
||||
AdditionalParameters: []string{"additional parameter"},
|
||||
AdditionalParameters: []string{"additional", "parameters"},
|
||||
Image: "dtzar/helm-kubectl:3.4.1",
|
||||
TargetRepositoryName: "test",
|
||||
TargetRepositoryURL: "https://charts.helm.sh/stable",
|
||||
},
|
||||
generalVerbose: true,
|
||||
expectedExecCalls: []mock.ExecCall{
|
||||
{Exec: "helm", Params: []string{"upgrade", "test_deployment", ".", "--debug", "--install", "--namespace", "test_namespace", "--force", "--wait", "--timeout", "3456s", "--atomic", "additional parameter"}},
|
||||
{Exec: "helm", Params: []string{"upgrade", "test_deployment", ".", "--debug", "--install", "--namespace", "test_namespace", "--force", "--wait", "--timeout", "3456s", "--atomic", "additional", "parameters"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
config: HelmExecuteOptions{
|
||||
DeploymentName: "test_deployment",
|
||||
ChartPath: ".",
|
||||
Namespace: "test_namespace",
|
||||
ForceUpdates: true,
|
||||
HelmDeployWaitSeconds: 3456,
|
||||
AdditionalParameters: []string{"--set", "image.repository=$IMAGE"},
|
||||
Image: "dtzar/helm-kubectl:3.4.1",
|
||||
TargetRepositoryName: "test",
|
||||
TargetRepositoryURL: "https://charts.helm.sh/stable",
|
||||
},
|
||||
generalVerbose: true,
|
||||
expectedExecCalls: []mock.ExecCall{
|
||||
{Exec: "helm", Params: []string{"upgrade", "test_deployment", ".", "--debug", "--install", "--namespace", "test_namespace", "--force", "--wait", "--timeout", "3456s", "--atomic", "--set", "image.repository=image"}},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -219,6 +239,8 @@ func TestRunHelmLint(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRunHelmInstall(t *testing.T) {
|
||||
os.Setenv("MY_SCRIPT", "dothings.sh")
|
||||
|
||||
testTable := []struct {
|
||||
config HelmExecuteOptions
|
||||
generalVerbose bool
|
||||
@@ -261,14 +283,31 @@ func TestRunHelmInstall(t *testing.T) {
|
||||
Namespace: "test-namespace",
|
||||
HelmDeployWaitSeconds: 525,
|
||||
KeepFailedDeployments: false,
|
||||
AdditionalParameters: []string{"--set-file my_script=dothings.sh"},
|
||||
AdditionalParameters: []string{"--set-file", "my_script=dothings.sh"},
|
||||
TargetRepositoryURL: "https://charts.helm.sh/stable",
|
||||
TargetRepositoryName: "test",
|
||||
},
|
||||
generalVerbose: true,
|
||||
expectedExecCalls: []mock.ExecCall{
|
||||
{Exec: "helm", Params: []string{"install", "testPackage", ".", "--namespace", "test-namespace", "--create-namespace", "--atomic", "--wait", "--timeout", "525s", "--set-file my_script=dothings.sh", "--debug", "--dry-run"}},
|
||||
{Exec: "helm", Params: []string{"install", "testPackage", ".", "--namespace", "test-namespace", "--create-namespace", "--atomic", "--wait", "--timeout", "525s", "--set-file my_script=dothings.sh", "--debug"}},
|
||||
{Exec: "helm", Params: []string{"install", "testPackage", ".", "--namespace", "test-namespace", "--create-namespace", "--atomic", "--wait", "--timeout", "525s", "--set-file", "my_script=dothings.sh", "--debug", "--dry-run"}},
|
||||
{Exec: "helm", Params: []string{"install", "testPackage", ".", "--namespace", "test-namespace", "--create-namespace", "--atomic", "--wait", "--timeout", "525s", "--set-file", "my_script=dothings.sh", "--debug"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
config: HelmExecuteOptions{
|
||||
ChartPath: ".",
|
||||
DeploymentName: "testPackage",
|
||||
Namespace: "test-namespace",
|
||||
HelmDeployWaitSeconds: 525,
|
||||
KeepFailedDeployments: false,
|
||||
AdditionalParameters: []string{"--set-file", "my_script=$MY_SCRIPT"},
|
||||
TargetRepositoryURL: "https://charts.helm.sh/stable",
|
||||
TargetRepositoryName: "test",
|
||||
},
|
||||
generalVerbose: true,
|
||||
expectedExecCalls: []mock.ExecCall{
|
||||
{Exec: "helm", Params: []string{"install", "testPackage", ".", "--namespace", "test-namespace", "--create-namespace", "--atomic", "--wait", "--timeout", "525s", "--set-file", "my_script=dothings.sh", "--debug", "--dry-run"}},
|
||||
{Exec: "helm", Params: []string{"install", "testPackage", ".", "--namespace", "test-namespace", "--create-namespace", "--atomic", "--wait", "--timeout", "525s", "--set-file", "my_script=dothings.sh", "--debug"}},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user