mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 10:30:59 +00:00
Add YAML anchor/alias expansion.
This commit is contained in:
@@ -808,15 +808,13 @@ items:
|
||||
}
|
||||
}
|
||||
|
||||
// This test is just an exploration of the low level (go-yaml)
|
||||
// representation of a small doc with an anchor. The anchor
|
||||
// structure is there, in the sense that an alias pointer is
|
||||
// readily available when a node's kind is an AliasNode.
|
||||
// That is, the anchor mapping has already been recognized.
|
||||
// However, the github.com/go-yaml/yaml/encoder.go code doesn't
|
||||
// appear to have an option to perform anchor replacements when
|
||||
// encoding (instead it emits the anchor definitions and
|
||||
// references, which is not a bad thing but not desired here).
|
||||
// This test shows the lower level (go-yaml) representation of a small doc
|
||||
// with an anchor. The anchor structure is there, in the sense that an
|
||||
// alias pointer is readily available when a node's kind is an AliasNode.
|
||||
// I.e. the anchor mapping name -> object was noted during unmarshalling.
|
||||
// However, at the time of writing github.com/go-yaml/yaml/encoder.go
|
||||
// doesn't appear to have an option to perform anchor replacements when
|
||||
// encoding. It emits anchor definitions and references (aliases) intact.
|
||||
func TestByteReader_AnchorBehavior(t *testing.T) {
|
||||
const input = `
|
||||
data:
|
||||
|
||||
@@ -903,6 +903,48 @@ func (rn *RNode) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeAnchor inflates all YAML aliases with their anchor values.
|
||||
// All YAML anchor data is permanently removed (feel free to call Copy first).
|
||||
func (rn *RNode) DeAnchor() (err error) {
|
||||
rn.value, err = deAnchor(rn.value)
|
||||
return
|
||||
}
|
||||
|
||||
// deAnchor removes all AliasNodes from the yaml.Node's tree, replacing
|
||||
// them with what they point to. All Anchor fields (these are used to mark
|
||||
// anchor definitions) are cleared.
|
||||
func deAnchor(yn *yaml.Node) (res *yaml.Node, err error) {
|
||||
if yn == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if yn.Anchor != "" {
|
||||
// This node defines an anchor. Clear the field so that it
|
||||
// doesn't show up when marshalling.
|
||||
if yn.Kind == yaml.AliasNode {
|
||||
// Maybe this is OK, but for now treating it as a bug.
|
||||
return nil, fmt.Errorf(
|
||||
"anchor %q defined using alias %v", yn.Anchor, yn.Alias)
|
||||
}
|
||||
yn.Anchor = ""
|
||||
}
|
||||
switch yn.Kind {
|
||||
case yaml.ScalarNode:
|
||||
return yn, nil
|
||||
case yaml.AliasNode:
|
||||
return deAnchor(yn.Alias)
|
||||
case yaml.DocumentNode, yaml.MappingNode, yaml.SequenceNode:
|
||||
for i := range yn.Content {
|
||||
yn.Content[i], err = deAnchor(yn.Content[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return yn, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("cannot deAnchor kind %q", yn.Kind)
|
||||
}
|
||||
}
|
||||
|
||||
// GetValidatedMetadata returns metadata after subjecting it to some tests.
|
||||
func (rn *RNode) GetValidatedMetadata() (ResourceMeta, error) {
|
||||
m, err := rn.GetMeta()
|
||||
|
||||
@@ -696,6 +696,31 @@ spec:
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeAnchor(t *testing.T) {
|
||||
rn, err := Parse(`
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: wildcard
|
||||
data:
|
||||
color: &color-used blue
|
||||
feeling: *color-used
|
||||
`)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, rn.DeAnchor())
|
||||
actual, err := rn.String()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(`
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: wildcard
|
||||
data:
|
||||
color: blue
|
||||
feeling: blue
|
||||
`), strings.TrimSpace(actual))
|
||||
}
|
||||
|
||||
func TestRNode_UnmarshalJSON(t *testing.T) {
|
||||
testCases := []struct {
|
||||
testName string
|
||||
|
||||
Reference in New Issue
Block a user