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
948bd7bb
Commit
948bd7bb
authored
Oct 27, 2017
by
Janet Kuo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add hook information when rejecting a request
parent
298c42bb
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
76 additions
and
5 deletions
+76
-5
BUILD
...g/src/k8s.io/apiserver/pkg/admission/plugin/webhook/BUILD
+1
-0
admission.go
...8s.io/apiserver/pkg/admission/plugin/webhook/admission.go
+22
-5
admission_test.go
.../apiserver/pkg/admission/plugin/webhook/admission_test.go
+53
-0
No files found.
staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/BUILD
View file @
948bd7bb
...
...
@@ -51,6 +51,7 @@ go_test(
"//vendor/k8s.io/api/admissionregistration/v1alpha1:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/equality:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
...
...
staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/admission.go
View file @
948bd7bb
...
...
@@ -218,11 +218,11 @@ func (a *GenericAdmissionWebhook) Admit(attr admission.Attributes) error {
}
glog
.
Warningf
(
"Failed calling webhook, failing closed %v: %v"
,
hook
.
Name
,
err
)
errCh
<-
err
errCh
<-
apierrors
.
NewInternalError
(
err
)
return
}
glog
.
Warningf
(
"rejected by webhook %
v %t: %v"
,
hook
.
Name
,
err
,
err
)
glog
.
Warningf
(
"rejected by webhook %
q: %#v"
,
hook
.
Name
,
err
)
errCh
<-
err
}(
&
hooks
[
i
])
}
...
...
@@ -273,12 +273,29 @@ func (a *GenericAdmissionWebhook) callHook(ctx context.Context, h *v1alpha1.Exte
return
nil
}
if
response
.
Status
.
Result
==
nil
{
return
fmt
.
Errorf
(
"admission webhook %q denied the request without explanation"
,
h
.
Name
)
return
toStatusErr
(
h
.
Name
,
response
.
Status
.
Result
)
}
// toStatusErr returns a StatusError with information about the webhook controller
func
toStatusErr
(
name
string
,
result
*
metav1
.
Status
)
*
apierrors
.
StatusError
{
deniedBy
:=
fmt
.
Sprintf
(
"admission webhook %q denied the request"
,
name
)
const
noExp
=
"without explanation"
if
result
==
nil
{
result
=
&
metav1
.
Status
{
Status
:
metav1
.
StatusFailure
}
}
switch
{
case
len
(
result
.
Message
)
>
0
:
result
.
Message
=
fmt
.
Sprintf
(
"%s: %s"
,
deniedBy
,
result
.
Message
)
case
len
(
result
.
Reason
)
>
0
:
result
.
Message
=
fmt
.
Sprintf
(
"%s: %s"
,
deniedBy
,
result
.
Reason
)
default
:
result
.
Message
=
fmt
.
Sprintf
(
"%s %s"
,
deniedBy
,
noExp
)
}
return
&
apierrors
.
StatusError
{
ErrStatus
:
*
res
ponse
.
Status
.
Res
ult
,
ErrStatus
:
*
result
,
}
}
...
...
staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/admission_test.go
View file @
948bd7bb
...
...
@@ -30,6 +30,7 @@ import (
"k8s.io/api/admission/v1alpha1"
registrationv1alpha1
"k8s.io/api/admissionregistration/v1alpha1"
api
"k8s.io/api/core/v1"
apierrors
"k8s.io/apimachinery/pkg/api/errors"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/admission"
...
...
@@ -280,6 +281,9 @@ func TestAdmit(t *testing.T) {
t
.
Errorf
(
" expected an error saying %q, but got %v"
,
tt
.
errorContains
,
err
)
}
}
if
_
,
isStatusErr
:=
err
.
(
*
apierrors
.
StatusError
);
err
!=
nil
&&
!
isStatusErr
{
t
.
Errorf
(
"%s: expected a StatusError, got %T"
,
name
,
err
)
}
})
}
}
...
...
@@ -333,3 +337,52 @@ type fakeAuthenticationInfoResolver struct {
func
(
c
*
fakeAuthenticationInfoResolver
)
ClientConfigFor
(
server
string
)
(
*
rest
.
Config
,
error
)
{
return
c
.
restConfig
,
nil
}
func
TestToStatusErr
(
t
*
testing
.
T
)
{
hookName
:=
"foo"
deniedBy
:=
fmt
.
Sprintf
(
"admission webhook %q denied the request"
,
hookName
)
tests
:=
[]
struct
{
name
string
result
*
metav1
.
Status
expectedError
string
}{
{
"nil result"
,
nil
,
deniedBy
+
" without explanation"
,
},
{
"only message"
,
&
metav1
.
Status
{
Message
:
"you shall not pass"
,
},
deniedBy
+
": you shall not pass"
,
},
{
"only reason"
,
&
metav1
.
Status
{
Reason
:
metav1
.
StatusReasonForbidden
,
},
deniedBy
+
": Forbidden"
,
},
{
"message and reason"
,
&
metav1
.
Status
{
Message
:
"you shall not pass"
,
Reason
:
metav1
.
StatusReasonForbidden
,
},
deniedBy
+
": you shall not pass"
,
},
{
"no message, no reason"
,
&
metav1
.
Status
{},
deniedBy
+
" without explanation"
,
},
}
for
_
,
test
:=
range
tests
{
err
:=
toStatusErr
(
hookName
,
test
.
result
)
if
err
==
nil
||
err
.
Error
()
!=
test
.
expectedError
{
t
.
Errorf
(
"%s: expected an error saying %q, but got %v"
,
test
.
name
,
test
.
expectedError
,
err
)
}
}
}
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