diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index e3fcb585b..cddc5f220 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -223,10 +223,6 @@ func (r *ByteReader) Read() ([]*yaml.RNode, error) { } if err != nil { - if r.WrapBareSeqNode { - // continue reading other resources if failed to parse a resource - continue - } return nil, errors.Wrap(err) } if yaml.IsMissingOrNull(node) { @@ -294,14 +290,16 @@ func (r *ByteReader) decode(originalYAML string, index int, decoder *yaml.Decode // sort the annotations by key so the output Resources is consistent (otherwise the // annotations will be in a random order) n := yaml.NewRNode(node) - wrappedNode := yaml.NewRNode(&yaml.Node{ - Kind: yaml.MappingNode, - }) - if r.WrapBareSeqNode && node.Kind == yaml.DocumentNode && node.Content[0].Kind == yaml.SequenceNode { + // check if it is a bare sequence node and wrap it with a yaml.BareSeqNodeWrappingKey + if r.WrapBareSeqNode && node.Kind == yaml.DocumentNode && len(node.Content) > 0 && + node.Content[0] != nil && node.Content[0].Kind == yaml.SequenceNode { + wrappedNode := yaml.NewRNode(&yaml.Node{ + Kind: yaml.MappingNode, + }) wrappedNode.PipeE(yaml.SetField(yaml.BareSeqNodeWrappingKey, n)) - } else { - wrappedNode = n + n = wrappedNode } + if r.SetAnnotations == nil { r.SetAnnotations = map[string]string{} } @@ -322,10 +320,10 @@ func (r *ByteReader) decode(originalYAML string, index int, decoder *yaml.Decode } sort.Strings(keys) for _, k := range keys { - _, err = wrappedNode.Pipe(yaml.SetAnnotation(k, r.SetAnnotations[k])) + _, err = n.Pipe(yaml.SetAnnotation(k, r.SetAnnotations[k])) if err != nil { return nil, errors.Wrap(err) } } - return wrappedNode, nil + return n, nil } diff --git a/kyaml/kio/byteio_readwriter_test.go b/kyaml/kio/byteio_readwriter_test.go index 5eba929e8..86508817d 100644 --- a/kyaml/kio/byteio_readwriter_test.go +++ b/kyaml/kio/byteio_readwriter_test.go @@ -664,7 +664,7 @@ func TestByteReadWriter_WrapBareSeqNode(t *testing.T) { readerErr: "wrong Node Kind for expected: MappingNode was SequenceNode", }, { - name: "error k fround_trip bare seq node", + name: "error round_trip bare seq node", wrapBareSeqNode: false, input: `# Use the old CRD because of the quantity validation issue: # https://github.com/kubeflow/kubeflow/issues/5722 @@ -692,6 +692,12 @@ func TestByteReadWriter_WrapBareSeqNode(t *testing.T) { input: `[{"op": "add", "path": "/spec/template/spec/containers/0/args/-", "value": "--namespaced"}]`, expectedOutput: `[{"op": "add", "path": "/spec/template/spec/containers/0/args/-", "value": "--namespaced"}]`, }, + { + name: "error round_trip invalid yaml node", + wrapBareSeqNode: false, + input: "I am not valid", + readerErr: "wrong Node Kind for expected: MappingNode was ScalarNode", + }, } for i := range testCases {