mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
Add YAML anchor/alias expansion.
This commit is contained in:
@@ -214,6 +214,35 @@ type ResMap interface {
|
||||
// namespaces. Cluster wide objects are never excluded.
|
||||
SubsetThatCouldBeReferencedByResource(*resource.Resource) ResMap
|
||||
|
||||
// DeAnchor replaces YAML aliases with structured data copied from anchors.
|
||||
// This cannot be undone; if desired, call DeepCopy first.
|
||||
// Subsequent marshalling to YAML will no longer have anchor
|
||||
// definitions ('&') or aliases ('*').
|
||||
//
|
||||
// Anchors are not expected to work across YAML 'documents'.
|
||||
// If three resources are loaded from one file containing three YAML docs:
|
||||
//
|
||||
// {resourceA}
|
||||
// ---
|
||||
// {resourceB}
|
||||
// ---
|
||||
// {resourceC}
|
||||
//
|
||||
// then anchors defined in A cannot be seen from B and C and vice versa.
|
||||
// OTOH, cross-resource links (a field in B referencing fields in A) will
|
||||
// work if the resources are gathered in a ResourceList:
|
||||
//
|
||||
// apiVersion: config.kubernetes.io/v1
|
||||
// kind: ResourceList
|
||||
// metadata:
|
||||
// name: someList
|
||||
// items:
|
||||
// - {resourceA}
|
||||
// - {resourceB}
|
||||
// - {resourceC}
|
||||
//
|
||||
DeAnchor() error
|
||||
|
||||
// DeepCopy copies the ResMap and underlying resources.
|
||||
DeepCopy() ResMap
|
||||
|
||||
|
||||
@@ -607,6 +607,16 @@ func (m *resWrangler) ToRNodeSlice() []*kyaml.RNode {
|
||||
return result
|
||||
}
|
||||
|
||||
// DeAnchor implements ResMap.
|
||||
func (m *resWrangler) DeAnchor() (err error) {
|
||||
for i := range m.rList {
|
||||
if err = m.rList[i].DeAnchor(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ApplySmPatch applies the patch, and errors on Id collisions.
|
||||
func (m *resWrangler) ApplySmPatch(
|
||||
selectedSet *resource.IdSet, patch *resource.Resource) error {
|
||||
|
||||
@@ -902,6 +902,100 @@ rules:
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeAnchorSingleDoc(t *testing.T) {
|
||||
input := `apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: wildcard
|
||||
data:
|
||||
color: &color-used blue
|
||||
feeling: *color-used
|
||||
`
|
||||
rm, err := rmF.NewResMapFromBytes([]byte(input))
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, rm.DeAnchor())
|
||||
yaml, err := rm.AsYaml()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(`
|
||||
apiVersion: v1
|
||||
data:
|
||||
color: blue
|
||||
feeling: blue
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: wildcard
|
||||
`), strings.TrimSpace(string(yaml)))
|
||||
}
|
||||
|
||||
// Anchor references don't cross YAML document boundaries.
|
||||
func TestDeAnchorMultiDoc(t *testing.T) {
|
||||
input := `apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: betty
|
||||
data:
|
||||
color: &color-used blue
|
||||
feeling: *color-used
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: bob
|
||||
data:
|
||||
color: red
|
||||
feeling: *color-used
|
||||
`
|
||||
_, err := rmF.NewResMapFromBytes([]byte(input))
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "unknown anchor 'color-used' referenced")
|
||||
}
|
||||
|
||||
// Anchor references cross list elements in a ResourceList.
|
||||
func TestDeAnchorResourceList(t *testing.T) {
|
||||
input := `apiVersion: config.kubernetes.io/v1
|
||||
kind: ResourceList
|
||||
metadata:
|
||||
name: aShortList
|
||||
items:
|
||||
- apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: betty
|
||||
data:
|
||||
color: &color-used blue
|
||||
feeling: *color-used
|
||||
- apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: bob
|
||||
data:
|
||||
color: red
|
||||
feeling: *color-used
|
||||
`
|
||||
rm, err := rmF.NewResMapFromBytes([]byte(input))
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, rm.DeAnchor())
|
||||
yaml, err := rm.AsYaml()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(`
|
||||
apiVersion: v1
|
||||
data:
|
||||
color: blue
|
||||
feeling: blue
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: betty
|
||||
---
|
||||
apiVersion: v1
|
||||
data:
|
||||
color: red
|
||||
feeling: blue
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: bob
|
||||
`), strings.TrimSpace(string(yaml)))
|
||||
}
|
||||
|
||||
func TestApplySmPatch_General(t *testing.T) {
|
||||
const (
|
||||
myDeployment = "Deployment"
|
||||
|
||||
Reference in New Issue
Block a user