Skip to content

Commit 744596a

Browse files
authored
Merge pull request #2 from EarnestResearch/refactor-mutating-webhook
Refactor mutatingWebhook to disallow nonsensical states
2 parents cb28ba9 + 0893cd9 commit 744596a

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ server :: Server API
6868
server = mutate
6969

7070
mutate :: W.AdmissionReviewRequest -> Handler W.AdmissionReviewResponse
71-
mutate req = pure $ W.mutatingWebhook req (\_ -> Right W.Allowed) addToleration
71+
mutate req = pure $ W.mutatingWebhook req (\_ -> Right addToleration)
7272

7373
addToleration :: W.Patch
7474
addToleration =

src/Kubernetes/Webhook.hs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
-- server = mutate
7777
--
7878
-- mutate :: W.AdmissionReviewRequest -> Handler W.AdmissionReviewResponse
79-
-- mutate req = pure $ W.mutatingWebhook req (\_ -> Right W.Allowed) addToleration
79+
-- mutate req = pure $ W.mutatingWebhook req (\_ -> Right addToleration)
8080
--
8181
-- addToleration :: W.Patch
8282
-- addToleration =
@@ -106,24 +106,22 @@ data Allowed = Allowed
106106
mutatingWebhook ::
107107
-- | the request the webhook receives from Kubernetes
108108
AdmissionReviewRequest ->
109-
-- | logic to validate the request or reject it with an error
110-
(AdmissionRequest -> Either Status Allowed) ->
111-
-- | the change to apply to the object
112-
Patch ->
109+
-- | logic to validate the request by returning the change to apply to the object or reject the request with an error
110+
(AdmissionRequest -> Either Status Patch) ->
113111
-- | the response sent back to Kubernetes
114112
AdmissionReviewResponse
115-
mutatingWebhook AdmissionReviewRequest {request = req} allow patch =
113+
mutatingWebhook AdmissionReviewRequest {request = req} mutator =
116114
admissionReviewResponse AdmissionResponse
117115
{ uid = rid,
118116
allowed = isRight processedRequest,
119-
patch = Just patch,
117+
patch = either (const Nothing) Just processedRequest,
120118
status = either Just (const Nothing) processedRequest,
121119
patchType = Just JSONPatch,
122120
auditAnnotations = Nothing
123121
}
124122
where
125123
AdmissionRequest {uid = rid} = req
126-
processedRequest = allow req
124+
processedRequest = mutator req
127125

128126
-- | Lets you create a validating admission webhook
129127
validatingWebhook ::

0 commit comments

Comments
 (0)