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
c91a79be
Commit
c91a79be
authored
Feb 16, 2016
by
k8s-merge-robot
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #21199 from erictune/validation-helper
Auto commit by PR queue bot
parents
dd6cc923
825dc470
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
0 deletions
+51
-0
validation.go
pkg/api/validation/validation.go
+14
-0
validation_test.go
pkg/api/validation/validation_test.go
+37
-0
No files found.
pkg/api/validation/validation.go
View file @
c91a79be
...
@@ -84,6 +84,20 @@ func ValidateLabels(labels map[string]string, fldPath *field.Path) field.ErrorLi
...
@@ -84,6 +84,20 @@ func ValidateLabels(labels map[string]string, fldPath *field.Path) field.ErrorLi
return
allErrs
return
allErrs
}
}
// ValidateHasLabel requires that api.ObjectMeta has a Label with key and expectedValue
func
ValidateHasLabel
(
meta
api
.
ObjectMeta
,
fldPath
*
field
.
Path
,
key
,
expectedValue
string
)
field
.
ErrorList
{
allErrs
:=
field
.
ErrorList
{}
actualValue
,
found
:=
meta
.
Labels
[
key
]
if
!
found
{
allErrs
=
append
(
allErrs
,
field
.
Required
(
fldPath
.
Child
(
"labels"
),
key
+
"="
+
expectedValue
))
return
allErrs
}
if
actualValue
!=
expectedValue
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
fldPath
.
Child
(
"labels"
),
meta
.
Labels
,
"expected "
+
key
+
"="
+
expectedValue
))
}
return
allErrs
}
// ValidateAnnotations validates that a set of annotations are correctly defined.
// ValidateAnnotations validates that a set of annotations are correctly defined.
func
ValidateAnnotations
(
annotations
map
[
string
]
string
,
fldPath
*
field
.
Path
)
field
.
ErrorList
{
func
ValidateAnnotations
(
annotations
map
[
string
]
string
,
fldPath
*
field
.
Path
)
field
.
ErrorList
{
allErrs
:=
field
.
ErrorList
{}
allErrs
:=
field
.
ErrorList
{}
...
...
pkg/api/validation/validation_test.go
View file @
c91a79be
...
@@ -5107,3 +5107,40 @@ func TestValidateConfigMapUpdate(t *testing.T) {
...
@@ -5107,3 +5107,40 @@ func TestValidateConfigMapUpdate(t *testing.T) {
}
}
}
}
}
}
func
TestValidateHasLabel
(
t
*
testing
.
T
)
{
successCase
:=
api
.
ObjectMeta
{
Name
:
"123"
,
Namespace
:
"ns"
,
Labels
:
map
[
string
]
string
{
"other"
:
"blah"
,
"foo"
:
"bar"
,
},
}
if
errs
:=
ValidateHasLabel
(
successCase
,
field
.
NewPath
(
"field"
),
"foo"
,
"bar"
);
len
(
errs
)
!=
0
{
t
.
Errorf
(
"expected success: %v"
,
errs
)
}
missingCase
:=
api
.
ObjectMeta
{
Name
:
"123"
,
Namespace
:
"ns"
,
Labels
:
map
[
string
]
string
{
"other"
:
"blah"
,
},
}
if
errs
:=
ValidateHasLabel
(
missingCase
,
field
.
NewPath
(
"field"
),
"foo"
,
"bar"
);
len
(
errs
)
==
0
{
t
.
Errorf
(
"expected failure"
)
}
wrongValueCase
:=
api
.
ObjectMeta
{
Name
:
"123"
,
Namespace
:
"ns"
,
Labels
:
map
[
string
]
string
{
"other"
:
"blah"
,
"foo"
:
"notbar"
,
},
}
if
errs
:=
ValidateHasLabel
(
wrongValueCase
,
field
.
NewPath
(
"field"
),
"foo"
,
"bar"
);
len
(
errs
)
==
0
{
t
.
Errorf
(
"expected failure"
)
}
}
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