Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to choose prepend/append patch items to list in merge #3048

Merged
merged 2 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion api/filters/patchstrategicmerge/patchstrategicmerge.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ var _ kio.Filter = Filter{}
func (pf Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
var result []*yaml.RNode
for i := range nodes {
r, err := merge2.Merge(pf.Patch, nodes[i])
r, err := merge2.Merge(
pf.Patch, nodes[i],
yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListPrepend,
},
)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions api/filters/patchstrategicmerge/patchstrategicmerge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ spec:
template:
spec:
containers:
- name: foo0
- name: foo1
- name: foo2
- name: foo3
- name: foo0
`,
},
"volumes patch": {
Expand Down Expand Up @@ -107,10 +107,10 @@ spec:
template:
spec:
volumes:
- name: foo0
- name: foo1
- name: foo2
- name: foo3
- name: foo0
`,
},
"nested patch": {
Expand Down Expand Up @@ -366,10 +366,10 @@ spec:
template:
spec:
containers:
- name: test
image: test
- name: test2
image: test2
- name: test
image: test
`,
},
}
Expand Down
4 changes: 2 additions & 2 deletions api/krusty/baseandoverlaymedium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,13 @@ spec:
spec:
containers:
- env:
- name: foo
value: bar
- name: FOO
valueFrom:
configMapKeyRef:
key: somekey
name: test-infra-app-env-8h5mh7f7ch
- name: foo
value: bar
image: nginx:1.8.0
name: nginx
ports:
Expand Down
6 changes: 3 additions & 3 deletions api/krusty/generatormergeandreplace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,12 @@ spec:
- gcePersistentDisk:
pdName: nginx-persistent-storage
name: nginx-persistent-storage
- configMap:
name: staging-team-foo-configmap-in-base-hc6g9dk6g9
name: configmap-in-base
- configMap:
name: staging-configmap-in-overlay-dc6fm46dhm
name: configmap-in-overlay
- configMap:
name: staging-team-foo-configmap-in-base-hc6g9dk6g9
name: configmap-in-base
---
apiVersion: v1
kind: Service
Expand Down
12 changes: 6 additions & 6 deletions api/krusty/multiplepatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ spec:
- gcePersistentDisk:
pdName: nginx-persistent-storage
name: nginx-persistent-storage
- configMap:
name: a-b-configmap-in-base-798k5k7g9f
name: configmap-in-base
- configMap:
name: a-configmap-in-overlay-dc6fm46dhm
name: configmap-in-overlay
- configMap:
name: a-b-configmap-in-base-798k5k7g9f
name: configmap-in-base
---
apiVersion: v1
kind: Service
Expand Down Expand Up @@ -351,12 +351,12 @@ spec:
- gcePersistentDisk:
pdName: nginx-persistent-storage
name: nginx-persistent-storage
- configMap:
name: staging-team-foo-configmap-in-base-798k5k7g9f
name: configmap-in-base
- configMap:
name: staging-configmap-in-overlay-dc6fm46dhm
name: configmap-in-overlay
- configMap:
name: staging-team-foo-configmap-in-base-798k5k7g9f
name: configmap-in-base
---
apiVersion: v1
kind: Service
Expand Down
45 changes: 26 additions & 19 deletions examples/patchMultipleObjects.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ operation/target/value tuples in a particular
syntax).

A kustomize file lets one specify many
patches. Each patch must be associated with
patches. Each patch must be associated with
a _target selector_:

[strategic merge patch]: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md
[JSON patch]: jsonpatch.md
[json patch]: jsonpatch.md

> ```yaml
> patches:
> - path: <relative path to file containing patch>
> target:
> group: <optional group>
> version: <optional version>
> kind: <optional kind>
> name: <optional name>
> namespace: <optional namespace>
> labelSelector: <optional label selector>
> annotationSelector: <optional annotation selector>
> - path: <relative path to file containing patch>
> target:
> group: <optional group>
> version: <optional version>
> kind: <optional kind>
> name: <optional name>
> namespace: <optional namespace>
> labelSelector: <optional label selector>
> annotationSelector: <optional annotation selector>
> ```

E.g. select resources with _name_ matching `foo*`:
Expand Down Expand Up @@ -61,17 +61,18 @@ The example below shows how to inject a
sidecar container for multiple Deployment
resources.


Make a place to work:

<!-- @demoHome @testAgainstLatestRelease -->

```
DEMO_HOME=$(mktemp -d)
```

Make a file describing two Deployments:

<!-- @createDeployments @testAgainstLatestRelease -->

```
cat <<EOF >$DEMO_HOME/deployments.yaml
apiVersion: apps/v1
Expand Down Expand Up @@ -111,6 +112,7 @@ Declare a [strategic merge patch] file
to inject a sidecar container:

<!-- @definePatch @testAgainstLatestRelease -->

```
cat <<EOF >$DEMO_HOME/patch.yaml
apiVersion: apps/v1
Expand All @@ -134,6 +136,7 @@ that specifies both a `patches` and `resources`
entry:

<!-- @createKustomization @testAgainstLatestRelease -->

```
cat <<EOF >$DEMO_HOME/kustomization.yaml
resources:
Expand All @@ -149,6 +152,7 @@ EOF
The expected result is:

<!-- @definedExpectedOutput @testAgainstLatestRelease -->

```
cat <<EOF >$DEMO_HOME/out_expected.yaml
apiVersion: apps/v1
Expand All @@ -162,16 +166,16 @@ spec:
old-label: old-value
spec:
containers:
- args:
- one
- two
image: nginx
name: nginx
- args:
- proxy
- sidecar
image: docker.io/istio/proxyv2
name: istio-proxy
- args:
- one
- two
image: nginx
name: nginx
---
apiVersion: apps/v1
kind: Deployment
Expand All @@ -184,25 +188,28 @@ spec:
key: value
spec:
containers:
- image: busybox
name: busybox
- args:
- proxy
- sidecar
image: docker.io/istio/proxyv2
name: istio-proxy
- image: busybox
name: busybox
EOF
```

Run the build:

<!-- @runIt @testAgainstLatestRelease -->

```
kustomize build $DEMO_HOME >$DEMO_HOME/out_actual.yaml
```

Confirm expectations:

<!-- @diffShouldExitZero @testAgainstLatestRelease -->

```
diff $DEMO_HOME/out_actual.yaml $DEMO_HOME/out_expected.yaml
```
Expand Down
4 changes: 3 additions & 1 deletion kyaml/kio/filters/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ func (c MergeFilter) Filter(input []*yaml.RNode) ([]*yaml.RNode, error) {
// first resources, don't merge it
merged = resources[i]
} else {
merged, err = merge2.Merge(patch, merged)
merged, err = merge2.Merge(patch, merged, yaml.MergeOptions{
ListIncreaseDirection: yaml.MergeOptionsListPrepend,
})
if err != nil {
return nil, err
}
Expand Down
Loading