Helm and Kubernetes: Atomic Upgrade Rollback and Hook Resource Persistence
0 reputation · 10 Apr 2023, 17:04 UTC
0 reputation · 10 Apr 2023, 17:04 UTC
When utilizing the --atomic flag during a helm upgrade, Helm is designed to automatically roll back the release to the previous successful revision if the upgrade fails. This mechanism ensures that the cluster returns to a known stable state without manual intervention.
A specific uncertainty exists regarding the interoperability between the atomic rollback process and Helm hooks. While the upgrade is marked as failed if a hook fails to reach a ready state within the specified timeout, the cleanup of resources created by those hooks is not consistently guaranteed. This can lead to a discrepancy where the release version is rolled back, but orphaned hook resources remain in the Kubernetes namespace.
Given the behavior in Helm 3.x, it is unclear if the rollback logic should encompass the deletion of all resources instantiated by a failed hook or if these are intended to persist for debugging purposes.
--atomic flag trigger the deletion of hook resources that were successfully created but failed to complete before the upgrade timed out?No, the --atomic flag does not trigger the deletion of hook resources. While --atomic ensures the release version rolls back to the previous successful revision upon failure, it does not manage the lifecycle of resources created by Helm hooks. These resources are managed independently of the release manifest.
In Helm 3.x, resources created via hooks (such as Jobs or Pods) are not tracked as part of the release's state. When a hook fails or times out, triggering an atomic rollback, Helm preserves the failed hook resource in the cluster. This behavior is intentional, allowing operators to retrieve logs (kubectl logs) and describe the resource (kubectl describe) to diagnose why the upgrade failed.
Helm does not distinguish between a "failed" hook and a "successful" one for the purpose of automatic purging during a rollback. Instead, cleanup is governed exclusively by the helm.sh/hook-delete-policy annotation. To automate the removal of hook resources regardless of the --atomic status, you must define a deletion policy in the resource metadata.
Common policy options include:
before-hook-creation: Deletes the previous hook resource before starting a new one.hook-succeeded: Deletes the resource only if the hook completes successfully.hook-failed: Deletes the resource only if the hook fails.To verify this behavior in your environment, you can deploy a Job hook designed to timeout:
post-install or pre-upgrade hook that sleeps longer than your timeout value.helm upgrade --install my-release ./my-chart --atomic --timeout 2m0shelm listkubectl get jobsNote: Please specify if you are using a custom hook-delete-policy, as this would override the default persistence behavior described above.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 10 Apr 2023, 18:05 UTC
In Helm 3.x, --atomic rolls the rendered release manifests back to the previous deployed revision after a failed upgrade. It is not a general garbage collector for hook-created objects, especially objects created transitively by a hook workload or cluster-scoped artifacts.
Hook deletion is normally driven by the helm.sh/hook-delete-policy annotation, for example hook-failed, rather than by the rollback itself. --cleanup-on-fail also has a narrower scope than full hook-resource cleanup.
A useful check is to run the same failing pre-upgrade hook in a throwaway namespace with and without hook-failed, using --wait --wait-for-jobs --timeout. Then inspect helm history and label-selected Jobs, Pods, ConfigMaps, Secrets, and cluster-scoped resources. Helm 3 minor versions can differ, so confirm behavior against your installed helm version.