diff --git a/cmd/integration/integration.go b/cmd/integration/integration.go index b983d6053a..c999d1c9e8 100644 --- a/cmd/integration/integration.go +++ b/cmd/integration/integration.go @@ -166,7 +166,7 @@ func runAtomicPutTest(c *client.Client) { var svc api.Service err := c.Post().Path("services").Body( api.Service{ - JSONBase: api.JSONBase{ID: "atomicService", APIVersion: "v1beta1"}, + JSONBase: api.JSONBase{ID: "atomicservice", APIVersion: "v1beta1"}, Port: 12345, Labels: map[string]string{ "name": "atomicService", diff --git a/pkg/api/validation.go b/pkg/api/validation.go index f281cefdf2..de34dcce39 100644 --- a/pkg/api/validation.go +++ b/pkg/api/validation.go @@ -302,6 +302,8 @@ func ValidateService(service *Service) []error { allErrs := errorList{} if service.ID == "" { allErrs.Append(makeInvalidError("Service.ID", service.ID)) + } else if !util.IsDNS952Label(service.ID) { + allErrs.Append(makeInvalidError("Service.ID", service.ID)) } if labels.Set(service.Selector).AsSelector().Empty() { allErrs.Append(makeInvalidError("Service.Selector", service.Selector)) diff --git a/pkg/util/validation.go b/pkg/util/validation.go index ede2cd63a3..3f1f0c433e 100644 --- a/pkg/util/validation.go +++ b/pkg/util/validation.go @@ -58,3 +58,13 @@ func IsCIdentifier(value string) bool { func IsValidPortNum(port int) bool { return 0 < port && port < 65536 } + +const dns952IdentifierFmt string = "[a-z]([-a-z0-9]*[a-z0-9])?" + +var dns952Regexp = regexp.MustCompile("^" + dns952IdentifierFmt + "$") + +const dns952MaxLength = 24 + +func IsDNS952Label(value string) bool { + return len(value) <= dns952MaxLength && dns952Regexp.MatchString(value) +} diff --git a/pkg/util/validation_test.go b/pkg/util/validation_test.go index 0be1e01d0c..3bd6666006 100644 --- a/pkg/util/validation_test.go +++ b/pkg/util/validation_test.go @@ -100,6 +100,7 @@ func TestIsCIdentifier(t *testing.T) { "-", "a-", "-a", "1-", "-1", "1_", "1_2", ".", "a.", ".a", "a.b", "1.", ".1", "1.2", " ", "a ", " a", "a b", "1 ", " 1", "1 2", + "#a#", } for _, val := range badValues { if IsCIdentifier(val) { @@ -123,3 +124,26 @@ func TestIsValidPortNum(t *testing.T) { } } } + +func TestIsDNS952(t *testing.T) { + goodValues := []string{ + "a", "ab", "abc", "a1", "a-b", "a-1", "a-1-2-b", "abc-123", + } + for _, val := range goodValues { + if !IsDNS952Label(val) { + t.Errorf("expected true for '%s'", val) + } + } + + badValues := []string{ + "", "1", "123", "1a", + "-", "a-", "-a", "1-", "-1", "1-2", + " ", "a ", " a", "a b", "1 ", " 1", "1 2", + "A", "AB", "AbC", "A1", "A-B", "A-1", "A-1-2-B", + } + for _, val := range badValues { + if IsDNS952Label(val) { + t.Errorf("expected false for '%s'", val) + } + } +}