Skip to content

Commit 803e9d6

Browse files
authored
Merge pull request kubernetes#130355 from yongruilin/validation_origin
validation: Add Origin field to field.Error for more precise error tracking
2 parents 60d0d67 + c7cf852 commit 803e9d6

File tree

6 files changed

+239
-136
lines changed

6 files changed

+239
-136
lines changed

pkg/apis/core/validation/validation.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func ValidateAnnotations(annotations map[string]string, fldPath *field.Path) fie
133133
func ValidateDNS1123Label(value string, fldPath *field.Path) field.ErrorList {
134134
allErrs := field.ErrorList{}
135135
for _, msg := range validation.IsDNS1123Label(value) {
136-
allErrs = append(allErrs, field.Invalid(fldPath, value, msg))
136+
allErrs = append(allErrs, field.Invalid(fldPath, value, msg).WithOrigin("format=dns-label"))
137137
}
138138
return allErrs
139139
}
@@ -142,7 +142,7 @@ func ValidateDNS1123Label(value string, fldPath *field.Path) field.ErrorList {
142142
func ValidateQualifiedName(value string, fldPath *field.Path) field.ErrorList {
143143
allErrs := field.ErrorList{}
144144
for _, msg := range validation.IsQualifiedName(value) {
145-
allErrs = append(allErrs, field.Invalid(fldPath, value, msg))
145+
allErrs = append(allErrs, field.Invalid(fldPath, value, msg).WithOrigin("format=qualified-name"))
146146
}
147147
return allErrs
148148
}
@@ -6266,7 +6266,7 @@ func ValidateReplicationControllerSpec(spec, oldSpec *core.ReplicationController
62666266
allErrs := field.ErrorList{}
62676267
allErrs = append(allErrs, ValidateNonnegativeField(int64(spec.MinReadySeconds), fldPath.Child("minReadySeconds"))...)
62686268
allErrs = append(allErrs, ValidateNonEmptySelector(spec.Selector, fldPath.Child("selector"))...)
6269-
allErrs = append(allErrs, ValidateNonnegativeField(int64(spec.Replicas), fldPath.Child("replicas"))...)
6269+
allErrs = append(allErrs, ValidateNonnegativeField(int64(spec.Replicas), fldPath.Child("replicas")).WithOrigin("minimum")...)
62706270
allErrs = append(allErrs, ValidatePodTemplateSpecForRC(spec.Template, spec.Selector, spec.Replicas, fldPath.Child("template"), opts)...)
62716271
return allErrs
62726272
}
@@ -7466,7 +7466,7 @@ func validateEndpointAddress(address *core.EndpointAddress, fldPath *field.Path)
74667466
// During endpoint update, verify that NodeName is a DNS subdomain and transition rules allow the update
74677467
if address.NodeName != nil {
74687468
for _, msg := range ValidateNodeName(*address.NodeName, false) {
7469-
allErrs = append(allErrs, field.Invalid(fldPath.Child("nodeName"), *address.NodeName, msg))
7469+
allErrs = append(allErrs, field.Invalid(fldPath.Child("nodeName"), *address.NodeName, msg).WithOrigin("format=dns-label"))
74707470
}
74717471
}
74727472
allErrs = append(allErrs, ValidateNonSpecialIP(address.IP, fldPath.Child("ip"))...)
@@ -7485,20 +7485,20 @@ func ValidateNonSpecialIP(ipAddress string, fldPath *field.Path) field.ErrorList
74857485
allErrs := field.ErrorList{}
74867486
ip := netutils.ParseIPSloppy(ipAddress)
74877487
if ip == nil {
7488-
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, "must be a valid IP address"))
7488+
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, "must be a valid IP address").WithOrigin("format=ip-sloppy"))
74897489
return allErrs
74907490
}
74917491
if ip.IsUnspecified() {
7492-
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, fmt.Sprintf("may not be unspecified (%v)", ipAddress)))
7492+
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, fmt.Sprintf("may not be unspecified (%v)", ipAddress)).WithOrigin("format=non-special-ip"))
74937493
}
74947494
if ip.IsLoopback() {
7495-
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, "may not be in the loopback range (127.0.0.0/8, ::1/128)"))
7495+
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, "may not be in the loopback range (127.0.0.0/8, ::1/128)").WithOrigin("format=non-special-ip"))
74967496
}
74977497
if ip.IsLinkLocalUnicast() {
7498-
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, "may not be in the link-local range (169.254.0.0/16, fe80::/10)"))
7498+
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, "may not be in the link-local range (169.254.0.0/16, fe80::/10)").WithOrigin("format=non-special-ip"))
74997499
}
75007500
if ip.IsLinkLocalMulticast() {
7501-
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, "may not be in the link-local multicast range (224.0.0.0/24, ff02::/10)"))
7501+
allErrs = append(allErrs, field.Invalid(fldPath, ipAddress, "may not be in the link-local multicast range (224.0.0.0/24, ff02::/10)").WithOrigin("format=non-special-ip"))
75027502
}
75037503
return allErrs
75047504
}
@@ -7511,7 +7511,7 @@ func validateEndpointPort(port *core.EndpointPort, requireName bool, fldPath *fi
75117511
allErrs = append(allErrs, ValidateDNS1123Label(port.Name, fldPath.Child("name"))...)
75127512
}
75137513
for _, msg := range validation.IsValidPortNum(int(port.Port)) {
7514-
allErrs = append(allErrs, field.Invalid(fldPath.Child("port"), port.Port, msg))
7514+
allErrs = append(allErrs, field.Invalid(fldPath.Child("port"), port.Port, msg).WithOrigin("portNum"))
75157515
}
75167516
if len(port.Protocol) == 0 {
75177517
allErrs = append(allErrs, field.Required(fldPath.Child("protocol"), ""))

0 commit comments

Comments
 (0)