Skip to content

Commit

Permalink
introduce helm-dependency-extra-args
Browse files Browse the repository at this point in the history
addresses #368

Signed-off-by: Krzysztof Nazarewski <[email protected]>
  • Loading branch information
nazarewk committed Mar 14, 2022
1 parent 8b974b2 commit b71548c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 37 deletions.
2 changes: 2 additions & 0 deletions ct/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func addCommonLintAndInstallFlags(flags *pflag.FlagSet) {
specified on a per-repo basis with an equals sign as delimiter
(e.g. 'myrepo=--username test --password secret'). May be specified
multiple times or separate values with commas`))
flags.StringSlice("helm-dependency-extra-args", []string{}, heredoc.Doc(`
Additional arguments for 'helm dependency build' (e.g. ["--skip-refresh"]`))
flags.Bool("debug", false, heredoc.Doc(`
Print CLI calls of external tools to stdout (caution: setting this may
expose sensitive data when helm-repo-extra-args contains passwords)`))
Expand Down
7 changes: 5 additions & 2 deletions pkg/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type Git interface {
//
// BuildDependencies builds the chart's dependencies
//
// BuildDependenciesWithArgs allows passing additional arguments to BuildDependencies
//
// LintWithValues runs `helm lint` for the given chart using the specified values file.
// Pass a zero value for valuesFile in order to run lint without specifying a values file.
//
Expand All @@ -82,6 +84,7 @@ type Git interface {
type Helm interface {
AddRepo(name string, url string, extraArgs []string) error
BuildDependencies(chart string) error
BuildDependenciesWithArgs(chart string, extraArgs []string) error
LintWithValues(chart string, valuesFile string) error
InstallWithValues(chart string, valuesFile string, namespace string, release string) error
Upgrade(chart string, namespace string, release string) error
Expand Down Expand Up @@ -367,15 +370,15 @@ func (t *Testing) processCharts(action func(chart *Chart) TestResult) ([]TestRes
defer t.git.RemoveWorktree(worktreePath)

for _, chart := range charts {
if err := t.helm.BuildDependencies(t.computePreviousRevisionPath(chart.Path())); err != nil {
if err := t.helm.BuildDependenciesWithArgs(t.computePreviousRevisionPath(chart.Path()), t.config.HelmDependencyExtraArgs); err != nil {
// Only print error (don't exit) if building dependencies for previous revision fails.
fmt.Println(errors.Wrapf(err, "Error building dependencies for previous revision of chart '%s'\n", chart))
}
}
}

for _, chart := range charts {
if err := t.helm.BuildDependencies(chart.Path()); err != nil {
if err := t.helm.BuildDependenciesWithArgs(chart.Path(), t.config.HelmDependencyExtraArgs); err != nil {
return nil, errors.Wrapf(err, "Error building dependencies for chart '%s'", chart)
}

Expand Down
49 changes: 39 additions & 10 deletions pkg/chart/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,29 @@ func (l *fakeLinter) Yamale(yamlFile, schemaFile string) error {
return nil
}

type fakeHelm struct{}
type fakeHelm struct {
mock.Mock
}

func (h fakeHelm) AddRepo(name, url string, extraArgs []string) error { return nil }
func (h fakeHelm) BuildDependencies(chart string) error { return nil }
func (h fakeHelm) LintWithValues(chart string, valuesFile string) error { return nil }
func (h fakeHelm) InstallWithValues(chart string, valuesFile string, namespace string, release string) error {
func (h *fakeHelm) AddRepo(name, url string, extraArgs []string) error { return nil }
func (h *fakeHelm) BuildDependencies(chart string) error { return nil }
func (h *fakeHelm) BuildDependenciesWithArgs(chart string, extraArgs []string) error {
h.Called(chart, extraArgs)
return nil
}
func (h fakeHelm) Upgrade(chart string, namespace string, release string) error {
func (h *fakeHelm) LintWithValues(chart string, valuesFile string) error { return nil }
func (h *fakeHelm) InstallWithValues(chart string, valuesFile string, namespace string, release string) error {
return nil
}
func (h fakeHelm) Test(namespace string, release string) error {
func (h *fakeHelm) Upgrade(chart string, namespace string, release string) error {
return nil
}
func (h fakeHelm) DeleteRelease(namespace string, release string) {}
func (h *fakeHelm) Test(namespace string, release string) error {
return nil
}
func (h *fakeHelm) DeleteRelease(namespace string, release string) {}

func (h fakeHelm) Version() (string, error) {
func (h *fakeHelm) Version() (string, error) {
return "v3.0.0", nil
}

Expand Down Expand Up @@ -138,7 +144,7 @@ func newTestingMock(cfg config.Configuration) Testing {
chartUtils: util.ChartUtils{},
accountValidator: fakeAccountValidator{},
linter: fakeMockLinter,
helm: fakeHelm{},
helm: new(fakeHelm),
}
}

Expand Down Expand Up @@ -320,6 +326,29 @@ func TestLintYamlValidation(t *testing.T) {
runTests(false, 0, 0)
}

func TestLintDependencyExtraArgs(t *testing.T) {
chart := "testdata/test_lints"
args := []string{"--skip-refresh"}

fakeMockHelm := new(fakeHelm)
ct.helm = fakeMockHelm
ct.config.HelmDependencyExtraArgs = args
ct.config.Charts = []string{chart}

t.Run("lint-helm-dependency-extra-args", func(t *testing.T) {
call := fakeMockHelm.On("BuildDependenciesWithArgs", chart, args).Return(nil)
call.Repeatability = 1

results, err := ct.LintCharts()
assert.Nil(t, err)
for _, result := range results {
assert.Nil(t, result.Error)
}
// -1 is set after Repeatability runs out
assert.Equal(t, -1, call.Repeatability)
})
}

func TestGenerateInstallConfig(t *testing.T) {
type testData struct {
name string
Expand Down
49 changes: 25 additions & 24 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,31 @@ var (
)

type Configuration struct {
Remote string `mapstructure:"remote"`
TargetBranch string `mapstructure:"target-branch"`
Since string `mapstructure:"since"`
BuildId string `mapstructure:"build-id"`
LintConf string `mapstructure:"lint-conf"`
ChartYamlSchema string `mapstructure:"chart-yaml-schema"`
ValidateMaintainers bool `mapstructure:"validate-maintainers"`
ValidateChartSchema bool `mapstructure:"validate-chart-schema"`
ValidateYaml bool `mapstructure:"validate-yaml"`
AdditionalCommands []string `mapstructure:"additional-commands"`
CheckVersionIncrement bool `mapstructure:"check-version-increment"`
ProcessAllCharts bool `mapstructure:"all"`
Charts []string `mapstructure:"charts"`
ChartRepos []string `mapstructure:"chart-repos"`
ChartDirs []string `mapstructure:"chart-dirs"`
ExcludedCharts []string `mapstructure:"excluded-charts"`
HelmExtraArgs string `mapstructure:"helm-extra-args"`
HelmRepoExtraArgs []string `mapstructure:"helm-repo-extra-args"`
Debug bool `mapstructure:"debug"`
Upgrade bool `mapstructure:"upgrade"`
SkipMissingValues bool `mapstructure:"skip-missing-values"`
Namespace string `mapstructure:"namespace"`
ReleaseLabel string `mapstructure:"release-label"`
ExcludeDeprecated bool `mapstructure:"exclude-deprecated"`
Remote string `mapstructure:"remote"`
TargetBranch string `mapstructure:"target-branch"`
Since string `mapstructure:"since"`
BuildId string `mapstructure:"build-id"`
LintConf string `mapstructure:"lint-conf"`
ChartYamlSchema string `mapstructure:"chart-yaml-schema"`
ValidateMaintainers bool `mapstructure:"validate-maintainers"`
ValidateChartSchema bool `mapstructure:"validate-chart-schema"`
ValidateYaml bool `mapstructure:"validate-yaml"`
AdditionalCommands []string `mapstructure:"additional-commands"`
CheckVersionIncrement bool `mapstructure:"check-version-increment"`
ProcessAllCharts bool `mapstructure:"all"`
Charts []string `mapstructure:"charts"`
ChartRepos []string `mapstructure:"chart-repos"`
ChartDirs []string `mapstructure:"chart-dirs"`
ExcludedCharts []string `mapstructure:"excluded-charts"`
HelmExtraArgs string `mapstructure:"helm-extra-args"`
HelmRepoExtraArgs []string `mapstructure:"helm-repo-extra-args"`
HelmDependencyExtraArgs []string `mapstructure:"helm-dependency-extra-args"`
Debug bool `mapstructure:"debug"`
Upgrade bool `mapstructure:"upgrade"`
SkipMissingValues bool `mapstructure:"skip-missing-values"`
Namespace string `mapstructure:"namespace"`
ReleaseLabel string `mapstructure:"release-label"`
ExcludeDeprecated bool `mapstructure:"exclude-deprecated"`
}

func LoadConfiguration(cfgFile string, cmd *cobra.Command, printConfig bool) (*Configuration, error) {
Expand Down
6 changes: 5 additions & 1 deletion pkg/tool/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func (h Helm) AddRepo(name string, url string, extraArgs []string) error {
}

func (h Helm) BuildDependencies(chart string) error {
return h.exec.RunProcess("helm", "dependency", "build", chart)
return h.BuildDependenciesWithArgs(chart, []string{})
}

func (h Helm) BuildDependenciesWithArgs(chart string, extraArgs []string) error {
return h.exec.RunProcess("helm", "dependency", "build", chart, extraArgs)
}

func (h Helm) LintWithValues(chart string, valuesFile string) error {
Expand Down

0 comments on commit b71548c

Please sign in to comment.