diff --git a/pkg/api/validation/validation.go b/pkg/api/validation/validation.go index 2e1d30814b0..484c0b6e469 100644 --- a/pkg/api/validation/validation.go +++ b/pkg/api/validation/validation.go @@ -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. diff --git a/pkg/client/unversioned/clientcmd/validation.go b/pkg/client/unversioned/clientcmd/validation.go index bd1bd735e09..1690f515e93 100644 --- a/pkg/client/unversioned/clientcmd/validation.go +++ b/pkg/client/unversioned/clientcmd/validation.go @@ -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 diff --git a/pkg/util/validation/validation.go b/pkg/util/validation/validation.go index 53a6ab2434e..9361a4bfacb 100644 --- a/pkg/util/validation/validation.go +++ b/pkg/util/validation/validation.go @@ -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_]*" diff --git a/pkg/util/validation/validation_test.go b/pkg/util/validation/validation_test.go index df4e661a496..ace4335b8bb 100644 --- a/pkg/util/validation/validation_test.go +++ b/pkg/util/validation/validation_test.go @@ -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) } }