mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 22:17:14 +00:00
Make IsValidPercent return error strings
This commit is contained in:
parent
87c1fc50a8
commit
bb208a02b3
@ -149,8 +149,8 @@ func ValidatePositiveIntOrPercent(intOrPercent intstr.IntOrString, fldPath *fiel
|
|||||||
allErrs := field.ErrorList{}
|
allErrs := field.ErrorList{}
|
||||||
switch intOrPercent.Type {
|
switch intOrPercent.Type {
|
||||||
case intstr.String:
|
case intstr.String:
|
||||||
if !validation.IsValidPercent(intOrPercent.StrVal) {
|
for _, msg := range validation.IsValidPercent(intOrPercent.StrVal) {
|
||||||
allErrs = append(allErrs, field.Invalid(fldPath, intOrPercent, "must be an integer or percentage (e.g '5%%')"))
|
allErrs = append(allErrs, field.Invalid(fldPath, intOrPercent, msg))
|
||||||
}
|
}
|
||||||
case intstr.Int:
|
case intstr.Int:
|
||||||
allErrs = append(allErrs, apivalidation.ValidateNonnegativeField(int64(intOrPercent.IntValue()), fldPath)...)
|
allErrs = append(allErrs, apivalidation.ValidateNonnegativeField(int64(intOrPercent.IntValue()), fldPath)...)
|
||||||
@ -161,7 +161,10 @@ func ValidatePositiveIntOrPercent(intOrPercent intstr.IntOrString, fldPath *fiel
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getPercentValue(intOrStringValue intstr.IntOrString) (int, bool) {
|
func getPercentValue(intOrStringValue intstr.IntOrString) (int, bool) {
|
||||||
if intOrStringValue.Type != intstr.String || !validation.IsValidPercent(intOrStringValue.StrVal) {
|
if intOrStringValue.Type != intstr.String {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
if len(validation.IsValidPercent(intOrStringValue.StrVal)) != 0 {
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
value, _ := strconv.Atoi(intOrStringValue.StrVal[:len(intOrStringValue.StrVal)-1])
|
value, _ := strconv.Atoi(intOrStringValue.StrVal[:len(intOrStringValue.StrVal)-1])
|
||||||
|
@ -632,7 +632,7 @@ func TestValidateDeployment(t *testing.T) {
|
|||||||
MaxSurge: intstr.FromString("20Percent"),
|
MaxSurge: intstr.FromString("20Percent"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
errorCases["must be an integer or percentage"] = invalidMaxSurgeDeployment
|
errorCases["must match the regex"] = invalidMaxSurgeDeployment
|
||||||
|
|
||||||
// MaxSurge and MaxUnavailable cannot both be zero.
|
// MaxSurge and MaxUnavailable cannot both be zero.
|
||||||
invalidRollingUpdateDeployment := validDeployment()
|
invalidRollingUpdateDeployment := validDeployment()
|
||||||
|
@ -221,8 +221,11 @@ const percentFmt string = "[0-9]+%"
|
|||||||
|
|
||||||
var percentRegexp = regexp.MustCompile("^" + percentFmt + "$")
|
var percentRegexp = regexp.MustCompile("^" + percentFmt + "$")
|
||||||
|
|
||||||
func IsValidPercent(percent string) bool {
|
func IsValidPercent(percent string) []string {
|
||||||
return percentRegexp.MatchString(percent)
|
if !percentRegexp.MatchString(percent) {
|
||||||
|
return []string{RegexError(percentFmt, "1%", "93%")}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const HTTPHeaderNameFmt string = "[-A-Za-z0-9]+"
|
const HTTPHeaderNameFmt string = "[-A-Za-z0-9]+"
|
||||||
|
@ -338,3 +338,39 @@ func TestIsHTTPHeaderName(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsValidPercent(t *testing.T) {
|
||||||
|
goodValues := []string{
|
||||||
|
"0%",
|
||||||
|
"00000%",
|
||||||
|
"1%",
|
||||||
|
"01%",
|
||||||
|
"99%",
|
||||||
|
"100%",
|
||||||
|
"101%",
|
||||||
|
}
|
||||||
|
for _, val := range goodValues {
|
||||||
|
if msgs := IsValidPercent(val); len(msgs) != 0 {
|
||||||
|
t.Errorf("expected true for %q: %v", val, msgs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
badValues := []string{
|
||||||
|
"",
|
||||||
|
"0",
|
||||||
|
"100",
|
||||||
|
"0.0%",
|
||||||
|
"99.9%",
|
||||||
|
"hundred",
|
||||||
|
" 1%",
|
||||||
|
"1% ",
|
||||||
|
"-0%",
|
||||||
|
"-1%",
|
||||||
|
"+1%",
|
||||||
|
}
|
||||||
|
for _, val := range badValues {
|
||||||
|
if msgs := IsValidPercent(val); len(msgs) == 0 {
|
||||||
|
t.Errorf("expected false for %q", val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user