feat: add flag to list to skip prepare

Signed-off-by: Viktor Oreshkin <imselfish@stek29.rocks>
This commit is contained in:
Viktor Oreshkin 2022-09-05 14:04:05 +03:00
parent 8b0ad72e77
commit f3788249e4
6 changed files with 95 additions and 42 deletions

View File

@ -32,6 +32,7 @@ func NewListCmd(globalCfg *config.GlobalImpl) *cobra.Command {
f := cmd.Flags() f := cmd.Flags()
f.BoolVar(&listOptions.KeepTempDir, "keep-temp-dir", false, "Keep temporary directory") f.BoolVar(&listOptions.KeepTempDir, "keep-temp-dir", false, "Keep temporary directory")
f.BoolVar(&listOptions.WithPreparedCharts, "with-prepared-charts", true, "prepare charts when listing releases")
f.StringVar(&listOptions.Output, "output", "", "output releases list as a json string") f.StringVar(&listOptions.Output, "output", "", "output releases list as a json string")
return cmd return cmd

View File

@ -547,6 +547,49 @@ func (a *App) ListReleases(c ListConfigProvider) error {
var releases []*HelmRelease var releases []*HelmRelease
err := a.ForEachState(func(run *Run) (_ bool, errs []error) { err := a.ForEachState(func(run *Run) (_ bool, errs []error) {
var stateReleases []*HelmRelease
var err error
if c.WithPreparedCharts() {
err = run.withPreparedCharts("list", state.ChartPrepareOptions{
SkipRepos: true,
SkipDeps: true,
}, func() {
rel, err := a.list(run)
if err != nil {
panic(err)
}
stateReleases = rel
})
} else {
stateReleases, err = a.list(run)
}
if err != nil {
errs = append(errs, err)
}
releases = append(releases, stateReleases...)
return
}, false, SetFilter(true))
if err != nil {
return err
}
if c.Output() == "json" {
err = FormatAsJson(releases)
} else {
err = FormatAsTable(releases)
}
return err
}
func (a *App) list(run *Run) ([]*HelmRelease, error) {
var releases []*HelmRelease
for _, r := range run.state.Releases { for _, r := range run.state.Releases {
labels := "" labels := ""
if r.Labels == nil { if r.Labels == nil {
@ -570,7 +613,7 @@ func (a *App) ListReleases(c ListConfigProvider) error {
enabled, err := state.ConditionEnabled(r, run.state.Values()) enabled, err := state.ConditionEnabled(r, run.state.Values())
if err != nil { if err != nil {
panic(err) return nil, err
} }
installed := r.Installed == nil || *r.Installed installed := r.Installed == nil || *r.Installed
@ -585,20 +628,7 @@ func (a *App) ListReleases(c ListConfigProvider) error {
}) })
} }
return return releases, nil
}, false, SetFilter(true))
if err != nil {
return err
}
if c.Output() == "json" {
err = FormatAsJson(releases)
} else {
err = FormatAsTable(releases)
}
return err
} }
func (a *App) within(dir string, do func() error) error { func (a *App) within(dir string, do func() error) error {

View File

@ -17,7 +17,7 @@ import (
"github.com/helmfile/helmfile/pkg/testutil" "github.com/helmfile/helmfile/pkg/testutil"
) )
func TestListWithEnvironment(t *testing.T) { func testListWithConfig(t *testing.T, cfg configImpl) {
type testcase struct { type testcase struct {
environment string environment string
ns string ns string
@ -26,7 +26,7 @@ func TestListWithEnvironment(t *testing.T) {
expected string expected string
} }
check := func(t *testing.T, tc testcase) { check := func(t *testing.T, tc testcase, cfg configImpl) {
t.Helper() t.Helper()
bs := &bytes.Buffer{} bs := &bytes.Buffer{}
@ -164,7 +164,7 @@ releases:
var listErr error var listErr error
out := testutil.CaptureStdout(func() { out := testutil.CaptureStdout(func() {
listErr = app.ListReleases(configImpl{}) listErr = app.ListReleases(cfg)
}) })
var gotErr string var gotErr string
@ -197,14 +197,14 @@ cache my-app true true app:test bitnami/redis
database my-app true true bitnami/postgres 11.6.22 database my-app true true bitnami/postgres 11.6.22
global kube-system true true incubator/raw global kube-system true true incubator/raw
`, `,
}) }, cfg)
}) })
t.Run("fail on unknown environment", func(t *testing.T) { t.Run("fail on unknown environment", func(t *testing.T) {
check(t, testcase{ check(t, testcase{
environment: "staging", environment: "staging",
error: `err: no releases found that matches specified selector() and environment(staging), in any helmfile`, error: `err: no releases found that matches specified selector() and environment(staging), in any helmfile`,
}) }, cfg)
}) })
t.Run("list releases matching selector and environment", func(t *testing.T) { t.Run("list releases matching selector and environment", func(t *testing.T) {
@ -215,7 +215,7 @@ global kube-system true true incubator/raw
external-secrets default true true app:test,chart:raw,name:external-secrets,namespace:default incubator/raw external-secrets default true true app:test,chart:raw,name:external-secrets,namespace:default incubator/raw
my-release default true true app:test,chart:raw,name:my-release,namespace:default incubator/raw my-release default true true app:test,chart:raw,name:my-release,namespace:default incubator/raw
`, `,
}) }, cfg)
}) })
t.Run("filters releases for environment used in one file only", func(t *testing.T) { t.Run("filters releases for environment used in one file only", func(t *testing.T) {
@ -225,7 +225,7 @@ my-release default true true app:test,chart:raw,name:my-release,
cache my-app true true app:test bitnami/redis 17.0.7 cache my-app true true app:test bitnami/redis 17.0.7
database my-app true true bitnami/postgres 11.6.22 database my-app true true bitnami/postgres 11.6.22
`, `,
}) }, cfg)
}) })
t.Run("filters releases for environment used in multiple files", func(t *testing.T) { t.Run("filters releases for environment used in multiple files", func(t *testing.T) {
@ -243,6 +243,15 @@ test3 true true incubator/raw
cache my-app true true app:test bitnami/redis 17.0.7 cache my-app true true app:test bitnami/redis 17.0.7
database my-app true true bitnami/postgres 11.6.22 database my-app true true bitnami/postgres 11.6.22
`, `,
}) }, cfg)
})
}
func TestListWithEnvironment(t *testing.T) {
t.Run("with prepared charts", func(t *testing.T) {
testListWithConfig(t, configImpl{withPreparedCharts: true})
})
t.Run("without prepared charts", func(t *testing.T) {
testListWithConfig(t, configImpl{withPreparedCharts: false})
}) })
} }

