From bae12410c1703954504d094a18f46cb01b9808be Mon Sep 17 00:00:00 2001 From: Gianluca Mardente Date: Fri, 18 Sep 2026 20:20:33 +0200 Subject: [PATCH] fix: uninstall Helm charts in reverse of install order When a profile contains multiple Helm charts, Sveltos installs them in the order they are listed. Previously, when a cluster stopped matching a profile, charts were uninstalled in that same forward order. This PR updates the cleanup logic so that Helm charts are uninstalled in reverse order, ensuring correct dependency handling during removal. While charts are properly uninstalled in reverse order when a cluster completely stops matching a profile, there is one scenario where the exact deletion order cannot be guaranteed: if a cluster remains matched to the profile, but charts 2 and 3 are actively removed from the profile's helmCharts section while chart 1 stays. --- controllers/handlers_helm.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/controllers/handlers_helm.go b/controllers/handlers_helm.go index b5c37b9b..568e2418 100644 --- a/controllers/handlers_helm.go +++ b/controllers/handlers_helm.go @@ -970,8 +970,12 @@ func uninstallHelmCharts(ctx context.Context, c client.Client, clusterSummary *c } releaseReports := make([]configv1beta1.ReleaseReport, 0) - for i := range clusterSummary.Spec.ClusterProfileSpec.HelmCharts { - currentChart := &clusterSummary.Spec.ClusterProfileSpec.HelmCharts[i] + // Uninstall in the reverse of install order (handled by walkChartsAndDeploy): a chart + // installed later in the list may depend on one installed earlier (e.g. a controller on a + // CRD chart), so tearing the whole profile down should undo that dependency last. + helmCharts := clusterSummary.Spec.ClusterProfileSpec.HelmCharts + for i := len(helmCharts) - 1; i >= 0; i-- { + currentChart := &helmCharts[i] instantiatedChart, err := getInstantiatedChartIdentity(ctx, dCtx, currentChart, logger) if err != nil {