-
Notifications
You must be signed in to change notification settings - Fork 173
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
base: main
Are you sure you want to change the base?
Conversation
Thanks for opening your first Pull Request here! Please check out our Contributing guidelines and confirm that you Signed off. |
6bed058
to
e8030ae
Compare
There was a problem hiding this 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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Signed-off-by: Denish Tomar <[email protected]>
Signed-off-by: Denish Tomar <[email protected]>
ae0cbbe
to
5f36f6a
Compare
Related issue #1473
Proposed Changes
This PR updates the nested foreach documentation to clarify that:
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