Skip to content

Normalize YAML flow-style docs in post-renderer [SURE-11642]#5180

Open
thardeck wants to merge 1 commit into
mainfrom
fix_json_flow_yaml
Open

Normalize YAML flow-style docs in post-renderer [SURE-11642]#5180
thardeck wants to merge 1 commit into
mainfrom
fix_json_flow_yaml

Conversation

@thardeck
Copy link
Copy Markdown
Collaborator

Helm v4's kyaml round-trips rendered manifests through its YAML parser, converting JSON output from toJson template functions into YAML flow-style with unquoted keys (e.g. {apiVersion: v1, kind: ConfigMap}). k8s.io/apimachinery's IsJSONBuffer treats the leading { as a signal that the document is already valid JSON and returns it unchanged; json.Unmarshal then fails with invalid character 'a' looking for beginning of object key string, causing the bundle deployment to fail in the post-render phase.

  • Add normalizeFlowStyleDocs in postrender.go: for each rendered document starting with { that does not pass json.Valid, round-trip it through sigs.k8s.io/yaml to produce block-style YAML before passing to yaml.ToObjects. Valid JSON documents are not touched.
  • Add hasFlowStyleCandidate as a zero-allocation fast-path: a raw byte scan checks whether any document boundary starts with {; when none does (the common case), the function returns immediately without allocating or rebuilding the buffer.

@thardeck thardeck self-assigned this May 21, 2026
@thardeck thardeck requested a review from a team as a code owner May 21, 2026 14:05
Copilot AI review requested due to automatic review settings May 21, 2026 14:05
@thardeck thardeck added this to Fleet May 21, 2026
@thardeck thardeck changed the title Normalize YAML flow-style docs in post-renderer [SURE-11642} Normalize YAML flow-style docs in post-renderer [SURE-11642] May 21, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 addresses Helm v4’s kyaml behavior that round-trips toJson output into YAML flow-style with unquoted keys (e.g. {apiVersion: v1, ...}), which later gets misdetected as JSON by apimachinery and can fail during post-render object decoding. The change adds a normalization step in Fleet’s Helm post-renderer to convert those flow-style YAML documents into block-style YAML before decoding.

Changes:

  • Add normalizeFlowStyleDocs + hasFlowStyleCandidate to detect and normalize {...}-prefixed non-JSON YAML documents prior to yaml.ToObjects.
  • Add a unit test covering the flow-style YAML regression in the post-renderer.
  • Add an install/template-level regression test exercising toJson rendering through Helm v4.

Reviewed changes

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

File Description
internal/helmdeployer/postrender.go Adds flow-style YAML normalization pre-pass before decoding rendered manifests.
internal/helmdeployer/postrender_test.go Adds a post-render regression test for flow-style YAML documents.
internal/helmdeployer/install_test.go Adds an end-to-end-ish regression test reproducing the Helm v4 toJson flow-style issue.

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

Comment thread internal/helmdeployer/postrender.go Outdated
Comment thread internal/helmdeployer/postrender_test.go Outdated
Comment thread internal/helmdeployer/install_test.go Outdated
@thardeck thardeck force-pushed the fix_json_flow_yaml branch from e734404 to d157ee6 Compare May 21, 2026 18:39
@thardeck thardeck requested a review from Copilot May 21, 2026 18:41
@thardeck thardeck moved this to 👀 In review in Fleet May 21, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 3 out of 3 changed files in this pull request and generated 4 comments.

Comment thread internal/helmdeployer/postrender.go
Comment thread internal/helmdeployer/postrender.go Outdated
Comment thread internal/helmdeployer/postrender.go
Comment thread internal/helmdeployer/postrender.go
Helm v4's kyaml round-trips rendered manifests through its YAML parser,
converting JSON output from toJson template functions into YAML flow-style
with unquoted keys, e.g. {apiVersion: v1, kind: ConfigMap, ...}.

k8s.io/apimachinery's IsJSONBuffer detects the leading '{' and returns
such documents unchanged assuming they are already valid JSON. Passing
them to json.Unmarshal then fails with 'invalid character' because the
keys are unquoted.

Add normalizeFlowStyleDocs to the post-renderer: before handing the
rendered buffer to yaml.ToObjects, each document starting with '{' is
checked with json.Valid; documents that fail are round-tripped through
sigs.k8s.io/yaml to produce block-style YAML that the existing decode
path handles correctly.

Add hasFlowStyleCandidate as a zero-allocation fast-path: on every render
call, a raw byte scan checks whether any document boundary starts with '{'.
When none does (the common case), normalizeFlowStyleDocs returns the
original slice immediately without any allocation or buffer rebuild.
@thardeck thardeck force-pushed the fix_json_flow_yaml branch from d157ee6 to dae1638 Compare May 21, 2026 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 👀 In review

Development

Successfully merging this pull request may close these issues.

2 participants