-
Notifications
You must be signed in to change notification settings - Fork 18
feat(helm): fail fast on stale Helm release locks (AROSLSRE-997) #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
geoberle
merged 7 commits into
Azure:main
from
raelga:raelga/aroslsre-997-helmdeploy-stale-lock
Jun 23, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
941fbb3
feat(helm): fail fast on stale Helm release locks
raelga c5b6d14
fix(helm): reject negative stale-lock threshold
raelga bb36f70
refactor(helm): extract age and secret-name helpers
raelga 90fbbc3
fix(helm): correct gci import grouping in stale_lock_test
raelga 953ecc9
test(helm): cover stale pending-rollback lock
raelga 30445b5
docs(helm): clarify stale-lock criterion references Info.LastDeployed
raelga 01d5ec0
fix(helm): treat zero LastDeployed as non-stale
raelga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package helm | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| helmrelease "helm.sh/helm/v4/pkg/release" | ||
| helmreleasev1 "helm.sh/helm/v4/pkg/release/v1" | ||
| ) | ||
|
|
||
| // DefaultStaleLockThreshold is how old a pending Helm release revision must be | ||
| // before helmdeploy treats it as a stale lock and fails fast instead of letting | ||
| // Helm return the opaque "another operation (install/upgrade/rollback) is in | ||
| // progress" error. | ||
| const DefaultStaleLockThreshold = 15 * time.Minute | ||
|
|
||
| // StaleReleaseLockError is returned when the latest release revision is stuck in | ||
| // a pending state (pending-install, pending-upgrade, or pending-rollback) and is | ||
| // older than the configured staleness threshold. It carries enough context for | ||
| // an operator to identify and remediate the stale lock. | ||
| type StaleReleaseLockError struct { | ||
| ReleaseName string | ||
| Namespace string | ||
| Revision int | ||
| Status string | ||
| Age time.Duration | ||
| Threshold time.Duration | ||
| SecretName string | ||
| } | ||
|
|
||
| func (e *StaleReleaseLockError) Error() string { | ||
| return fmt.Sprintf( | ||
| "helm release %q in namespace %q is stuck in pending state %q at revision %d "+ | ||
| "(age %s exceeds staleness threshold %s); a previous Helm operation most likely "+ | ||
| "crashed or timed out and left a stale release lock.\n"+ | ||
| "To recover, back up and delete the stale Helm release secret, then retry the deployment:\n"+ | ||
| " kubectl --namespace %s get secret %s -o yaml > %s.backup.yaml\n"+ | ||
| " kubectl --namespace %s delete secret %s\n"+ | ||
| "Only delete the secret after confirming no Helm operation is genuinely still running.", | ||
| e.ReleaseName, e.Namespace, e.Status, e.Revision, | ||
| e.Age.Round(time.Second), e.Threshold, | ||
| e.Namespace, e.SecretName, e.SecretName, | ||
| e.Namespace, e.SecretName, | ||
| ) | ||
| } | ||
|
|
||
| // checkForStaleReleaseLock inspects the latest revision in the provided release | ||
| // history. If that revision is in a pending state and its Info.LastDeployed | ||
| // timestamp is older than threshold, it returns a *StaleReleaseLockError so the | ||
| // caller can fail fast with actionable diagnostics. Pending revisions whose | ||
| // Info.LastDeployed is within the threshold (i.e. a genuinely in-flight | ||
| // operation) are left alone and return nil. | ||
| func checkForStaleReleaseLock(logger logr.Logger, threshold time.Duration, versionsi []helmrelease.Releaser) error { | ||
| versions, err := releaseListToV1List(versionsi) | ||
| if err != nil { | ||
| // Mirror isReleaseUninstalled: a conversion failure should not block the | ||
| // deployment - just skip the stale-lock diagnostics. | ||
| logger.Error(err, "cannot convert release list to v1 release list for stale-lock check") | ||
| return nil | ||
| } | ||
| if len(versions) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| latest := versions[len(versions)-1] | ||
| if latest.Info == nil || !latest.Info.Status.IsPending() { | ||
| return nil | ||
| } | ||
|
|
||
| age := releaseAge(latest) | ||
| if age < threshold { | ||
| logger.Info( | ||
| "Latest release revision is in a pending state but within the staleness threshold; continuing.", | ||
| "release", latest.Name, | ||
| "namespace", latest.Namespace, | ||
| "revision", latest.Version, | ||
| "status", latest.Info.Status, | ||
| "age", age.Round(time.Second).String(), | ||
| "threshold", threshold.String(), | ||
| ) | ||
| return nil | ||
| } | ||
|
|
||
| secretName := releaseSecretName(latest) | ||
| logger.Info( | ||
| "Detected stale Helm release lock; failing fast.", | ||
| "release", latest.Name, | ||
| "namespace", latest.Namespace, | ||
| "revision", latest.Version, | ||
| "status", latest.Info.Status, | ||
| "age", age.Round(time.Second).String(), | ||
| "threshold", threshold.String(), | ||
| "secret", secretName, | ||
| ) | ||
| return &StaleReleaseLockError{ | ||
| ReleaseName: latest.Name, | ||
| Namespace: latest.Namespace, | ||
| Revision: latest.Version, | ||
| Status: latest.Info.Status.String(), | ||
| Age: age, | ||
| Threshold: threshold, | ||
| SecretName: secretName, | ||
| } | ||
| } | ||
|
|
||
| // releaseAge reports how long ago the given release revision was last deployed. | ||
| func releaseAge(release *helmreleasev1.Release) time.Duration { | ||
| // A zero LastDeployed (e.g. a never-successfully-deployed pending-install) | ||
| // is treated as age 0 so it is never flagged stale - failing fast on a | ||
| // missing timestamp would break genuinely in-flight operations. | ||
| if release == nil || release.Info == nil || release.Info.LastDeployed.IsZero() { | ||
| return 0 | ||
| } | ||
| return time.Since(release.Info.LastDeployed) | ||
| } | ||
|
|
||
| // releaseSecretName returns the name of the Kubernetes secret backing the given | ||
| // release revision, as used by Helm's default secret storage driver. | ||
| func releaseSecretName(release *helmreleasev1.Release) string { | ||
| return fmt.Sprintf("sh.helm.release.v1.%s.v%d", release.Name, release.Version) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.