From 7aa23df55b4ab298c87453b7813d829afe694063 Mon Sep 17 00:00:00 2001 From: Gianluca Mardente Date: Fri, 20 Nov 2026 10:20:35 +0100 Subject: [PATCH] fix: empty chartName for Flux-sourced Helm charts breaks drift detection ChartName is optional when a Helm chart comes from a Flux source, but it was copied as-is into the ResourceSummary where chartName has MinLength=1. With ContinuousWithDriftDetection the ResourceSummary creation failed, and the deploy was retried forever, bumping the Helm release revision each time. Report the last segment of the Flux source path (falling back to the release name) as chart name in the ResourceSummary. The value is informational only: drift is matched to a chart by release name and namespace, and the chart identity used elsewhere is unchanged. Fixes #2109 --- controllers/export_test.go | 1 + controllers/handlers_helm.go | 25 ++++++++++++++++- controllers/handlers_helm_test.go | 45 +++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/controllers/export_test.go b/controllers/export_test.go index bb0024e9..e1e44954 100644 --- a/controllers/export_test.go +++ b/controllers/export_test.go @@ -201,6 +201,7 @@ var ( DesiredValuesAreSubset = desiredValuesAreSubset GetCredentialsAndCAFiles = getCredentialsAndCAFiles GetInstantiatedChartIdentity = getInstantiatedChartIdentity + ChartNameForResourceSummary = chartNameForResourceSummary GetHelmChartValuesFrom = getHelmChartValuesFrom GetHelmChartInstantiatedValues = getHelmChartInstantiatedValues LocateChartWithTimeout = locateChartWithTimeout diff --git a/controllers/handlers_helm.go b/controllers/handlers_helm.go index f561f9c6..d85d6df5 100644 --- a/controllers/handlers_helm.go +++ b/controllers/handlers_helm.go @@ -3908,6 +3908,29 @@ func getHelmChartValuesFrom(ctx context.Context, c client.Client, clusterSummary return getValuesFrom(ctx, c, clusterSummary, helmChart.ValuesFrom, logger) } +// chartNameForResourceSummary returns the chart name to report in the ResourceSummary, where it +// must not be empty. ChartName is optional when the chart comes from a Flux source, so in that +// case fall back to the last segment of the path within the source, and then to the release name. +// The value is informational only: drift is matched to a chart by release name and namespace. +func chartNameForResourceSummary(requestedChart *configv1beta1.HelmChart) string { + if requestedChart.ChartName != "" { + return requestedChart.ChartName + } + + if isReferencingFluxSource(requestedChart) { + _, sourcePath, err := getReferencedFluxSourceFromURL(requestedChart) + if err == nil { + // path.Base returns "." for an empty path and "/" for a root path: neither is a name + name := path.Base(sourcePath) + if name != "." && name != "/" { + return name + } + } + } + + return requestedChart.ReleaseName +} + // collectResourcesFromManagedHelmChartsForDriftDetection collects resources considering all // helm charts contained in a ClusterSummary that are currently managed by the // ClusterProfile instance. @@ -3989,7 +4012,7 @@ func collectResourcesFromManagedHelmChartsForDriftDetection(ctx context.Context, l.V(logs.LogDebug).Info(fmt.Sprintf("found %d resources", len(resources))) helmInfo := libsveltosv1beta1.HelmResources{ - ChartName: instantiatedChart.ChartName, + ChartName: chartNameForResourceSummary(instantiatedChart), ReleaseName: instantiatedChart.ReleaseName, ReleaseNamespace: instantiatedChart.ReleaseNamespace, Resources: unstructuredToSveltosResources(resources), diff --git a/controllers/handlers_helm_test.go b/controllers/handlers_helm_test.go index 4453f0a1..4ca3a070 100644 --- a/controllers/handlers_helm_test.go +++ b/controllers/handlers_helm_test.go @@ -4056,3 +4056,48 @@ var _ = Describe("findRemovedCRDVersions", func() { Expect(controllers.FindRemovedCRDVersions(newCRD("v1", "v2:unserved"), newCRD("v1", "v2"))).To(BeEmpty()) }) }) + +var _ = Describe("chartNameForResourceSummary", func() { + const releaseName = "my-release" + + It("returns the chart name when set", func() { + chart := &configv1beta1.HelmChart{ + ChartName: "podinfo", + ReleaseName: releaseName, + RepositoryURL: "GitRepository://flux-system/repo/charts/other", + } + Expect(controllers.ChartNameForResourceSummary(chart)).To(Equal("podinfo")) + }) + + It("returns the last path segment for a Flux source when chart name is empty", func() { + chart := &configv1beta1.HelmChart{ + ReleaseName: releaseName, + RepositoryURL: "GitRepository://flux-system/repo/charts/podinfo", + } + Expect(controllers.ChartNameForResourceSummary(chart)).To(Equal("podinfo")) + }) + + It("ignores a trailing slash in the Flux source path", func() { + chart := &configv1beta1.HelmChart{ + ReleaseName: releaseName, + RepositoryURL: "OCIRepository://flux-system/repo/charts/podinfo/", + } + Expect(controllers.ChartNameForResourceSummary(chart)).To(Equal("podinfo")) + }) + + It("returns the release name when the Flux source path is the root", func() { + chart := &configv1beta1.HelmChart{ + ReleaseName: releaseName, + RepositoryURL: "GitRepository://flux-system/repo/./", + } + Expect(controllers.ChartNameForResourceSummary(chart)).To(Equal(releaseName)) + }) + + It("returns the release name when the Flux URL is malformed", func() { + chart := &configv1beta1.HelmChart{ + ReleaseName: releaseName, + RepositoryURL: "GitRepository://flux-system", + } + Expect(controllers.ChartNameForResourceSummary(chart)).To(Equal(releaseName)) + }) +})