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)) + }) +})