Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions controllers/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ var (
DesiredValuesAreSubset = desiredValuesAreSubset
GetCredentialsAndCAFiles = getCredentialsAndCAFiles
GetInstantiatedChartIdentity = getInstantiatedChartIdentity
ChartNameForResourceSummary = chartNameForResourceSummary
GetHelmChartValuesFrom = getHelmChartValuesFrom
GetHelmChartInstantiatedValues = getHelmChartInstantiatedValues
LocateChartWithTimeout = locateChartWithTimeout
Expand Down
25 changes: 24 additions & 1 deletion controllers/handlers_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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),
Expand Down
45 changes: 45 additions & 0 deletions controllers/handlers_helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})