Skip to content

Commit bfc442b

Browse files
Merge pull request #18 from sabre1041/olm-creation-time-webhook
Steps to enable creation time webhooks using OLM
2 parents 2766ff4 + 74f3417 commit bfc442b

File tree

1 file changed

+99
-5
lines changed

1 file changed

+99
-5
lines changed

readme.md

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,30 +116,120 @@ The lookup function, if used by the template, is executed with a client which im
116116

117117
The creation time webhook is not installed by the operator. This is because there is no way to know which specific object type should be intercepted and intercepting all of the types would be too inefficient. It's up to the administrator then to install the webhook. Here is some guidance.
118118

119-
If you installed the operator via OLM, use the following webhook template:
119+
The configurations that needs to be applied in order to support creation time webhooks depends on how the operator was installed (OLM or Helm chart).
120+
121+
#### Enabling creation time time webhook (OLM)
122+
123+
If you installed the operator via OLM, the certificate that the OLM generates to expose the webhook need to be applied dynamically to the `MutatingWebhookConfiguration` resource. To support this action, we can use the Patch operator to perform the needed configurations.
124+
125+
First, create the following resources which will create a ServiceAccount and RBAC policies so that the operator can apply the needed configurations:
126+
127+
```yaml
128+
---
129+
apiVersion: v1
130+
kind: ServiceAccount
131+
metadata:
132+
creationTimestamp: null
133+
name: mutatingwebhook-patcher
134+
namespace: patch-operator
135+
---
136+
apiVersion: rbac.authorization.k8s.io/v1
137+
kind: ClusterRole
138+
metadata:
139+
labels:
140+
name: mutatingwebhookconfiguration-patcher
141+
rules:
142+
- apiGroups:
143+
- ""
144+
resources:
145+
- secrets
146+
verbs:
147+
- get
148+
- list
149+
- watch
150+
- apiGroups:
151+
- "admissionregistration.k8s.io"
152+
resources:
153+
- mutatingwebhookconfigurations
154+
verbs:
155+
- get
156+
- list
157+
- watch
158+
- patch
159+
- update
160+
---
161+
apiVersion: rbac.authorization.k8s.io/v1
162+
kind: ClusterRoleBinding
163+
metadata:
164+
name: mutatingwebhookconfiguration-patcher
165+
roleRef:
166+
apiGroup: rbac.authorization.k8s.io
167+
kind: ClusterRole
168+
name: mutatingwebhookconfiguration-patcher
169+
subjects:
170+
- kind: ServiceAccount
171+
name: mutatingwebhook-patcher
172+
namespace: patch-operator
173+
```
174+
175+
Next, apply the following _Patch_ resource which will look up the `Secret` the OLM created containing the CA used by the webhook
176+
177+
```yaml
178+
apiVersion: redhatcop.redhat.io/v1alpha1
179+
kind: Patch
180+
metadata:
181+
name: patch-operator-mutatingwebhookconfiguration
182+
namespace: patch-operator
183+
spec:
184+
serviceAccountRef:
185+
name: mutatingwebhook-patcher
186+
patches:
187+
patch-operator-mutatingwebhookconfigurations:
188+
targetObjectRef:
189+
apiVersion: admissionregistration.k8s.io/v1
190+
kind: MutatingWebhookConfiguration
191+
labelSelector:
192+
matchLabels:
193+
redhat-cop.redhat.io/patch-operator: "true"
194+
patchTemplate: '[{"op": "replace", "path": "/webhooks/0/clientConfig/caBundle", "value":"{{ (index (index . 1).data "olmCAKey") }}"}]'
195+
patchType: application/json-patch+json
196+
sourceObjectRefs:
197+
- apiVersion: v1
198+
kind: Secret
199+
name: patch-operator-controller-manager-service-cert
200+
namespace: patch-operator
201+
```
202+
203+
Note that the `targetObjectRef` uses a _Label Selector_ to query for _MutatingWebhookConfigurations_ with the label `redhat-cop.redhat.io/patch-operator: "true"`.
204+
205+
The following is an example of a _MutatingWebhookConfiguration_ with the required label that can be used to support the creation time webhook.
120206
121207
```yaml
208+
---
122209
apiVersion: admissionregistration.k8s.io/v1
123210
kind: MutatingWebhookConfiguration
124211
metadata:
125212
name: patch-operator-inject
126-
annotations:
127-
service.beta.openshift.io/inject-cabundle: "true"
213+
labels:
214+
redhat-cop.redhat.io/patch-operator: "true"
128215
webhooks:
129216
- admissionReviewVersions:
130217
- v1
131218
clientConfig:
132219
service:
133-
name: patch-operator-webhook-service
220+
name: patch-operator-controller-manager-service
134221
namespace: patch-operator
135222
path: /inject
223+
caBundle: Cg==
136224
failurePolicy: Fail
137225
name: patch-operator-inject.redhatcop.redhat.io
138226
rules:
139227
- << add your intercepted objects here >>
140228
sideEffects: None
141229
```
142230
231+
#### Enabling creation time time webhook (Helm)
232+
143233
If you installed the operator via the Helm chart and are using cert-manager, use the following webhook template:
144234
145235
```yaml
@@ -164,7 +254,11 @@ webhooks:
164254
sideEffects: None
165255
```
166256
167-
You should need to enable the webhook only for `CREATE` operations. So for example to enable the webhook on configmaps:
257+
No additional steps are needed since as cert-manager manages setting the `caBundle` field on the MutatingWebhookConfiguration
258+
259+
#### Webhook rules
260+
261+
For the rules that apply to webhooks, you should need to enable the webhook only for `CREATE` operations. So for example to enable the webhook on configmaps:
168262

169263
```yaml
170264
rules:

0 commit comments

Comments
 (0)