|
| 1 | +## Prerequisites |
| 2 | +CRD conversion webhook support was introduced as alpha feature in Kubernetes 1.13 |
| 3 | +release and has gone beta in Kubernetes 1.15 release. So ensure that you have a |
| 4 | +Kubernetes cluster that supports CRD conversion feature enabled. |
| 5 | +Refer to [instructions](../TODO.txt) to enable CRD conversion feature in your |
| 6 | +cluster. |
| 7 | +Refer to [instructions](../reference/kind.md) to setup a local cluster with |
| 8 | +Kind. |
| 9 | + |
| 10 | +### What are we building ? |
| 11 | +In this tutorial, we will implement a simple Disk API. Disk API has a field |
| 12 | +called price that represents price per GB. We will go through three |
| 13 | +iterations to evolve the price field specification. |
| 14 | + |
| 15 | +- In v1 version of Disk API, price field is string with "AMOUNT CURRENCY" format. |
| 16 | + Example values could be "10 USD", "100 USD". |
| 17 | +- In v2 version of Disk, price field is represented by a structure `Price` |
| 18 | + that has `amount` and `currency` as separate fields. |
| 19 | +- In v3 version of Disk, we rename the price field to `pricePerGB` to make it |
| 20 | + more explicit. |
| 21 | + |
| 22 | +Here are some sample manifests of the three versions representing same Disk |
| 23 | +object. |
| 24 | +```yaml |
| 25 | + |
| 26 | +apiVersion: infra.kubebuilder.io/v1 |
| 27 | +kind: Disk |
| 28 | +metadata: |
| 29 | + name: disk-sample |
| 30 | +spec: |
| 31 | + price: 10 USD <--- price as string |
| 32 | +---------------------------------------- |
| 33 | + |
| 34 | +apiVersion: infra.kubebuilder.io/v2 |
| 35 | +kind: Disk |
| 36 | +metadata: |
| 37 | + name: disk-sample |
| 38 | +spec: |
| 39 | + price: <---- price as structured object |
| 40 | + amount: 10 |
| 41 | + currency: USD |
| 42 | +---------------------------------------- |
| 43 | + |
| 44 | +apiVersion: infra.kubebuilder.io/v3 |
| 45 | +kind: Disk |
| 46 | +metadata: |
| 47 | + name: disk-sample |
| 48 | +spec: |
| 49 | + pricePerGB: <--- price is renamed to pricePerGB |
| 50 | + amount: 10 |
| 51 | + currency: USD |
| 52 | +``` |
| 53 | +
|
| 54 | +## Tutorial |
| 55 | +Now that we have covered the basics and the goal, we are all set to begin this |
| 56 | +tutorial. We will go through the following steps: |
| 57 | +
|
| 58 | +- Project Setup |
| 59 | +- Adding API with versions v1, v2, v3 of Disk API |
| 60 | +- Setting up Webhooks |
| 61 | +- CRD Generation |
| 62 | +- Configuring Kustomization |
| 63 | +- Deploying and testing |
| 64 | +
|
| 65 | +
|
| 66 | +### Project Setup |
| 67 | +Assuming you have created a new directory and cd in to it. Let's initialize the project. |
| 68 | +```bash |
| 69 | + |
| 70 | +# Initialize Go module |
| 71 | +go mod init infra.kubebuilder.io |
| 72 | + |
| 73 | +# Initilize Kubebuilder project |
| 74 | +kubebuilder init --domain kubebuilder.io |
| 75 | + |
| 76 | +``` |
| 77 | + |
| 78 | + |
| 79 | +### Version v1 |
| 80 | + |
| 81 | +Let's create version `v1` of our `Disk` API. |
| 82 | + |
| 83 | +```bash |
| 84 | + |
| 85 | +# create v1 version with resource and controller |
| 86 | +kubebuilder create api --group infra --kind Disk --version v1 |
| 87 | +Create Resource [y/n] |
| 88 | +y |
| 89 | +Create Controller [y/n] |
| 90 | +y |
| 91 | +``` |
| 92 | + |
| 93 | +Let's take a look at file `api/v1/disk_types.go`. |
| 94 | + |
| 95 | +{{#literatego ./testdata/project/api/v1/disk_types.go}} |
| 96 | + |
| 97 | +### Version v2 |
| 98 | + |
| 99 | +Let's add version `v2` to the `Disk` API. We will not add any controller this |
| 100 | +time because we already have a controller for `Disk` API. |
| 101 | + |
| 102 | +```bash |
| 103 | +# create v2 version without controller |
| 104 | +kubebuilder create api --group infra --kind Disk --version v2 |
| 105 | +Create Resource [y/n] |
| 106 | +y |
| 107 | +Create Controller [y/n] |
| 108 | +n |
| 109 | +``` |
| 110 | + |
| 111 | +Now, let's take a look at file `api/v2/disk_types.go`. |
| 112 | + |
| 113 | +{{#literatego ./testdata/project/api/v2/disk_types.go}} |
| 114 | + |
| 115 | +### Version v3 |
| 116 | + |
| 117 | +Let's add version `v3` to the `Disk` API and once again, we will not add any |
| 118 | +controller since we already have controller for the `Disk` API. |
| 119 | + |
| 120 | +```bash |
| 121 | +# create v3 version without controller |
| 122 | +kubebuilder create api --group infra --kind Disk --version v3 |
| 123 | +Create Resource [y/n] |
| 124 | +y |
| 125 | +Create Controller [y/n] |
| 126 | +n |
| 127 | + |
| 128 | +``` |
| 129 | + |
| 130 | +{{#literatego ./testdata/project/api/v3/disk_types.go}} |
| 131 | + |
| 132 | +Now that we have all the API implementations in place, let's take a look at what |
| 133 | +is required to setup conversion webhook for our `Disk` API. |
| 134 | + |
| 135 | +### Setting up Webhooks |
| 136 | +In `2.0.0+` release, Kubebuilder introduced new command `create webhook` to make |
| 137 | +it easy to setup admission and conversion webhooks. Run the following command to |
| 138 | +setup conversion webhook. Note that we can specify any version from v1, v2 or v3 |
| 139 | +in this command because there is single conversion webhook for a Kind. |
| 140 | + |
| 141 | +```bash |
| 142 | +kubebuilder create webhook --group infra --kind Disk --version v1 --conversion |
| 143 | + |
| 144 | +Writing scaffold for you to edit... |
| 145 | +api/v1/disk_webhook.go |
| 146 | +``` |
| 147 | +Above commands does the following: |
| 148 | +- Scaffolds a new file `api/v1/disk_webhook.go` to implement webhook setup method. |
| 149 | +- Updates `main.go` to setup webhooks with the manager instance. |
| 150 | + |
| 151 | +Let's take a quick look at the `api/v1/disk_webhook.go` file. |
| 152 | + |
| 153 | +{{#literatego ./testdata/project/api/v1/disk_webhook.go}} |
| 154 | + |
| 155 | +If you look at `main.go`, you will notice the following snippet that invokes the |
| 156 | +SetupWebhook method. |
| 157 | +```Go |
| 158 | + ..... |
| 159 | + |
| 160 | + if err = (&infrav1.Disk{}).SetupWebhookWithManager(mgr); err != nil { |
| 161 | + setupLog.Error(err, "unable to create webhook", "webhook", "Disk") |
| 162 | + os.Exit(1) |
| 163 | + } |
| 164 | + |
| 165 | + .... |
| 166 | +``` |
| 167 | + |
| 168 | +### CRD Generation |
| 169 | + |
| 170 | +The `controller-gen` tool that generates the CRD manifest takes a parameter to indicate if our API has multiple versions. We need to specify `trivialVersions=false` in CRD_OPTIONS in your project's Makefile to enable multi-version. |
| 171 | + |
| 172 | +``` bash |
| 173 | +... |
| 174 | +CRD_OPTIONS ?= "crd:trivialVersions=false" |
| 175 | +... |
| 176 | +``` |
| 177 | + |
| 178 | +Run `make manifests` to ensure that CRD manifests gets generated under `config/crd/bases/` directory. |
| 179 | +[TODO](../TODO.md) embed a compressed form of the generated CRD `testdata/project/config/crd` |
| 180 | + |
| 181 | +### Manifests Generation |
| 182 | + |
| 183 | +Kubebuilder generates Kubernetes manifests under 'config' directory with webhook |
| 184 | +bits disabled. Follow the steps below to enable conversion webhook in manifests |
| 185 | +generation. |
| 186 | + |
| 187 | +- Ensure that `patches/webhook_in_<kind>.yaml` and `patches/cainjection_in_<kind>.yaml` are enabled in `config/crds/kustomization.yaml` file. |
| 188 | +- Ensure that `../certmanager` and `../webhook` directories are enabled under `bases` section in `config/default/kustomization.yaml` file. |
| 189 | +- Ensure that `manager_webhook_patch.yaml` is enabled under `patches` section in `config/default/kustomization.yaml` file. |
| 190 | +- Enable all the vars under section `CERTMANAGER` in `config/default/kustomization.yaml` file. |
| 191 | + |
0 commit comments