mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 00:52:55 +00:00
Clean up bare sequence node wrapping
This commit is contained in:
@@ -223,10 +223,6 @@ func (r *ByteReader) Read() ([]*yaml.RNode, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if r.WrapBareSeqNode {
|
|
||||||
// continue reading other resources if failed to parse a resource
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return nil, errors.Wrap(err)
|
return nil, errors.Wrap(err)
|
||||||
}
|
}
|
||||||
if yaml.IsMissingOrNull(node) {
|
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
|
// sort the annotations by key so the output Resources is consistent (otherwise the
|
||||||
// annotations will be in a random order)
|
// annotations will be in a random order)
|
||||||
n := yaml.NewRNode(node)
|
n := yaml.NewRNode(node)
|
||||||
wrappedNode := yaml.NewRNode(&yaml.Node{
|
// check if it is a bare sequence node and wrap it with a yaml.BareSeqNodeWrappingKey
|
||||||
Kind: yaml.MappingNode,
|
if r.WrapBareSeqNode && node.Kind == yaml.DocumentNode && len(node.Content) > 0 &&
|
||||||
})
|
node.Content[0] != nil && node.Content[0].Kind == yaml.SequenceNode {
|
||||||
if r.WrapBareSeqNode && node.Kind == yaml.DocumentNode && node.Content[0].Kind == yaml.SequenceNode {
|
wrappedNode := yaml.NewRNode(&yaml.Node{
|
||||||
|
Kind: yaml.MappingNode,
|
||||||
|
})
|
||||||
wrappedNode.PipeE(yaml.SetField(yaml.BareSeqNodeWrappingKey, n))
|
wrappedNode.PipeE(yaml.SetField(yaml.BareSeqNodeWrappingKey, n))
|
||||||
} else {
|
n = wrappedNode
|
||||||
wrappedNode = n
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.SetAnnotations == nil {
|
if r.SetAnnotations == nil {
|
||||||
r.SetAnnotations = map[string]string{}
|
r.SetAnnotations = map[string]string{}
|
||||||
}
|
}
|
||||||
@@ -322,10 +320,10 @@ func (r *ByteReader) decode(originalYAML string, index int, decoder *yaml.Decode
|
|||||||
}
|
}
|
||||||
sort.Strings(keys)
|
sort.Strings(keys)
|
||||||
for _, k := range 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 {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err)
|
return nil, errors.Wrap(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return wrappedNode, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -664,7 +664,7 @@ func TestByteReadWriter_WrapBareSeqNode(t *testing.T) {
|
|||||||
readerErr: "wrong Node Kind for expected: MappingNode was SequenceNode",
|
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,
|
wrapBareSeqNode: false,
|
||||||
input: `# Use the old CRD because of the quantity validation issue:
|
input: `# Use the old CRD because of the quantity validation issue:
|
||||||
# https://github.com/kubeflow/kubeflow/issues/5722
|
# 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"}]`,
|
input: `[{"op": "add", "path": "/spec/template/spec/containers/0/args/-", "value": "--namespaced"}]`,
|
||||||
expectedOutput: `[{"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 {
|
for i := range testCases {
|
||||||
|
|||||||
Reference in New Issue
Block a user