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
80a53d52
Commit
80a53d52
authored
May 31, 2017
by
Chao Xu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
do not allow subresources in initializer rules
parent
ab3e7a73
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
169 additions
and
101 deletions
+169
-101
types.go
pkg/apis/admissionregistration/types.go
+5
-1
types.go
pkg/apis/admissionregistration/v1alpha1/types.go
+5
-1
validation.go
pkg/apis/admissionregistration/validation/validation.go
+31
-6
validation_test.go
pkg/apis/admissionregistration/validation/validation_test.go
+128
-93
No files found.
pkg/apis/admissionregistration/types.go
View file @
80a53d52
...
@@ -64,6 +64,7 @@ type Initializer struct {
...
@@ -64,6 +64,7 @@ type Initializer struct {
// Rules describes what resources/subresources the initializer cares about.
// Rules describes what resources/subresources the initializer cares about.
// The initializer cares about an operation if it matches _any_ Rule.
// The initializer cares about an operation if it matches _any_ Rule.
// Rule.Resources must not include subresources.
Rules
[]
Rule
Rules
[]
Rule
// FailurePolicy defines what happens if the responsible initializer controller
// FailurePolicy defines what happens if the responsible initializer controller
...
@@ -97,7 +98,10 @@ type Rule struct {
...
@@ -97,7 +98,10 @@ type Rule struct {
// '*/scale' means all scale subresources.
// '*/scale' means all scale subresources.
// '*/*' means all resources and their subresources.
// '*/*' means all resources and their subresources.
//
//
// If '*' or '*/*' is present, the length of the slice must be one.
// If wildcard is present, the validation rule will ensure resources do not
// overlap with each other.
//
// Depending on the enclosing object, subresources might not be allowed.
// Required.
// Required.
Resources
[]
string
Resources
[]
string
}
}
...
...
pkg/apis/admissionregistration/v1alpha1/types.go
View file @
80a53d52
...
@@ -66,6 +66,7 @@ type Initializer struct {
...
@@ -66,6 +66,7 @@ type Initializer struct {
// Rules describes what resources/subresources the initializer cares about.
// Rules describes what resources/subresources the initializer cares about.
// The initializer cares about an operation if it matches _any_ Rule.
// The initializer cares about an operation if it matches _any_ Rule.
// Rule.Resources must not include subresources.
Rules
[]
Rule
`json:"rules,omitempty" protobuf:"bytes,2,rep,name=rules"`
Rules
[]
Rule
`json:"rules,omitempty" protobuf:"bytes,2,rep,name=rules"`
// FailurePolicy defines what happens if the responsible initializer controller
// FailurePolicy defines what happens if the responsible initializer controller
...
@@ -99,7 +100,10 @@ type Rule struct {
...
@@ -99,7 +100,10 @@ type Rule struct {
// '*/scale' means all scale subresources.
// '*/scale' means all scale subresources.
// '*/*' means all resources and their subresources.
// '*/*' means all resources and their subresources.
//
//
// If '*' or '*/*' is present, the length of the slice must be one.
// If wildcard is present, the validation rule will ensure resources do not
// overlap with each other.
//
// Depending on the enclosing object, subresources might not be allowed.
// Required.
// Required.
Resources
[]
string
`json:"resources,omitempty" protobuf:"bytes,3,rep,name=resources"`
Resources
[]
string
`json:"resources,omitempty" protobuf:"bytes,3,rep,name=resources"`
}
}
...
...
pkg/apis/admissionregistration/validation/validation.go
View file @
80a53d52
...
@@ -49,7 +49,8 @@ func validateInitializer(initializer *admissionregistration.Initializer, fldPath
...
@@ -49,7 +49,8 @@ func validateInitializer(initializer *admissionregistration.Initializer, fldPath
}
}
for
i
,
rule
:=
range
initializer
.
Rules
{
for
i
,
rule
:=
range
initializer
.
Rules
{
allErrors
=
append
(
allErrors
,
validateRule
(
&
rule
,
fldPath
.
Child
(
"rules"
)
.
Index
(
i
))
...
)
notAllowSubresources
:=
false
allErrors
=
append
(
allErrors
,
validateRule
(
&
rule
,
fldPath
.
Child
(
"rules"
)
.
Index
(
i
),
notAllowSubresources
)
...
)
}
}
// TODO: relax the validation rule when admissionregistration is beta.
// TODO: relax the validation rule when admissionregistration is beta.
if
initializer
.
FailurePolicy
!=
nil
&&
*
initializer
.
FailurePolicy
!=
admissionregistration
.
Ignore
{
if
initializer
.
FailurePolicy
!=
nil
&&
*
initializer
.
FailurePolicy
!=
admissionregistration
.
Ignore
{
...
@@ -102,10 +103,10 @@ func validateResources(resources []string, fldPath *field.Path) field.ErrorList
...
@@ -102,10 +103,10 @@ func validateResources(resources []string, fldPath *field.Path) field.ErrorList
}
}
res
,
sub
:=
parts
[
0
],
parts
[
1
]
res
,
sub
:=
parts
[
0
],
parts
[
1
]
if
_
,
ok
:=
resourcesWithWildcardSubresoures
[
res
];
ok
{
if
_
,
ok
:=
resourcesWithWildcardSubresoures
[
res
];
ok
{
allErrors
=
append
(
allErrors
,
field
.
Invalid
(
fldPath
.
Index
(
i
),
res
ources
[
i
],
fmt
.
Sprintf
(
"if '%s/*' is present, must not specify %s"
,
res
,
resources
[
i
]
)))
allErrors
=
append
(
allErrors
,
field
.
Invalid
(
fldPath
.
Index
(
i
),
res
Sub
,
fmt
.
Sprintf
(
"if '%s/*' is present, must not specify %s"
,
res
,
resSub
)))
}
}
if
_
,
ok
:=
subResoucesWithWildcardResource
[
sub
];
ok
{
if
_
,
ok
:=
subResoucesWithWildcardResource
[
sub
];
ok
{
allErrors
=
append
(
allErrors
,
field
.
Invalid
(
fldPath
.
Index
(
i
),
res
ources
[
i
],
fmt
.
Sprintf
(
"if '*/%s' is present, must not specify %s"
,
sub
,
resources
[
i
]
)))
allErrors
=
append
(
allErrors
,
field
.
Invalid
(
fldPath
.
Index
(
i
),
res
Sub
,
fmt
.
Sprintf
(
"if '*/%s' is present, must not specify %s"
,
sub
,
resSub
)))
}
}
if
sub
==
"*"
{
if
sub
==
"*"
{
resourcesWithWildcardSubresoures
[
res
]
=
struct
{}{}
resourcesWithWildcardSubresoures
[
res
]
=
struct
{}{}
...
@@ -123,7 +124,26 @@ func validateResources(resources []string, fldPath *field.Path) field.ErrorList
...
@@ -123,7 +124,26 @@ func validateResources(resources []string, fldPath *field.Path) field.ErrorList
return
allErrors
return
allErrors
}
}
func
validateRule
(
rule
*
admissionregistration
.
Rule
,
fldPath
*
field
.
Path
)
field
.
ErrorList
{
func
validateResourcesNoSubResources
(
resources
[]
string
,
fldPath
*
field
.
Path
)
field
.
ErrorList
{
var
allErrors
field
.
ErrorList
if
len
(
resources
)
==
0
{
allErrors
=
append
(
allErrors
,
field
.
Required
(
fldPath
,
""
))
}
for
i
,
resource
:=
range
resources
{
if
resource
==
""
{
allErrors
=
append
(
allErrors
,
field
.
Required
(
fldPath
.
Index
(
i
),
""
))
}
if
strings
.
Contains
(
resource
,
"/"
)
{
allErrors
=
append
(
allErrors
,
field
.
Invalid
(
fldPath
.
Index
(
i
),
resource
,
"must not specify subresources"
))
}
}
if
len
(
resources
)
>
1
&&
hasWildcard
(
resources
)
{
allErrors
=
append
(
allErrors
,
field
.
Invalid
(
fldPath
,
resources
,
"if '*' is present, must not specify other resources"
))
}
return
allErrors
}
func
validateRule
(
rule
*
admissionregistration
.
Rule
,
fldPath
*
field
.
Path
,
allowSubResource
bool
)
field
.
ErrorList
{
var
allErrors
field
.
ErrorList
var
allErrors
field
.
ErrorList
if
len
(
rule
.
APIGroups
)
==
0
{
if
len
(
rule
.
APIGroups
)
==
0
{
allErrors
=
append
(
allErrors
,
field
.
Required
(
fldPath
.
Child
(
"apiGroups"
),
""
))
allErrors
=
append
(
allErrors
,
field
.
Required
(
fldPath
.
Child
(
"apiGroups"
),
""
))
...
@@ -143,7 +163,11 @@ func validateRule(rule *admissionregistration.Rule, fldPath *field.Path) field.E
...
@@ -143,7 +163,11 @@ func validateRule(rule *admissionregistration.Rule, fldPath *field.Path) field.E
allErrors
=
append
(
allErrors
,
field
.
Required
(
fldPath
.
Child
(
"apiVersions"
)
.
Index
(
i
),
""
))
allErrors
=
append
(
allErrors
,
field
.
Required
(
fldPath
.
Child
(
"apiVersions"
)
.
Index
(
i
),
""
))
}
}
}
}
if
allowSubResource
{
allErrors
=
append
(
allErrors
,
validateResources
(
rule
.
Resources
,
fldPath
.
Child
(
"resources"
))
...
)
allErrors
=
append
(
allErrors
,
validateResources
(
rule
.
Resources
,
fldPath
.
Child
(
"resources"
))
...
)
}
else
{
allErrors
=
append
(
allErrors
,
validateResourcesNoSubResources
(
rule
.
Resources
,
fldPath
.
Child
(
"resources"
))
...
)
}
return
allErrors
return
allErrors
}
}
...
@@ -154,7 +178,7 @@ func ValidateInitializerConfigurationUpdate(newIC, oldIC *admissionregistration.
...
@@ -154,7 +178,7 @@ func ValidateInitializerConfigurationUpdate(newIC, oldIC *admissionregistration.
func
ValidateExternalAdmissionHookConfiguration
(
e
*
admissionregistration
.
ExternalAdmissionHookConfiguration
)
field
.
ErrorList
{
func
ValidateExternalAdmissionHookConfiguration
(
e
*
admissionregistration
.
ExternalAdmissionHookConfiguration
)
field
.
ErrorList
{
allErrors
:=
genericvalidation
.
ValidateObjectMeta
(
&
e
.
ObjectMeta
,
false
,
genericvalidation
.
NameIsDNSSubdomain
,
field
.
NewPath
(
"metadata"
))
allErrors
:=
genericvalidation
.
ValidateObjectMeta
(
&
e
.
ObjectMeta
,
false
,
genericvalidation
.
NameIsDNSSubdomain
,
field
.
NewPath
(
"metadata"
))
for
i
,
hook
:=
range
e
.
ExternalAdmissionHooks
{
for
i
,
hook
:=
range
e
.
ExternalAdmissionHooks
{
allErrors
=
append
(
allErrors
,
validateExternalAdmissionHook
(
&
hook
,
field
.
NewPath
(
"externalAdmissionHook"
)
.
Index
(
i
))
...
)
allErrors
=
append
(
allErrors
,
validateExternalAdmissionHook
(
&
hook
,
field
.
NewPath
(
"externalAdmissionHook
s
"
)
.
Index
(
i
))
...
)
}
}
return
allErrors
return
allErrors
}
}
...
@@ -212,7 +236,8 @@ func validateRuleWithOperations(ruleWithOperations *admissionregistration.RuleWi
...
@@ -212,7 +236,8 @@ func validateRuleWithOperations(ruleWithOperations *admissionregistration.RuleWi
allErrors
=
append
(
allErrors
,
field
.
NotSupported
(
fldPath
.
Child
(
"operations"
)
.
Index
(
i
),
operation
,
supportedOperations
.
List
()))
allErrors
=
append
(
allErrors
,
field
.
NotSupported
(
fldPath
.
Child
(
"operations"
)
.
Index
(
i
),
operation
,
supportedOperations
.
List
()))
}
}
}
}
allErrors
=
append
(
allErrors
,
validateRule
(
&
ruleWithOperations
.
Rule
,
fldPath
)
...
)
allowSubResource
:=
true
allErrors
=
append
(
allErrors
,
validateRule
(
&
ruleWithOperations
.
Rule
,
fldPath
,
allowSubResource
)
...
)
return
allErrors
return
allErrors
}
}
...
...
pkg/apis/admissionregistration/validation/validation_test.go
View file @
80a53d52
This diff is collapsed.
Click to expand it.
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