summaryrefslogtreecommitdiff
path: root/vendor/k8s.io/apimachinery/pkg/util/intstr/intstr.go
diff options
context:
space:
mode:
authordependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>2020-12-09 09:17:32 +0000
committerDaniel J Walsh <dwalsh@redhat.com>2020-12-09 06:05:03 -0500
commit37fbf28d74927b959b36ceade7fde3402ea08e05 (patch)
treed053b6a2fd3e05946bbf71134d80efcb9a8a449c /vendor/k8s.io/apimachinery/pkg/util/intstr/intstr.go
parentdd295f297b6dd51d22c64c75f4ef4f80f953bbde (diff)
downloadpodman-37fbf28d74927b959b36ceade7fde3402ea08e05.tar.gz
podman-37fbf28d74927b959b36ceade7fde3402ea08e05.tar.bz2
podman-37fbf28d74927b959b36ceade7fde3402ea08e05.zip
Bump k8s.io/apimachinery from 0.19.4 to 0.20.0
Bumps [k8s.io/apimachinery](https://github.com/kubernetes/apimachinery) from 0.19.4 to 0.20.0. - [Release notes](https://github.com/kubernetes/apimachinery/releases) - [Commits](https://github.com/kubernetes/apimachinery/compare/v0.19.4...v0.20.0) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
Diffstat (limited to 'vendor/k8s.io/apimachinery/pkg/util/intstr/intstr.go')
-rw-r--r--vendor/k8s.io/apimachinery/pkg/util/intstr/intstr.go70
1 files changed, 54 insertions, 16 deletions
diff --git a/vendor/k8s.io/apimachinery/pkg/util/intstr/intstr.go b/vendor/k8s.io/apimachinery/pkg/util/intstr/intstr.go
index 6576def82..c0e8927fe 100644
--- a/vendor/k8s.io/apimachinery/pkg/util/intstr/intstr.go
+++ b/vendor/k8s.io/apimachinery/pkg/util/intstr/intstr.go
@@ -25,7 +25,6 @@ import (
"strconv"
"strings"
- "github.com/google/gofuzz"
"k8s.io/klog/v2"
)
@@ -90,6 +89,9 @@ func (intstr *IntOrString) UnmarshalJSON(value []byte) error {
// String returns the string value, or the Itoa of the int value.
func (intstr *IntOrString) String() string {
+ if intstr == nil {
+ return "<nil>"
+ }
if intstr.Type == String {
return intstr.StrVal
}
@@ -129,21 +131,6 @@ func (IntOrString) OpenAPISchemaType() []string { return []string{"string"} }
// the OpenAPI spec of this type.
func (IntOrString) OpenAPISchemaFormat() string { return "int-or-string" }
-func (intstr *IntOrString) Fuzz(c fuzz.Continue) {
- if intstr == nil {
- return
- }
- if c.RandBool() {
- intstr.Type = Int
- c.Fuzz(&intstr.IntVal)
- intstr.StrVal = ""
- } else {
- intstr.Type = String
- intstr.IntVal = 0
- c.Fuzz(&intstr.StrVal)
- }
-}
-
func ValueOrDefault(intOrPercent *IntOrString, defaultValue IntOrString) *IntOrString {
if intOrPercent == nil {
return &defaultValue
@@ -151,6 +138,33 @@ func ValueOrDefault(intOrPercent *IntOrString, defaultValue IntOrString) *IntOrS
return intOrPercent
}
+// GetScaledValueFromIntOrPercent is meant to replace GetValueFromIntOrPercent.
+// This method returns a scaled value from an IntOrString type. If the IntOrString
+// is a percentage string value it's treated as a percentage and scaled appropriately
+// in accordance to the total, if it's an int value it's treated as a a simple value and
+// if it is a string value which is either non-numeric or numeric but lacking a trailing '%' it returns an error.
+func GetScaledValueFromIntOrPercent(intOrPercent *IntOrString, total int, roundUp bool) (int, error) {
+ if intOrPercent == nil {
+ return 0, errors.New("nil value for IntOrString")
+ }
+ value, isPercent, err := getIntOrPercentValueSafely(intOrPercent)
+ if err != nil {
+ return 0, fmt.Errorf("invalid value for IntOrString: %v", err)
+ }
+ if isPercent {
+ if roundUp {
+ value = int(math.Ceil(float64(value) * (float64(total)) / 100))
+ } else {
+ value = int(math.Floor(float64(value) * (float64(total)) / 100))
+ }
+ }
+ return value, nil
+}
+
+// GetValueFromIntOrPercent was deprecated in favor of
+// GetScaledValueFromIntOrPercent. This method was treating all int as a numeric value and all
+// strings with or without a percent symbol as a percentage value.
+// Deprecated
func GetValueFromIntOrPercent(intOrPercent *IntOrString, total int, roundUp bool) (int, error) {
if intOrPercent == nil {
return 0, errors.New("nil value for IntOrString")
@@ -169,6 +183,8 @@ func GetValueFromIntOrPercent(intOrPercent *IntOrString, total int, roundUp bool
return value, nil
}
+// getIntOrPercentValue is a legacy function and only meant to be called by GetValueFromIntOrPercent
+// For a more correct implementation call getIntOrPercentSafely
func getIntOrPercentValue(intOrStr *IntOrString) (int, bool, error) {
switch intOrStr.Type {
case Int:
@@ -183,3 +199,25 @@ func getIntOrPercentValue(intOrStr *IntOrString) (int, bool, error) {
}
return 0, false, fmt.Errorf("invalid type: neither int nor percentage")
}
+
+func getIntOrPercentValueSafely(intOrStr *IntOrString) (int, bool, error) {
+ switch intOrStr.Type {
+ case Int:
+ return intOrStr.IntValue(), false, nil
+ case String:
+ isPercent := false
+ s := intOrStr.StrVal
+ if strings.HasSuffix(s, "%") {
+ isPercent = true
+ s = strings.TrimSuffix(intOrStr.StrVal, "%")
+ } else {
+ return 0, false, fmt.Errorf("invalid type: string is not a percentage")
+ }
+ v, err := strconv.Atoi(s)
+ if err != nil {
+ return 0, false, fmt.Errorf("invalid value %q: %v", intOrStr.StrVal, err)
+ }
+ return int(v), isPercent, nil
+ }
+ return 0, false, fmt.Errorf("invalid type: neither int nor percentage")
+}