fix: apply name references to kinds of unknown scope across namespaces#6201
fix: apply name references to kinds of unknown scope across namespaces#6201jojinkb wants to merge 1 commit into
Conversation
The nameref filter and SubsetThatCouldBeReferencedByResource drop every referral candidate that does not share the referrer's namespace, unless the candidate is known to be cluster-scoped. Kinds absent from the openapi data (e.g. custom resources) are assumed to be namespace-scoped, so a nameReference configured for such a kind was silently skipped whenever the referrer and the referral ended up in different namespaces, for example after a NamespaceTransformer with unsetOnly: true moved the referral. The referrer then kept the old name (a dangling reference) even though the target itself was renamed by namePrefix. The namespace comparison is meaningless when the scope of a candidate's kind is only guessed at: the object may actually be cluster-scoped, or kustomize's own namespace transformation may have moved it. So, when no candidate shares the referrer's namespace, fall back to the candidates whose scope is unknown to the openapi data. Candidates in the referrer's namespace still always win, and behavior for kinds whose scope is known is unchanged. Signed-off-by: Jojin <jojin.kb@gmail.com>
|
Hi @jojinkb. Thanks for your PR. I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with Regular contributors should join the org to skip this step. Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: jojinkb The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Fixes #5696
What happened
As reported (and reproduced by @stormqueen1990 at HEAD), a custom
nameReferenceconfiguration stops being applied as soon as the referrer and the referral end up in different namespaces — for example when aNamespaceTransformerwithunsetOnly: truemoves the referral into one namespace while the referrer keeps its explicit namespace. The referral itself is renamed bynamePrefix, but the field holding the back-reference silently keeps the old name, producing a dangling reference:Root cause
Name-reference candidates are filtered by namespace twice:
SubsetThatCouldBeReferencedByResource(api/resmap/reswrangler.go) and thesameCurrentNamespaceAsReferrersieve in the nameref filter (api/filters/nameref/nameref.go). Both let a candidate through only if it shares the referrer's current namespace, or if it is known to be cluster-scoped per the openapi data (plus the RoleBinding/ServiceAccount special cases).For kinds the openapi data doesn't cover — every custom resource, including the
Meshkind above, which is in fact cluster-scoped in its real API — kustomize guesses "namespace-scoped". That guess makes the namespace comparison meaningless, yet it was applied absolutely: any namespace mismatch (often introduced by kustomize's own namespace transformation) unconditionally suppressed the reference update, with no way to express the correct scope in anameReferenceconfiguration.The fix
When the strict same-namespace sieve leaves no candidates, fall back to the candidates whose kind's scope is unknown to the openapi data (
openapi.IsNamespaceScopednot found), instead of silently dropping the reference. The existing disambiguation pipeline (prefix/suffix comparison, identity check, ambiguity error) still runs on the fallback set.SubsetThatCouldBeReferencedByResourceis relaxed the same way so such candidates reach the filter at all.Deliberately narrow consequences:
An alternative discussed in the issue was a new opt-in configuration field (e.g.
allowedNsonnameReferenceentries). That requires extending the transformer-config API surface, and every user of a cluster-scoped custom resource would need to discover and set it; treating "scope unknown" as "namespace comparison unreliable" fixes the reported configurations without new API. Happy to rework in that direction if maintainers prefer the explicit knob.Verification
TestIssue5696NameReferenceWithNamespaceTransformerinapi/krusty/namereference_test.go(the exact reproduction from the issue; fails at HEAD withkuma.io/mesh: test, passes with this change).api/filters/nameref/nameref_test.gocovering: unknown-scope candidate in another namespace (updated), known namespaced kind in another namespace (still not updated), same-namespace candidate preferred over cross-namespace one.TestSubsetThatCouldBeReferencedByResourceinapi/resmap/reswrangler_test.goextended with an unknown-scope kind in an unrelated namespace, now expected in every referrer's subset.go test ./filters/nameref/ ./resmap/ ./internal/accumulator/ ./krusty/inapi/passes.unsetOnly,namespace:+ PatchTransformer, and the map-stylegorillaRefexample) now build with correct names and namespaces.This change was developed with AI assistance (Claude Code); I have reviewed and tested it.