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

Conversation

Denish3436
Copy link

Related issue #1473

Proposed Changes

This PR updates the nested foreach documentation to clarify that:

  • Outer loop elements can be referenced as {{ element0 }}.
  • Inner loop elements can be referenced as {{ element1 }}.

This eliminates anti-patterns where users assign outer loop elements to context variables. The update improves clarity, ensuring users can write cleaner and more efficient Kyverno policies.

Checklist

  • I have read the contributing guidelines.
  • I have inspected the website preview for accuracy.
  • I have signed off my issue.

Copy link

welcome bot commented Feb 14, 2025

Thanks for opening your first Pull Request here! Please check out our Contributing guidelines and confirm that you Signed off.

@Denish3436 Denish3436 force-pushed the fix-nested-foreach-doc branch from 6bed058 to e8030ae Compare February 14, 2025 20:58
Copy link

@kushal9897 kushal9897 left a comment

Choose a reason for hiding this comment

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

It's look good

@@ -1187,12 +1187,14 @@ spec:
mutate:
foreach:
- list: request.object.spec.tls[]
as: element0
Copy link

@iamsgarg-ob iamsgarg-ob Feb 19, 2025

Choose a reason for hiding this comment

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

in older versions of kyverno where the as: clause is not available, folks can access the element values using {{element0}}/ {{element1}} which was the cause of a lot of confusion at least for me.

could you maybe add anohter example which includes usage of both element0 and element1? In the current code element0 isn't really used in this example.

@@ -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

@Denish3436 Denish3436 force-pushed the fix-nested-foreach-doc branch from ae0cbbe to 5f36f6a Compare February 22, 2025 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants