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
f2abdcf4
Commit
f2abdcf4
authored
May 20, 2019
by
Jordan Liggitt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Consider equivalent resources when calling webhook
parent
8504a0e5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
3 deletions
+46
-3
BUILD
...s.io/apiserver/pkg/admission/plugin/webhook/generic/BUILD
+6
-1
webhook.go
...apiserver/pkg/admission/plugin/webhook/generic/webhook.go
+40
-2
webhook_test.go
...rver/pkg/admission/plugin/webhook/generic/webhook_test.go
+0
-0
No files found.
staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/generic/BUILD
View file @
f2abdcf4
...
...
@@ -43,15 +43,20 @@ filegroup(
go_test(
name = "go_default_test",
srcs = ["conversion_test.go"],
srcs = [
"conversion_test.go",
"webhook_test.go",
],
embed = [":go_default_library"],
deps = [
"//staging/src/k8s.io/api/admissionregistration/v1beta1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/admission:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/namespace:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/apis/example:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/apis/example/v1:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/apis/example2/v1:go_default_library",
...
...
staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/generic/webhook.go
View file @
f2abdcf4
...
...
@@ -24,6 +24,7 @@ import (
admissionv1beta1
"k8s.io/api/admission/v1beta1"
"k8s.io/api/admissionregistration/v1beta1"
apierrors
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/admission"
genericadmissioninit
"k8s.io/apiserver/pkg/admission/initializer"
"k8s.io/apiserver/pkg/admission/plugin/webhook/config"
...
...
@@ -127,7 +128,7 @@ func (a *Webhook) ValidateInitialization() error {
// shouldCallHook returns invocation details if the webhook should be called, nil if the webhook should not be called,
// or an error if an error was encountered during evaluation.
func
(
a
*
Webhook
)
shouldCallHook
(
h
*
v1beta1
.
Webhook
,
attr
admission
.
Attributes
)
(
*
WebhookInvocation
,
*
apierrors
.
StatusError
)
{
func
(
a
*
Webhook
)
shouldCallHook
(
h
*
v1beta1
.
Webhook
,
attr
admission
.
Attributes
,
o
admission
.
ObjectInterfaces
)
(
*
WebhookInvocation
,
*
apierrors
.
StatusError
)
{
var
err
*
apierrors
.
StatusError
var
invocation
*
WebhookInvocation
for
_
,
r
:=
range
h
.
Rules
{
...
...
@@ -142,6 +143,36 @@ func (a *Webhook) shouldCallHook(h *v1beta1.Webhook, attr admission.Attributes)
break
}
}
if
invocation
==
nil
&&
h
.
MatchPolicy
!=
nil
&&
*
h
.
MatchPolicy
==
v1beta1
.
Equivalent
{
attrWithOverride
:=
&
attrWithResourceOverride
{
Attributes
:
attr
}
equivalents
:=
o
.
GetEquivalentResourceMapper
()
.
EquivalentResourcesFor
(
attr
.
GetResource
(),
attr
.
GetSubresource
())
// honor earlier rules first
OuterLoop
:
for
_
,
r
:=
range
h
.
Rules
{
// see if the rule matches any of the equivalent resources
for
_
,
equivalent
:=
range
equivalents
{
if
equivalent
==
attr
.
GetResource
()
{
// exclude attr.GetResource(), which we already checked
continue
}
attrWithOverride
.
resource
=
equivalent
m
:=
rules
.
Matcher
{
Rule
:
r
,
Attr
:
attrWithOverride
}
if
m
.
Matches
()
{
kind
:=
o
.
GetEquivalentResourceMapper
()
.
KindFor
(
equivalent
,
attr
.
GetSubresource
())
if
kind
.
Empty
()
{
return
nil
,
apierrors
.
NewInternalError
(
fmt
.
Errorf
(
"unable to convert to %v: unknown kind"
,
equivalent
))
}
invocation
=
&
WebhookInvocation
{
Webhook
:
h
,
Resource
:
equivalent
,
Subresource
:
attr
.
GetSubresource
(),
Kind
:
kind
,
}
break
OuterLoop
}
}
}
}
if
invocation
==
nil
{
return
nil
,
nil
...
...
@@ -155,6 +186,13 @@ func (a *Webhook) shouldCallHook(h *v1beta1.Webhook, attr admission.Attributes)
return
invocation
,
nil
}
type
attrWithResourceOverride
struct
{
admission
.
Attributes
resource
schema
.
GroupVersionResource
}
func
(
a
*
attrWithResourceOverride
)
GetResource
()
schema
.
GroupVersionResource
{
return
a
.
resource
}
// Dispatch is called by the downstream Validate or Admit methods.
func
(
a
*
Webhook
)
Dispatch
(
attr
admission
.
Attributes
,
o
admission
.
ObjectInterfaces
)
error
{
if
rules
.
IsWebhookConfigurationResource
(
attr
)
{
...
...
@@ -169,7 +207,7 @@ func (a *Webhook) Dispatch(attr admission.Attributes, o admission.ObjectInterfac
var
relevantHooks
[]
*
WebhookInvocation
for
i
:=
range
hooks
{
invocation
,
err
:=
a
.
shouldCallHook
(
&
hooks
[
i
],
attr
)
invocation
,
err
:=
a
.
shouldCallHook
(
&
hooks
[
i
],
attr
,
o
)
if
err
!=
nil
{
return
err
}
...
...
staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/generic/webhook_test.go
0 → 100644
View file @
f2abdcf4
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