Skip to content

Commit cabbea0

Browse files
author
Mengqi Yu
committed
handle comments in list correctly
1 parent 732a852 commit cabbea0

4 files changed

Lines changed: 56 additions & 38 deletions

File tree

kyaml/comments/comments.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ func (c *copier) VisitList(s walk.Sources, _ *openapi.ResourceSchema, _ walk.Lis
5454
origin := originItems[i]
5555

5656
if dest.Value == origin.Value {
57-
copy(yaml.NewRNode(dest), yaml.NewRNode(origin))
57+
// We should do it recursively on each node in the list.
58+
if err := CopyComments(yaml.NewRNode(dest), yaml.NewRNode(origin)); err != nil {
59+
return nil, err
60+
}
5861
}
5962
}
6063

kyaml/comments/comments_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,48 @@ spec:
8585
`,
8686
},
8787

88+
{
89+
name: "associative_list_2",
90+
from: `
91+
apiVersion: constraints.gatekeeper.sh/v1beta1
92+
kind: EnforceFoo
93+
metadata:
94+
name: enforce-foo
95+
spec:
96+
parameters:
97+
naming_rules:
98+
- kind: Bar
99+
patterns:
100+
# comment 1
101+
- ^(dev|prod|staging|qa|shared)$
102+
`,
103+
to: `
104+
apiVersion: constraints.gatekeeper.sh/v1beta1
105+
kind: EnforceFoo
106+
metadata:
107+
name: enforce-foo
108+
spec:
109+
parameters:
110+
naming_rules:
111+
- kind: Bar
112+
patterns:
113+
- ^(dev|prod|staging|qa|shared)$
114+
`,
115+
expected: `
116+
apiVersion: constraints.gatekeeper.sh/v1beta1
117+
kind: EnforceFoo
118+
metadata:
119+
name: enforce-foo
120+
spec:
121+
parameters:
122+
naming_rules:
123+
- kind: Bar
124+
patterns:
125+
# comment 1
126+
- ^(dev|prod|staging|qa|shared)$
127+
`,
128+
},
129+
88130
{
89131
name: "keep_comments",
90132
from: `# A

kyaml/kio/byteio_writer.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ type ByteWriter struct {
5050

5151
var _ Writer = ByteWriter{}
5252

53-
func (w ByteWriter) Write(nodes []*yaml.RNode) error {
53+
func (w ByteWriter) Write(inputNodes []*yaml.RNode) error {
54+
// Copy the nodes to prevent writer from mutating the original nodes.
55+
var nodes []*yaml.RNode
56+
for i := range inputNodes {
57+
nodes = append(nodes, inputNodes[i].Copy())
58+
}
5459
yaml.DoSerializationHacksOnNodes(nodes)
5560
if w.Sort {
5661
if err := kioutil.SortNodes(nodes); err != nil {
@@ -131,7 +136,6 @@ func (w ByteWriter) Write(nodes []*yaml.RNode) error {
131136
items.Content = append(items.Content, nodes[i].YNode())
132137
}
133138
err := encoder.Encode(doc)
134-
yaml.UndoSerializationHacksOnNodes(nodes)
135139
return err
136140
}
137141

kyaml/yaml/serialization.go

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,43 +34,12 @@ func DoSerializationHacks(node *yaml.Node) {
3434
// https://github.com/go-yaml/yaml/issues/587 in go-yaml.v3
3535
// Remove this hack when the issue has been resolved
3636
if len(node.Content) > 0 && node.Content[0].Kind == ScalarNode {
37-
node.HeadComment = node.Content[0].HeadComment
37+
// Don't clobber the head comment if it's not empty.
38+
if node.HeadComment == "" && node.Content[0].HeadComment != "" {
39+
node.HeadComment = node.Content[0].HeadComment
40+
}
3841
node.Content[0].HeadComment = ""
3942
}
4043
}
4144
}
4245
}
43-
44-
func UndoSerializationHacksOnNodes(nodes []*RNode) {
45-
for _, node := range nodes {
46-
UndoSerializationHacks(node.YNode())
47-
}
48-
}
49-
50-
// UndoSerializationHacks reverts the changes made by DoSerializationHacks
51-
// Refer to https://github.com/go-yaml/yaml/issues/587 for more details
52-
func UndoSerializationHacks(node *yaml.Node) {
53-
switch node.Kind {
54-
case DocumentNode:
55-
for _, node := range node.Content {
56-
DoSerializationHacks(node)
57-
}
58-
59-
case MappingNode:
60-
for _, node := range node.Content {
61-
DoSerializationHacks(node)
62-
}
63-
64-
case SequenceNode:
65-
for _, node := range node.Content {
66-
// revert the changes made in DoSerializationHacks
67-
// This is necessary to address serialization issue
68-
// https://github.com/go-yaml/yaml/issues/587 in go-yaml.v3
69-
// Remove this hack when the issue has been resolved
70-
if len(node.Content) > 0 && node.Content[0].Kind == ScalarNode {
71-
node.Content[0].HeadComment = node.HeadComment
72-
node.HeadComment = ""
73-
}
74-
}
75-
}
76-
}

0 commit comments

Comments
 (0)