Skip to content

Commit

Permalink
Merge pull request #4193 from phanimarupaka/AddressBareSeqNodeComments
Browse files Browse the repository at this point in the history
Clean up bare sequence node wrapping
  • Loading branch information
k8s-ci-robot authored Sep 20, 2021
2 parents 1cb9312 + 1d9b6cb commit c47fc48
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
22 changes: 10 additions & 12 deletions kyaml/kio/byteio_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{}
}
Expand All @@ -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
}
8 changes: 7 additions & 1 deletion kyaml/kio/byteio_readwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit c47fc48

Please sign in to comment.