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
6e415f76
Commit
6e415f76
authored
Feb 02, 2015
by
Brian Grant
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4017 from smarterclayton/relax_annotation_validations
Slightly relax annotation validation
parents
6d5c56b2
b3017365
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
15 deletions
+62
-15
validation.go
pkg/api/validation/validation.go
+25
-15
validation_test.go
pkg/api/validation/validation_test.go
+37
-0
No files found.
pkg/api/validation/validation.go
View file @
6e415f76
...
...
@@ -30,6 +30,28 @@ import (
"github.com/golang/glog"
)
// ValidateLabels validates that a set of labels are correctly defined.
func
ValidateLabels
(
labels
map
[
string
]
string
,
field
string
)
errs
.
ValidationErrorList
{
allErrs
:=
errs
.
ValidationErrorList
{}
for
k
:=
range
labels
{
if
!
util
.
IsQualifiedName
(
k
)
{
allErrs
=
append
(
allErrs
,
errs
.
NewFieldInvalid
(
field
,
k
,
""
))
}
}
return
allErrs
}
// ValidateAnnotations validates that a set of annotations are correctly defined.
func
ValidateAnnotations
(
annotations
map
[
string
]
string
,
field
string
)
errs
.
ValidationErrorList
{
allErrs
:=
errs
.
ValidationErrorList
{}
for
k
:=
range
annotations
{
if
!
util
.
IsQualifiedName
(
strings
.
ToLower
(
k
))
{
allErrs
=
append
(
allErrs
,
errs
.
NewFieldInvalid
(
field
,
k
,
""
))
}
}
return
allErrs
}
// ValidateNameFunc validates that the provided name is valid for a given resource type.
// Not all resources have the same validation rules for names.
type
ValidateNameFunc
func
(
name
string
)
(
bool
,
string
)
...
...
@@ -72,7 +94,7 @@ func ValidateObjectMeta(meta *api.ObjectMeta, requiresNamespace bool, nameFn Val
}
}
allErrs
=
append
(
allErrs
,
ValidateLabels
(
meta
.
Labels
,
"labels"
)
...
)
allErrs
=
append
(
allErrs
,
Validate
Label
s
(
meta
.
Annotations
,
"annotations"
)
...
)
allErrs
=
append
(
allErrs
,
Validate
Annotation
s
(
meta
.
Annotations
,
"annotations"
)
...
)
// Clear self link internally
// TODO: move to its own area
...
...
@@ -107,7 +129,7 @@ func ValidateObjectMetaUpdate(old, meta *api.ObjectMeta) errs.ValidationErrorLis
}
allErrs
=
append
(
allErrs
,
ValidateLabels
(
meta
.
Labels
,
"labels"
)
...
)
allErrs
=
append
(
allErrs
,
Validate
Label
s
(
meta
.
Annotations
,
"annotations"
)
...
)
allErrs
=
append
(
allErrs
,
Validate
Annotation
s
(
meta
.
Annotations
,
"annotations"
)
...
)
// Clear self link internally
// TODO: move to its own area
...
...
@@ -492,17 +514,6 @@ func ValidatePodSpec(spec *api.PodSpec) errs.ValidationErrorList {
return
allErrs
}
// ValidateLabels validates that a set of labels are correctly defined.
func
ValidateLabels
(
labels
map
[
string
]
string
,
field
string
)
errs
.
ValidationErrorList
{
allErrs
:=
errs
.
ValidationErrorList
{}
for
k
:=
range
labels
{
if
!
util
.
IsQualifiedName
(
k
)
{
allErrs
=
append
(
allErrs
,
errs
.
NewFieldInvalid
(
field
,
k
,
""
))
}
}
return
allErrs
}
// ValidatePodUpdate tests to see if the update is legal
func
ValidatePodUpdate
(
newPod
,
oldPod
*
api
.
Pod
)
errs
.
ValidationErrorList
{
allErrs
:=
errs
.
ValidationErrorList
{}
...
...
@@ -609,7 +620,6 @@ func ValidateReplicationControllerSpec(spec *api.ReplicationControllerSpec) errs
if
!
selector
.
Matches
(
labels
)
{
allErrs
=
append
(
allErrs
,
errs
.
NewFieldInvalid
(
"template.labels"
,
spec
.
Template
.
Labels
,
"selector does not match template"
))
}
allErrs
=
append
(
allErrs
,
ValidateLabels
(
spec
.
Template
.
Annotations
,
"annotations"
)
...
)
allErrs
=
append
(
allErrs
,
ValidatePodTemplateSpec
(
spec
.
Template
)
.
Prefix
(
"template"
)
...
)
// RestartPolicy has already been first-order validated as per ValidatePodTemplateSpec().
if
spec
.
Template
.
Spec
.
RestartPolicy
.
Always
==
nil
{
...
...
@@ -625,7 +635,7 @@ func ValidateReplicationControllerSpec(spec *api.ReplicationControllerSpec) errs
func
ValidatePodTemplateSpec
(
spec
*
api
.
PodTemplateSpec
)
errs
.
ValidationErrorList
{
allErrs
:=
errs
.
ValidationErrorList
{}
allErrs
=
append
(
allErrs
,
ValidateLabels
(
spec
.
Labels
,
"labels"
)
...
)
allErrs
=
append
(
allErrs
,
Validate
Label
s
(
spec
.
Annotations
,
"annotations"
)
...
)
allErrs
=
append
(
allErrs
,
Validate
Annotation
s
(
spec
.
Annotations
,
"annotations"
)
...
)
allErrs
=
append
(
allErrs
,
ValidatePodSpec
(
&
spec
.
Spec
)
.
Prefix
(
"spec"
)
...
)
allErrs
=
append
(
allErrs
,
ValidateReadOnlyPersistentDisks
(
spec
.
Spec
.
Volumes
)
.
Prefix
(
"spec.volumes"
)
...
)
return
allErrs
...
...
pkg/api/validation/validation_test.go
View file @
6e415f76
...
...
@@ -74,6 +74,43 @@ func TestValidateLabels(t *testing.T) {
}
}
func
TestValidateAnnotations
(
t
*
testing
.
T
)
{
successCases
:=
[]
map
[
string
]
string
{
{
"simple"
:
"bar"
},
{
"now-with-dashes"
:
"bar"
},
{
"1-starts-with-num"
:
"bar"
},
{
"1234"
:
"bar"
},
{
"simple/simple"
:
"bar"
},
{
"now-with-dashes/simple"
:
"bar"
},
{
"now-with-dashes/now-with-dashes"
:
"bar"
},
{
"now.with.dots/simple"
:
"bar"
},
{
"now-with.dashes-and.dots/simple"
:
"bar"
},
{
"1-num.2-num/3-num"
:
"bar"
},
{
"1234/5678"
:
"bar"
},
{
"1.2.3.4/5678"
:
"bar"
},
{
"UpperCase123"
:
"bar"
},
}
for
i
:=
range
successCases
{
errs
:=
ValidateAnnotations
(
successCases
[
i
],
"field"
)
if
len
(
errs
)
!=
0
{
t
.
Errorf
(
"case[%d] expected success, got %#v"
,
i
,
errs
)
}
}
errorCases
:=
[]
map
[
string
]
string
{
{
"nospecialchars^=@"
:
"bar"
},
{
"cantendwithadash-"
:
"bar"
},
{
"only/one/slash"
:
"bar"
},
{
strings
.
Repeat
(
"a"
,
254
)
:
"bar"
},
}
for
i
:=
range
errorCases
{
errs
:=
ValidateAnnotations
(
errorCases
[
i
],
"field"
)
if
len
(
errs
)
!=
1
{
t
.
Errorf
(
"case[%d] expected failure"
,
i
)
}
}
}
func
TestValidateVolumes
(
t
*
testing
.
T
)
{
successCase
:=
[]
api
.
Volume
{
{
Name
:
"abc"
},
...
...
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