Make IsDNS952Label return error strings

This commit is contained in:
Tim Hockin 2015-12-19 21:02:12 -08:00
parent 5862a60ae7
commit 54a3efb364
4 changed files with 17 additions and 12 deletions

View File

@ -58,7 +58,6 @@ func InclusiveRangeErrorMsg(lo, hi int) string {
return fmt.Sprintf(`must be between %d and %d, inclusive`, lo, hi)
}
var DNS952LabelErrorMsg string = fmt.Sprintf(`must be a DNS 952 label (at most %d characters, matching regex %s): e.g. "my-name"`, validation.DNS952LabelMaxLength, validation.DNS952LabelFmt)
var pdPartitionErrorMsg string = InclusiveRangeErrorMsg(1, 255)
var PortRangeErrorMsg string = InclusiveRangeErrorMsg(1, 65535)
var IdRangeErrorMsg string = InclusiveRangeErrorMsg(0, math.MaxInt32)
@ -256,10 +255,7 @@ func NameIsDNS952Label(name string, prefix bool) []string {
if prefix {
name = maskTrailingDash(name)
}
if validation.IsDNS952Label(name) {
return nil
}
return []string{DNS952LabelErrorMsg}
return validation.IsDNS952Label(name)
}
// Validates that given value is not negative.

View File

@ -260,8 +260,10 @@ func validateContext(contextName string, context clientcmdapi.Context, config cl
validationErrors = append(validationErrors, fmt.Errorf("cluster %q was not found for context %q", context.Cluster, contextName))
}
if (len(context.Namespace) != 0) && !validation.IsDNS952Label(context.Namespace) {
validationErrors = append(validationErrors, fmt.Errorf("namespace %q for context %q does not conform to the kubernetes DNS952 rules", context.Namespace, contextName))
if len(context.Namespace) != 0 {
if len(validation.IsDNS1123Label(context.Namespace)) != 0 {
validationErrors = append(validationErrors, fmt.Errorf("namespace %q for context %q does not conform to the kubernetes DNS_LABEL rules", context.Namespace, contextName))
}
}
return validationErrors

View File

@ -128,8 +128,15 @@ var dns952LabelRegexp = regexp.MustCompile("^" + DNS952LabelFmt + "$")
// IsDNS952Label tests for a string that conforms to the definition of a label in
// DNS (RFC 952).
func IsDNS952Label(value string) bool {
return len(value) <= DNS952LabelMaxLength && dns952LabelRegexp.MatchString(value)
func IsDNS952Label(value string) []string {
var errs []string
if len(value) > DNS952LabelMaxLength {
errs = append(errs, MaxLenError(DNS952LabelMaxLength))
}
if !dns952LabelRegexp.MatchString(value) {
errs = append(errs, RegexError(DNS952LabelFmt, "my-name", "abc-123"))
}
return errs
}
const CIdentifierFmt string = "[A-Za-z_][A-Za-z0-9_]*"

View File

@ -92,8 +92,8 @@ func TestIsDNS952Label(t *testing.T) {
strings.Repeat("a", 24),
}
for _, val := range goodValues {
if !IsDNS952Label(val) {
t.Errorf("expected true for '%s'", val)
if msgs := IsDNS952Label(val); len(msgs) != 0 {
t.Errorf("expected true for '%s': %v", val, msgs)
}
}
@ -107,7 +107,7 @@ func TestIsDNS952Label(t *testing.T) {
strings.Repeat("a", 25),
}
for _, val := range badValues {
if IsDNS952Label(val) {
if msgs := IsDNS952Label(val); len(msgs) == 0 {
t.Errorf("expected false for '%s'", val)
}
}