fix kyaml issue where dropping Style created issues

dropping the node style creates a compatibility issue where quotes around "on" are dropped
because yaml.v3 interprets it as a string.

other yaml parsers interpret on as a bool value, and parse it as a bool rather than string.

fix: retain the original style so it is kept as quoted.

- fmt: don't drop the styles
- merge2: keep the style when merging elements
- setting a field: if changing the value of a scalar field, retain its style by default
This commit is contained in:
Phillip Wittrock
2019-12-19 15:05:49 -08:00
parent 7e56c2c768
commit 98431f6a00
13 changed files with 147 additions and 85 deletions

View File

@@ -90,7 +90,6 @@ type formatter struct {
// fmtNode recursively formats the Document Contents.
func (f *formatter) fmtNode(n *yaml.Node, path string) error {
n.Style = 0
// sort the order of mapping fields
if n.Kind == yaml.MappingNode {
sort.Sort(sortedMapContents(*n))

View File

@@ -17,6 +17,35 @@ import (
"sigs.k8s.io/kustomize/kyaml/kio/filters/testyaml"
)
func TestFormatInput_Style(t *testing.T) {
y := `
apiVersion: v1
kind: Foo
metadata:
name: foo
spec:
notBoolean: "true"
notBoolean2: "on"
isBoolean: on
isBoolean2: true
`
expected := `apiVersion: v1
kind: Foo
metadata:
name: foo
spec:
isBoolean: on
isBoolean2: true
notBoolean: "true"
notBoolean2: "on"
`
s, err := FormatInput(strings.NewReader(y))
assert.NoError(t, err)
assert.Equal(t, expected, s.String())
}
// TestFormatInput_configMap verifies a ConfigMap yaml is formatted correctly
func TestFormatInput_configMap(t *testing.T) {
y := `
@@ -229,7 +258,7 @@ webhooks:
- CONNECT
- CREATE
- UPDATE # this list is indented by 2
scope: Namespaced
scope: "Namespaced"
timeoutSeconds: 1
`
s, err := FormatInput(strings.NewReader(y))
@@ -467,7 +496,7 @@ func TestFormatFileOrDirectory_YamlExtFileWithJson(t *testing.T) {
// check the result is formatted as yaml
b, err := ioutil.ReadFile(f.Name())
assert.NoError(t, err)
assert.Equal(t, string(testyaml.FormattedYaml1), string(b))
assert.Equal(t, string(testyaml.FormattedJSON1), string(b))
}
// TestFormatFileOrDirectory_partialKubernetesYamlFile verifies that if a yaml file contains both

View File

@@ -55,3 +55,7 @@ status2:
- 1
- 2
`)
var FormattedJSON1 = []byte(`{"apiVersion": "example.com/v1beta1", "kind": "MyType", "spec": "a", "status": {"conditions": [
3, 1, 2]}}
`)