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
932a0729
Commit
932a0729
authored
Mar 22, 2018
by
hzxuzhonghu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add e2e case for crd webhook
parent
4c268311
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
97 additions
and
0 deletions
+97
-0
webhook.go
test/e2e/apimachinery/webhook.go
+97
-0
No files found.
test/e2e/apimachinery/webhook.go
View file @
932a0729
...
@@ -27,6 +27,7 @@ import (
...
@@ -27,6 +27,7 @@ import (
extensions
"k8s.io/api/extensions/v1beta1"
extensions
"k8s.io/api/extensions/v1beta1"
rbacv1beta1
"k8s.io/api/rbac/v1beta1"
rbacv1beta1
"k8s.io/api/rbac/v1beta1"
apiextensionsv1beta1
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsv1beta1
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
crdclientset
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/errors"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
...
@@ -161,6 +162,13 @@ var _ = SIGDescribe("AdmissionWebhook", func() {
...
@@ -161,6 +162,13 @@ var _ = SIGDescribe("AdmissionWebhook", func() {
testMutatingCRDWebhook
(
f
,
testcrd
.
Crd
,
testcrd
.
DynamicClient
)
testMutatingCRDWebhook
(
f
,
testcrd
.
Crd
,
testcrd
.
DynamicClient
)
})
})
It
(
"Should deny crd creation"
,
func
()
{
crdWebhookCleanup
:=
registerValidatingWebhookForCRD
(
f
,
context
)
defer
crdWebhookCleanup
()
testCRDDenyWebhook
(
f
)
})
// TODO: add more e2e tests for mutating webhooks
// TODO: add more e2e tests for mutating webhooks
// 1. mutating webhook that mutates pod
// 1. mutating webhook that mutates pod
// 2. mutating webhook that sends empty patch
// 2. mutating webhook that sends empty patch
...
@@ -1121,3 +1129,92 @@ func testMutatingCRDWebhook(f *framework.Framework, crd *apiextensionsv1beta1.Cu
...
@@ -1121,3 +1129,92 @@ func testMutatingCRDWebhook(f *framework.Framework, crd *apiextensionsv1beta1.Cu
framework
.
Failf
(
"
\n
expected %#v
\n
, got %#v
\n
"
,
expectedCRData
,
mutatedCR
.
Object
[
"data"
])
framework
.
Failf
(
"
\n
expected %#v
\n
, got %#v
\n
"
,
expectedCRData
,
mutatedCR
.
Object
[
"data"
])
}
}
}
}
func
registerValidatingWebhookForCRD
(
f
*
framework
.
Framework
,
context
*
certContext
)
func
()
{
client
:=
f
.
ClientSet
By
(
"Registering the crd webhook via the AdmissionRegistration API"
)
namespace
:=
f
.
Namespace
.
Name
configName
:=
webhookConfigName
_
,
err
:=
client
.
AdmissionregistrationV1beta1
()
.
ValidatingWebhookConfigurations
()
.
Create
(
&
v1beta1
.
ValidatingWebhookConfiguration
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
configName
,
},
Webhooks
:
[]
v1beta1
.
Webhook
{
{
Name
:
"deny-crd.k8s.io"
,
Rules
:
[]
v1beta1
.
RuleWithOperations
{{
Operations
:
[]
v1beta1
.
OperationType
{
v1beta1
.
Create
},
Rule
:
v1beta1
.
Rule
{
APIGroups
:
[]
string
{
"apiextensions.k8s.io"
},
APIVersions
:
[]
string
{
"*"
},
Resources
:
[]
string
{
"customresourcedefinitions"
},
},
}},
ClientConfig
:
v1beta1
.
WebhookClientConfig
{
Service
:
&
v1beta1
.
ServiceReference
{
Namespace
:
namespace
,
Name
:
serviceName
,
Path
:
strPtr
(
"/always-deny"
),
},
CABundle
:
context
.
signingCert
,
},
},
},
})
framework
.
ExpectNoError
(
err
,
"registering crd webhook config %s with namespace %s"
,
configName
,
namespace
)
// The webhook configuration is honored in 10s.
time
.
Sleep
(
10
*
time
.
Second
)
return
func
()
{
client
.
AdmissionregistrationV1beta1
()
.
ValidatingWebhookConfigurations
()
.
Delete
(
configName
,
nil
)
}
}
func
testCRDDenyWebhook
(
f
*
framework
.
Framework
)
{
By
(
"Creating a custom resource definition that should be denied by the webhook"
)
name
:=
fmt
.
Sprintf
(
"e2e-test-%s-%s-crd"
,
f
.
BaseName
,
"deny"
)
kind
:=
fmt
.
Sprintf
(
"E2e-test-%s-%s-crd"
,
f
.
BaseName
,
"deny"
)
group
:=
fmt
.
Sprintf
(
"%s-crd-test.k8s.io"
,
f
.
BaseName
)
apiVersion
:=
"v1"
testcrd
:=
&
framework
.
TestCrd
{
Name
:
name
,
Kind
:
kind
,
ApiGroup
:
group
,
ApiVersion
:
apiVersion
,
}
// Creating a custom resource definition for use by assorted tests.
config
,
err
:=
framework
.
LoadConfig
()
if
err
!=
nil
{
framework
.
Failf
(
"failed to load config: %v"
,
err
)
return
}
apiExtensionClient
,
err
:=
crdclientset
.
NewForConfig
(
config
)
if
err
!=
nil
{
framework
.
Failf
(
"failed to initialize apiExtensionClient: %v"
,
err
)
return
}
crd
:=
&
apiextensionsv1beta1
.
CustomResourceDefinition
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
testcrd
.
GetMetaName
()},
Spec
:
apiextensionsv1beta1
.
CustomResourceDefinitionSpec
{
Group
:
testcrd
.
ApiGroup
,
Version
:
testcrd
.
ApiVersion
,
Names
:
apiextensionsv1beta1
.
CustomResourceDefinitionNames
{
Plural
:
testcrd
.
GetPluralName
(),
Singular
:
testcrd
.
Name
,
Kind
:
testcrd
.
Kind
,
ListKind
:
testcrd
.
GetListName
(),
},
Scope
:
apiextensionsv1beta1
.
NamespaceScoped
,
},
}
// create CRD
_
,
err
=
apiExtensionClient
.
ApiextensionsV1beta1
()
.
CustomResourceDefinitions
()
.
Create
(
crd
)
Expect
(
err
)
.
NotTo
(
BeNil
())
expectedErrMsg
:=
"this webhook denies all requests"
if
!
strings
.
Contains
(
err
.
Error
(),
expectedErrMsg
)
{
framework
.
Failf
(
"expect error contains %q, got %q"
,
expectedErrMsg
,
err
.
Error
())
}
}
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