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 built-in function for parsing resource strings #1561

Closed
lucperkins opened this issue Jul 13, 2019 · 1 comment · Fixed by #1672
Closed

Add built-in function for parsing resource strings #1561

lucperkins opened this issue Jul 13, 2019 · 1 comment · Fixed by #1672
Assignees

Comments

@lucperkins
Copy link
Contributor

lucperkins commented Jul 13, 2019

I was writing some Rego policies for Kubernetes Admission Control objects the other day and wanted to create some example rules regarding resource usage. One barrier that I ran into is that it's quite difficult in Rego to work with strings like 100Mi, 5gb, and 100KB (i.e. the system described here). A built-in function converting those strings into an integer number of bytes would make it dramatically easier to, for example, make comparisons, assign limits, etc. Here's an example:

deny {
    ram_usage > numbytes("5GB")
    disk_usage > numbytes("500MB")
}

I have a PR basically ready to go for this if it sounds useful to others.

@tsandall
Copy link
Member

@lucperkins and I discussed this offline. This would be fairly useful for writing k8s admission control policies. In fact, @timothyhinrichs put together a video where he implemented conversions inside of Rego: https://www.youtube.com/watch?v=iBHf7Nzf-Lg. As long as we construct the ast.Number value using math/big operations we won't have to worry about loss of precision.

tsandall added a commit to tsandall/opa that referenced this issue Aug 26, 2019
Also fix-up built-in function declaration to match updated name.

Fixes open-policy-agent#1561

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
tsandall added a commit that referenced this issue Aug 26, 2019
Also fix-up built-in function declaration to match updated name.

Fixes #1561

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants