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
7bd741b9
Commit
7bd741b9
authored
Sep 25, 2015
by
Brian Grant
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #14495 from derekwaynecarr/improve_quota_validation
Ensure ResourceQuota values are non-negative
parents
236b454c
b29722c7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
5 deletions
+32
-5
validation.go
pkg/api/validation/validation.go
+15
-3
validation_test.go
pkg/api/validation/validation_test.go
+17
-2
No files found.
pkg/api/validation/validation.go
View file @
7bd741b9
...
@@ -220,6 +220,15 @@ func ValidatePositiveField(value int64, fieldName string) errs.ValidationErrorLi
...
@@ -220,6 +220,15 @@ func ValidatePositiveField(value int64, fieldName string) errs.ValidationErrorLi
return
allErrs
return
allErrs
}
}
// Validates that a Quantity is not negative
func
ValidatePositiveQuantity
(
value
resource
.
Quantity
,
fieldName
string
)
errs
.
ValidationErrorList
{
allErrs
:=
errs
.
ValidationErrorList
{}
if
value
.
Cmp
(
resource
.
Quantity
{})
<
0
{
allErrs
=
append
(
allErrs
,
errs
.
NewFieldInvalid
(
fieldName
,
value
.
String
(),
isNegativeErrorMsg
))
}
return
allErrs
}
// ValidateObjectMeta validates an object's metadata on creation. It expects that name generation has already
// ValidateObjectMeta validates an object's metadata on creation. It expects that name generation has already
// been performed.
// been performed.
func
ValidateObjectMeta
(
meta
*
api
.
ObjectMeta
,
requiresNamespace
bool
,
nameFn
ValidateNameFunc
)
errs
.
ValidationErrorList
{
func
ValidateObjectMeta
(
meta
*
api
.
ObjectMeta
,
requiresNamespace
bool
,
nameFn
ValidateNameFunc
)
errs
.
ValidationErrorList
{
...
@@ -1701,14 +1710,17 @@ func ValidateResourceQuota(resourceQuota *api.ResourceQuota) errs.ValidationErro
...
@@ -1701,14 +1710,17 @@ func ValidateResourceQuota(resourceQuota *api.ResourceQuota) errs.ValidationErro
allErrs
:=
errs
.
ValidationErrorList
{}
allErrs
:=
errs
.
ValidationErrorList
{}
allErrs
=
append
(
allErrs
,
ValidateObjectMeta
(
&
resourceQuota
.
ObjectMeta
,
true
,
ValidateResourceQuotaName
)
.
Prefix
(
"metadata"
)
...
)
allErrs
=
append
(
allErrs
,
ValidateObjectMeta
(
&
resourceQuota
.
ObjectMeta
,
true
,
ValidateResourceQuotaName
)
.
Prefix
(
"metadata"
)
...
)
for
k
:=
range
resourceQuota
.
Spec
.
Hard
{
for
k
,
v
:=
range
resourceQuota
.
Spec
.
Hard
{
allErrs
=
append
(
allErrs
,
validateResourceName
(
string
(
k
),
string
(
resourceQuota
.
TypeMeta
.
Kind
))
...
)
allErrs
=
append
(
allErrs
,
validateResourceName
(
string
(
k
),
string
(
resourceQuota
.
TypeMeta
.
Kind
))
...
)
allErrs
=
append
(
allErrs
,
ValidatePositiveQuantity
(
v
,
string
(
k
))
...
)
}
}
for
k
:=
range
resourceQuota
.
Status
.
Hard
{
for
k
,
v
:=
range
resourceQuota
.
Status
.
Hard
{
allErrs
=
append
(
allErrs
,
validateResourceName
(
string
(
k
),
string
(
resourceQuota
.
TypeMeta
.
Kind
))
...
)
allErrs
=
append
(
allErrs
,
validateResourceName
(
string
(
k
),
string
(
resourceQuota
.
TypeMeta
.
Kind
))
...
)
allErrs
=
append
(
allErrs
,
ValidatePositiveQuantity
(
v
,
string
(
k
))
...
)
}
}
for
k
:=
range
resourceQuota
.
Status
.
Used
{
for
k
,
v
:=
range
resourceQuota
.
Status
.
Used
{
allErrs
=
append
(
allErrs
,
validateResourceName
(
string
(
k
),
string
(
resourceQuota
.
TypeMeta
.
Kind
))
...
)
allErrs
=
append
(
allErrs
,
validateResourceName
(
string
(
k
),
string
(
resourceQuota
.
TypeMeta
.
Kind
))
...
)
allErrs
=
append
(
allErrs
,
ValidatePositiveQuantity
(
v
,
string
(
k
))
...
)
}
}
return
allErrs
return
allErrs
}
}
...
...
pkg/api/validation/validation_test.go
View file @
7bd741b9
...
@@ -3131,12 +3131,23 @@ func TestValidateResourceQuota(t *testing.T) {
...
@@ -3131,12 +3131,23 @@ func TestValidateResourceQuota(t *testing.T) {
api
.
ResourceCPU
:
resource
.
MustParse
(
"100"
),
api
.
ResourceCPU
:
resource
.
MustParse
(
"100"
),
api
.
ResourceMemory
:
resource
.
MustParse
(
"10000"
),
api
.
ResourceMemory
:
resource
.
MustParse
(
"10000"
),
api
.
ResourcePods
:
resource
.
MustParse
(
"10"
),
api
.
ResourcePods
:
resource
.
MustParse
(
"10"
),
api
.
ResourceServices
:
resource
.
MustParse
(
"
1
0"
),
api
.
ResourceServices
:
resource
.
MustParse
(
"0"
),
api
.
ResourceReplicationControllers
:
resource
.
MustParse
(
"10"
),
api
.
ResourceReplicationControllers
:
resource
.
MustParse
(
"10"
),
api
.
ResourceQuotas
:
resource
.
MustParse
(
"10"
),
api
.
ResourceQuotas
:
resource
.
MustParse
(
"10"
),
},
},
}
}
negativeSpec
:=
api
.
ResourceQuotaSpec
{
Hard
:
api
.
ResourceList
{
api
.
ResourceCPU
:
resource
.
MustParse
(
"-100"
),
api
.
ResourceMemory
:
resource
.
MustParse
(
"-10000"
),
api
.
ResourcePods
:
resource
.
MustParse
(
"-10"
),
api
.
ResourceServices
:
resource
.
MustParse
(
"-10"
),
api
.
ResourceReplicationControllers
:
resource
.
MustParse
(
"-10"
),
api
.
ResourceQuotas
:
resource
.
MustParse
(
"-10"
),
},
}
successCases
:=
[]
api
.
ResourceQuota
{
successCases
:=
[]
api
.
ResourceQuota
{
{
{
ObjectMeta
:
api
.
ObjectMeta
{
ObjectMeta
:
api
.
ObjectMeta
{
...
@@ -3173,6 +3184,10 @@ func TestValidateResourceQuota(t *testing.T) {
...
@@ -3173,6 +3184,10 @@ func TestValidateResourceQuota(t *testing.T) {
api
.
ResourceQuota
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"abc"
,
Namespace
:
"^Invalid"
},
Spec
:
spec
},
api
.
ResourceQuota
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"abc"
,
Namespace
:
"^Invalid"
},
Spec
:
spec
},
DNS1123LabelErrorMsg
,
DNS1123LabelErrorMsg
,
},
},
"negative-limits"
:
{
api
.
ResourceQuota
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"abc"
,
Namespace
:
"foo"
},
Spec
:
negativeSpec
},
isNegativeErrorMsg
,
},
}
}
for
k
,
v
:=
range
errorCases
{
for
k
,
v
:=
range
errorCases
{
errs
:=
ValidateResourceQuota
(
&
v
.
R
)
errs
:=
ValidateResourceQuota
(
&
v
.
R
)
...
@@ -3182,7 +3197,7 @@ func TestValidateResourceQuota(t *testing.T) {
...
@@ -3182,7 +3197,7 @@ func TestValidateResourceQuota(t *testing.T) {
for
i
:=
range
errs
{
for
i
:=
range
errs
{
field
:=
errs
[
i
]
.
(
*
errors
.
ValidationError
)
.
Field
field
:=
errs
[
i
]
.
(
*
errors
.
ValidationError
)
.
Field
detail
:=
errs
[
i
]
.
(
*
errors
.
ValidationError
)
.
Detail
detail
:=
errs
[
i
]
.
(
*
errors
.
ValidationError
)
.
Detail
if
field
!=
"metadata.name"
&&
field
!=
"metadata.namespace"
{
if
field
!=
"metadata.name"
&&
field
!=
"metadata.namespace"
&&
!
api
.
IsStandardResourceName
(
field
)
{
t
.
Errorf
(
"%s: missing prefix for: %v"
,
k
,
field
)
t
.
Errorf
(
"%s: missing prefix for: %v"
,
k
,
field
)
}
}
if
detail
!=
v
.
D
{
if
detail
!=
v
.
D
{
...
...
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