Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add util to validate namespaced names #2779

Merged
merged 1 commit into from
Dec 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 1 addition & 13 deletions pkg/api/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,19 +385,7 @@ func ValidatePodSpec(spec *api.PodSpec) errs.ValidationErrorList {
func validateLabels(labels map[string]string, field string) errs.ValidationErrorList {
allErrs := errs.ValidationErrorList{}
for k := range labels {
var n, ns string
parts := strings.Split(k, "/")
switch len(parts) {
case 1:
n = parts[0]
case 2:
ns = parts[0]
n = parts[1]
default:
allErrs = append(allErrs, errs.NewFieldInvalid(field, k, ""))
continue
}
if (ns != "" && !util.IsDNSSubdomain(ns)) || !util.IsDNSLabel(n) {
if !util.IsQualifiedName(k) {
allErrs = append(allErrs, errs.NewFieldInvalid(field, k, ""))
}
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/util/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package util

import (
"regexp"
"strings"
)

// IsDNSLabel tests for a string that conforms to the definition of a label in
Expand Down Expand Up @@ -82,3 +83,23 @@ func IsCIdentifier(value string) bool {
func IsValidPortNum(port int) bool {
return 0 < port && port < 65536
}

// IsQualifiedName tests whether a string fits the "optionally-namespaced
// name" pattern: [ DNS_SUBDOMAIN "/" ] DNS_LABEL
func IsQualifiedName(value string) bool {
var n, ns string
parts := strings.Split(value, "/")
switch len(parts) {
case 1:
n = parts[0]
case 2:
ns = parts[0]
n = parts[1]
default:
return false
}
if (ns != "" && !IsDNSSubdomain(ns)) || !IsDNSLabel(n) {
return false
}
return true
}
36 changes: 36 additions & 0 deletions pkg/util/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,39 @@ func TestIsValidPortNum(t *testing.T) {
}
}
}

func TestIsQualifiedName(t *testing.T) {
successCases := []string{
"simple",
"now-with-dashes",
"1-starts-with-num",
"1234",
"simple/simple",
"now-with-dashes/simple",
"now-with-dashes/now-with-dashes",
"now.with.dots/simple",
"now-with.dashes-and.dots/simple",
"1-num.2-num/3-num",
"1234/5678",
"1.2.3.4/5678",
}
for i := range successCases {
if !IsQualifiedName(successCases[i]) {
t.Errorf("case[%d] expected success", i)
}
}

errorCases := []string{
"NoUppercase123",
"nospecialchars%^=@",
"cantendwithadash-",
"-cantstartwithadash",
"only/one/slash",
strings.Repeat("a", 254),
}
for i := range errorCases {
if IsQualifiedName(errorCases[i]) {
t.Errorf("case[%d] expected failure", i)
}
}
}