fix: prevent preparing chart for disabled releases (#1210)

* fix: prevent preparing chart for disabled releases

Previously, PrepareCharts does not filter any releases whose condition
is disabled with no selectors.

Prevent following things from
- using any unnecessary computing resources for disabled charts
- throwing any error from wrong configurations for disabled charts

Signed-off-by: Seonghoi lee <mighty1231@gmail.com>

* fix: working for integration test about list and build

Some tests require that PrepareCharts without any selector
may not add any labels on the release.

make markExcludedReleases do not add any label without any selectors

Signed-off-by: Seonghoi lee <mighty1231@gmail.com>

* fix: prevent resolved chart version loss

State loss the resolved chart version info from st.Releases
when st.GetSelectedReleases() be called

update st.Releases after st.GetSelectedReleases() in prepareCharts

Signed-off-by: Seonghoi lee <mighty1231@gmail.com>

* fix: preserve resolved version from resolveDeps

In PrepareCharts, the version, resolved from resolveDeps,
is removed after invoking GetSelectedReleases.

Do updateDeps at the first before GetSelectedReleases call

Signed-off-by: Seonghoi lee <mighty1231@gmail.com>

---------

Signed-off-by: Seonghoi lee <mighty1231@gmail.com>
This commit is contained in:
Seonghoi Lee 2024-01-02 09:56:07 +09:00 committed by GitHub
parent 589eb19b8a
commit 4871a92b8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 27 deletions

View File

@ -1123,21 +1123,6 @@ type PrepareChartKey struct {
//
// If exists, it will also patch resources by json patches, strategic-merge patches, and injectors.
func (st *HelmState) PrepareCharts(helm helmexec.Interface, dir string, concurrency int, helmfileCommand string, opts ChartPrepareOptions) (map[PrepareChartKey]string, []error) {
var selected []ReleaseSpec
if len(st.Selectors) > 0 {
var err error
// This and releasesNeedCharts ensures that we run operations like helm-dep-build and prepare-hook calls only on
// releases that are (1) selected by the selectors and (2) to be installed.
selected, err = st.GetSelectedReleases(opts.IncludeTransitiveNeeds)
if err != nil {
return nil, []error{err}
}
} else {
selected = st.Releases
}
if !opts.SkipResolve {
updated, err := st.ResolveDeps()
if err != nil {
@ -1145,6 +1130,10 @@ func (st *HelmState) PrepareCharts(helm helmexec.Interface, dir string, concurre
}
*st = *updated
}
selected, err := st.GetSelectedReleases(opts.IncludeTransitiveNeeds)
if err != nil {
return nil, []error{err}
}
releases := releasesNeedCharts(selected)
@ -2204,18 +2193,21 @@ func markExcludedReleases(releases []ReleaseSpec, selectors []string, commonLabe
filters = append(filters, f)
}
for _, r := range releases {
if r.Labels == nil {
r.Labels = map[string]string{}
}
// Let the release name, namespace, and chart be used as a tag
r.Labels["name"] = r.Name
r.Labels["namespace"] = r.Namespace
// Strip off just the last portion for the name stable/newrelic would give newrelic
chartSplit := strings.Split(r.Chart, "/")
r.Labels["chart"] = chartSplit[len(chartSplit)-1]
// Merge CommonLabels into release labels
for k, v := range commonLabels {
r.Labels[k] = v
// Do not add any label without any filter, see #276
if len(filters) > 0 {
if r.Labels == nil {
r.Labels = map[string]string{}
}
// Let the release name, namespace, and chart be used as a tag
r.Labels["name"] = r.Name
r.Labels["namespace"] = r.Namespace
// Strip off just the last portion for the name stable/newrelic would give newrelic
chartSplit := strings.Split(r.Chart, "/")
r.Labels["chart"] = chartSplit[len(chartSplit)-1]
// Merge CommonLabels into release labels
for k, v := range commonLabels {
r.Labels[k] = v
}
}
var filterMatch bool
for _, f := range filters {

View File

@ -1,3 +1,5 @@
mysecret: MYSECRET
raw2:
enabled: false
release-disabled:
enabled: false

View File

@ -54,3 +54,20 @@ releases:
values:
- mysecret: {{ .Environment.Values.mysecret }}
- values.yaml
- name: release-disabled
chart: ../../../charts/helmx
namespace: release-disabled
condition: release-disabled.enabled
values:
- values-not-found.yaml
jsonPatches:
- target:
version: v1
kind: ConfigMap
name: release-name
patch:
- op: add
path: /metadata/annotations
value:
foo: bar