Use OpenAPI when merging (3way) resources

- When merging (3way) resources use the patch strategy from the openAPI if the definition exists for the field
- Allow disabling of guessing patch strategy merge keys when no definition exists
- Support defining strategy and key directly on configuration fields through line and header coments
- Support attaching schema to parent fields of lists, and propagating -- e.g. that a field is a PodTemplate
This commit is contained in:
Phillip Wittrock
2020-02-27 10:08:40 -08:00
parent 8991b193c6
commit 5d1a0346b5
24 changed files with 991 additions and 507 deletions

View File

@@ -4,21 +4,21 @@
package merge2_test
var elementTestCases = []testCase{
{`merge Element -- keep field in dest`,
`
{description: `merge Element -- keep field in dest`,
source: `
kind: Deployment
items:
- name: foo
image: foo:v1
`,
`
dest: `
kind: Deployment
items:
- name: foo
image: foo:v0
command: ['run.sh']
`,
`
expected: `
kind: Deployment
items:
- name: foo
@@ -27,21 +27,21 @@ items:
`,
},
{`merge Element -- add field to dest`,
`
{description: `merge Element -- add field to dest`,
source: `
kind: Deployment
items:
- name: foo
image: foo:v1
command: ['run.sh']
`,
`
dest: `
kind: Deployment
items:
- name: foo
image: foo:v0
`,
`
expected: `
kind: Deployment
items:
- name: foo
@@ -50,19 +50,19 @@ items:
`,
},
{`merge Element -- add list, empty in dest`,
`
{description: `merge Element -- add list, empty in dest`,
source: `
kind: Deployment
items:
- name: foo
image: foo:v1
command: ['run.sh']
`,
`
dest: `
kind: Deployment
items: []
`,
`
expected: `
kind: Deployment
items:
- name: foo
@@ -71,18 +71,18 @@ items:
`,
},
{`merge Element -- add list, missing from dest`,
`
{description: `merge Element -- add list, missing from dest`,
source: `
kind: Deployment
items:
- name: foo
image: foo:v1
command: ['run.sh']
`,
`
dest: `
kind: Deployment
`,
`
expected: `
kind: Deployment
items:
- name: foo
@@ -91,8 +91,8 @@ items:
`,
},
{`merge Element -- add Element first`,
`
{description: `merge Element -- add Element first`,
source: `
kind: Deployment
items:
- name: bar
@@ -101,13 +101,13 @@ items:
- name: foo
image: foo:v1
`,
`
dest: `
kind: Deployment
items:
- name: foo
image: foo:v0
`,
`
expected: `
kind: Deployment
items:
- name: foo
@@ -118,8 +118,8 @@ items:
`,
},
{`merge Element -- add Element second`,
`
{description: `merge Element -- add Element second`,
source: `
kind: Deployment
items:
- name: foo
@@ -128,13 +128,13 @@ items:
image: bar:v1
command: ['run2.sh']
`,
`
dest: `
kind: Deployment
items:
- name: foo
image: foo:v0
`,
`
expected: `
kind: Deployment
items:
- name: foo
@@ -148,11 +148,11 @@ items:
//
// Test Case
//
{`keep list -- list missing from src`,
`
{description: `keep list -- list missing from src`,
source: `
kind: Deployment
`,
`
dest: `
kind: Deployment
items:
- name: foo
@@ -161,7 +161,7 @@ items:
image: bar:v1
command: ['run2.sh']
`,
`
expected: `
kind: Deployment
items:
- name: foo
@@ -175,14 +175,14 @@ items:
//
// Test Case
//
{`keep Element -- element missing in src`,
`
{description: `keep Element -- element missing in src`,
source: `
kind: Deployment
items:
- name: foo
image: foo:v1
`,
`
dest: `
kind: Deployment
items:
- name: foo
@@ -191,7 +191,7 @@ items:
image: bar:v1
command: ['run2.sh']
`,
`
expected: `
kind: Deployment
items:
- name: foo
@@ -205,12 +205,12 @@ items:
//
// Test Case
//
{`keep element -- empty list in src`,
`
{description: `keep element -- empty list in src`,
source: `
kind: Deployment
items: {}
`,
`
dest: `
kind: Deployment
items:
- name: foo
@@ -219,7 +219,7 @@ items:
image: bar:v1
command: ['run2.sh']
`,
`
expected: `
kind: Deployment
items:
- name: foo
@@ -233,12 +233,12 @@ items:
//
// Test Case
//
{`remove Element -- null in src`,
`
{description: `remove Element -- null in src`,
source: `
kind: Deployment
items: null
`,
`
dest: `
kind: Deployment
items:
- name: foo
@@ -247,8 +247,97 @@ items:
image: bar:v1
command: ['run2.sh']
`,
`
expected: `
kind: Deployment
`,
},
//
// Test Case
//
{description: `no infer merge keys no merge'`,
source: `
kind: Deployment
containers:
- name: foo
command: ['run2.sh']
`,
dest: `
kind: Deployment
containers:
- name: foo
image: foo:bar
`,
expected: `
kind: Deployment
containers:
- name: foo
command: ['run2.sh']
`,
noInfer: true,
},
//
// Test Case
//
{description: `no infer merge keys merge using schema`,
source: `
kind: Deployment
apiVersion: apps/v1
spec:
template:
spec:
containers:
- name: foo
command: ['run2.sh']
`,
dest: `
kind: Deployment
apiVersion: apps/v1
spec:
template:
spec:
containers:
- name: foo
image: foo:bar
`,
expected: `
kind: Deployment
apiVersion: apps/v1
spec:
template:
spec:
containers:
- name: foo
image: foo:bar
command: ['run2.sh']
`,
noInfer: true,
},
//
// Test Case
//
{description: `no infer merge keys merge using explicit schema as line comment'`,
source: `
kind: Deployment
containers:
- name: foo
command: ['run2.sh']
`,
dest: `
kind: Deployment
containers: # {"items":{"$ref": "#/definitions/io.k8s.api.core.v1.Container"},"type":"array","x-kubernetes-patch-merge-key":"name","x-kubernetes-patch-strategy": "merge"}
- name: foo # hell ow
image: foo:bar
`,
expected: `
kind: Deployment
containers: # {"items":{"$ref": "#/definitions/io.k8s.api.core.v1.Container"},"type":"array","x-kubernetes-patch-merge-key":"name","x-kubernetes-patch-strategy": "merge"}
- name: foo
image: foo:bar
command: ['run2.sh']
`,
noInfer: true,
},
}