Skip to content

Commit a86c62a

Browse files
committed
fix: preserve original YAML formatting in resource output
Previously, Resource.AsYAML() used yaml.JSONToYAML which caused two issues: 1. Long lines were automatically wrapped at ~80 characters, breaking shell commands and template strings (e.g., Helm-style {{ }}) 2. Quote styles were inconsistently modified - sometimes removed, sometimes added This change switches to using kyaml's encoder (RNode.MustString) which preserves the original YAML formatting, including: - Long lines remain on a single line - Quote styles ('single', "double", or unquoted) are preserved This is a more consistent behavior that respects the user's original YAML structure. Related to: - #947 - #3969
1 parent 008b7a0 commit a86c62a

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

api/krusty/stringquoteblank_test.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ import (
99
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
1010
)
1111

12-
// This test is for output string style.
13-
// Currently all quotes will be removed if the string is valid as plain (unquoted) style.
14-
// If a string cannot be unquoted, it will be put into a pair of single quotes.
15-
// See https://yaml.org/spec/1.2/spec.html#id2788859 for more details about what kind of string
16-
// is invalid as plain style.
12+
// This test verifies that long lines are NOT wrapped in YAML output.
13+
// See https://github.com/kubernetes-sigs/kustomize/issues/947
1714
func TestLongLineBreaks(t *testing.T) {
1815
th := kusttest_test.MakeHarness(t)
1916
th.WriteF("deployment.yaml", `
@@ -59,27 +56,24 @@ spec:
5956
template:
6057
spec:
6158
containers:
62-
- env:
59+
- name: mariadb
60+
image: test
61+
env:
6362
- name: SHORT_STRING
6463
value: short_string
6564
- name: SHORT_STRING_WITH_SINGLE_QUOTE
66-
value: short_string
65+
value: 'short_string'
6766
- name: SHORT_STRING_WITH_DOUBLE_QUOTE
68-
value: short_string
67+
value: "short_string"
6968
- name: SHORT_STRING_BLANK
7069
value: short string
7170
- name: LONG_STRING_BLANK
72-
value: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas
73-
suscipit ex non molestie varius.
71+
value: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas suscipit ex non molestie varius.
7472
- name: LONG_STRING_BLANK_WITH_SINGLE_QUOTE
75-
value: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas
76-
suscipit ex non molestie varius.
73+
value: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas suscipit ex non molestie varius.'
7774
- name: LONG_STRING_BLANK_WITH_DOUBLE_QUOTE
78-
value: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas
79-
suscipit ex non molestie varius.
75+
value: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas suscipit ex non molestie varius."
8076
- name: INVALID_PLAIN_STYLE_STRING
8177
value: ': test'
82-
image: test
83-
name: mariadb
8478
`)
8579
}

api/resource/resource.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,14 +380,13 @@ func (r *Resource) String() string {
380380
// AsYAML returns the resource in Yaml form.
381381
// Easier to read than JSON.
382382
func (r *Resource) AsYAML() ([]byte, error) {
383-
json, err := r.MarshalJSON()
384-
if err != nil {
385-
return nil, err
386-
}
387-
return yaml.JSONToYAML(json)
383+
// Use kyaml's encoder directly to preserve original formatting
384+
// and avoid line wrapping issues with sigs.k8s.io/yaml.JSONToYAML.
385+
// See https://github.com/kubernetes-sigs/kustomize/issues/947
386+
return []byte(r.MustString()), nil
388387
}
389388

390-
// MustYaml returns YAML or panics.
389+
// MustYaml returns YAML or panics.
391390
func (r *Resource) MustYaml() string {
392391
yml, err := r.AsYAML()
393392
if err != nil {

0 commit comments

Comments
 (0)