Skip to content

Commit aabb51e

Browse files
authored
Introduce helm-extra-set-args command line parameter (#402)
* Introduce helm-extra-set-args Signed-off-by: ilmax <[email protected]> Signed-off-by: Massimiliano Donini <[email protected]> * Fix and add integration test - Fixed error when no values are passed in - Added integration test Signed-off-by: Massimiliano Donini <[email protected]> * Generate docs Signed-off-by: Massimiliano Donini <[email protected]>
1 parent e14c2b2 commit aabb51e

13 files changed

+54
-27
lines changed

ct/cmd/install.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ func addInstallFlags(flags *flag.FlagSet) {
7777
flags.String("release-label", "app.kubernetes.io/instance", heredoc.Doc(`
7878
The label to be used as a selector when inspecting resources created by charts.
7979
This is only used if namespace is specified`))
80+
flags.String("helm-extra-set-args", "", heredoc.Doc(`
81+
Additional arguments for Helm. Must be passed as a single quoted string
82+
(e.g. "--set=name=value"`))
8083
}
8184

8285
func install(cmd *cobra.Command, args []string) error {
@@ -91,7 +94,11 @@ func install(cmd *cobra.Command, args []string) error {
9194
return fmt.Errorf("Error loading configuration: %s", err)
9295
}
9396

94-
testing, err := chart.NewTesting(*configuration)
97+
extraSetArgs, err := cmd.Flags().GetString("helm-extra-set-args")
98+
if err != nil {
99+
return err
100+
}
101+
testing, err := chart.NewTesting(*configuration, extraSetArgs)
95102
if err != nil {
96103
fmt.Println(err)
97104
}

ct/cmd/lint.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ func lint(cmd *cobra.Command, args []string) error {
8888
return fmt.Errorf("Error loading configuration: %s", err)
8989
}
9090

91-
testing, err := chart.NewTesting(*configuration)
91+
emptyExtraSetArgs := ""
92+
testing, err := chart.NewTesting(*configuration, emptyExtraSetArgs)
9293
if err != nil {
9394
return err
9495
}

ct/cmd/lintAndInstall.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ func lintAndInstall(cmd *cobra.Command, args []string) error {
5151
return fmt.Errorf("Error loading configuration: %s", err)
5252
}
5353

54-
testing, err := chart.NewTesting(*configuration)
54+
extraSetArgs, err := cmd.Flags().GetString("helm-extra-set-args")
55+
if err != nil {
56+
return err
57+
}
58+
testing, err := chart.NewTesting(*configuration, extraSetArgs)
5559
if err != nil {
5660
return err
5761
}

ct/cmd/listChanged.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ func listChanged(cmd *cobra.Command, args []string) error {
5050
return fmt.Errorf("Error loading configuration: %s", err)
5151
}
5252

53-
testing, err := chart.NewTesting(*configuration)
53+
emptyExtraSetArgs := ""
54+
testing, err := chart.NewTesting(*configuration, emptyExtraSetArgs)
5455
if err != nil {
5556
return err
5657
}

doc/ct.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ in given chart directories.
2626
* [ct list-changed](ct_list-changed.md) - List changed charts
2727
* [ct version](ct_version.md) - Print version information
2828

29-
###### Auto generated by spf13/cobra on 21-Apr-2020
29+
###### Auto generated by spf13/cobra on 16-Mar-2022

doc/ct_install.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ ct install [flags]
5050
or separate values with commas
5151
--helm-extra-args string Additional arguments for Helm. Must be passed as a single quoted string
5252
(e.g. "--timeout 500s"
53+
--helm-extra-set-args string Additional arguments for Helm. Must be passed as a single quoted string
54+
(e.g. "--set=name=value"
5355
--helm-repo-extra-args strings Additional arguments for the 'helm repo add' command to be
5456
specified on a per-repo basis with an equals sign as delimiter
5557
(e.g. 'myrepo=--username test --password secret'). May be specified
@@ -75,4 +77,4 @@ ct install [flags]
7577

7678
* [ct](ct.md) - The Helm chart testing tool
7779

78-
###### Auto generated by spf13/cobra on 28-Oct-2020
80+
###### Auto generated by spf13/cobra on 16-Mar-2022

doc/ct_lint-and-install.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ ct lint-and-install [flags]
4242
or separate values with commas
4343
--helm-extra-args string Additional arguments for Helm. Must be passed as a single quoted string
4444
(e.g. "--timeout 500s"
45+
--helm-extra-set-args string Additional arguments for Helm. Must be passed as a single quoted string
46+
(e.g. "--set=name=value"
4547
--helm-repo-extra-args strings Additional arguments for the 'helm repo add' command to be
4648
specified on a per-repo basis with an equals sign as delimiter
4749
(e.g. 'myrepo=--username test --password secret'). May be specified
@@ -74,4 +76,4 @@ ct lint-and-install [flags]
7476

7577
* [ct](ct.md) - The Helm chart testing tool
7678

77-
###### Auto generated by spf13/cobra on 28-Oct-2020
79+
###### Auto generated by spf13/cobra on 16-Mar-2022

doc/ct_lint.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,4 @@ ct lint [flags]
7373

7474
* [ct](ct.md) - The Helm chart testing tool
7575

76-
###### Auto generated by spf13/cobra on 28-Oct-2020
76+
###### Auto generated by spf13/cobra on 16-Mar-2022

doc/ct_list-changed.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ ct list-changed [flags]
3232

3333
* [ct](ct.md) - The Helm chart testing tool
3434

35-
###### Auto generated by spf13/cobra on 28-Oct-2020
35+
###### Auto generated by spf13/cobra on 16-Mar-2022

doc/ct_version.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
Print version information
44

5-
### Synopsis
6-
7-
Print version information
8-
95
```
106
ct version [flags]
117
```
@@ -20,4 +16,4 @@ ct version [flags]
2016

2117
* [ct](ct.md) - The Helm chart testing tool
2218

23-
###### Auto generated by spf13/cobra on 21-Apr-2020
19+
###### Auto generated by spf13/cobra on 16-Mar-2022

pkg/chart/chart.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,13 @@ type TestResult struct {
252252
}
253253

254254
// NewTesting creates a new Testing struct with the given config.
255-
func NewTesting(config config.Configuration) (Testing, error) {
255+
func NewTesting(config config.Configuration, extraSetArgs string) (Testing, error) {
256256
procExec := exec.NewProcessExecutor(config.Debug)
257257
extraArgs := strings.Fields(config.HelmExtraArgs)
258258

259259
testing := Testing{
260260
config: config,
261-
helm: tool.NewHelm(procExec, extraArgs),
261+
helm: tool.NewHelm(procExec, extraArgs, strings.Fields(extraSetArgs)),
262262
git: tool.NewGit(procExec),
263263
kubectl: tool.NewKubectl(procExec),
264264
linter: tool.NewLinter(procExec),

pkg/chart/integration_test.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"github.com/stretchr/testify/assert"
3030
)
3131

32-
func newTestingHelmIntegration(cfg config.Configuration) Testing {
32+
func newTestingHelmIntegration(cfg config.Configuration, extraSetArgs string) Testing {
3333
fakeMockLinter := new(fakeLinter)
3434
procExec := exec.NewProcessExecutor(true)
3535
extraArgs := strings.Fields(cfg.HelmExtraArgs)
@@ -40,7 +40,7 @@ func newTestingHelmIntegration(cfg config.Configuration) Testing {
4040
chartUtils: util.ChartUtils{},
4141
accountValidator: fakeAccountValidator{},
4242
linter: fakeMockLinter,
43-
helm: tool.NewHelm(procExec, extraArgs),
43+
helm: tool.NewHelm(procExec, extraArgs, strings.Fields(extraSetArgs)),
4444
kubectl: tool.NewKubectl(procExec),
4545
}
4646
}
@@ -51,6 +51,7 @@ func TestInstallChart(t *testing.T) {
5151
cfg config.Configuration
5252
chartDir string
5353
output TestResult
54+
extraSet string
5455
}
5556

5657
cases := []testCase{
@@ -63,6 +64,7 @@ func TestInstallChart(t *testing.T) {
6364
},
6465
"test_charts/must-pass-upgrade-install",
6566
TestResult{mustNewChart("test_charts/must-pass-upgrade-install"), nil},
67+
"",
6668
},
6769
{
6870
"install only in random namespace",
@@ -71,12 +73,22 @@ func TestInstallChart(t *testing.T) {
7173
},
7274
"test_charts/must-pass-upgrade-install",
7375
TestResult{mustNewChart("test_charts/must-pass-upgrade-install"), nil},
76+
"",
77+
},
78+
{
79+
"install with override set",
80+
config.Configuration{
81+
Debug: true,
82+
},
83+
"test_charts/must-pass-upgrade-install",
84+
TestResult{mustNewChart("test_charts/must-pass-upgrade-install"), nil},
85+
"--set=image.tag=latest",
7486
},
7587
}
7688

7789
for _, tc := range cases {
7890
t.Run(tc.name, func(t *testing.T) {
79-
ct := newTestingHelmIntegration(tc.cfg)
91+
ct := newTestingHelmIntegration(tc.cfg, tc.extraSet)
8092
namespace := tc.cfg.Namespace
8193
if namespace != "" {
8294
ct.kubectl.CreateNamespace(namespace)
@@ -107,7 +119,7 @@ func TestUpgradeChart(t *testing.T) {
107119
Debug: true,
108120
Upgrade: true,
109121
}
110-
ct := newTestingHelmIntegration(cfg)
122+
ct := newTestingHelmIntegration(cfg, "")
111123
processError := fmt.Errorf("Error waiting for process: exit status 1")
112124

113125
cases := []testCase{

pkg/tool/helm.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ import (
2121
)
2222

2323
type Helm struct {
24-
exec exec.ProcessExecutor
25-
extraArgs []string
24+
exec exec.ProcessExecutor
25+
extraArgs []string
26+
extraSetArgs []string
2627
}
2728

28-
func NewHelm(exec exec.ProcessExecutor, extraArgs []string) Helm {
29+
func NewHelm(exec exec.ProcessExecutor, extraArgs []string, extraSetArgs []string) Helm {
2930
return Helm{
30-
exec: exec,
31-
extraArgs: extraArgs,
31+
exec: exec,
32+
extraArgs: extraArgs,
33+
extraSetArgs: extraSetArgs,
3234
}
3335
}
3436

@@ -56,7 +58,7 @@ func (h Helm) InstallWithValues(chart string, valuesFile string, namespace strin
5658
}
5759

5860
if err := h.exec.RunProcess("helm", "install", release, chart, "--namespace", namespace,
59-
"--wait", values, h.extraArgs); err != nil {
61+
"--wait", values, h.extraArgs, h.extraSetArgs); err != nil {
6062
return err
6163
}
6264

@@ -65,7 +67,7 @@ func (h Helm) InstallWithValues(chart string, valuesFile string, namespace strin
6567

6668
func (h Helm) Upgrade(chart string, namespace string, release string) error {
6769
if err := h.exec.RunProcess("helm", "upgrade", release, chart, "--namespace", namespace,
68-
"--reuse-values", "--wait", h.extraArgs); err != nil {
70+
"--reuse-values", "--wait", h.extraArgs, h.extraSetArgs); err != nil {
6971
return err
7072
}
7173

0 commit comments

Comments
 (0)