Skip to content

fix: preserve resolved scalar type for replacement-created fields - #6211

Open
onematchfox wants to merge 2 commits into
kubernetes-sigs:masterfrom
onematchfox:fix-replacements-bool
Open

fix: preserve resolved scalar type for replacement-created fields#6211
onematchfox wants to merge 2 commits into
kubernetes-sigs:masterfrom
onematchfox:fix-replacements-bool

Conversation

@onematchfox

Copy link
Copy Markdown

Every field created by a replacements entry (options.create: true) was left with an empty YAML tag on its scalar value, since setFieldValue only ever copied .Value onto the newly created node and never resolved a type for it. This was invisible in isolation, because encoding an untagged, unstyled scalar implicitly resolves its type from its text (e.g. "true" -> bool).

However, once any strategic merge patch was later applied to the same resource -- even to a completely unrelated part of the document -- kyaml's merge2 walk revisits every field via FieldSetter.Filter, which contains a YAML-1.1-compatibility safeguard that treats an untagged scalar as a plain string and force-quotes it if the value looks like a YAML 1.1 keyword. That safeguard can't distinguish "this is genuinely a string" from "this has no resolved type yet", so it silently turned e.g. a replacement-created readOnly: true into the string readOnly: "true".

This PR was written in part with the assistance of generative AI.

…n bug

Demonstrates a bug where a strategic merge patch that only touches an unrelated
part of a resource corrupts the type of sibling fields created by a
`replacements` entry with `options.create: true`, turning e.g. `readOnly: true`
into `readOnly: "true"`.

Two cases are covered:

- `TestReplacementsCreateBoolFieldSurvivesUnrelatedStrategicMergePatch`, where
  the replacement source is a ConfigMap (whose `data` values are always
  strings).
- `TestReplacementsCreateBoolFieldFromLocalConfigSurvivesUnrelatedStrategicMergePatch`,
  where the source is a local-config resource with a natively typed boolean
  field, showing the bug is not specific to string-typed sources.

Signed-off-by: Brian Fox <878612+onematchfox@users.noreply.github.com>
Every field created by a `replacements` entry (`options.create: true`) was left
with an empty YAML tag on its scalar value, since `setFieldValue` only ever
copied `.Value` onto the newly created node and never resolved a type for it.
This was invisible in isolation, because encoding an untagged, unstyled scalar
implicitly resolves its type from its text (e.g. "true" -> bool).

However, once any strategic merge patch was later applied to the same resource
-- even to a completely unrelated part of the document -- `kyaml`'s `merge2`
walk revisits every field via `FieldSetter.Filter`, which contains a
YAML-1.1-compatibility safeguard that treats an untagged scalar as a plain
string and force-quotes it if the value looks like a YAML 1.1 keyword. That
safeguard can't distinguish "this is genuinely a string" from "this has no
resolved type yet", so it silently turned e.g. a replacement-created `readOnly:
true` into the string `readOnly: "true"`.

Resolve and set the field's implicit YAML 1.2 tag right after creation, so its
type is fixed before any later transformer can reinterpret it.

Signed-off-by: Brian Fox <878612+onematchfox@users.noreply.github.com>
@kubernetes-prow

Copy link
Copy Markdown
Contributor

This PR has multiple commits, and the default merge method is: merge.
You can request commits to be squashed using the label: tide/merge-method-squash

Details

Instructions 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.

@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @onematchfox!

It looks like this is your first PR to kubernetes-sigs/kustomize 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/kustomize has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@kubernetes-prow kubernetes-prow Bot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 4, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Hi @onematchfox. 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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions 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.

@kubernetes-prow kubernetes-prow Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 4, 2026
@kubernetes-prow
kubernetes-prow Bot requested review from koba1t and sarab97 August 4, 2026 13:30

@saitejabandaru-in saitejabandaru-in left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM! Using resolveImplicitTag to set the YAML 1.2 tag dynamically for newly created scalars is a clean way to ensure booleans and integers aren't silently coerced to strings later in the pipeline.

@saitejabandaru-in saitejabandaru-in left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM! This is a great fix for type preservation in replacement scalars.

@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: onematchfox, saitejabandaru-in
Once this PR has been reviewed and has the lgtm label, please assign varshaprasad96 for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants