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

Improve Nested foreach Documentation #1474

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 30 additions & 2 deletions content/en/docs/writing-policies/mutate.md
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ spec:
secretName: mytlscertsecret
```

This type of advanced mutation can be performed with nested foreach loops as shown below. Notice that in the JSON patch, the `path` value references the current index of `tls[]` as `{{elementIndex0}}` and the current index of `hosts[]` as `{{elementIndex1}}`. In the `value` field, the `{{element}}` variable still references the current value of the `hosts[]` array being processed.
This type of advanced mutation can be performed with nested foreach loops as shown below. Notice that in the JSON patch, the `path` value references the current index of `tls[]` as `{{elementIndex0}}` and the current index of `hosts[]` as `{{elementIndex1}}`. In the `value` field, the `{{element1}}` variable still references the current value of the `hosts[]` array being processed.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a statement here that looks something like the following:

While {{elementIndex0}} can be used to reference the current index of tls[] you can use {{element0}} to access the current element in the tls[] being processed in the foreach loop. Similarly {{element1}} can be used to access the hosts[] value in the nested for each.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamsgarg-ob Thanks for the feedback, I'll make the required changes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamsgarg-ob I have updated the required changes, could you plz review it...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


```yaml
apiVersion: kyverno.io/v1
Expand All @@ -1187,13 +1187,41 @@ spec:
mutate:
foreach:
- list: request.object.spec.tls[]
as: element0 # Outer loop element (tls array)
foreach:
- list: "element.hosts"
as: element1 # Inner loop element (hosts array)
patchesJson6902: |-
- path: /spec/tls/{{elementIndex0}}/hosts/{{elementIndex1}}
op: replace
value: "{{ replace_all('{{element}}', '.old.com', '.new.com') }}"
value: "{{ replace_all('{{element1}}', '.old.com', '.new.com') }}"
```
For older Kyverno versions that do not support as:, elements can be accessed directly using {{element0}} and {{element1}}.

```yaml
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: replace-dns-suffix-legacy
spec:
background: false
rules:
- name: replace-dns-suffix
match:
any:
- resources:
kinds:
- Ingress
mutate:
foreach:
- list: request.object.spec.tls[]
foreach:
- list: element.hosts
patchesJson6902: |-
- path: /spec/tls/{{elementIndex0}}/hosts/{{elementIndex1}}
op: replace
value: "{{ replace_all('{{element}}', '.old.com', '.new.com') }}"
```

## GitOps Considerations

Expand Down