Skip to content

Commit 64af935

Browse files
authored
Merge pull request #250 from flant/feat_conversion_binding
feat: support for conversion webhooks
2 parents 9ee8a67 + 3d378bf commit 64af935

File tree

94 files changed

+3369
-707
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+3369
-707
lines changed

BINDING_CONVERSION.md

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# kubernetesCustomResourceConversion
2+
3+
This binding transforms a hook into a handler for conversions defined in CustomResourceDefinition. The Shell-operator updates a CRD with .spec.conversion, starts HTTPS server, and runs hooks to handle [ConversionReview requests](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/#conversionreview-request-0).
4+
5+
> Note: shell-operator use `apiextensions.k8s.io/v1`, so Kubernetes 1.16+ is required.
6+
7+
## Syntax
8+
9+
```yaml
10+
configVersion: v1
11+
onStartup: 10
12+
kubernetes:
13+
- name: additionalObjects
14+
...
15+
kubernetesCustomResourceConversion:
16+
- name: alpha1_to_alpha2
17+
# Include snapshots by binding names.
18+
includeSnapshotsFrom: ["additionalObjects"]
19+
# Or use group name to include all snapshots in a group.
20+
group: "group name"
21+
# A CRD name.
22+
crdName: crontabs.stable.example.com
23+
# An array of conversions supported by this hook.
24+
conversion:
25+
- fromVersion: stable.example.com/v1alpha1
26+
toVersion: stable.example.com/v1alpha2
27+
```
28+
29+
## Parameters
30+
31+
- `name` — a required parameter. It is used to distinguish between multiple schedules during runtime. For more information see [binding context](HOOKS.md#binding-context).
32+
33+
- `includeSnapshotsFrom` — an array of names of `kubernetes` bindings in a hook. When specified, a list of monitored objects from these bindings will be added to the binding context in the `snapshots` field.
34+
35+
- `group` — a key to include snapshots from a group of `schedule` and `kubernetes` bindings. See [grouping](HOOKS.md#an-example-of-a-binding-context-with-group).
36+
37+
- `crdName` — a required name of a CRD.
38+
39+
- `conversions` — a required list of conversion rules. These rules are used to determine if a custom resource in ConversionReview can be converted by the hook.
40+
41+
- `fromVersion` — a version of a custom resource that hook can convert.
42+
43+
- `toVersion` — a version of a custom resource that hook can produce.
44+
45+
46+
## Example
47+
48+
```
49+
configVersion: v1
50+
kubernetesCustomResourceConversion:
51+
- name: conversions
52+
crdName: crontabs.stable.example.com
53+
conversions:
54+
- fromVersion: unstable.crontab.io/v1beta1
55+
toVersion: stable.example.com/v1beta1
56+
- fromVersion: stable.example.com/v1beta1
57+
toVersion: stable.example.com/v1beta2
58+
- fromVersion: v1beta2
59+
toVersion: v1
60+
```
61+
62+
The Shell-operator will execute this hook to convert custom resources 'crontabs.stable.example.com' from unstable.crontab.io/v1beta1 to stable.example.com/v1beta1, from stable.example.com/v1beta1 to stable.example.com/v1beta2, from unstable.crontab.io/v1beta1 to stable.example.com/v1 and so on.
63+
64+
See example [210-conversion-webhook](./examples/210-conversion-webhook).
65+
66+
## Hook input and output
67+
68+
> Note that the `group` parameter is only for including snapshots. `kubernetesCustomResourceConversion` hook is never executed on `schedule` or `kubernetes` events with binding context with `"type":"Group"`.
69+
70+
The hook receives a binding context and should return response in `$CONVERSION_RESPONSE_PATH`.
71+
72+
$BINDING_CONTEXT_PATH file example:
73+
74+
```yaml
75+
[{
76+
# Name as defined in binding configuration.
77+
"binding": "alpha1_to_alpha2",
78+
# type "Conversion" to distinguish from other events.
79+
"type": "Conversion",
80+
# Snapshots as defined by includeSnapshotsFrom or group.
81+
"snapshots": { ... }
82+
# fromVersion and toVersion as defined in a conversion rule.
83+
"fromVersion": "unstable.crontab.io/v1beta1",
84+
"toVersion": "stable.example.com/v1beta1",
85+
# ConversionReview object.
86+
"review": {
87+
"apiVersion": "apiextensions.k8s.io/v1",
88+
"kind": "ConversionReview",
89+
"request": {
90+
"desiredAPIVersion": "stable.example.com/v1beta1",
91+
"objects": [
92+
{
93+
# A source version.
94+
"apiVersion": "unstable.crontab.io/v1beta1",
95+
"kind": "CronTab",
96+
"metadata": {
97+
"name": "crontab-v1alpha1",
98+
"namespace": "example-210",
99+
...
100+
},
101+
"spec": {
102+
"cron": [
103+
"*",
104+
"*",
105+
"*",
106+
"*",
107+
"*/5"
108+
],
109+
"imageName": [
110+
"repo.example.com/my-awesome-cron-image:v1"
111+
]
112+
}
113+
}
114+
],
115+
"uid": "42f90c87-87f5-4686-8109-eba065c7fa6e"
116+
}
117+
}
118+
}]
119+
```
120+
121+
Response example:
122+
```
123+
cat <<EOF >$CONVERSION_RESPONSE_PATH
124+
{"convertedObjects": [{
125+
# A converted version.
126+
"apiVersion": "stable.example.com/v1beta1",
127+
"kind": "CronTab",
128+
"metadata": {
129+
"name": "crontab-v1alpha1",
130+
"namespace": "example-210",
131+
...
132+
},
133+
"spec": {
134+
"cron": [
135+
"*",
136+
"*",
137+
"*",
138+
"*",
139+
"*/5"
140+
],
141+
"imageName": [
142+
"repo.example.com/my-awesome-cron-image:v1"
143+
]
144+
}
145+
}]}
146+
EOF
147+
```
148+
149+
Return a message if something goes wrong:
150+
```
151+
cat <<EOF >$CONVERSION_RESPONSE_PATH
152+
{"failedMessage":"Conversion of crontabs.stable.example.com is failed"}
153+
EOF
154+
```
155+
156+
User will see an error message:
157+
158+
```
159+
Error from server: conversion webhook for unstable.crontab.io/v1beta1, Kind=CronTab failed: Conversion of crontabs.stable.example.com is failed
160+
```
161+
162+
Empty or invalid $CONVERSION_RESPONSE_PATH file is considered as a fail with a short message about the problem and a more verbose error in the log.
163+
164+
## HTTP server and Kubernetes configuration
165+
166+
Shell-operator should create an HTTP endpoint with TLS support and register an endpoint in the CustomResourceDefinition resource.
167+
168+
There should be a Service for shell-operator (see [Service Reference](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/#service-reference)).
169+
170+
There are command line options and corresponding environment variables to setup TLS certificates and a service name:
171+
172+
```
173+
--conversion-webhook-service-name="shell-operator-conversion-svc"
174+
A name of a service for clientConfig in CRD. Can be set with
175+
$CONVERSION_WEBHOOK_SERVICE_NAME.
176+
--conversion-webhook-server-cert="/conversion-certs/tls.crt"
177+
A path to a server certificate for clientConfig in CRD. Can be set with
178+
$CONVERSION_WEBHOOK_SERVER_CERT.
179+
--conversion-webhook-server-key="/conversion-certs/tls.key"
180+
A path to a server private key for clientConfig in CRD. Can be set with
181+
$CONVERSION_WEBHOOK_SERVER_KEY.
182+
--conversion-webhook-ca="/conversion-certs/ca.crt"
183+
A path to a ca certificate for clientConfig in CRD. Can be set with
184+
$CONVERSION_WEBHOOK_CA.
185+
--conversion-webhook-client-ca=CONVERSION-WEBHOOK-CLIENT-CA ...
186+
A path to a server certificate for CRD.spec.conversion.webhook. Can be set
187+
with $CONVERSION_WEBHOOK_CLIENT_CA.
188+
```

BINDING_VALIDATING.md

+20-10
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,24 @@ There should be a Service for shell-operator (see [Availability](https://kuberne
212212
Command line options:
213213

214214
```
215-
--validating-webhook-server-cert="/validating-certs/cert.crt"
216-
A path to a server certificate for ValidatingWebhook. Can be set with $VALIDATING_WEBHOOK_SERVER_CERT.
217-
--validating-webhook-server-key="/validating-certs/cert.key"
218-
A path to a server private key for ValidatingWebhook. Can be set with $VALIDATING_WEBHOOK_SERVER_KEY.
219-
--validating-webhook-ca="/validating-certs/ca.crt"
220-
A path to a ca bundle for ValidatingWebhook. Can be set with $VALIDATING_WEBHOOK_CA.
221-
--validating-webhook-client-ca=VALIDATING-WEBHOOK-CLIENT-CA ...
222-
A path to a server certificate for ValidatingWebhook. Can be set with $VALIDATING_WEBHOOK_CLIENT_CA.
223-
--validating-webhook-service-name=VALIDATING-WEBHOOK-SERVICE-NAME ...
224-
A name of a service in front of a shell-operator. Can be set with $VALIDATING_WEBHOOK_SERVICE_NAME.
215+
--validating-webhook-configuration-name="shell-operator-hooks"
216+
A name of a ValidatingWebhookConfiguration resource. Can be set with
217+
$VALIDATING_WEBHOOK_CONFIGURATION_NAME.
218+
--validating-webhook-service-name="shell-operator-validating-svc"
219+
A name of a service used in ValidatingWebhookConfiguration. Can be set
220+
with $VALIDATING_WEBHOOK_SERVICE_NAME.
221+
--validating-webhook-server-cert="/validating-certs/tls.crt"
222+
A path to a server certificate for service used in
223+
ValidatingWebhookConfiguration. Can be set with
224+
$VALIDATING_WEBHOOK_SERVER_CERT.
225+
--validating-webhook-server-key="/validating-certs/tls.key"
226+
A path to a server private key for service used in
227+
ValidatingWebhookConfiguration. Can be set with
228+
$VALIDATING_WEBHOOK_SERVER_KEY.
229+
--validating-webhook-ca="/validating-certs/ca.crt"
230+
A path to a ca certificate for ValidatingWebhookConfiguration. Can be set
231+
with $VALIDATING_WEBHOOK_CA.
232+
--validating-webhook-client-ca=VALIDATING-WEBHOOK-CLIENT-CA ...
233+
A path to a server certificate for ValidatingWebhookConfiguration. Can be
234+
set with $VALIDATING_WEBHOOK_CLIENT_CA.
225235
```

HOOKS.md

+6
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,12 @@ Use a hook as handler for [ValidationWebhookConfiguration](https://kubernetes.io
328328

329329
See syntax and parameters in [BINDING_VALIDATING.md](BINDING_VALIDATING.md)
330330

331+
### kubernetesCustomResourceConversion
332+
333+
Use a hook as handler for [custom resource conversion](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning).
334+
335+
See syntax and parameters in [BINDING_CONVERSION.md](BINDING_CONVERSION.md)
336+
331337
## Binding context
332338

333339
When an event associated with a hook is triggered, Shell-operator executes the hook without arguments. The information about the event that led to the hook execution is called the **binding context** and is written in JSON format to a temporary file. The path to this file is available to hook via environment variable `BINDING_CONTEXT_PATH`.

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Shell-operator provides:
2020
- __Kubernetes object events__: hook can be triggered by `add`, `update` or `delete` events. **[Learn more](HOOKS.md) about hooks.**
2121
- __Object selector and properties filter__: shell-operator can monitor a particular set of objects and detect changes in their properties.
2222
- __Simple configuration__: hook binding definition is a JSON or YAML document on script's stdout.
23+
- __Validating webhook machinery__: hook can handle validating for Kubernetes resources.
24+
- __Conversion webhook machinery__: hook can handle version conversion for Kubernetes resources.
2325

2426
**Contents**:
2527
* [Quickstart](#quickstart)

RUNNING.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,17 @@ You can configure the operator with the following environment variables and cli
4949
| --log-no-time | LOG_NO_TIME | `false` | Disable timestamp logging if flag is present. Useful when output is redirected to logging system that already adds timestamps. |
5050
| --debug-keep-tmp-files | DEBUG_KEEP_TMP_FILES | `"no"` | Set to `yes` to keep files in $SHELL_OPERATOR_TMP_DIR for debugging purposes. Note that it can generate many files. |
5151
| --debug-unix-socket | DEBUG_UNIX_SOCKET | `"/var/run/shell-operator/debug.socket"` | Path to the unix socket file for debugging purposes. |
52-
52+
| --validating-webhook-configuration-name | VALIDATING_WEBHOOK_CONFIGURATION_NAME | `"shell-operator-hooks"` | A name of a ValidatingWebhookConfiguration resource. |
53+
| --validating-webhook-service-name | VALIDATING_WEBHOOK_SERVICE_NAME | `"shell-operator-validating-svc"` | A name of a service used in ValidatingWebhookConfiguration. |
54+
| --validating-webhook-server-cert | VALIDATING_WEBHOOK_SERVER_CERT | `"/validating-certs/tls.crt"` | A path to a server certificate for service used in ValidatingWebhookConfiguration. |
55+
| --validating-webhook-server-key | VALIDATING_WEBHOOK_SERVER_KEY | `"/validating-certs/tls.key"` | A path to a server private key for service used in ValidatingWebhookConfiguration. |
56+
| --validating-webhook-ca | VALIDATING_WEBHOOK_CA | `"/validating-certs/ca.crt"` | A path to a ca certificate for ValidatingWebhookConfiguration. |
57+
| --validating-webhook-client-ca | VALIDATING_WEBHOOK_CLIENT_CA | [] | A path to a server certificate for ValidatingWebhookConfiguration. |
58+
| --conversion-webhook-service-name | CONVERSION_WEBHOOK_SERVICE_NAME | `"shell-operator-conversion-svc"` | A name of a service for clientConfig in CRD. |
59+
| --conversion-webhook-server-cert | CONVERSION_WEBHOOK_SERVER_CERT | `"/conversion-certs/tls.crt"` | A path to a server certificate for clientConfig in CRD. |
60+
| --conversion-webhook-server-key | CONVERSION_WEBHOOK_SERVER_KEY | `"/conversion-certs/tls.key"` | A path to a server private key for clientConfig in CRD. |
61+
| --conversion-webhook-ca | CONVERSION_WEBHOOK_CA | `"/conversion-certs/ca.crt"` | A path to a ca certificate for clientConfig in CRD. |
62+
| --conversion-webhook-client-ca | CONVERSION_WEBHOOK_CLIENT_CA | [] | A path to a server certificate for CRD.spec.conversion.webhook. |
5363

5464
## Debug
5565

examples/204-validating-webhook/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ An HTTP server behind the ValidatingWebhookConfiguration requires a certificate
1414
./gen-certs.sh
1515
```
1616

17-
> Note: `gen-certs.sh` requires [cfssl utility](https://github.com/cloudflare/cfssl/releases/latest) and an [Approval Permission](https://kubernetes.io/docs/tasks/tls/managing-tls-in-a-cluster) in cluster.
17+
> Note: `gen-certs.sh` requires [cfssl utility](https://github.com/cloudflare/cfssl/releases/latest).
1818
1919
### Build and install example
2020

0 commit comments

Comments
 (0)