Merge pull request #9472 from deads2k/expose-name-validation

expose common name validation methods
This commit is contained in:
Dawn Chen 2015-08-06 10:51:51 -07:00
commit 536313c82c
2 changed files with 37 additions and 37 deletions

View File

@ -44,9 +44,9 @@ func intervalErrorMsg(lo, hi int) string {
var labelValueErrorMsg string = fmt.Sprintf(`must have at most %d characters, matching regex %s: e.g. "MyValue" or ""`, util.LabelValueMaxLength, util.LabelValueFmt) var labelValueErrorMsg string = fmt.Sprintf(`must have at most %d characters, matching regex %s: e.g. "MyValue" or ""`, util.LabelValueMaxLength, util.LabelValueFmt)
var qualifiedNameErrorMsg string = fmt.Sprintf(`must be a qualified name (at most %d characters, matching regex %s), with an optional DNS subdomain prefix (at most %d characters, matching regex %s) and slash (/): e.g. "MyName" or "example.com/MyName"`, util.QualifiedNameMaxLength, util.QualifiedNameFmt, util.DNS1123SubdomainMaxLength, util.DNS1123SubdomainFmt) var qualifiedNameErrorMsg string = fmt.Sprintf(`must be a qualified name (at most %d characters, matching regex %s), with an optional DNS subdomain prefix (at most %d characters, matching regex %s) and slash (/): e.g. "MyName" or "example.com/MyName"`, util.QualifiedNameMaxLength, util.QualifiedNameFmt, util.DNS1123SubdomainMaxLength, util.DNS1123SubdomainFmt)
var dnsSubdomainErrorMsg string = fmt.Sprintf(`must be a DNS subdomain (at most %d characters, matching regex %s): e.g. "example.com"`, util.DNS1123SubdomainMaxLength, util.DNS1123SubdomainFmt) var DNSSubdomainErrorMsg string = fmt.Sprintf(`must be a DNS subdomain (at most %d characters, matching regex %s): e.g. "example.com"`, util.DNS1123SubdomainMaxLength, util.DNS1123SubdomainFmt)
var dns1123LabelErrorMsg string = fmt.Sprintf(`must be a DNS label (at most %d characters, matching regex %s): e.g. "my-name"`, util.DNS1123LabelMaxLength, util.DNS1123LabelFmt) var DNS1123LabelErrorMsg string = fmt.Sprintf(`must be a DNS label (at most %d characters, matching regex %s): e.g. "my-name"`, util.DNS1123LabelMaxLength, util.DNS1123LabelFmt)
var dns952LabelErrorMsg string = fmt.Sprintf(`must be a DNS 952 label (at most %d characters, matching regex %s): e.g. "my-name"`, util.DNS952LabelMaxLength, util.DNS952LabelFmt) var DNS952LabelErrorMsg string = fmt.Sprintf(`must be a DNS 952 label (at most %d characters, matching regex %s): e.g. "my-name"`, util.DNS952LabelMaxLength, util.DNS952LabelFmt)
var pdPartitionErrorMsg string = intervalErrorMsg(0, 255) var pdPartitionErrorMsg string = intervalErrorMsg(0, 255)
var portRangeErrorMsg string = intervalErrorMsg(0, 65536) var portRangeErrorMsg string = intervalErrorMsg(0, 65536)
var portNameErrorMsg string = fmt.Sprintf(`must be an IANA_SVC_NAME (at most 15 characters, matching regex %s, it must contain at least one letter [a-z], and hyphens cannot be adjacent to other hyphens): e.g. "http"`, util.IdentifierNoHyphensBeginEndFmt) var portNameErrorMsg string = fmt.Sprintf(`must be an IANA_SVC_NAME (at most 15 characters, matching regex %s, it must contain at least one letter [a-z], and hyphens cannot be adjacent to other hyphens): e.g. "http"`, util.IdentifierNoHyphensBeginEndFmt)
@ -101,7 +101,7 @@ func maskTrailingDash(name string) string {
// Prefix indicates this name will be used as part of generation, in which case // Prefix indicates this name will be used as part of generation, in which case
// trailing dashes are allowed. // trailing dashes are allowed.
func ValidatePodName(name string, prefix bool) (bool, string) { func ValidatePodName(name string, prefix bool) (bool, string) {
return nameIsDNSSubdomain(name, prefix) return NameIsDNSSubdomain(name, prefix)
} }
// ValidateReplicationControllerName can be used to check whether the given replication // ValidateReplicationControllerName can be used to check whether the given replication
@ -109,35 +109,35 @@ func ValidatePodName(name string, prefix bool) (bool, string) {
// Prefix indicates this name will be used as part of generation, in which case // Prefix indicates this name will be used as part of generation, in which case
// trailing dashes are allowed. // trailing dashes are allowed.
func ValidateReplicationControllerName(name string, prefix bool) (bool, string) { func ValidateReplicationControllerName(name string, prefix bool) (bool, string) {
return nameIsDNSSubdomain(name, prefix) return NameIsDNSSubdomain(name, prefix)
} }
// ValidateServiceName can be used to check whether the given service name is valid. // ValidateServiceName can be used to check whether the given service name is valid.
// Prefix indicates this name will be used as part of generation, in which case // Prefix indicates this name will be used as part of generation, in which case
// trailing dashes are allowed. // trailing dashes are allowed.
func ValidateServiceName(name string, prefix bool) (bool, string) { func ValidateServiceName(name string, prefix bool) (bool, string) {
return nameIsDNS952Label(name, prefix) return NameIsDNS952Label(name, prefix)
} }
// ValidateNodeName can be used to check whether the given node name is valid. // ValidateNodeName can be used to check whether the given node name is valid.
// Prefix indicates this name will be used as part of generation, in which case // Prefix indicates this name will be used as part of generation, in which case
// trailing dashes are allowed. // trailing dashes are allowed.
func ValidateNodeName(name string, prefix bool) (bool, string) { func ValidateNodeName(name string, prefix bool) (bool, string) {
return nameIsDNSSubdomain(name, prefix) return NameIsDNSSubdomain(name, prefix)
} }
// ValidateNamespaceName can be used to check whether the given namespace name is valid. // ValidateNamespaceName can be used to check whether the given namespace name is valid.
// Prefix indicates this name will be used as part of generation, in which case // Prefix indicates this name will be used as part of generation, in which case
// trailing dashes are allowed. // trailing dashes are allowed.
func ValidateNamespaceName(name string, prefix bool) (bool, string) { func ValidateNamespaceName(name string, prefix bool) (bool, string) {
return nameIsDNSLabel(name, prefix) return NameIsDNSLabel(name, prefix)
} }
// ValidateLimitRangeName can be used to check whether the given limit range name is valid. // ValidateLimitRangeName can be used to check whether the given limit range name is valid.
// Prefix indicates this name will be used as part of generation, in which case // Prefix indicates this name will be used as part of generation, in which case
// trailing dashes are allowed. // trailing dashes are allowed.
func ValidateLimitRangeName(name string, prefix bool) (bool, string) { func ValidateLimitRangeName(name string, prefix bool) (bool, string) {
return nameIsDNSSubdomain(name, prefix) return NameIsDNSSubdomain(name, prefix)
} }
// ValidateResourceQuotaName can be used to check whether the given // ValidateResourceQuotaName can be used to check whether the given
@ -145,61 +145,61 @@ func ValidateLimitRangeName(name string, prefix bool) (bool, string) {
// Prefix indicates this name will be used as part of generation, in which case // Prefix indicates this name will be used as part of generation, in which case
// trailing dashes are allowed. // trailing dashes are allowed.
func ValidateResourceQuotaName(name string, prefix bool) (bool, string) { func ValidateResourceQuotaName(name string, prefix bool) (bool, string) {
return nameIsDNSSubdomain(name, prefix) return NameIsDNSSubdomain(name, prefix)
} }
// ValidateSecretName can be used to check whether the given secret name is valid. // ValidateSecretName can be used to check whether the given secret name is valid.
// Prefix indicates this name will be used as part of generation, in which case // Prefix indicates this name will be used as part of generation, in which case
// trailing dashes are allowed. // trailing dashes are allowed.
func ValidateSecretName(name string, prefix bool) (bool, string) { func ValidateSecretName(name string, prefix bool) (bool, string) {
return nameIsDNSSubdomain(name, prefix) return NameIsDNSSubdomain(name, prefix)
} }
// ValidateServiceAccountName can be used to check whether the given service account name is valid. // ValidateServiceAccountName can be used to check whether the given service account name is valid.
// Prefix indicates this name will be used as part of generation, in which case // Prefix indicates this name will be used as part of generation, in which case
// trailing dashes are allowed. // trailing dashes are allowed.
func ValidateServiceAccountName(name string, prefix bool) (bool, string) { func ValidateServiceAccountName(name string, prefix bool) (bool, string) {
return nameIsDNSSubdomain(name, prefix) return NameIsDNSSubdomain(name, prefix)
} }
// ValidateEndpointsName can be used to check whether the given endpoints name is valid. // ValidateEndpointsName can be used to check whether the given endpoints name is valid.
// Prefix indicates this name will be used as part of generation, in which case // Prefix indicates this name will be used as part of generation, in which case
// trailing dashes are allowed. // trailing dashes are allowed.
func ValidateEndpointsName(name string, prefix bool) (bool, string) { func ValidateEndpointsName(name string, prefix bool) (bool, string) {
return nameIsDNSSubdomain(name, prefix) return NameIsDNSSubdomain(name, prefix)
} }
// nameIsDNSSubdomain is a ValidateNameFunc for names that must be a DNS subdomain. // NameIsDNSSubdomain is a ValidateNameFunc for names that must be a DNS subdomain.
func nameIsDNSSubdomain(name string, prefix bool) (bool, string) { func NameIsDNSSubdomain(name string, prefix bool) (bool, string) {
if prefix { if prefix {
name = maskTrailingDash(name) name = maskTrailingDash(name)
} }
if util.IsDNS1123Subdomain(name) { if util.IsDNS1123Subdomain(name) {
return true, "" return true, ""
} }
return false, dnsSubdomainErrorMsg return false, DNSSubdomainErrorMsg
} }
// nameIsDNSLabel is a ValidateNameFunc for names that must be a DNS 1123 label. // NameIsDNSLabel is a ValidateNameFunc for names that must be a DNS 1123 label.
func nameIsDNSLabel(name string, prefix bool) (bool, string) { func NameIsDNSLabel(name string, prefix bool) (bool, string) {
if prefix { if prefix {
name = maskTrailingDash(name) name = maskTrailingDash(name)
} }
if util.IsDNS1123Label(name) { if util.IsDNS1123Label(name) {
return true, "" return true, ""
} }
return false, dns1123LabelErrorMsg return false, DNS1123LabelErrorMsg
} }
// nameIsDNS952Label is a ValidateNameFunc for names that must be a DNS 952 label. // NameIsDNS952Label is a ValidateNameFunc for names that must be a DNS 952 label.
func nameIsDNS952Label(name string, prefix bool) (bool, string) { func NameIsDNS952Label(name string, prefix bool) (bool, string) {
if prefix { if prefix {
name = maskTrailingDash(name) name = maskTrailingDash(name)
} }
if util.IsDNS952Label(name) { if util.IsDNS952Label(name) {
return true, "" return true, ""
} }
return false, dns952LabelErrorMsg return false, DNS952LabelErrorMsg
} }
// ValidateObjectMeta validates an object's metadata on creation. It expects that name generation has already // ValidateObjectMeta validates an object's metadata on creation. It expects that name generation has already
@ -229,7 +229,7 @@ func ValidateObjectMeta(meta *api.ObjectMeta, requiresNamespace bool, nameFn Val
if len(meta.Namespace) == 0 { if len(meta.Namespace) == 0 {
allErrs = append(allErrs, errs.NewFieldRequired("namespace")) allErrs = append(allErrs, errs.NewFieldRequired("namespace"))
} else if ok, _ := ValidateNamespaceName(meta.Namespace, false); !ok { } else if ok, _ := ValidateNamespaceName(meta.Namespace, false); !ok {
allErrs = append(allErrs, errs.NewFieldInvalid("namespace", meta.Namespace, dns1123LabelErrorMsg)) allErrs = append(allErrs, errs.NewFieldInvalid("namespace", meta.Namespace, DNS1123LabelErrorMsg))
} }
} else { } else {
if len(meta.Namespace) != 0 { if len(meta.Namespace) != 0 {
@ -290,7 +290,7 @@ func validateVolumes(volumes []api.Volume) (util.StringSet, errs.ValidationError
if len(vol.Name) == 0 { if len(vol.Name) == 0 {
el = append(el, errs.NewFieldRequired("name")) el = append(el, errs.NewFieldRequired("name"))
} else if !util.IsDNS1123Label(vol.Name) { } else if !util.IsDNS1123Label(vol.Name) {
el = append(el, errs.NewFieldInvalid("name", vol.Name, dns1123LabelErrorMsg)) el = append(el, errs.NewFieldInvalid("name", vol.Name, DNS1123LabelErrorMsg))
} else if allNames.Has(vol.Name) { } else if allNames.Has(vol.Name) {
el = append(el, errs.NewFieldDuplicate("name", vol.Name)) el = append(el, errs.NewFieldDuplicate("name", vol.Name))
} }
@ -474,7 +474,7 @@ func validateRBD(rbd *api.RBDVolumeSource) errs.ValidationErrorList {
} }
func ValidatePersistentVolumeName(name string, prefix bool) (bool, string) { func ValidatePersistentVolumeName(name string, prefix bool) (bool, string) {
return nameIsDNSSubdomain(name, prefix) return NameIsDNSSubdomain(name, prefix)
} }
func ValidatePersistentVolume(pv *api.PersistentVolume) errs.ValidationErrorList { func ValidatePersistentVolume(pv *api.PersistentVolume) errs.ValidationErrorList {
@ -853,7 +853,7 @@ func validateContainers(containers []api.Container, volumes util.StringSet) errs
if len(ctr.Name) == 0 { if len(ctr.Name) == 0 {
cErrs = append(cErrs, errs.NewFieldRequired("name")) cErrs = append(cErrs, errs.NewFieldRequired("name"))
} else if !util.IsDNS1123Label(ctr.Name) { } else if !util.IsDNS1123Label(ctr.Name) {
cErrs = append(cErrs, errs.NewFieldInvalid("name", ctr.Name, dns1123LabelErrorMsg)) cErrs = append(cErrs, errs.NewFieldInvalid("name", ctr.Name, DNS1123LabelErrorMsg))
} else if allNames.Has(ctr.Name) { } else if allNames.Has(ctr.Name) {
cErrs = append(cErrs, errs.NewFieldDuplicate("name", ctr.Name)) cErrs = append(cErrs, errs.NewFieldDuplicate("name", ctr.Name))
} else { } else {
@ -1139,7 +1139,7 @@ func validateServicePort(sp *api.ServicePort, requireName bool, allNames *util.S
allErrs = append(allErrs, errs.NewFieldRequired("name")) allErrs = append(allErrs, errs.NewFieldRequired("name"))
} else if sp.Name != "" { } else if sp.Name != "" {
if !util.IsDNS1123Label(sp.Name) { if !util.IsDNS1123Label(sp.Name) {
allErrs = append(allErrs, errs.NewFieldInvalid("name", sp.Name, dns1123LabelErrorMsg)) allErrs = append(allErrs, errs.NewFieldInvalid("name", sp.Name, DNS1123LabelErrorMsg))
} else if allNames.Has(sp.Name) { } else if allNames.Has(sp.Name) {
allErrs = append(allErrs, errs.NewFieldDuplicate("name", sp.Name)) allErrs = append(allErrs, errs.NewFieldDuplicate("name", sp.Name))
} else { } else {
@ -1682,7 +1682,7 @@ func validateEndpointPort(port *api.EndpointPort, requireName bool) errs.Validat
allErrs = append(allErrs, errs.NewFieldRequired("name")) allErrs = append(allErrs, errs.NewFieldRequired("name"))
} else if port.Name != "" { } else if port.Name != "" {
if !util.IsDNS1123Label(port.Name) { if !util.IsDNS1123Label(port.Name) {
allErrs = append(allErrs, errs.NewFieldInvalid("name", port.Name, dns1123LabelErrorMsg)) allErrs = append(allErrs, errs.NewFieldInvalid("name", port.Name, DNS1123LabelErrorMsg))
} }
} }
if !util.IsValidPortNum(port.Port) { if !util.IsValidPortNum(port.Port) {

View File

@ -107,7 +107,7 @@ func TestValidateObjectMetaUpdateIgnoresCreationTimestamp(t *testing.T) {
// Ensure trailing slash is allowed in generate name // Ensure trailing slash is allowed in generate name
func TestValidateObjectMetaTrimsTrailingSlash(t *testing.T) { func TestValidateObjectMetaTrimsTrailingSlash(t *testing.T) {
errs := ValidateObjectMeta(&api.ObjectMeta{Name: "test", GenerateName: "foo-"}, false, nameIsDNSSubdomain) errs := ValidateObjectMeta(&api.ObjectMeta{Name: "test", GenerateName: "foo-"}, false, NameIsDNSSubdomain)
if len(errs) != 0 { if len(errs) != 0 {
t.Fatalf("unexpected errors: %v", errs) t.Fatalf("unexpected errors: %v", errs)
} }
@ -504,8 +504,8 @@ func TestValidateVolumes(t *testing.T) {
t.Errorf("%s: expected errors to have field %s: %v", k, v.F, errs[i]) t.Errorf("%s: expected errors to have field %s: %v", k, v.F, errs[i])
} }
detail := errs[i].(*errors.ValidationError).Detail detail := errs[i].(*errors.ValidationError).Detail
if detail != "" && detail != dns1123LabelErrorMsg { if detail != "" && detail != DNS1123LabelErrorMsg {
t.Errorf("%s: expected error detail either empty or %s, got %s", k, dns1123LabelErrorMsg, detail) t.Errorf("%s: expected error detail either empty or %s, got %s", k, DNS1123LabelErrorMsg, detail)
} }
} }
} }
@ -2804,11 +2804,11 @@ func TestValidateLimitRange(t *testing.T) {
}, },
"invalid Name": { "invalid Name": {
api.LimitRange{ObjectMeta: api.ObjectMeta{Name: "^Invalid", Namespace: "foo"}, Spec: spec}, api.LimitRange{ObjectMeta: api.ObjectMeta{Name: "^Invalid", Namespace: "foo"}, Spec: spec},
dnsSubdomainErrorMsg, DNSSubdomainErrorMsg,
}, },
"invalid Namespace": { "invalid Namespace": {
api.LimitRange{ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: "^Invalid"}, Spec: spec}, api.LimitRange{ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: "^Invalid"}, Spec: spec},
dns1123LabelErrorMsg, DNS1123LabelErrorMsg,
}, },
"duplicate limit type": { "duplicate limit type": {
api.LimitRange{ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: "foo"}, Spec: invalidSpecDuplicateType}, api.LimitRange{ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: "foo"}, Spec: invalidSpecDuplicateType},
@ -2879,11 +2879,11 @@ func TestValidateResourceQuota(t *testing.T) {
}, },
"invalid Name": { "invalid Name": {
api.ResourceQuota{ObjectMeta: api.ObjectMeta{Name: "^Invalid", Namespace: "foo"}, Spec: spec}, api.ResourceQuota{ObjectMeta: api.ObjectMeta{Name: "^Invalid", Namespace: "foo"}, Spec: spec},
dnsSubdomainErrorMsg, DNSSubdomainErrorMsg,
}, },
"invalid Namespace": { "invalid Namespace": {
api.ResourceQuota{ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: "^Invalid"}, Spec: spec}, api.ResourceQuota{ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: "^Invalid"}, Spec: spec},
dns1123LabelErrorMsg, DNS1123LabelErrorMsg,
}, },
} }
for k, v := range errorCases { for k, v := range errorCases {
@ -3341,12 +3341,12 @@ func TestValidateEndpoints(t *testing.T) {
"invalid namespace": { "invalid namespace": {
endpoints: api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "mysvc", Namespace: "no@#invalid.;chars\"allowed"}}, endpoints: api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "mysvc", Namespace: "no@#invalid.;chars\"allowed"}},
errorType: "FieldValueInvalid", errorType: "FieldValueInvalid",
errorDetail: dns1123LabelErrorMsg, errorDetail: DNS1123LabelErrorMsg,
}, },
"invalid name": { "invalid name": {
endpoints: api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "-_Invliad^&Characters", Namespace: "namespace"}}, endpoints: api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "-_Invliad^&Characters", Namespace: "namespace"}},
errorType: "FieldValueInvalid", errorType: "FieldValueInvalid",
errorDetail: dnsSubdomainErrorMsg, errorDetail: DNSSubdomainErrorMsg,
}, },
"empty addresses": { "empty addresses": {
endpoints: api.Endpoints{ endpoints: api.Endpoints{