Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions api/krusty/stringquoteblank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ import (
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
)

// This test is for output string style.
// Currently all quotes will be removed if the string is valid as plain (unquoted) style.
// If a string cannot be unquoted, it will be put into a pair of single quotes.
// See https://yaml.org/spec/1.2/spec.html#id2788859 for more details about what kind of string
// is invalid as plain style.
// This test verifies that long lines are NOT wrapped in YAML output.
// See https://github.com/kubernetes-sigs/kustomize/issues/947
func TestLongLineBreaks(t *testing.T) {
th := kusttest_test.MakeHarness(t)
th.WriteF("deployment.yaml", `
Expand Down Expand Up @@ -59,27 +56,24 @@ spec:
template:
spec:
containers:
- env:
- name: mariadb
image: test
env:
- name: SHORT_STRING
value: short_string
- name: SHORT_STRING_WITH_SINGLE_QUOTE
value: short_string
value: 'short_string'
- name: SHORT_STRING_WITH_DOUBLE_QUOTE
value: short_string
value: "short_string"
- name: SHORT_STRING_BLANK
value: short string
- name: LONG_STRING_BLANK
value: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas
suscipit ex non molestie varius.
value: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas suscipit ex non molestie varius.
- name: LONG_STRING_BLANK_WITH_SINGLE_QUOTE
value: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas
suscipit ex non molestie varius.
value: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas suscipit ex non molestie varius.'
- name: LONG_STRING_BLANK_WITH_DOUBLE_QUOTE
value: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas
suscipit ex non molestie varius.
value: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas suscipit ex non molestie varius."
- name: INVALID_PLAIN_STYLE_STRING
value: ': test'
image: test
name: mariadb
`)
}
22 changes: 18 additions & 4 deletions api/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package resource

import (
"encoding/json"
"fmt"
"log"
"strings"
Expand Down Expand Up @@ -380,11 +381,24 @@ func (r *Resource) String() string {
// AsYAML returns the resource in Yaml form.
// Easier to read than JSON.
func (r *Resource) AsYAML() ([]byte, error) {
json, err := r.MarshalJSON()
// Check for duplicate keys by attempting to decode to map and marshal to JSON.
// This detects duplicate keys without the overhead of MarshalJSON() which
// calls String() internally, avoiding double serialization.
m, err := r.Map()
if err != nil {
return nil, err
}
return yaml.JSONToYAML(json)
// Add context about what failed and why to help with debugging
id := r.CurId()
return nil, fmt.Errorf("failed to convert resource %s to YAML: duplicate key detection failed: %w", id, err)
}
// Verify the map can be marshaled to JSON (catches any remaining duplicate key issues)
if _, err := json.Marshal(m); err != nil {
id := r.CurId()
return nil, fmt.Errorf("failed to convert resource %s to YAML: duplicate key detection failed: %w", id, err)
}
// Use kyaml's encoder directly to preserve original formatting
// and avoid line wrapping issues with sigs.k8s.io/yaml.JSONToYAML.
// See https://github.com/kubernetes-sigs/kustomize/issues/947
return []byte(r.MustString()), nil
}

// MustYaml returns YAML or panics.
Expand Down
Loading