feat: add flag to list to skip prepare
Signed-off-by: Viktor Oreshkin <imselfish@stek29.rocks>
This commit is contained in:
parent
8b0ad72e77
commit
f3788249e4
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -547,44 +547,30 @@ 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) {
|
||||||
for _, r := range run.state.Releases {
|
var stateReleases []*HelmRelease
|
||||||
labels := ""
|
var err error
|
||||||
if r.Labels == nil {
|
|
||||||
r.Labels = map[string]string{}
|
|
||||||
}
|
|
||||||
for k, v := range run.state.CommonLabels {
|
|
||||||
r.Labels[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
var keys []string
|
if c.WithPreparedCharts() {
|
||||||
for k := range r.Labels {
|
err = run.withPreparedCharts("list", state.ChartPrepareOptions{
|
||||||
keys = append(keys, k)
|
SkipRepos: true,
|
||||||
}
|
SkipDeps: true,
|
||||||
sort.Strings(keys)
|
}, func() {
|
||||||
|
rel, err := a.list(run)
|
||||||
for _, k := range keys {
|
if err != nil {
|
||||||
v := r.Labels[k]
|
panic(err)
|
||||||
labels = fmt.Sprintf("%s,%s:%s", labels, k, v)
|
}
|
||||||
}
|
stateReleases = rel
|
||||||
labels = strings.Trim(labels, ",")
|
|
||||||
|
|
||||||
enabled, err := state.ConditionEnabled(r, run.state.Values())
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
installed := r.Installed == nil || *r.Installed
|
|
||||||
releases = append(releases, &HelmRelease{
|
|
||||||
Name: r.Name,
|
|
||||||
Namespace: r.Namespace,
|
|
||||||
Installed: installed,
|
|
||||||
Enabled: enabled,
|
|
||||||
Labels: labels,
|
|
||||||
Chart: r.Chart,
|
|
||||||
Version: r.Version,
|
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
stateReleases, err = a.list(run)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
releases = append(releases, stateReleases...)
|
||||||
|
|
||||||
return
|
return
|
||||||
}, false, SetFilter(true))
|
}, false, SetFilter(true))
|
||||||
|
|
||||||
|
|
@ -601,6 +587,50 @@ func (a *App) ListReleases(c ListConfigProvider) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *App) list(run *Run) ([]*HelmRelease, error) {
|
||||||
|
var releases []*HelmRelease
|
||||||
|
|
||||||
|
for _, r := range run.state.Releases {
|
||||||
|
labels := ""
|
||||||
|
if r.Labels == nil {
|
||||||
|
r.Labels = map[string]string{}
|
||||||
|
}
|
||||||
|
for k, v := range run.state.CommonLabels {
|
||||||
|
r.Labels[k] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
var keys []string
|
||||||
|
for k := range r.Labels {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
|
||||||
|
for _, k := range keys {
|
||||||
|
v := r.Labels[k]
|
||||||
|
labels = fmt.Sprintf("%s,%s:%s", labels, k, v)
|
||||||
|
}
|
||||||
|
labels = strings.Trim(labels, ",")
|
||||||
|
|
||||||
|
enabled, err := state.ConditionEnabled(r, run.state.Values())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
installed := r.Installed == nil || *r.Installed
|
||||||
|
releases = append(releases, &HelmRelease{
|
||||||
|
Name: r.Name,
|
||||||
|
Namespace: r.Namespace,
|
||||||
|
Installed: installed,
|
||||||
|
Enabled: enabled,
|
||||||
|
Labels: labels,
|
||||||
|
Chart: r.Chart,
|
||||||
|
Version: r.Version,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return releases, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (a *App) within(dir string, do func() error) error {
|
func (a *App) within(dir string, do func() error) error {
|
||||||
if dir == "." {
|
if dir == "." {
|
||||||
return do()
|
return do()
|
||||||
|
|
|
||||||
|
|
@ -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})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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{}
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue