Visitar URL original
WIP: helm upgrade: check ownership for deletion case by cg49996w11 · Pull Request #32257 · helm/helm · GitHub
Skip to content

WIP: helm upgrade: check ownership for deletion case - #32257

Closed
cg49996w11 wants to merge 2 commits into
helm:mainfrom
cg49996w11:cg49996w11-feat/check-ownership-during-upgrade
Closed

cg49996w11 wants to merge 2 commits into
helm:mainfrom
cg49996w11:cg49996w11-feat/check-ownership-during-upgrade

Conversation

@cg49996w11

@cg49996w11 cg49996w11 commented Jun 22, 2026 •

Copy link
Copy Markdown

What this PR does
This PR adds an ownership-aware safeguard to Helm’s Kubernetes update/delete flow during helm upgrade, preventing deletion of orphaned resources when they are annotated as belonging to a different Helm release.

Fixes #32218

Signed-off-by: cg49996w11 <cg49996w11@gmail.com>
Copilot AI review requested due to automatic review settings June 22, 2026 19:02
@pull-request-size pull-request-size Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 22, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an ownership-aware safeguard to Helm’s Kubernetes update/delete flow during helm upgrade, preventing deletion of orphaned resources when they are annotated as belonging to a different Helm release.

Changes:

  • Introduces exported constants for Helm ownership annotations (meta.helm.sh/release-name / meta.helm.sh/release-namespace).
  • Extends kube.Client.Update with an optional “ownership” update option and uses it from the upgrade action to skip deleting resources owned by other releases.
  • Adds a unit test covering the “skip delete when owned by a different release” scenario.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
pkg/kube/resource_policy.go Adds exported constants for Helm ownership annotations.
pkg/kube/client.go Adds ownership-aware deletion guard and a new ClientUpdateOptionOwnership option.
pkg/kube/client_test.go Adds a test asserting resources owned by a different release are not deleted.
pkg/action/upgrade.go Enables ownership-aware deletion behavior during upgrade by passing the new option.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +29 to +33
// ReleaseNameAnnotation is the annotation that tracks which release owns a resource
const ReleaseNameAnnotation = "meta.helm.sh/release-name"

// ReleaseNamespaceAnnotation is the annotation that tracks which release namespace owns a resource
const ReleaseNamespaceAnnotation = "meta.helm.sh/release-namespace"
Comment thread pkg/kube/client.go
Comment on lines +809 to +812
// ClientUpdateOptionOwnership specifies the release name and namespace that owns the resources being updated.
// When set, orphaned resources (present in the original list but not in the target list) will only be deleted
// if their meta.helm.sh/release-name and meta.helm.sh/release-namespace annotations match the specified
// release. Resources annotated as belonging to a different release will be skipped.
Comment thread pkg/kube/client.go
Comment on lines +813 to +820
func ClientUpdateOptionOwnership(releaseName, releaseNamespace string) ClientUpdateOption {
return func(o *clientUpdateOptions) error {
o.releaseName = releaseName
o.releaseNamespace = releaseNamespace

return nil
}
}
@gjenkins8
gjenkins8 marked this pull request as draft June 23, 2026 01:20
@gjenkins8

Copy link
Copy Markdown
Member

Converted to draft to reflect "WIP: " title

Signed-off-by: cg49996w11 <cg49996w11@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Comment thread pkg/kube/client.go
Comment on lines +677 to +691
if releaseName != "" && annotations != nil {
annoReleaseName := annotations[ReleaseNameAnnotation]
annoReleaseNS := annotations[ReleaseNamespaceAnnotation]
ownedByDifferentRelease := (annoReleaseName != "" && annoReleaseName != releaseName) ||
(releaseNamespace != "" && annoReleaseNS != "" && annoReleaseNS != releaseNamespace)
if ownedByDifferentRelease {
c.Logger().Warn("skipping delete of resource not owned by this release",
slog.String("namespace", info.Namespace),
slog.String("name", info.Name),
slog.String("kind", info.Mapping.GroupVersionKind.Kind),
slog.String("release", releaseName),
)
continue
}
}
Comment thread pkg/action/upgrade.go
Comment on lines 475 to +477
kube.ClientUpdateOptionServerSideApply(serverSideApply, u.ForceConflicts),
kube.ClientUpdateOptionUpgradeClientSideFieldManager(upgradeClientSideFieldManager))
kube.ClientUpdateOptionUpgradeClientSideFieldManager(upgradeClientSideFieldManager),
kube.ClientUpdateOptionOwnership(upgradedRelease.Name, upgradedRelease.Namespace))
Comment thread pkg/kube/client_test.go
err := opt(&o)
assert.ErrorContains(t, err, "releaseName must not be empty")

// Non-empty releaseName with empty namespace is allowed (cluster-scoped resources).
@github-actions

Copy link
Copy Markdown

This pull request has been marked as stale because it has been open for 90 days with no activity. This pull request will be automatically closed in 30 days if no further activity occurs.

@github-actions github-actions Bot added the Stale label Oct 11, 2026
silence-coding added a commit to silence-coding/helm that referenced this pull request Oct 21, 2026
The delete phase of Update (pkg/kube/client.go) is annotation-blind: a
resource present in the original release but absent from the target is
deleted without consulting meta.helm.sh/release-name. When a resource has
been adopted by another release (via --take-ownership), the source release
still deletes it on its next upgrade/rollback, corrupting the adopter's
state. This is helm#32218.

The uninstall path already guards against this via verifyOwnershipBeforeDelete
(helm#31584). This extends the same principle to the shared update() delete loop,
which is reached by upgrade, rollback, and install.

Add an opt-in ClientUpdateOptionOwnership(releaseName). When set, the delete
loop reads the live resource's release-name annotation and skips deletion
when it belongs to a different release. Resources owned by the operating
release, or carrying no annotation, are deleted as before. When releaseName
is empty the safeguard is disabled and behavior is unchanged.

Wire the option into all three Update call sites:
  - pkg/action/upgrade.go   (releasingUpgrade)
  - pkg/action/rollback.go  (performRollback)
  - pkg/action/install.go   (performInstall)

Compared to the stalled draft helm#32257 (upgrade-only), this also covers
rollback and install, and checks only release-name (not namespace) to
avoid edge-case false skips across namespaces.

Export ReleaseNameAnnotation / ReleaseNamespaceAnnotation from pkg/kube so
pkg/action can reuse the constants instead of duplicating them.

Verified on a k3s cluster with a cross-release transfer scenario: after
release B adopts a resource from A via --take-ownership, upgrading A no
longer deletes it. The unmodified build deletes it. A bidirectional swap
(A->B and B->A via upgrade) keeps both resources alive with correct
ownership on the fixed build; the unmodified build loses one. Regular
upgrades that remove a release's own resources still delete them.

Signed-off-by: silence-coding <32766901+silence-coding@users.noreply.github.com>
silence-coding added a commit to silence-coding/helm that referenced this pull request Oct 21, 2026
The delete phase of Update (pkg/kube/client.go) is annotation-blind: a
resource present in the original release but absent from the target is
deleted without consulting meta.helm.sh/release-name. When a resource has
been adopted by another release (via --take-ownership), the source release
still deletes it on its next upgrade/rollback, corrupting the adopter's
state. This is helm#32218.

The uninstall path already guards against this via verifyOwnershipBeforeDelete
(helm#31584). This extends the same principle to the shared update() delete loop,
which is reached by upgrade, rollback, and install.

Add an opt-in ClientUpdateOptionOwnership(releaseName). When set, the delete
loop reads the live resource's release-name annotation and skips deletion
when it belongs to a different release. Resources owned by the operating
release, or carrying no annotation, are deleted as before. When releaseName
is empty the safeguard is disabled and behavior is unchanged.

Wire the option into all three Update call sites:
  - pkg/action/upgrade.go   (releasingUpgrade)
  - pkg/action/rollback.go  (performRollback)
  - pkg/action/install.go   (performInstall)

Compared to the stalled draft helm#32257 (upgrade-only), this also covers
rollback and install, and checks only release-name (not namespace) to
avoid edge-case false skips across namespaces.

Verified on a k3s cluster with a cross-release transfer scenario: after
release B adopts a resource from A via --take-ownership, upgrading A no
longer deletes it. The unmodified build deletes it. A bidirectional swap
(A->B and B->A via upgrade) keeps both resources alive with correct
ownership on the fixed build; the unmodified build loses one. Regular
upgrades that remove a release's own resources still delete them.

Signed-off-by: silence-coding <32766901+silence-coding@users.noreply.github.com>
silence-coding added a commit to silence-coding/helm that referenced this pull request Oct 21, 2026
The delete phase of Update (pkg/kube/client.go) is annotation-blind: a
resource present in the original release but absent from the target is
deleted without consulting meta.helm.sh/release-name. When a resource has
been adopted by another release (via --take-ownership), the source release
still deletes it on its next upgrade/rollback, corrupting the adopter's
state. This is helm#32218.

The uninstall path already guards against this via verifyOwnershipBeforeDelete
(helm#31584). This extends the same principle to the shared update() delete loop,
which is reached by upgrade, rollback, and install.

Add an opt-in ClientUpdateOptionOwnership(releaseName). When set, the delete
loop reads the live resource's release-name annotation and skips deletion
when it belongs to a different release. Resources owned by the operating
release, or carrying no annotation, are deleted as before. When releaseName
is empty the safeguard is disabled and behavior is unchanged.

Wire the option into all three Update call sites:
  - pkg/action/upgrade.go   (releasingUpgrade)
  - pkg/action/rollback.go  (performRollback)
  - pkg/action/install.go   (performInstall)

Compared to the stalled draft helm#32257 (upgrade-only), this also covers
rollback and install, and checks only release-name (not namespace) to
avoid edge-case false skips across namespaces.

Verified on a k3s cluster with a cross-release transfer scenario: after
release B adopts a resource from A via --take-ownership, upgrading A no
longer deletes it. The unmodified build deletes it. A bidirectional swap
(A->B and B->A via upgrade) keeps both resources alive with correct
ownership on the fixed build; the unmodified build loses one. Regular
upgrades that remove a release's own resources still delete them.

Signed-off-by: silence-coding <32766901+silence-coding@users.noreply.github.com>
@github-actions github-actions Bot closed this Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L Denotes a PR that changes 100-499 lines, ignoring generated files. Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Helm ignores ownership during upgrade

3 participants