Skip to content

feat: preserve explicit YAML document start markers - #6294

Open
aausch wants to merge 1 commit into
kubernetes-sigs:masterfrom
aausch:docstart-preserve
Open

aausch wants to merge 1 commit into
kubernetes-sigs:masterfrom
aausch:docstart-preserve

Conversation

@aausch

@aausch aausch commented Sep 25, 2026 •

Copy link
Copy Markdown

What this does

Adds PreserveDocStart to kio.ByteReader and kio.LocalPackageReader, mirroring the existing PreserveSeqIndent option. When set, a leading --- is recorded in the internal.config.kubernetes.io/docstart annotation at read time and re-emitted by ByteWriter for the first document of the stream. It is off by default, so no existing output changes.

Why

kyaml drops a leading --- on round trip. The reader consumes the marker, and the encoder only writes a separator ahead of the second and later documents, so a file that began with an explicit document start comes back without one.

This is visible to anyone who patches manifests in place with kyaml. The case I hit is Flux's image-automation-controller, which rewrites an image tag in a manifest and commits the result: every such commit also silently strips the ---, which then fails YAML linters that require the marker. It was reported as fluxcd/flux2#4435 and closed with "that's what kustomize kyaml does when it patches the YAML, nothing we can do about it" — correct as of today, hence this option. The companion change is fluxcd/image-automation-controller#1093, which sets the flag and is blocked on a kyaml release carrying this.

Implementation notes

  • Only the first document of a stream needs the annotation. splitDocuments splits on a separator preceded by a newline, so a marker only survives in the original text of the first document; every later document is written with a separator regardless.
  • The marker is written directly to the writer ahead of the encoder, which has emitted nothing at that point, so the two writes stay ordered.
  • Nodes wrapped in a List/ResourceList are a single document and carry no marker of their own, so the wrapping path is untouched.
  • kioutil.DocStartAnnotation is added to api/resource.BuildAnnotations alongside SeqIndentAnnotation.

Tests

  • TestByteReadWriter_PreserveDocStart — marker preserved, no marker invented, marker dropped when the option is off, and both multi-document orderings round-trip unchanged.
  • TestByteReadWriter_PreserveDocStartOmitReaderAnnotations — the option conflicts with OmitReaderAnnotations, matching how PreserveSeqIndent behaves.
  • TestLocalPackageReader_Read_PreserveDocStart — the annotation reaches only the resource that was preceded by a marker.
  • go test ./... passes in kyaml and in api.

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

/kind feature

kyaml drops a leading "---" when it round-trips a file: the reader
consumes the marker and the encoder only writes a separator ahead of the
second and later documents in a stream. Files that begin with an
explicit document start therefore come back without one, which shows up
as spurious diffs in tools that patch manifests in place, and as
failures in repositories whose YAML linters require the marker.

Add PreserveDocStart to ByteReader and LocalPackageReader, mirroring
PreserveSeqIndent: the reader records the marker in an internal
annotation, and ByteWriter re-emits it for the first document. Off by
default, so existing output is unchanged.
@kubernetes-prow kubernetes-prow Bot added the kind/feature Categorizes issue or PR as related to a new feature. label Sep 25, 2026
@linux-foundation-easycla

linux-foundation-easycla Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: aausch / name: Alex Ausch (d531936)

@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @aausch!

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: no Indicates the PR's author has not signed the CNCF CLA. labels Sep 25, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Hi @aausch. 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 Sep 25, 2026
@kubernetes-prow
kubernetes-prow Bot requested review from koba1t and sarab97 September 25, 2026 14:10
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: aausch
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

@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Sep 25, 2026

This branch has not been deployed

No deployments
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. kind/feature Categorizes issue or PR as related to a new feature. 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.

1 participant