add option to choose prepend/append in merge

This commit is contained in:
Donny Xia
2020-09-28 16:17:50 -07:00
parent fafe24c9df
commit 74d0d7960e
35 changed files with 592 additions and 176 deletions

View File

@@ -3,6 +3,10 @@
package merge2_test
import (
"sigs.k8s.io/kustomize/kyaml/yaml"
)
var elementTestCases = []testCase{
{description: `merge Element -- keep field in dest`,
source: `
@@ -37,6 +41,9 @@ spec:
image: foo:v1
command: ['run.sh']
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge Element -- add field to dest`,
@@ -72,6 +79,9 @@ spec:
image: foo:v1
command: ['run.sh']
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge Element -- add list, empty in dest`,
@@ -105,6 +115,9 @@ spec:
image: foo:v1
command: ['run.sh']
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge Element -- add list, missing from dest`,
@@ -134,6 +147,9 @@ spec:
image: foo:v1
command: ['run.sh']
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge Element -- add Element first`,
@@ -173,6 +189,9 @@ spec:
image: bar:v1
command: ['run2.sh']
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge Element -- add Element second`,
@@ -212,7 +231,94 @@ spec:
image: bar:v1
command: ['run2.sh']
`,
},
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge Element -- add Element third`,
source: `
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: bar
image: bar:v1
command: ['run2.sh']
- name: foo
image: foo:v1
`,
dest: `
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: foo
image: foo:v0
`,
expected: `
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: bar
image: bar:v1
command: ['run2.sh']
- name: foo
image: foo:v1
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListPrepend,
},
},
{description: `merge Element -- add Element fourth`,
source: `
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
`,
dest: `
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: foo
image: foo:v0
`,
expected: `
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: foo
image: foo:v1
- name: bar
image: bar:v1
command: ['run2.sh']
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListPrepend,
},
},
//
// Test Case
@@ -248,6 +354,9 @@ spec:
image: bar:v1
command: ['run2.sh']
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -290,6 +399,9 @@ spec:
image: bar:v1
command: ['run2.sh']
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -330,6 +442,9 @@ spec:
image: bar:v1
command: ['run2.sh']
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -364,6 +479,9 @@ spec:
template:
spec: {}
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -393,6 +511,9 @@ containers:
command: ['run2.sh']
`,
infer: true,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -431,6 +552,9 @@ spec:
command: ['run2.sh']
`,
infer: false,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -460,6 +584,9 @@ containers: # {"items":{"$ref": "#/definitions/io.k8s.api.core.v1.Container"},"t
command: ['run2.sh']
`,
infer: false,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge_primitive_finalizers`,
@@ -488,6 +615,9 @@ metadata:
- c
- a
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge_primitive_items`,
@@ -513,5 +643,8 @@ items:
- c
- a
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
}

View File

@@ -3,6 +3,10 @@
package merge2_test
import (
"sigs.k8s.io/kustomize/kyaml/yaml"
)
var listTestCases = []testCase{
{description: `strategic merge patch delete 1`,
source: `
@@ -38,6 +42,9 @@ spec:
- name: foo2
- name: foo3
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `strategic merge patch delete 2`,
source: `
@@ -73,8 +80,48 @@ spec:
- name: foo1
- name: foo2
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge k8s deployment containers`,
{description: `merge k8s deployment containers - prepend`,
source: `
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: foo1
- name: foo2
- name: foo3
`,
dest: `
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: foo0
`,
expected: `
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: foo1
- name: foo2
- name: foo3
- name: foo0
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListPrepend,
},
},
{description: `merge k8s deployment containers - append`,
source: `
apiVersion: apps/v1
kind: Deployment
@@ -107,8 +154,11 @@ spec:
- name: foo2
- name: foo3
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge k8s deployment volumes`,
{description: `merge k8s deployment volumes - append`,
source: `
apiVersion: apps/v1
kind: Deployment
@@ -141,6 +191,46 @@ spec:
- name: foo2
- name: foo3
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge k8s deployment volumes - prepend`,
source: `
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
volumes:
- name: foo1
- name: foo2
- name: foo3
`,
dest: `
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
volumes:
- name: foo0
`,
expected: `
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
volumes:
- name: foo1
- name: foo2
- name: foo3
- name: foo0
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListPrepend,
},
},
{description: `merge k8s deployment containers -- $patch directive`,
source: `
@@ -267,6 +357,9 @@ items:
- 2
- 3
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `replace List -- missing from dest`,
@@ -287,6 +380,9 @@ items:
- 2
- 3
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -314,6 +410,9 @@ items:
- 2
- 3
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -337,6 +436,9 @@ items:
- 2
- 3
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -357,6 +459,9 @@ items:
expected: `
kind: Deployment
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -378,5 +483,8 @@ items:
kind: Deployment
items: []
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
}