View File

@ -2220,6 +2220,7 @@ type configImpl struct {
skipNeeds bool skipNeeds bool
includeNeeds bool includeNeeds bool
includeTransitiveNeeds bool includeTransitiveNeeds bool
withPreparedCharts bool
} }
func (c configImpl) Selectors() []string { func (c configImpl) Selectors() []string {
@ -2294,6 +2295,10 @@ func (c configImpl) Output() string {
return c.output return c.output
} }
func (c configImpl) WithPreparedCharts() bool {
return c.withPreparedCharts
}
type applyConfig struct { type applyConfig struct {
args string args string
values []string values []string

View File

@ -236,6 +236,7 @@ type interactive interface {
type ListConfigProvider interface { type ListConfigProvider interface {
Output() string Output() string
WithPreparedCharts() bool
} }
type CacheConfigProvider interface{} type CacheConfigProvider interface{}

View File

@ -6,6 +6,8 @@ type ListOptions struct {
Output string Output string
// KeepTempDir is the keep temp dir flag // KeepTempDir is the keep temp dir flag
KeepTempDir bool KeepTempDir bool
// WithPreparedCharts makes list call `withPreparedCharts` when listing
WithPreparedCharts bool
} }
// NewListOptions creates a new Apply // NewListOptions creates a new Apply
@ -36,3 +38,8 @@ func (c *ListImpl) Args() string {
func (c *ListImpl) Output() string { func (c *ListImpl) Output() string {
return c.ListOptions.Output return c.ListOptions.Output
} }
// WithPreparedCharts returns withPreparedCharts flag
func (c *ListImpl) WithPreparedCharts() bool {
return c.ListOptions.WithPreparedCharts
}