From 65317e96f6fc01ab14f54e5814640d172add3430 Mon Sep 17 00:00:00 2001 From: Thomas Loubiou Date: Thu, 4 Mar 2021 01:29:44 +0100 Subject: [PATCH] Fix incorrect chart bug in multi-cluster setup (#1698) When the same release name is used accross namespaces/kubecontexts a bad chart name could be used Fixes #1694 --- pkg/app/run.go | 9 +++++++-- pkg/state/state.go | 18 +++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/pkg/app/run.go b/pkg/app/run.go index 56cfebe5..221ba098 100644 --- a/pkg/app/run.go +++ b/pkg/app/run.go @@ -16,7 +16,7 @@ type Run struct { helm helmexec.Interface ctx Context - ReleaseToChart map[string]string + ReleaseToChart map[state.PrepareChartKey]string Ask func(string) bool } @@ -68,7 +68,12 @@ func (r *Run) withPreparedCharts(helmfileCommand string, opts state.ChartPrepare for i := range r.state.Releases { rel := &r.state.Releases[i] - if chart := releaseToChart[rel.Name]; chart != "" { + key := state.PrepareChartKey{ + Name: rel.Name, + Namespace: rel.Namespace, + KubeContext: rel.KubeContext, + } + if chart := releaseToChart[key]; chart != "" { rel.Chart = chart } } diff --git a/pkg/state/state.go b/pkg/state/state.go index b5393a06..40c9607a 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -891,6 +891,8 @@ type ChartPrepareOptions struct { type chartPrepareResult struct { releaseName string + releaseNamespace string + releaseContext string chartName string chartPath string err error @@ -912,6 +914,10 @@ func (st *HelmState) GetRepositoryAndNameFromChartName(chartName string) (*Repos return nil, chartName } +type PrepareChartKey struct { + Namespace, Name, KubeContext string +} + // PrepareCharts creates temporary directories of charts. // // Each resulting "chart" can be one of the followings: @@ -926,7 +932,7 @@ func (st *HelmState) GetRepositoryAndNameFromChartName(chartName string) (*Repos // Otheriwse, if a chart is not a helm chart, it will call "chartify" to turn it into a chart. // // 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[string]string, []error) { +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 { @@ -944,7 +950,7 @@ func (st *HelmState) PrepareCharts(helm helmexec.Interface, dir string, concurre releases := releasesNeedCharts(selected) - temp := make(map[string]string, len(releases)) + temp := make(map[PrepareChartKey]string, len(releases)) errs := []error{} @@ -1139,6 +1145,8 @@ func (st *HelmState) PrepareCharts(helm helmexec.Interface, dir string, concurre results <- &chartPrepareResult{ releaseName: release.Name, chartName: chartName, + releaseNamespace: release.Namespace, + releaseContext: release.KubeContext, chartPath: chartPath, buildDeps: buildDeps, chartFetchedByGoGetter: chartFetchedByGoGetter, @@ -1154,7 +1162,11 @@ func (st *HelmState) PrepareCharts(helm helmexec.Interface, dir string, concurre return } - temp[downloadRes.releaseName] = downloadRes.chartPath + temp[PrepareChartKey{ + Namespace: downloadRes.releaseNamespace, + KubeContext: downloadRes.releaseContext, + Name: downloadRes.releaseName, + }] = downloadRes.chartPath if downloadRes.buildDeps { builds = append(builds, downloadRes)