View File

@@ -3,6 +3,10 @@
package merge2_test
import (
"sigs.k8s.io/kustomize/kyaml/yaml"
)
var mapTestCases = []testCase{
{description: `strategic merge patch delete 1`,
@@ -16,6 +20,9 @@ spec:
foo: bar1
`,
expected: ``,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `strategic merge patch delete 2`,
@@ -33,6 +40,9 @@ spec:
expected: `
kind: Deployment
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `strategic merge patch delete 3`,
@@ -61,6 +71,9 @@ spec:
metadata:
name: wut
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `strategic merge patch delete 4`,
@@ -89,6 +102,9 @@ spec:
`,
expected: `
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `strategic merge patch replace 1`,
@@ -111,6 +127,9 @@ spec:
metal: heavy
veggie: carrot
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge Map -- update field in dest`,
@@ -131,6 +150,9 @@ spec:
foo: bar1
baz: buz
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge Map -- add field to dest`,
@@ -151,6 +173,9 @@ spec:
foo: bar1
baz: buz
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge Map -- add list, empty in dest`,
@@ -170,6 +195,9 @@ spec:
baz: buz
foo: bar1
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge Map -- add list, missing from dest`,
@@ -188,6 +216,9 @@ spec:
foo: bar1
baz: buz
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge Map -- add Map first`,
@@ -208,6 +239,9 @@ spec:
foo: bar1
baz: buz
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `merge Map -- add Map second`,
@@ -228,6 +262,9 @@ spec:
foo: bar1
baz: buz
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -249,6 +286,9 @@ spec:
foo: bar1
baz: buz
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -272,6 +312,9 @@ spec:
baz: buz
items: {}
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -291,5 +334,8 @@ spec:
expected: `
kind: Deployment
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
}

View File

@@ -12,12 +12,16 @@ import (
)
// Merge merges fields from src into dest.
func Merge(src, dest *yaml.RNode) (*yaml.RNode, error) {
return walk.Walker{Sources: []*yaml.RNode{dest, src}, Visitor: Merger{}}.Walk()
func Merge(src, dest *yaml.RNode, mergeOptions yaml.MergeOptions) (*yaml.RNode, error) {
return walk.Walker{
Sources: []*yaml.RNode{dest, src},
Visitor: Merger{},
MergeOptions: mergeOptions,
}.Walk()
}
// Merge parses the arguments, and merges fields from srcStr into destStr.
func MergeStrings(srcStr, destStr string, infer bool) (string, error) {
func MergeStrings(srcStr, destStr string, infer bool, mergeOptions yaml.MergeOptions) (string, error) {
src, err := yaml.Parse(srcStr)
if err != nil {
return "", err
@@ -31,6 +35,7 @@ func MergeStrings(srcStr, destStr string, infer bool) (string, error) {
Sources: []*yaml.RNode{dest, src},
Visitor: Merger{},
InferAssociativeLists: infer,
MergeOptions: mergeOptions,
}.Walk()
if err != nil {
return "", err

View File

@@ -55,7 +55,9 @@ metadata:
m: n1
`)
result, err := Merge(src, dest)
result, err := Merge(src, dest, yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
})
if !assert.NoError(t, err) {
return
}
@@ -114,7 +116,9 @@ metadata:
annotations: null
`)
result, err := Merge(src, dest)
result, err := Merge(src, dest, yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
})
if !assert.NoError(t, err) {
return
}
@@ -174,7 +178,9 @@ metadata:
m: n1
`)
result, err := Merge(dest, src)
result, err := Merge(dest, src, yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
})
if !assert.NoError(t, err) {
return
}
@@ -241,7 +247,9 @@ spec:
value: "Another Env Not In The Dest"
`)
result, err := Merge(src, dest)
result, err := Merge(src, dest, yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
})
if !assert.NoError(t, err) {
return
}
@@ -305,7 +313,9 @@ spec:
args: ['e', 'd', 'f']
`)
result, err := Merge(src, dest)
result, err := Merge(src, dest, yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
})
if !assert.NoError(t, err) {
return
}
@@ -365,7 +375,9 @@ a:
b:
# header comment
c: d
`, true)
`, true, yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
})
if !assert.NoError(t, err) {
return
}
@@ -385,7 +397,9 @@ a:
b:
c: d
# footer comment
`, true)
`, true, yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
})
if !assert.NoError(t, err) {
return
}
@@ -404,7 +418,9 @@ a:
a:
b:
c: d # line comment
`, true)
`, true, yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
})
if !assert.NoError(t, err) {
return
}
@@ -426,7 +442,9 @@ a:
b:
# replace comment
c: d
`, true)
`, true, yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
})
if !assert.NoError(t, err) {
return
}
@@ -447,7 +465,9 @@ a:
b:
c: d
# replace comment
`, true)
`, true, yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
})
if !assert.NoError(t, err) {
return
}
@@ -466,7 +486,9 @@ a:
a:
b:
c: d # replace comment
`, true)
`, true, yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
})
if !assert.NoError(t, err) {
return
}
@@ -484,7 +506,9 @@ a:
a:
b:
c: d # replace comment
`, true)
`, true, yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
})
if !assert.NoError(t, err) {
return
}

View File

@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/assert"
"sigs.k8s.io/kustomize/kyaml/kio/filters"
"sigs.k8s.io/kustomize/kyaml/yaml"
. "sigs.k8s.io/kustomize/kyaml/yaml/merge2"
)
@@ -20,7 +21,7 @@ func TestMerge(t *testing.T) {
for j := range testCases[i] {
tc := testCases[i][j]
t.Run(tc.description, func(t *testing.T) {
actual, err := MergeStrings(tc.source, tc.dest, tc.infer)
actual, err := MergeStrings(tc.source, tc.dest, tc.infer, tc.mergeOptions)
if !assert.NoError(t, err, tc.description) {
t.FailNow()
}
@@ -43,9 +44,10 @@ func TestMerge(t *testing.T) {
}
type testCase struct {
description string
source string
dest string
expected string
infer bool
description string
source string
dest string
expected string
infer bool
mergeOptions yaml.MergeOptions
}

View File

@@ -3,6 +3,10 @@
package merge2_test
import (
"sigs.k8s.io/kustomize/kyaml/yaml"
)
var scalarTestCases = []testCase{
{description: `replace scalar -- different value in dest`,
source: `
@@ -17,6 +21,9 @@ field: value0
kind: Deployment
field: value1
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
{description: `replace scalar -- missing from dest`,
@@ -31,6 +38,9 @@ kind: Deployment
kind: Deployment
field: value1
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -49,6 +59,9 @@ field: value1
kind: Deployment
field: value1
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -66,6 +79,9 @@ field: value1
kind: Deployment
field: value1
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -83,6 +99,9 @@ field: value1
expected: `
kind: Deployment
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -100,6 +119,9 @@ field: value1
expected: `
kind: Deployment
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -116,6 +138,9 @@ kind: Deployment
expected: `
kind: Deployment
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
//
@@ -133,5 +158,8 @@ kind: Deployment
kind: Deployment
field: {}
`,
mergeOptions: yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListAppend,
},
},
}