Skip to content

Commit bb00054

Browse files
authored
Merge pull request kubernetes#77578 from jpbetz/admission-integration-filter
Fix admission webhook integration tests to filter out controller requests
2 parents 124926f + b0aab03 commit bb00054

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

test/integration/apiserver/admissionwebhook/admission_test.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ import (
5252
)
5353

5454
const (
55-
testNamespace = "webhook-integration"
55+
testNamespace = "webhook-integration"
56+
testClientUsername = "webhook-integration-client"
5657

5758
mutation = "mutation"
5859
validation = "validation"
@@ -336,19 +337,34 @@ func TestWebhookV1beta1(t *testing.T) {
336337
})
337338
defer master.Cleanup()
338339

339-
if _, err := master.Client.CoreV1().Namespaces().Create(&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: testNamespace}}); err != nil {
340+
// Configure a client with a distinct user name so that it is easy to distinguish requests
341+
// made by the client from requests made by controllers. We use this to filter out requests
342+
// before recording them to ensure we don't accidentally mistake requests from controllers
343+
// as requests made by the client.
344+
clientConfig := master.Config
345+
clientConfig.Impersonate.UserName = testClientUsername
346+
clientConfig.Impersonate.Groups = []string{"system:masters", "system:authenticated"}
347+
client, err := clientset.NewForConfig(clientConfig)
348+
if err != nil {
340349
t.Fatal(err)
341350
}
342-
if err := createV1beta1MutationWebhook(master.Client, webhookServer.URL+"/"+mutation); err != nil {
351+
352+
if _, err := client.CoreV1().Namespaces().Create(&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: testNamespace}}); err != nil {
343353
t.Fatal(err)
344354
}
345-
if err := createV1beta1ValidationWebhook(master.Client, webhookServer.URL+"/"+validation); err != nil {
355+
if err := createV1beta1MutationWebhook(client, webhookServer.URL+"/"+mutation); err != nil {
356+
t.Fatal(err)
357+
}
358+
if err := createV1beta1ValidationWebhook(client, webhookServer.URL+"/"+validation); err != nil {
346359
t.Fatal(err)
347360
}
348361

349362
// gather resources to test
350-
dynamicClient := master.Dynamic
351-
_, resources, err := master.Client.Discovery().ServerGroupsAndResources()
363+
dynamicClient, err := dynamic.NewForConfig(clientConfig)
364+
if err != nil {
365+
t.Fatal(err)
366+
}
367+
_, resources, err := client.Discovery().ServerGroupsAndResources()
352368
if err != nil {
353369
t.Fatalf("Failed to get ServerGroupsAndResources with error: %+v", err)
354370
}
@@ -412,7 +428,7 @@ func TestWebhookV1beta1(t *testing.T) {
412428
t: t,
413429
admissionHolder: holder,
414430
client: dynamicClient,
415-
clientset: master.Client,
431+
clientset: client,
416432
verb: verb,
417433
gvr: gvr,
418434
resource: resource,
@@ -938,7 +954,11 @@ func newWebhookHandler(t *testing.T, holder *holder, phase string) http.Handler
938954
}
939955
review.Request.OldObject.Object = u
940956
}
941-
holder.record(phase, review.Request)
957+
958+
if review.Request.UserInfo.Username == testClientUsername {
959+
// only record requests originating from this integration test's client
960+
holder.record(phase, review.Request)
961+
}
942962

943963
review.Response = &v1beta1.AdmissionResponse{
944964
Allowed: true,

0 commit comments

Comments
 (0)