@@ -26,107 +26,107 @@ import (
26
26
admissionv1beta1 "k8s.io/api/admission/v1beta1"
27
27
)
28
28
29
- var _ = Describe ("Admission Webhooks" , func () {
30
- Describe ("Multi-handler Webhooks" , func () {
31
- alwaysAllow := & fakeHandler {
29
+ var _ = Describe ("Multi-Handler Admission Webhooks" , func () {
30
+ alwaysAllow := & fakeHandler {
31
+ fn : func (ctx context.Context , req Request ) Response {
32
+ return Response {
33
+ AdmissionResponse : admissionv1beta1.AdmissionResponse {
34
+ Allowed : true ,
35
+ },
36
+ }
37
+ },
38
+ }
39
+ alwaysDeny := & fakeHandler {
40
+ fn : func (ctx context.Context , req Request ) Response {
41
+ return Response {
42
+ AdmissionResponse : admissionv1beta1.AdmissionResponse {
43
+ Allowed : false ,
44
+ },
45
+ }
46
+ },
47
+ }
48
+
49
+ Context ("with validating handlers" , func () {
50
+ It ("should deny the request if any handler denies the request" , func () {
51
+ By ("setting up a handler with accept and deny" )
52
+ handler := MultiValidatingHandler (alwaysAllow , alwaysDeny )
53
+
54
+ By ("checking that the handler denies the request" )
55
+ resp := handler .Handle (context .Background (), Request {})
56
+ Expect (resp .Allowed ).To (BeFalse ())
57
+ })
58
+
59
+ It ("should allow the request if all handlers allow the request" , func () {
60
+ By ("setting up a handler with only accept" )
61
+ handler := MultiValidatingHandler (alwaysAllow , alwaysAllow )
62
+
63
+ By ("checking that the handler allows the request" )
64
+ resp := handler .Handle (context .Background (), Request {})
65
+ Expect (resp .Allowed ).To (BeTrue ())
66
+ })
67
+ })
68
+
69
+ Context ("with mutating handlers" , func () {
70
+ patcher1 := & fakeHandler {
32
71
fn : func (ctx context.Context , req Request ) Response {
33
72
return Response {
73
+ Patches : []jsonpatch.JsonPatchOperation {
74
+ {
75
+ Operation : "add" ,
76
+ Path : "/metadata/annotation/new-key" ,
77
+ Value : "new-value" ,
78
+ },
79
+ {
80
+ Operation : "replace" ,
81
+ Path : "/spec/replicas" ,
82
+ Value : "2" ,
83
+ },
84
+ },
34
85
AdmissionResponse : admissionv1beta1.AdmissionResponse {
35
- Allowed : true ,
86
+ Allowed : true ,
87
+ PatchType : func () * admissionv1beta1.PatchType { pt := admissionv1beta1 .PatchTypeJSONPatch ; return & pt }(),
36
88
},
37
89
}
38
90
},
39
91
}
40
- alwaysDeny := & fakeHandler {
92
+ patcher2 := & fakeHandler {
41
93
fn : func (ctx context.Context , req Request ) Response {
42
94
return Response {
95
+ Patches : []jsonpatch.JsonPatchOperation {
96
+ {
97
+ Operation : "add" ,
98
+ Path : "/metadata/annotation/hello" ,
99
+ Value : "world" ,
100
+ },
101
+ },
43
102
AdmissionResponse : admissionv1beta1.AdmissionResponse {
44
- Allowed : false ,
103
+ Allowed : true ,
104
+ PatchType : func () * admissionv1beta1.PatchType { pt := admissionv1beta1 .PatchTypeJSONPatch ; return & pt }(),
45
105
},
46
106
}
47
107
},
48
108
}
49
109
50
- Context ("with validating handlers" , func () {
51
- It ("should deny the request if any handler denies the request" , func () {
52
- By ("setting up a handler with accept and deny" )
53
- handler := MultiValidatingHandler (alwaysAllow , alwaysDeny )
54
-
55
- By ("checking that the handler denies the request" )
56
- resp := handler .Handle (context .Background (), Request {})
57
- Expect (resp .Allowed ).To (BeFalse ())
58
- })
110
+ It ("should not return any patches if the request is denied" , func () {
111
+ By ("setting up a webhook with some patches and a deny" )
112
+ handler := MultiMutatingHandler (patcher1 , patcher2 , alwaysDeny )
59
113
60
- It ("should allow the request if all handlers allow the request" , func () {
61
- By ("setting up a handler with only accept" )
62
- handler := MultiValidatingHandler (alwaysAllow , alwaysAllow )
63
-
64
- By ("checking that the handler allows the request" )
65
- resp := handler .Handle (context .Background (), Request {})
66
- Expect (resp .Allowed ).To (BeTrue ())
67
- })
114
+ By ("checking that the handler denies the request and produces no patches" )
115
+ resp := handler .Handle (context .Background (), Request {})
116
+ Expect (resp .Allowed ).To (BeFalse ())
117
+ Expect (resp .Patches ).To (BeEmpty ())
68
118
})
69
119
70
- Context ("with mutating handlers" , func () {
71
- patcher1 := & fakeHandler {
72
- fn : func (ctx context.Context , req Request ) Response {
73
- return Response {
74
- Patches : []jsonpatch.JsonPatchOperation {
75
- {
76
- Operation : "add" ,
77
- Path : "/metadata/annotation/new-key" ,
78
- Value : "new-value" ,
79
- },
80
- {
81
- Operation : "replace" ,
82
- Path : "/spec/replicas" ,
83
- Value : "2" ,
84
- },
85
- },
86
- AdmissionResponse : admissionv1beta1.AdmissionResponse {
87
- Allowed : true ,
88
- PatchType : func () * admissionv1beta1.PatchType { pt := admissionv1beta1 .PatchTypeJSONPatch ; return & pt }(),
89
- },
90
- }
91
- },
92
- }
93
- patcher2 := & fakeHandler {
94
- fn : func (ctx context.Context , req Request ) Response {
95
- return Response {
96
- Patches : []jsonpatch.JsonPatchOperation {
97
- {
98
- Operation : "add" ,
99
- Path : "/metadata/annotation/hello" ,
100
- Value : "world" ,
101
- },
102
- },
103
- AdmissionResponse : admissionv1beta1.AdmissionResponse {
104
- Allowed : true ,
105
- PatchType : func () * admissionv1beta1.PatchType { pt := admissionv1beta1 .PatchTypeJSONPatch ; return & pt }(),
106
- },
107
- }
108
- },
109
- }
120
+ It ("should produce all patches if the requests are all allowed" , func () {
121
+ By ("setting up a webhook with some patches" )
122
+ handler := MultiMutatingHandler (patcher1 , patcher2 , alwaysAllow )
110
123
111
- It ("should not return any patches if the request is denied" , func () {
112
- By ("setting up a webhook with some patches and a deny" )
113
- handler := MultiMutatingHandler (patcher1 , patcher2 , alwaysDeny )
114
-
115
- By ("checking that the handler denies the request and produces no patches" )
116
- resp := handler .Handle (context .Background (), Request {})
117
- Expect (resp .Allowed ).To (BeFalse ())
118
- Expect (resp .Patches ).To (BeEmpty ())
119
- })
120
-
121
- It ("should produce all patches if the requests are all allowed" , func () {
122
- By ("setting up a webhook with some patches" )
123
- handler := MultiMutatingHandler (patcher1 , patcher2 , alwaysAllow )
124
-
125
- By ("checking that the handler accepts the request and returns all patches" )
126
- resp := handler .Handle (context .Background (), Request {})
127
- Expect (resp .Allowed ).To (BeTrue ())
128
- Expect (resp .Patch ).To (Equal ([]byte (`[{"op":"add","path":"/metadata/annotation/new-key","value":"new-value"},{"op":"replace","path":"/spec/replicas","value":"2"},{"op":"add","path":"/metadata/annotation/hello","value":"world"}]` )))
129
- })
124
+ By ("checking that the handler accepts the request and returns all patches" )
125
+ resp := handler .Handle (context .Background (), Request {})
126
+ Expect (resp .Allowed ).To (BeTrue ())
127
+ Expect (resp .Patch ).To (Equal ([]byte (
128
+ `[{"op":"add","path":"/metadata/annotation/new-key","value":"new-value"},` +
129
+ `{"op":"replace","path":"/spec/replicas","value":"2"},{"op":"add","path":"/metadata/annotation/hello","value":"world"}]` )))
130
130
})
131
131
})
132
132
})
0 commit comments