|
1 | 1 | package flytek8s
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "github.com/pkg/errors" |
| 4 | + "math" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/pkg/errors" // For decimal arithmetic |
5 | 8 | v1 "k8s.io/api/core/v1"
|
6 | 9 | "k8s.io/apimachinery/pkg/api/resource"
|
7 | 10 |
|
@@ -35,8 +38,23 @@ func ToK8sResourceList(resources []*core.Resources_ResourceEntry, onOOMConfig pl
|
35 | 38 | case core.Resources_MEMORY:
|
36 | 39 | if !v.IsZero() {
|
37 | 40 | if onOOMConfig != nil && onOOMConfig.GetExponent() > 0 && onOOMConfig.GetFactor() > 1.0 {
|
38 |
| - // TODO: Create this function |
39 |
| - // v.Mul(math.Pow(float64(onOOMConfig.GetFactor()), float64(onOOMConfig.GetExponent()))) |
| 41 | + // Convert to multiplier into string and parse it to quantity |
| 42 | + multiplier := math.Pow(float64(onOOMConfig.GetFactor()), float64(onOOMConfig.GetExponent())) |
| 43 | + multiplier_str := strconv.FormatFloat(multiplier, 'f', -1, 64) |
| 44 | + multiplier_quantity, err := resource.ParseQuantity(multiplier_str) |
| 45 | + if err != nil { |
| 46 | + return nil, errors.Wrap(err, "Failed to parse resource as a valid quantity.") |
| 47 | + } |
| 48 | + |
| 49 | + // Multiply the value by the multiplier |
| 50 | + v.AsDec().Mul(v.AsDec(), multiplier_quantity.AsDec()) |
| 51 | + |
| 52 | + // Convert the value to a string and parse it to quantity |
| 53 | + v, err := resource.ParseQuantity(v.AsDec().String()) |
| 54 | + if err != nil { |
| 55 | + return nil, errors.Wrap(err, "Failed to parse resource as a valid quantity.") |
| 56 | + } |
| 57 | + |
40 | 58 | limitVal, err := resource.ParseQuantity(onOOMConfig.GetLimit())
|
41 | 59 | if err != nil {
|
42 | 60 | return nil, errors.Wrap(err, "Failed to parse resource as a valid quantity.")
|
|
0 commit comments