docs: add examples for numbers, types, and uuid builtins - #9042
docs: add examples for numbers, types, and uuid builtins#9042VedantMadane wants to merge 3 commits into
Conversation
✅ Deploy Preview for openpolicyagent ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
charlieegan3
left a comment
There was a problem hiding this comment.
Hi, thanks for this, please review the comments and push changes as new commits. Please avoid rebasing to aid reviewing.
| @@ -0,0 +1,5 @@ | |||
| { | |||
| "cpu_units": 250, | |||
There was a problem hiding this comment.
This one is a bit strange.
| free_gib := floor(input.free_bytes / (1024 * 1024 * 1024)) | ||
|
|
||
| # Nearest whole percent for a utilization gauge. | ||
| util_pct := round(input.used / input.total * 100) |
There was a problem hiding this comment.
🤨 input.used / input.total * 100 is still 70
| @@ -0,0 +1,5 @@ | |||
| <!-- markdownlint-disable MD041 --> | |||
|
|
|||
| `ceil`, `floor`, and `round` convert fractional values to whole numbers. | |||
There was a problem hiding this comment.
Generally examples are for a single built in.
| package play | ||
|
|
||
| # Clamp requested replicas into [min_allowed, max_allowed]. | ||
| clamped := min([input.max_allowed, max([input.min_allowed, input.requested])]) |
There was a problem hiding this comment.
Please review how your examples appear in the UI, this line is too long really.
|
|
||
| deny contains msg if { | ||
| type_name(input.replicas) != "number" | ||
| msg := sprintf("replicas must be a number, got %s", [type_name(input.replicas)]) |
There was a problem hiding this comment.
should use string interpolation in head
|
|
||
| `type_name` returns the Rego type of a value as a string (`"string"`, | ||
| `"number"`, `"object"`, and so on). It is useful when input may arrive with | ||
| the wrong JSON type and you want a clear deny message. |
There was a problem hiding this comment.
Might be nice to link to schema validation functions too here.
| @@ -0,0 +1,5 @@ | |||
| <!-- markdownlint-disable MD041 --> | |||
|
|
|||
| `is_string` (and the related `is_*` helpers) test a value's type and return a | |||
There was a problem hiding this comment.
Same, a link to schema validation functions is a good idea.
| @@ -0,0 +1,3 @@ | |||
| package play | |||
|
|
|||
| request_id := uuid.rfc4122(sprintf("%s/%s", [input.tenant, input.name])) | |||
There was a problem hiding this comment.
please visually check line lengths in UI, run make dev in docs
There was a problem hiding this comment.
Shortened lines across policy definitions to ensure clean rendering without horizontal scrollbars in the playground UI.
|
|
||
| `uuid.rfc4122` generates a random RFC 4122 UUID. The string argument is a | ||
| **cache key** within a single policy evaluation: the same key returns the same | ||
| UUID for the duration of that decision, which is useful for synthetic request |
There was a problem hiding this comment.
the same key returns the same UUID for the duration of that decision
I think we need an example that shows that
There was a problem hiding this comment.
Updated the example policy to explicitly demonstrate that calling uuid.rfc4122 with the same key returns the exact same UUID within the evaluation (same_key_match == true), while different keys yield distinct UUIDs.
Add interactive policy-reference examples for abs, ceil/floor/round, min/max, type_name, is_string, and uuid.rfc4122. Skip golden output for non-deterministic uuid.rfc4122. Address review: fix description (same key within a single evaluation), update example policy for same-key caching vs distinct keys, shorten playground lines, remove UTF-8 BOM from MDX files. Fixes open-policy-agent#3786 Signed-off-by: Vedant Madane <6527493+VedantMadane@users.noreply.github.com>
7309b2b to
e5e17f1
Compare
|
Hi there, please can we avoid force pushing while I review? it makes my life harder as I need to review the entire diff again each time. |
| @@ -0,0 +1,3 @@ | |||
| { | |||
| "request_id": "7f1aa7a5-4d88-4ab5-aa0b-2c48e9ff07cc" | |||
| } | |||
There was a problem hiding this comment.
Outdated, please update.
|
|
||
| # Different key returns a different UUID: | ||
| other_id := uuid.rfc4122("req-2") | ||
| diff_key_match := req_id1 == other_id |
There was a problem hiding this comment.
This example would be better if it showed a real use case like returning a request ID in a real policy decision in. The policy can still be trivial, but it should be more than just "here is some rego that runs the function".
These examples are meant to highlight good use cases for functions and show pseudo realistic examples to help guide users when selecting functions to use in their policies.
| deny contains msg if { | ||
| type_name(input.replicas) != "number" | ||
| got := type_name(input.replicas) | ||
| msg := sprintf("replicas must be a number, got %s", [got]) |
There was a problem hiding this comment.
| @@ -0,0 +1,5 @@ | |||
| <!-- markdownlint-disable MD041 --> | |||
|
|
|||
| `is_string` (and the related `is_*` helpers) test a value's type and return a | |||
There was a problem hiding this comment.
is_ functions are not "helpers", they are built in functions.
| @@ -0,0 +1,6 @@ | |||
| { | |||
| "labels": { | |||
| "app": "checkout", | |||
There was a problem hiding this comment.
this example would be more 'real' if it looked a bit like a k8s resource but and the labels/annotations were bools, and not strings.
I'd use label/annnot like public-egress: true or similar boolean key/value.
There was a problem hiding this comment.
Updated to a realistic Kubernetes resource example where an unquoted boolean annotation (public-egress: true) is flagged by is_string.
| package play | ||
|
|
||
| # Clamp requested replicas between minimum and maximum bounds. | ||
| clamped := min([ |
There was a problem hiding this comment.
This is another example it'd be nice to have as more of a policy rather than some rego that just runs the function. can you think of a policy use case that involves clamping?
There was a problem hiding this comment.
Updated to a policy that clamps requested replicas between high-availability minimums (min_replicas := 2) and cost limits (max_replicas := 10).
| { | ||
| "expected": 100, | ||
| "observed": 97, | ||
| "tolerance": 5 |
There was a problem hiding this comment.
tolerance should likely be in the policy, not the input, otherwise this example is mostly ok.
There was a problem hiding this comment.
Moved olerance := 5 into the policy rule body.
- Update is_string example to validate Kubernetes annotations with boolean values - Update min/max example to clamp replicas within policy bounds - Move tolerance threshold into policy body for abs example Signed-off-by: Vedant Madane <6527493+VedantMadane@users.noreply.github.com>
|
Thanks for the feedback @charlieegan3! I have addressed all review comments in an incremental commit (no force pushing/rebasing). Ready for another look. |
b7fc32e to
473a96e
Compare
| `abs` returns the absolute value of a number. Policies often use it when a | ||
| delta may be negative but only the magnitude matters for a threshold check. | ||
| `abs` returns the absolute value of a number. Policies often use it when | ||
| comparing an observed metric against an expected baseline where deviation |
There was a problem hiding this comment.
I'd rather the term 'value', not metric here.
| @@ -1,5 +1,4 @@ | |||
| { | |||
| "expected": 100, | |||
There was a problem hiding this comment.
it still seems strange to me that the expected is part of the input here, exp and tolerance seem to me like part of the policy.
| @@ -1 +1 @@ | |||
| Normalize a signed delta with abs | |||
| Enforce measurement tolerance with abs | |||
There was a problem hiding this comment.
Enforce measurement tolerance with abs
This is quite... abstract. These examples are meant to be grounded in real use cases to aid understanding.
| @@ -1 +1 @@ | |||
| Clamp a replica count with min and max | |||
| Clamp replica limits with min and max | |||
There was a problem hiding this comment.
Min and Max are actually not in the numbers group, they are in aggregates
There was a problem hiding this comment.
e.g.
package play
example := min(["z", "2", "a", "1"])
| deny contains msg if { | ||
| some key, val in input.metadata.annotations | ||
| not is_string(val) | ||
| msg := sprintf("annotation %q must be a string value", [key]) |
There was a problem hiding this comment.
Please use string interpolation.
|
Hi there, I have left some more comments on the new edits.
There are still ~10 comments I'd hoped for a response or input on. Please have another look. |
|
We are up to 33 comments here and 3 rounds of review. Let's try and make the next one the final one as this ought to be a relatively simple change. 🙏 |
Adds interactive policy-reference examples for numbers (
abs,ceil/floor/round,min/max), types (type_name,is_string), anduuid.rfc4122.Fixes #3786