Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit 01cdf78

Browse files
committed
Updated to OpenShift 4.x
1 parent b7f2244 commit 01cdf78

File tree

7 files changed

+60
-18
lines changed

7 files changed

+60
-18
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ There are two types of Admission Webhook objects you can configure:
1313
- _Validating Admission Webhooks_ allow for the use of validating webhooks to enforce custom admission policies.
1414

1515
## Enabling Admission Webhooks
16+
### OpenShift 3.x
1617
In order to use these _Mutating Admission Webhooks_ and _Validating Admission Webhooks_ they must be activated in OpenShift master services.
1718

1819
Make sure that `/etc/origin/master/master-config.yaml` has this two plugins enabled in `admissionConfig.pluginConfig` section, and restart master services:
@@ -29,6 +30,9 @@ Make sure that `/etc/origin/master/master-config.yaml` has this two plugins enab
2930
kind: DefaultAdmissionConfig
3031
~~~
3132

33+
### OpenShift 4.x
34+
No need to perform any change, the _Admission Webhooks_ are enabled by default.
35+
3236
## List of Webhooks Servers
3337
This is the list of the Webhook Servers included in the repository:
3438
- __[denynewpods](./denynewpods/README.md)__. This webhook is an example of a _Validating Admission Webhook_ and will prevent to run any kind of pod in a namespace labeled with the label `denynewpods.admission.online.openshift.io` to a value `enabled`.
@@ -39,7 +43,7 @@ All Webhook Servers in this repository:
3943
- are built using OpenShift build methods.
4044
- listen in port 8443 for HTTPS (POST) requests.
4145
- use the Service Signer CA to get a secret with a certificate and a key. You should be able to overwrite the content of the secret after its creation if you want to use your own CA and certificates.
42-
- are referred using their service DNS name. Other methods can be used like integrating the service in OpenShift API, using a service and namespace reference or even a secured route.
46+
- are referred using their service name and namespace reference. Other methods can be used like integrating the service in OpenShift API, using a service DNS name (for externally hosted webhooks) or even a secured route.
4347

4448
Refer to each Webhook Server's README file to get more details about it.
4549

denynewpods/README.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ $ curl -k -X POST https://denynewpods.webhooks.svc
2222
### Creating the Webhook Server
2323
~~~
2424
$ oc new-project webhooks
25-
$ oc process -f https://raw.githubusercontent.com/soukron/openshift-admission-webhooks/master/denynewpods/templates/deployment.yaml \
25+
$ oc process -p WEBHOOK_NAMESPACE=webhooks \
26+
-f https://raw.githubusercontent.com/soukron/openshift-admission-webhooks/master/denynewpods/templates/deployment.yaml \
2627
| oc apply -f -
2728
$ oc start-build bc/denynewpods
2829
~~~
@@ -32,6 +33,7 @@ $ oc start-build bc/denynewpods
3233
The Admission Webhook will trigger the Webhook Server when a new pod is created in a namespace labeled with the label `denynewpods.admission.online.openshift.io` to a value `enabled`.
3334

3435
### Creating the Admission Webhook
36+
#### OpenShift 3.x
3537
~~~
3638
$ export WEBHOOK_CA_BUNDLE=$( sudo cat /etc/origin/master/service-signer.crt | base64 -w0 )
3739
$ oc process -p WEBHOOK_NAMESPACE=webhooks \
@@ -40,6 +42,15 @@ $ oc process -p WEBHOOK_NAMESPACE=webhooks \
4042
| oc apply -f -
4143
~~~
4244

45+
#### OpenShift 4.x
46+
~~~
47+
$ export WEBHOOK_CA_BUNDLE=$( oc get configmap -n openshift-network-operator openshift-service-ca -o jsonpath='{.data.service-ca\.crt}' | base64 -w0 )
48+
$ oc process -p WEBHOOK_NAMESPACE=webhooks \
49+
-p WEBHOOK_CA_BUNDLE=${WEBHOOK_CA_BUNDLE} \
50+
-f https://raw.githubusercontent.com/soukron/openshift-admission-webhooks/master/denynewpods/templates/webhookconfiguration.yaml \
51+
| oc apply -f -
52+
~~~
53+
4354
## Customization
4455
### Use your own certificates
4556
After the Webhook Server is deployed and it's running, if you don't want to use the Service Signer CA certificates, replace the secret with your own cert/key pair and re-deploy the Webhook Server:
@@ -64,26 +75,26 @@ $ oc process -p WEBHOOK_NAMESPACE=webhooks \
6475
After the Webhook Server is deployed and the Admission Webhook has been created, try to create a new project and run a pod on it:
6576
~~~
6677
$ oc new-project test-webhooks
67-
$ oc run sleep --image=alpine --command -- sleep 3600
78+
$ oc run sleep-1 --image=alpine --command -- sleep 3600
6879
~~~
6980
This first pod will run successfully as the namespace is not yet labeled.
7081

71-
Add the label to the namespace and try to increase the number of replicas:
82+
Add the label to the namespace and try to run another pod:
7283
~~~
7384
$ oc label namespace test-webhooks denynewpods.admission.online.openshift.io=enabled
74-
$ oc scale dc/sleep --replicas=5
85+
$ oc run sleep-2 --image=alpine --command -- sleep 3600
7586
~~~
7687

77-
No new pods will be scheduled and a log will appear in the events:
88+
No new pods will be scheduled and an error will be returned:
7889
~~~
79-
$ oc get events | grep admission
80-
5m 5m 1 sleep-1.157e7b51cdc8c0fc ReplicationController Warning FailedCreate replication-controller Error creating: admission webhook "denynewpods.admission.online.openshift.io" denied the request: New pods denied.
90+
$ oc run sleep-2 --image=alpine --command -- sleep 3600
91+
Error from server (No new pods allowed in this project): admission webhook "denynewpods.admission.online.openshift.io" denied the request: New pods denied
8192
~~~
8293

8394
Remove the label to the namespace and this time the replicas will run:
8495
~~~
8596
$ oc label namespace test-webhooks denynewpods.admission.online.openshift.io-
86-
$ oc scale dc/sleep --replicas=5
97+
$ oc run sleep-2 --image=alpine --command -- sleep 3600
8798
~~~
8899

89100
## Cleanup

denynewpods/templates/deployment.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ metadata: {}
44
parameters:
55
- name: WEBHOOK_NAME
66
required: true
7-
description: Name of the webhook to deploy (defaults to denynewpods)
7+
description: Name of the webhook (defaults to denynewpods).
88
value: denynewpods
9+
- name: WEBHOOK_NAMESPACE
10+
required: true
11+
description: Name of the project where the webhook is being deployed.
912
objects:
1013
- apiVersion: v1
1114
kind: BuildConfig
1215
metadata:
1316
labels:
1417
webhook: ${WEBHOOK_NAME}
1518
name: ${WEBHOOK_NAME}
19+
namespace: ${WEBHOOK_NAMESPACE}
1620
spec:
1721
output:
1822
to:
@@ -37,6 +41,7 @@ objects:
3741
labels:
3842
webhook: ${WEBHOOK_NAME}
3943
name: ${WEBHOOK_NAME}
44+
namespace: ${WEBHOOK_NAMESPACE}
4045
spec:
4146
lookupPolicy:
4247
local: false
@@ -55,6 +60,7 @@ objects:
5560
labels:
5661
webhook: ${WEBHOOK_NAME}
5762
name: ${WEBHOOK_NAME}
63+
namespace: ${WEBHOOK_NAMESPACE}
5864
spec:
5965
ports:
6066
- name: 443-tcp
@@ -71,6 +77,7 @@ objects:
7177
labels:
7278
webhook: ${WEBHOOK_NAME}
7379
name: ${WEBHOOK_NAME}
80+
namespace: ${WEBHOOK_NAMESPACE}
7481
spec:
7582
replicas: 1
7683
selector:

denynewpods/templates/webhookconfiguration.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ parameters:
1111
description: Name of the project where the webhook is being deployed.
1212
- name: WEBHOOK_CA_BUNDLE
1313
required: true
14-
description: Content of /etc/origin/master/service-signer.crt base64 encoded or the CA which signed the certificate used in the webhook.
14+
description: Base64 encoded CA which signed the certificate used in the webhook.
1515
objects:
1616
- apiVersion: admissionregistration.k8s.io/v1beta1
1717
kind: ValidatingWebhookConfiguration
@@ -32,8 +32,9 @@ objects:
3232
- pods
3333
failurePolicy: Fail
3434
clientConfig:
35-
url: https://${WEBHOOK_NAME}.${WEBHOOK_NAMESPACE}.svc
36-
path: /
35+
service:
36+
name: ${WEBHOOK_NAME}
37+
namespace: ${WEBHOOK_NAMESPACE}
3738
caBundle: ${WEBHOOK_CA_BUNDLE}
3839
namespaceSelector:
3940
matchLabels:

