Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
7f2d0c0f
Commit
7f2d0c0f
authored
Dec 08, 2014
by
Daniel Smith
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2779 from thockin/qualname
Add util to validate namespaced names
parents
c33a5d6c
5ecce5d6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
13 deletions
+58
-13
validation.go
pkg/api/validation/validation.go
+1
-13
validation.go
pkg/util/validation.go
+21
-0
validation_test.go
pkg/util/validation_test.go
+36
-0
No files found.
pkg/api/validation/validation.go
View file @
7f2d0c0f
...
@@ -385,19 +385,7 @@ func ValidatePodSpec(spec *api.PodSpec) errs.ValidationErrorList {
...
@@ -385,19 +385,7 @@ func ValidatePodSpec(spec *api.PodSpec) errs.ValidationErrorList {
func
validateLabels
(
labels
map
[
string
]
string
,
field
string
)
errs
.
ValidationErrorList
{
func
validateLabels
(
labels
map
[
string
]
string
,
field
string
)
errs
.
ValidationErrorList
{
allErrs
:=
errs
.
ValidationErrorList
{}
allErrs
:=
errs
.
ValidationErrorList
{}
for
k
:=
range
labels
{
for
k
:=
range
labels
{
var
n
,
ns
string
if
!
util
.
IsQualifiedName
(
k
)
{
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
)
{
allErrs
=
append
(
allErrs
,
errs
.
NewFieldInvalid
(
field
,
k
,
""
))
allErrs
=
append
(
allErrs
,
errs
.
NewFieldInvalid
(
field
,
k
,
""
))
}
}
}
}
...
...
pkg/util/validation.go
View file @
7f2d0c0f
...
@@ -18,6 +18,7 @@ package util
...
@@ -18,6 +18,7 @@ package util
import
(
import
(
"regexp"
"regexp"
"strings"
)
)
// IsDNSLabel tests for a string that conforms to the definition of a label in
// IsDNSLabel tests for a string that conforms to the definition of a label in
...
@@ -82,3 +83,23 @@ func IsCIdentifier(value string) bool {
...
@@ -82,3 +83,23 @@ func IsCIdentifier(value string) bool {
func
IsValidPortNum
(
port
int
)
bool
{
func
IsValidPortNum
(
port
int
)
bool
{
return
0
<
port
&&
port
<
65536
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
}
pkg/util/validation_test.go
View file @
7f2d0c0f
...
@@ -153,3 +153,39 @@ func TestIsValidPortNum(t *testing.T) {
...
@@ -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
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment