Skip to content

Commit 89b9414

Browse files
danwinshipk8s-publishing-bot
authored andcommitted
Make validation.IsValidIP return a field.ErrorList for consistency
Kubernetes-commit: 519dd6887dc275f77d5be32a1f711ff71117c87d
1 parent cc2017e commit 89b9414

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

pkg/util/validation/validation.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,12 @@ func IsValidPortName(port string) []string {
350350
}
351351

352352
// IsValidIP tests that the argument is a valid IP address.
353-
func IsValidIP(value string) []string {
353+
func IsValidIP(fldPath *field.Path, value string) field.ErrorList {
354+
var allErrors field.ErrorList
354355
if netutils.ParseIPSloppy(value) == nil {
355-
return []string{"must be a valid IP address, (e.g. 10.9.8.7 or 2001:db8::ffff)"}
356+
allErrors = append(allErrors, field.Invalid(fldPath, value, "must be a valid IP address, (e.g. 10.9.8.7 or 2001:db8::ffff)"))
356357
}
357-
return nil
358+
return allErrors
358359
}
359360

360361
// IsValidIPv4Address tests that the argument is a valid IPv4 address.

pkg/util/validation/validation_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -458,20 +458,20 @@ func TestIsValidIP(t *testing.T) {
458458
},
459459
} {
460460
t.Run(tc.name, func(t *testing.T) {
461-
msgs := IsValidIP(tc.in)
461+
errs := IsValidIP(field.NewPath(""), tc.in)
462462
if tc.err == "" {
463-
if len(msgs) != 0 {
464-
t.Errorf("expected %q to be valid but got: %v", tc.in, msgs)
463+
if len(errs) != 0 {
464+
t.Errorf("expected %q to be valid but got: %v", tc.in, errs)
465465
}
466466
} else {
467-
if len(msgs) != 1 {
468-
t.Errorf("expected %q to have 1 error but got: %v", tc.in, msgs)
469-
} else if !strings.Contains(msgs[0], tc.err) {
470-
t.Errorf("expected error for %q to contain %q but got: %q", tc.in, tc.err, msgs[0])
467+
if len(errs) != 1 {
468+
t.Errorf("expected %q to have 1 error but got: %v", tc.in, errs)
469+
} else if !strings.Contains(errs[0].Detail, tc.err) {
470+
t.Errorf("expected error for %q to contain %q but got: %q", tc.in, tc.err, errs[0].Detail)
471471
}
472472
}
473473

474-
errs := IsValidIPv4Address(field.NewPath(""), tc.in)
474+
errs = IsValidIPv4Address(field.NewPath(""), tc.in)
475475
if tc.family == 4 {
476476
if len(errs) != 0 {
477477
t.Errorf("expected %q to pass IsValidIPv4Address but got: %v", tc.in, errs)

0 commit comments

Comments
 (0)