keep \n in the end of resource yaml

This commit is contained in:
Donny Xia
2021-01-08 15:36:04 -08:00
parent 6f2f401f6b
commit 378eaedc82
2 changed files with 42 additions and 0 deletions

View File

@@ -440,3 +440,40 @@ metadata:
name: cm-o2-5k95kd76ft name: cm-o2-5k95kd76ft
`) `)
} }
func TestConfigMapGeneratorLiteralNewline(t *testing.T) {
th := kusttest_test.MakeHarness(t)
th.WriteK("/app", `
generators:
- configmaps.yaml
`)
th.WriteF("/app/configmaps.yaml", `
apiVersion: builtin
kind: ConfigMapGenerator
metadata:
name: testing
literals:
- |
initial.txt=greetings
everyone
- |
final.txt=different
behavior
---
`)
m := th.Run("/app", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(
m, `
apiVersion: v1
data:
final.txt: |
different
behavior
initial.txt: |
greetings
everyone
kind: ConfigMap
metadata:
name: testing-tt4769fb52
`)
}

View File

@@ -149,6 +149,11 @@ func (r *ByteReader) Read() ([]*yaml.RNode, error) {
index := 0 index := 0
for i := range values { for i := range values {
// the Split used above will eat the tail '\n' from each resource. This may affect the
// literal string value since '\n' is meaningful in it.
if i != len(values)-1 {
values[i] += "\n"
}
decoder := yaml.NewDecoder(bytes.NewBufferString(values[i])) decoder := yaml.NewDecoder(bytes.NewBufferString(values[i]))
node, err := r.decode(index, decoder) node, err := r.decode(index, decoder)
if err == io.EOF { if err == io.EOF {