|
| 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 | +``` |
0 commit comments