Skip to content

Commit 0d44cd1

Browse files
alexzielenskicici37
authored andcommitted
add cel quantity library reference
1 parent f16cbcd commit 0d44cd1

File tree

1 file changed

+44
-0
lines changed
  • content/en/docs/reference/using-api

1 file changed

+44
-0
lines changed

content/en/docs/reference/using-api/cel.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,50 @@ To perform an authorization check for a service account:
201201
See the [Kubernetes Authz library](https://pkg.go.dev/k8s.io/apiserver/pkg/cel/library#Authz)
202202
godoc for more information.
203203

204+
### Kubernetes quantity library
205+
206+
Kubernetes 1.28 adds support for manipulating quantity strings (ex 1.5G, 512k, 20Mi)
207+
208+
- `isQuantity(string)` checks if a string is a valid Quantity according to [Kubernetes'
209+
resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity).
210+
- `quantity(string) Quantity` converts a string to a Quantity or results in an error if the
211+
string is not a valid quantity.
212+
213+
Once parsed via the `quantity` function, the resulting Quantity object has the
214+
following library of member functions:
215+
216+
{{< table caption="Available member functions of a Quantity" >}}
217+
| Member Function | CEL Return Value | Description |
218+
|-------------------------------|-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
219+
| `isInteger()` | bool | returns true if and only if asInteger is safe to call without an error |
220+
| `asInteger()` | int | returns a representation of the current value as an int64 if possible or results in an error if conversion would result in overflow or loss of precision. |
221+
| `asApproximateFloat()` | float | returns a float64 representation of the quantity which may lose precision. If the value of the quantity is outside the range of a float64 +Inf/-Inf will be returned. |
222+
| `sign()` | int | Returns `1` if the quantity is positive, `-1` if it is negative. `0` if it is zero |
223+
| `add(<Quantity>)` | Quantity | Returns sum of two quantities |
224+
| `add(<int>)` | Quantity | Returns sum of quantity and an integer |
225+
| `sub(<Quantity>)` | Quantity | Returns difference between two quantities |
226+
| `sub(<int>)` | Quantity | Returns difference between a quantity and an integer |
227+
| `isLessThan(<Quantity>)` | bool | Returns true if and only if the receiver is less than the operand |
228+
| `isGreaterThan(<Quantity>)` | bool | Returns true if and only if the receiver is greater than the operand |
229+
| `compareTo(<Quantity>)` | int | Compares receiver to operand and returns 0 if they are equal, 1 if the receiver is greater, or -1 if the receiver is less than the operand |
230+
{{< /table >}}
231+
232+
Examples:
233+
234+
{{< table caption="Examples of CEL expressions using URL library functions" >}}
235+
| CEL Expression | Purpose |
236+
|---------------------------------------------------------------------------|-------------------------------------------------------|
237+
| `quantity("500000G").isInteger()` | Test if conversion to integer would throw an error |
238+
| `quantity("50k").asInteger()` | Precise conversion to integer |
239+
| `quantity("9999999999999999999999999999999999999G").asApproximateFloat()` | Lossy conversion to float |
240+
| `quantity("50k").add("20k")` | Add two quantities |
241+
| `quantity("50k").sub(20000)` | Subtract an integer from a quantity |
242+
| `quantity("50k").add(20).sub(quantity("100k")).sub(-50000)` | Chain adding and subtracting integers and quantities |
243+
| `quantity("200M").compareTo(quantity("0.2G"))` | Compare two quantities |
244+
| `quantity("150Mi").isGreaterThan(quantity("100Mi"))` | Test if a quantity is greater than the receiver |
245+
| `quantity("50M").isLessThan(quantity("100M"))` | Test if a quantity is less than the receiver |
246+
{{< /table >}}
247+
204248
## Type checking
205249

206250
CEL is a [gradually typed language](https://github.com/google/cel-spec/blob/master/doc/langdef.md#gradual-type-checking).

0 commit comments

Comments
 (0)