Skip to content

Commit

Permalink
Introduce helm-extra-set-args command line parameter (#402)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
ilmax authored Mar 23, 2022
1 parent e14c2b2 commit aabb51e
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 27 deletions.
9 changes: 8 additions & 1 deletion ct/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func addInstallFlags(flags *flag.FlagSet) {
flags.String("release-label", "app.kubernetes.io/instance", heredoc.Doc(`
The label to be used as a selector when inspecting resources created by charts.
This is only used if namespace is specified`))
flags.String("helm-extra-set-args", "", heredoc.Doc(`
Additional arguments for Helm. Must be passed as a single quoted string
(e.g. "--set=name=value"`))
}

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

testing, err := chart.NewTesting(*configuration)
extraSetArgs, err := cmd.Flags().GetString("helm-extra-set-args")
if err != nil {
return err
}
testing, err := chart.NewTesting(*configuration, extraSetArgs)
if err != nil {
fmt.Println(err)
}
Expand Down
3 changes: 2 additions & 1 deletion ct/cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func lint(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Error loading configuration: %s", err)
}

testing, err := chart.NewTesting(*configuration)
emptyExtraSetArgs := ""
testing, err := chart.NewTesting(*configuration, emptyExtraSetArgs)
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion ct/cmd/lintAndInstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ func lintAndInstall(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Error loading configuration: %s", err)
}

testing, err := chart.NewTesting(*configuration)
extraSetArgs, err := cmd.Flags().GetString("helm-extra-set-args")
if err != nil {
return err
}
testing, err := chart.NewTesting(*configuration, extraSetArgs)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion ct/cmd/listChanged.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func listChanged(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Error loading configuration: %s", err)
}

testing, err := chart.NewTesting(*configuration)
emptyExtraSetArgs := ""
testing, err := chart.NewTesting(*configuration, emptyExtraSetArgs)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion doc/ct.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ in given chart directories.
* [ct list-changed](ct_list-changed.md) - List changed charts
* [ct version](ct_version.md) - Print version information

###### Auto generated by spf13/cobra on 21-Apr-2020
###### Auto generated by spf13/cobra on 16-Mar-2022
4 changes: 3 additions & 1 deletion doc/ct_install.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ ct install [flags]
or separate values with commas
--helm-extra-args string Additional arguments for Helm. Must be passed as a single quoted string
(e.g. "--timeout 500s"
--helm-extra-set-args string Additional arguments for Helm. Must be passed as a single quoted string
(e.g. "--set=name=value"
--helm-repo-extra-args strings Additional arguments for the 'helm repo add' command to be
specified on a per-repo basis with an equals sign as delimiter
(e.g. 'myrepo=--username test --password secret'). May be specified
Expand All @@ -75,4 +77,4 @@ ct install [flags]

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

###### Auto generated by spf13/cobra on 28-Oct-2020
###### Auto generated by spf13/cobra on 16-Mar-2022
4 changes: 3 additions & 1 deletion doc/ct_lint-and-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ ct lint-and-install [flags]
or separate values with commas
--helm-extra-args string Additional arguments for Helm. Must be passed as a single quoted string
(e.g. "--timeout 500s"
--helm-extra-set-args string Additional arguments for Helm. Must be passed as a single quoted string
(e.g. "--set=name=value"
--helm-repo-extra-args strings Additional arguments for the 'helm repo add' command to be
specified on a per-repo basis with an equals sign as delimiter
(e.g. 'myrepo=--username test --password secret'). May be specified
Expand Down Expand Up @@ -74,4 +76,4 @@ ct lint-and-install [flags]

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

###### Auto generated by spf13/cobra on 28-Oct-2020
###### Auto generated by spf13/cobra on 16-Mar-2022
2 changes: 1 addition & 1 deletion doc/ct_lint.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ ct lint [flags]

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

###### Auto generated by spf13/cobra on 28-Oct-2020
###### Auto generated by spf13/cobra on 16-Mar-2022
2 changes: 1 addition & 1 deletion doc/ct_list-changed.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ ct list-changed [flags]

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

###### Auto generated by spf13/cobra on 28-Oct-2020
###### Auto generated by spf13/cobra on 16-Mar-2022
6 changes: 1 addition & 5 deletions doc/ct_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

Print version information

### Synopsis

Print version information

```
ct version [flags]
```
Expand All @@ -20,4 +16,4 @@ ct version [flags]

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

###### Auto generated by spf13/cobra on 21-Apr-2020
###### Auto generated by spf13/cobra on 16-Mar-2022
4 changes: 2 additions & 2 deletions pkg/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,13 @@ type TestResult struct {
}

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

testing := Testing{
config: config,
helm: tool.NewHelm(procExec, extraArgs),
helm: tool.NewHelm(procExec, extraArgs, strings.Fields(extraSetArgs)),
git: tool.NewGit(procExec),
kubectl: tool.NewKubectl(procExec),
linter: tool.NewLinter(procExec),
Expand Down
20 changes: 16 additions & 4 deletions pkg/chart/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/stretchr/testify/assert"
)

func newTestingHelmIntegration(cfg config.Configuration) Testing {
func newTestingHelmIntegration(cfg config.Configuration, extraSetArgs string) Testing {
fakeMockLinter := new(fakeLinter)
procExec := exec.NewProcessExecutor(true)
extraArgs := strings.Fields(cfg.HelmExtraArgs)
Expand All @@ -40,7 +40,7 @@ func newTestingHelmIntegration(cfg config.Configuration) Testing {
chartUtils: util.ChartUtils{},
accountValidator: fakeAccountValidator{},
linter: fakeMockLinter,
helm: tool.NewHelm(procExec, extraArgs),
helm: tool.NewHelm(procExec, extraArgs, strings.Fields(extraSetArgs)),
kubectl: tool.NewKubectl(procExec),
}
}
Expand All @@ -51,6 +51,7 @@ func TestInstallChart(t *testing.T) {
cfg config.Configuration
chartDir string
output TestResult
extraSet string
}

cases := []testCase{
Expand All @@ -63,6 +64,7 @@ func TestInstallChart(t *testing.T) {
},
"test_charts/must-pass-upgrade-install",
TestResult{mustNewChart("test_charts/must-pass-upgrade-install"), nil},
"",
},
{
"install only in random namespace",
Expand All @@ -71,12 +73,22 @@ func TestInstallChart(t *testing.T) {
},
"test_charts/must-pass-upgrade-install",
TestResult{mustNewChart("test_charts/must-pass-upgrade-install"), nil},
"",
},
{
"install with override set",
config.Configuration{
Debug: true,
},
"test_charts/must-pass-upgrade-install",
TestResult{mustNewChart("test_charts/must-pass-upgrade-install"), nil},
"--set=image.tag=latest",
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
ct := newTestingHelmIntegration(tc.cfg)
ct := newTestingHelmIntegration(tc.cfg, tc.extraSet)
namespace := tc.cfg.Namespace
if namespace != "" {
ct.kubectl.CreateNamespace(namespace)
Expand Down Expand Up @@ -107,7 +119,7 @@ func TestUpgradeChart(t *testing.T) {
Debug: true,
Upgrade: true,
}
ct := newTestingHelmIntegration(cfg)
ct := newTestingHelmIntegration(cfg, "")
processError := fmt.Errorf("Error waiting for process: exit status 1")

cases := []testCase{
Expand Down
16 changes: 9 additions & 7 deletions pkg/tool/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ import (
)

type Helm struct {
exec exec.ProcessExecutor
extraArgs []string
exec exec.ProcessExecutor
extraArgs []string
extraSetArgs []string
}

func NewHelm(exec exec.ProcessExecutor, extraArgs []string) Helm {
func NewHelm(exec exec.ProcessExecutor, extraArgs []string, extraSetArgs []string) Helm {
return Helm{
exec: exec,
extraArgs: extraArgs,
exec: exec,
extraArgs: extraArgs,
extraSetArgs: extraSetArgs,
}
}

Expand Down Expand Up @@ -56,7 +58,7 @@ func (h Helm) InstallWithValues(chart string, valuesFile string, namespace strin
}

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

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

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

Expand Down

0 comments on commit aabb51e

Please sign in to comment.