Skip to content
Merged
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 @@ -360,6 +360,7 @@ var (
FindRemovedCRDVersions = findRemovedCRDVersions
WithRestConfig = withRestConfig
GetHelmUpgradeClient = getHelmUpgradeClient
GetHelmUninstallClient = getHelmUninstallClient
)

type (
Expand Down
53 changes: 43 additions & 10 deletions controllers/handlers_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,11 @@ func uninstallHelmCharts(ctx context.Context, c client.Client, clusterSummary *c
}

if currentRelease != nil && currentRelease.Status != string(releasecommon.StatusUninstalled) {
err = doUninstallRelease(ctx, clusterSummary, instantiatedChart, kubeconfig, registryOptions, logger)
// ClusterProfile/Profile deleted or stopped matching this cluster: an admin
// lifecycle decision, not an internal recovery heuristic. Respect the chart's
// own hook settings.
err = doUninstallRelease(ctx, clusterSummary, instantiatedChart, kubeconfig, registryOptions,
false, logger)
if err != nil {
if !errors.Is(err, driver.ErrReleaseNotFound) {
return nil, err
Expand Down Expand Up @@ -1687,7 +1691,11 @@ func handleInstall(ctx context.Context, dCtx *deploymentContext,
if !isPullMode {
if fs := getFeatureSummaryForFeatureID(dCtx.clusterSummary, libsveltosv1beta1.FeatureHelm); fs != nil {
if fs.ConsecutiveFailures%recoverAfter == 0 && fs.FailureMessage != nil {
err := doUninstallRelease(ctx, dCtx.clusterSummary, currentChart, kubeconfig, registryOptions, logger)
// Internal recovery heuristic, not an admin decision: nothing validated that
// this release's recorded Hooks reflect what Sveltos itself last deployed, so
// they must not run. See getHelmUninstallClient.
err := doUninstallRelease(ctx, dCtx.clusterSummary, currentChart, kubeconfig, registryOptions,
true, logger)
if err != nil {
// Ignore release not found error
if errors.Is(err, driver.ErrReleaseNotFound) || strings.Contains(err.Error(), "release: not found") {
Expand Down Expand Up @@ -1760,7 +1768,8 @@ func handleUninstall(ctx context.Context, clusterSummary *configv1beta1.ClusterS

var report *configv1beta1.ReleaseReport
logger.V(logs.LogDebug).Info("uninstall helm release")
err := doUninstallRelease(ctx, clusterSummary, currentChart, kubeconfig, registryOptions, logger)
// Explicit HelmChartActionUninstall: an admin decision. Respect the chart's own hook settings.
err := doUninstallRelease(ctx, clusterSummary, currentChart, kubeconfig, registryOptions, false, logger)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2250,9 +2259,12 @@ func checkDependencies(chartRequested chartbase.Charter, installClient *action.I

// uninstallRelease removes helm release from a managed Cluster.
// No action in DryRun mode.
// uninstallRelease uninstalls a Helm release. forceDisableHooks is documented on
// getHelmUninstallClient: pass true only when recovering from a release whose stored
// state (and so whose Hooks) cannot be trusted.
func uninstallRelease(ctx context.Context, clusterSummary *configv1beta1.ClusterSummary,
releaseName, releaseNamespace, kubeconfig string, registryOptions *registryClientOptions,
helmChart *configv1beta1.HelmChart, logger logr.Logger) error {
helmChart *configv1beta1.HelmChart, forceDisableHooks bool, logger logr.Logger) error {

// No-op in DryRun mode
if clusterSummary.Spec.ClusterProfileSpec.SyncMode == configv1beta1.SyncModeDryRun {
Expand Down Expand Up @@ -2291,7 +2303,7 @@ func uninstallRelease(ctx context.Context, clusterSummary *configv1beta1.Cluster
return err
}

uninstallClient := getHelmUninstallClient(helmChart, actionConfig)
uninstallClient := getHelmUninstallClient(helmChart, actionConfig, forceDisableHooks)

_, err = uninstallClient.Run(releaseName)
if err != nil {
Expand Down Expand Up @@ -3228,8 +3240,10 @@ func recoverRelease(ctx context.Context, clusterSummary *configv1beta1.ClusterSu
return nil
}

// This release's state was just found inconsistent (status), so its stored Hooks
// cannot be trusted: forceDisableHooks=true. See getHelmUninstallClient.
return uninstallRelease(ctx, clusterSummary, requestedChart.ReleaseName,
requestedChart.ReleaseNamespace, kubeconfig, registryOptions, requestedChart, logger)
requestedChart.ReleaseNamespace, kubeconfig, registryOptions, requestedChart, true, logger)
}

// shouldInstall returns true if action is not uninstall and there is no release to manage
Expand Down Expand Up @@ -3421,9 +3435,15 @@ func doInstallRelease(ctx context.Context, dCtx *deploymentContext,

// doUninstallRelease uninstalls helm release from the Cluster.
// No action in DryRun mode.
// doUninstallRelease uninstalls a Helm release that an admin decision (not an internal
// recovery heuristic) determined should go away: an explicit HelmChartActionUninstall, or a
// whole ClusterSummary being torn down because its ClusterProfile/Profile was deleted or
// stopped matching. forceDisableHooks is documented on getHelmUninstallClient; it must stay
// false here so the chart's own Options.DisableHooks (the admin's own choice for this chart)
// is respected, same as any other admin-intended uninstall.
func doUninstallRelease(ctx context.Context, clusterSummary *configv1beta1.ClusterSummary,
requestedChart *configv1beta1.HelmChart, kubeconfig string, registryOptions *registryClientOptions,
logger logr.Logger) error {
forceDisableHooks bool, logger logr.Logger) error {

if clusterSummary.Spec.ClusterProfileSpec.SyncMode == configv1beta1.SyncModeDryRun {
return nil
Expand All @@ -3435,7 +3455,7 @@ func doUninstallRelease(ctx context.Context, clusterSummary *configv1beta1.Clust
requestedChart.RepositoryName))

return uninstallRelease(ctx, clusterSummary, requestedChart.ReleaseName, requestedChart.ReleaseNamespace,
kubeconfig, registryOptions, requestedChart, logger)
kubeconfig, registryOptions, requestedChart, forceDisableHooks, logger)
}

// doUpgradeRelease upgrades helm release in the Cluster.
Expand Down Expand Up @@ -3544,7 +3564,7 @@ func undeployStaleReleases(ctx context.Context, c client.Client, clusterSummary
}

if err := uninstallRelease(ctx, clusterSummary, staleReleases[i].Name,
staleReleases[i].Namespace, kubeconfig, &registryClientOptions{}, nil, logger); err != nil {
staleReleases[i].Namespace, kubeconfig, &registryClientOptions{}, nil, false, logger); err != nil {
return nil, err
}

Expand Down Expand Up @@ -4960,8 +4980,18 @@ func getHelmUpgradeClient(requestedChart *configv1beta1.HelmChart, actionConfig
return upgradeClient
}

// getHelmUninstallClient builds the Helm uninstall client. forceDisableHooks must be true
// when the uninstall is recovering from a release whose state we do not trust (see
// recoverRelease): the release being uninstalled was found pending/uninstalling/unknown,
// meaning its Hooks come from a Secret that is not known to reflect what Sveltos actually
// deployed. Anyone able to write Secrets in the release namespace can set those Hooks, so
// running them would execute attacker-controlled manifests with Sveltos' own, usually far
// more privileged, credentials for the managed cluster. requestedChart.Options' DisableHooks
// setting must not override this: it reflects what the admin wants for a normal deploy or
// uninstall of a release whose content Sveltos itself produced, not for clearing a release
// state that cannot be trusted.
func getHelmUninstallClient(requestedChart *configv1beta1.HelmChart, actionConfig *action.Configuration,
) *action.Uninstall {
forceDisableHooks bool) *action.Uninstall {

uninstallClient := action.NewUninstall(actionConfig)
uninstallClient.DryRun = false
Expand All @@ -4976,6 +5006,9 @@ func getHelmUninstallClient(requestedChart *configv1beta1.HelmChart, actionConfi
uninstallClient.KeepHistory = getKeepHistoryValue(requestedChart.Options)
uninstallClient.DeletionPropagation = getDeletionPropagation(requestedChart.Options)
}
if forceDisableHooks {
uninstallClient.DisableHooks = true
}
return uninstallClient
}

Expand Down
36 changes: 36 additions & 0 deletions controllers/handlers_helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3333,6 +3333,42 @@ var _ = Describe("getHelmUpgradeClient Force", func() {
})
})

var _ = Describe("getHelmUninstallClient forceDisableHooks", func() {
It("disables hooks when forceDisableHooks is true, even if the chart wants hooks enabled", func() {
requestedChart := &configv1beta1.HelmChart{
ReleaseName: randomString(),
ReleaseNamespace: randomString(),
Options: &configv1beta1.HelmOptions{
DisableHooks: false,
},
}

uninstallClient := controllers.GetHelmUninstallClient(requestedChart, &action.Configuration{}, true)

Expect(uninstallClient.DisableHooks).To(BeTrue())
})

It("respects the chart's own DisableHooks setting when forceDisableHooks is false", func() {
requestedChart := &configv1beta1.HelmChart{
ReleaseName: randomString(),
ReleaseNamespace: randomString(),
Options: &configv1beta1.HelmOptions{
DisableHooks: false,
},
}

uninstallClient := controllers.GetHelmUninstallClient(requestedChart, &action.Configuration{}, false)

Expect(uninstallClient.DisableHooks).To(BeFalse())
})

It("still disables hooks when forceDisableHooks is true and requestedChart is nil", func() {
uninstallClient := controllers.GetHelmUninstallClient(nil, &action.Configuration{}, true)

Expect(uninstallClient.DisableHooks).To(BeTrue())
})
})

var _ = Describe("getCRDObjects", func() {
newChartWithFiles := func(name string, fileNames ...string) *chart.Chart {
files := make([]*common.File, 0, len(fileNames))
Expand Down