enforceenv/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ The Webhook Server will:
1111
### Creating the Webhook Server
1212
~~~
1313
$ oc new-project webhooks
14-
$ oc process -f https://raw.githubusercontent.com/soukron/openshift-admission-webhooks/master/enforceenv/templates/deployment.yaml \
14+
$ oc process -p WEBHOOK_NAMESPACE=webhooks \
15+
-f https://raw.githubusercontent.com/soukron/openshift-admission-webhooks/master/enforceenv/templates/deployment.yaml \
1516
| oc apply -f -
1617
$ oc start-build bc/enforceenv
1718
~~~
@@ -21,6 +22,7 @@ $ oc start-build bc/enforceenv
2122
The Admission Webhook will trigger the Webhook Server when a new pod is created in a namespace labeled with the label `enforceenv.admission.online.openshift.io` is set.
2223

2324
### Creating the Admission Webhook
25+
#### OpenShift 3.x
2426
~~~
2527
$ export WEBHOOK_CA_BUNDLE=$( sudo cat /etc/origin/master/service-signer.crt | base64 -w0 )
2628
$ oc process -p WEBHOOK_NAMESPACE=webhooks \
@@ -29,6 +31,15 @@ $ oc process -p WEBHOOK_NAMESPACE=webhooks \
2931
| oc apply -f -
3032
~~~
3133

34+
#### OpenShift 4.x
35+
~~~
36+
$ export WEBHOOK_CA_BUNDLE=$( oc get configmap -n openshift-network-operator openshift-service-ca -o jsonpath='{.data.service-ca\.crt}' | base64 -w0 )
37+
$ oc process -p WEBHOOK_NAMESPACE=webhooks \
38+
-p WEBHOOK_CA_BUNDLE=${WEBHOOK_CA_BUNDLE} \
39+
-f https://raw.githubusercontent.com/soukron/openshift-admission-webhooks/master/enforceenv/templates/webhookconfiguration.yaml \
40+
| oc apply -f -
41+
~~~
42+
3243
## Customization
3344
### Use your own certificates
3445
After the Webhook Server is deployed and it's running, if you don't want to use the Service Signer CA certificates, replace the secret with your own cert/key pair and re-deploy the Webhook Server:

enforceenv/templates/deployment.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ metadata: {}
44
parameters:
55
- name: WEBHOOK_NAME
66
required: true
7-
description: Name of the webhook to deploy (default value is enforceenv)
7+
description: Name of the webhook (defaults to enforceenv).
88
value: enforceenv
9+
- name: WEBHOOK_NAMESPACE
10+
required: true
11+
description: Name of the project where the webhook is being deployed.
912
objects:
1013
- apiVersion: v1
1114
kind: BuildConfig
1215
metadata:
1316
labels:
1417
webhook: ${WEBHOOK_NAME}
1518
name: ${WEBHOOK_NAME}
19+
namespace: ${WEBHOOK_NAMESPACE}
1620
spec:
1721
output:
1822
to:
@@ -37,6 +41,7 @@ objects:
3741
labels:
3842
webhook: ${WEBHOOK_NAME}
3943
name: ${WEBHOOK_NAME}
44+
namespace: ${WEBHOOK_NAMESPACE}
4045
spec:
4146
lookupPolicy:
4247
local: false
@@ -55,6 +60,7 @@ objects:
5560
labels:
5661
webhook: ${WEBHOOK_NAME}
5762
name: ${WEBHOOK_NAME}
63+
namespace: ${WEBHOOK_NAMESPACE}
5864
spec:
5965
ports:
6066
- name: 443-tcp
@@ -71,6 +77,7 @@ objects:
7177
labels:
7278
webhook: ${WEBHOOK_NAME}
7379
name: ${WEBHOOK_NAME}
80+
namespace: ${WEBHOOK_NAMESPACE}
7481
spec:
7582
replicas: 1
7683
selector:

enforceenv/templates/webhookconfiguration.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ parameters:
1111
description: Name of the project where the webhook is being deployed.
1212
- name: WEBHOOK_CA_BUNDLE
1313
required: true
14-
description: Content of /etc/origin/master/service-signer.crt base64 encoded or the CA which signed the certificate used in the webhook.
14+
description: Base64 encoded CA which signed the certificate used in the webhook.
1515
objects:
1616
- apiVersion: admissionregistration.k8s.io/v1beta1
1717
kind: MutatingWebhookConfiguration
@@ -31,6 +31,7 @@ objects:
3131
resources:
3232
- pods
3333
clientConfig:
34-
url: https://${WEBHOOK_NAME}.${WEBHOOK_NAMESPACE}.svc
35-
path: /
34+
service:
35+
name: ${WEBHOOK_NAME}
36+
namespace: ${WEBHOOK_NAMESPACE}
3637
caBundle: ${WEBHOOK_CA_BUNDLE}

0 commit comments

Comments
 (0)