Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1 | # Kubernetes tutorial |
| 2 | |
| 3 | In this tutorial we show how to convert Kubernetes configuration files |
| 4 | for a collection of microservices. |
| 5 | |
| 6 | The configuration files are scrubbed and renamed versions of |
| 7 | real-life configuration files. |
| 8 | The files are organized in a directory hierarchy grouping related services |
| 9 | in subdirectories. |
| 10 | This is a common pattern. |
| 11 | The `cue` tooling has been optimized for this use case. |
| 12 | |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 13 | In this tutorial we will address the following topics: |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 14 | |
| 15 | 1. convert the given YAML files to CUE |
| 16 | 1. hoist common patterns to parent directories |
| 17 | 1. use the tooling to rewrite CUE files to drop unnecessary fields |
| 18 | 1. repeat from step 2 for different subdirectories |
| 19 | 1. define commands to operate on the configuration |
Joel Longtine | b654466 | 2019-06-11 16:59:12 +0000 | [diff] [blame] | 20 | 1. extract CUE templates directly from Kubernetes Go source |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 21 | 1. manually tailor the configuration |
| 22 | 1. map a Kubernetes configuration to `docker-compose` (TODO) |
| 23 | |
| 24 | |
| 25 | ## The given data set |
| 26 | |
| 27 | The data set is based on a real-life case, using different names for the |
| 28 | services. |
| 29 | All the inconsistencies of the real setup are replicated in the files |
| 30 | to get a realistic impression of how a conversion to CUE would behave |
| 31 | in practice. |
| 32 | |
| 33 | The given YAML files are ordered in following directory |
| 34 | (you can use `find` if you don't have tree): |
| 35 | |
| 36 | ``` |
Marcel van Lohuizen | 275105a | 2019-12-06 21:32:41 +0100 | [diff] [blame] | 37 | $ tree ./original | head |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 38 | . |
| 39 | └── services |
| 40 | ├── frontend |
| 41 | │ ├── bartender |
| 42 | │ │ └── kube.yaml |
| 43 | │ ├── breaddispatcher |
| 44 | │ │ └── kube.yaml |
| 45 | │ ├── host |
| 46 | │ │ └── kube.yaml |
| 47 | │ ├── maitred |
| 48 | ... |
| 49 | ``` |
| 50 | |
| 51 | Each subdirectory contains related microservices that often share similar |
| 52 | characteristics and configurations. |
| 53 | The configurations include a large variety of Kubernetes objects, including |
| 54 | services, deployments, config maps, |
| 55 | a daemon set, a stateful set, and a cron job. |
| 56 | |
| 57 | The result of the first tutorial is in the `quick`, for "quick and dirty" |
| 58 | directory. |
| 59 | A manually optimized configuration can be found int `manual` |
| 60 | directory. |
| 61 | |
| 62 | |
| 63 | ## Importing existing configuration |
| 64 | |
| 65 | We first make a copy of the data directory. |
| 66 | |
| 67 | ``` |
Marcel van Lohuizen | ac39cd7 | 2019-06-11 22:27:54 +0200 | [diff] [blame] | 68 | $ cp -a original tmp |
Marcel van Lohuizen | b13155b | 2019-10-25 16:52:30 +0200 | [diff] [blame] | 69 | $ cd tmp |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 70 | ``` |
| 71 | |
Marcel van Lohuizen | b13155b | 2019-10-25 16:52:30 +0200 | [diff] [blame] | 72 | We initialize a module so that we can treat all our configuration files |
| 73 | in the subdirectories as part of one package. |
| 74 | We do that later by giving all the same package name. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 75 | |
| 76 | ``` |
Marcel van Lohuizen | b13155b | 2019-10-25 16:52:30 +0200 | [diff] [blame] | 77 | $ cue mod init |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 78 | ``` |
Marcel van Lohuizen | b13155b | 2019-10-25 16:52:30 +0200 | [diff] [blame] | 79 | |
| 80 | Creating a module also allows our packages import external packages. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 81 | |
| 82 | Let's try to use the `cue import` command to convert the given YAML files |
| 83 | into CUE. |
| 84 | |
| 85 | ``` |
Marcel van Lohuizen | b13155b | 2019-10-25 16:52:30 +0200 | [diff] [blame] | 86 | $ cd services |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 87 | $ cue import ./... |
Marcel van Lohuizen | 2437f9d | 2019-10-29 12:22:38 +0100 | [diff] [blame] | 88 | must specify package name with the -p flag |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 89 | ``` |
| 90 | |
| 91 | Since we have multiple packages and files, we need to specify the package to |
| 92 | which they should belong. |
| 93 | |
| 94 | ``` |
| 95 | $ cue import ./... -p kube |
Marcel van Lohuizen | 2437f9d | 2019-10-29 12:22:38 +0100 | [diff] [blame] | 96 | list, flag, or files flag needed to handle multiple objects in file "./frontend/bartender/kube.yaml" |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 97 | ``` |
| 98 | |
| 99 | Many of the files contain more than one Kubernetes object. |
| 100 | Moreover, we are creating a single configuration that contains all objects |
| 101 | of all files. |
| 102 | We need to organize all Kubernetes objects such that each is individually |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 103 | identifiable within a single configuration. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 104 | We do so by defining a different struct for each type putting each object |
| 105 | in this respective struct keyed by its name. |
| 106 | This allows objects of different types to share the same name, |
| 107 | just as is allowed by Kubernetes. |
| 108 | To accomplish this, we tell `cue` to put each object in the configuration |
| 109 | tree at the path with the "kind" as first element and "name" as second. |
| 110 | |
| 111 | ``` |
Marcel van Lohuizen | f8afaed | 2020-02-07 15:07:14 +0100 | [diff] [blame] | 112 | $ cue import ./... -p kube -l 'strings.ToCamel(kind)' -l metadata.name -f |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 113 | ``` |
| 114 | |
| 115 | The added `-l` flag defines the labels for each object, based on values from |
| 116 | each object, using the usual CUE syntax for field labels. |
| 117 | In this case, we use a camelcase variant of the `kind` field of each object and |
| 118 | use the `name` field of the `metadata` section as the name for each object. |
| 119 | We also added the `-f` flag to overwrite the few files that succeeded before. |
| 120 | |
| 121 | Let's see what happened: |
| 122 | |
| 123 | ``` |
| 124 | $ tree . | head |
| 125 | . |
| 126 | └── services |
| 127 | ├── frontend |
| 128 | │ ├── bartender |
| 129 | │ │ ├── kube.cue |
| 130 | │ │ └── kube.yaml |
| 131 | │ ├── breaddispatcher |
| 132 | │ │ ├── kube.cue |
| 133 | │ │ └── kube.yaml |
| 134 | ... |
| 135 | ``` |
| 136 | |
| 137 | Each of the YAML files is converted to corresponding CUE files. |
| 138 | Comments of the YAML files are preserved. |
| 139 | |
| 140 | The result is not fully pleasing, though. |
| 141 | Take a look at `mon/prometheus/configmap.cue`. |
| 142 | |
| 143 | ``` |
| 144 | $ cat mon/prometheus/configmap.cue |
| 145 | package kube |
| 146 | |
| 147 | apiVersion: "v1" |
| 148 | kind: "ConfigMap" |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 149 | metadata: name: "prometheus" |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 150 | data: { |
| 151 | "alert.rules": """ |
| 152 | groups: |
| 153 | - name: rules.yaml |
| 154 | ... |
| 155 | ``` |
| 156 | |
| 157 | The configuration file still contains YAML embedded in a string value of one |
| 158 | of the fields. |
| 159 | The original YAML file might have looked like it was all structured data, but |
| 160 | the majority of it was a string containing, hopefully, valid YAML. |
| 161 | |
| 162 | The `-R` option attempts to detect structured YAML or JSON strings embedded |
| 163 | in the configuration files and then converts these recursively. |
| 164 | |
Marcel van Lohuizen | 3960473 | 2019-12-01 17:26:43 +0100 | [diff] [blame] | 165 | <-- TODO: update import label format --> |
| 166 | |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 167 | ``` |
Marcel van Lohuizen | f8afaed | 2020-02-07 15:07:14 +0100 | [diff] [blame] | 168 | $ cue import ./... -p kube -l 'strings.ToCamel(kind)' -l metadata.name -f -R |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 169 | ``` |
| 170 | |
| 171 | Now the file looks like: |
| 172 | |
| 173 | ``` |
Marcel van Lohuizen | 7dbf2dc | 2019-06-07 19:37:04 +0200 | [diff] [blame] | 174 | $ cat mon/prometheus/configmap.cue |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 175 | package kube |
| 176 | |
| 177 | import "encoding/yaml" |
| 178 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 179 | configMap: prometheus: { |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 180 | apiVersion: "v1" |
| 181 | kind: "ConfigMap" |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 182 | metadata: name: "prometheus" |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 183 | data: { |
| 184 | "alert.rules": yaml.Marshal(_cue_alert_rules) |
| 185 | _cue_alert_rules: { |
| 186 | groups: [{ |
| 187 | ... |
| 188 | ``` |
| 189 | |
| 190 | That looks better! |
| 191 | The resulting configuration file replaces the original embedded string |
Marko Mikulicic | c3d0b48 | 2019-07-09 13:58:21 +0200 | [diff] [blame] | 192 | with a call to `yaml.Marshal` converting a structured CUE source to |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 193 | a string with an equivalent YAML file. |
| 194 | Fields starting with an underscore (`_`) are not included when emitting |
| 195 | a configuration file (they are when enclosed in double quotes). |
| 196 | |
| 197 | ``` |
| 198 | $ cue eval ./mon/prometheus -e configMap.prometheus |
| 199 | apiVersion: "v1" |
| 200 | kind: "ConfigMap" |
| 201 | metadata: { |
| 202 | name: "prometheus" |
| 203 | } |
| 204 | data: { |
| 205 | "alert.rules": """ |
| 206 | groups: |
| 207 | - name: rules.yaml |
| 208 | ... |
| 209 | ``` |
| 210 | |
| 211 | Yay! |
| 212 | |
| 213 | |
| 214 | ## Quick 'n Dirty Conversion |
| 215 | |
| 216 | In this tutorial we show how to quickly eliminate boilerplate from a set |
| 217 | of configurations. |
| 218 | Manual tailoring will usually give better results, but takes considerably |
| 219 | more thought, while taking the quick and dirty approach gets you mostly there. |
Marcel van Lohuizen | 50a01f3 | 2018-12-20 21:47:59 +0100 | [diff] [blame] | 220 | The result of such a quick conversion also forms a good basis for |
| 221 | a more thoughtful manual optimization. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 222 | |
| 223 | ### Create top-level template |
| 224 | |
| 225 | Now we have imported the YAML files we can start the simplification process. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 226 | |
| 227 | Before we start the restructuring, lets save a full evaluation so that we |
| 228 | can verify that simplifications yield the same results. |
| 229 | |
| 230 | ``` |
Marcel van Lohuizen | db4e4d2 | 2019-04-18 08:43:57 +0200 | [diff] [blame] | 231 | $ cue eval -c ./... > snapshot |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 232 | ``` |
| 233 | |
Marcel van Lohuizen | db4e4d2 | 2019-04-18 08:43:57 +0200 | [diff] [blame] | 234 | The `-c` option tells `cue` that only concrete values, that is valid JSON, |
| 235 | are allowed. |
Marcel van Lohuizen | 50a01f3 | 2018-12-20 21:47:59 +0100 | [diff] [blame] | 236 | We focus on the objects defined in the various `kube.cue` files. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 237 | A quick inspection reveals that many of the Deployments and Services share |
| 238 | common structure. |
| 239 | |
| 240 | We copy one of the files containing both as a basis for creating our template |
| 241 | to the root of the directory tree. |
| 242 | |
| 243 | ``` |
| 244 | $ cp frontend/breaddispatcher/kube.cue . |
| 245 | ``` |
| 246 | |
| 247 | Modify this file as below. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 248 | |
| 249 | ``` |
| 250 | $ cat <<EOF > kube.cue |
| 251 | package kube |
| 252 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 253 | service: [ID=_]: { |
Marko Mikulicic | c3d0b48 | 2019-07-09 13:58:21 +0200 | [diff] [blame] | 254 | apiVersion: "v1" |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 255 | kind: "Service" |
| 256 | metadata: { |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 257 | name: ID |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 258 | labels: { |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 259 | app: ID // by convention |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 260 | domain: "prod" // always the same in the given files |
| 261 | component: string // varies per directory |
| 262 | } |
| 263 | } |
| 264 | spec: { |
| 265 | // Any port has the following properties. |
| 266 | ports: [...{ |
| 267 | port: int |
Marcel van Lohuizen | e5d8d09 | 2019-01-30 15:58:07 +0100 | [diff] [blame] | 268 | protocol: *"TCP" | "UDP" // from the Kubernetes definition |
| 269 | name: string | *"client" |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 270 | }] |
| 271 | selector: metadata.labels // we want those to be the same |
| 272 | } |
| 273 | } |
| 274 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 275 | deployment: [ID=_]: { |
Marcel van Lohuizen | 8f9ef31 | 2019-12-01 17:07:59 +0100 | [diff] [blame] | 276 | apiVersion: "apps/v1" |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 277 | kind: "Deployment" |
Marcel van Lohuizen | 3960473 | 2019-12-01 17:26:43 +0100 | [diff] [blame] | 278 | metadata: name: ID |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 279 | spec: { |
| 280 | // 1 is the default, but we allow any number |
Marcel van Lohuizen | db4e4d2 | 2019-04-18 08:43:57 +0200 | [diff] [blame] | 281 | replicas: *1 | int |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 282 | template: { |
Marcel van Lohuizen | 3960473 | 2019-12-01 17:26:43 +0100 | [diff] [blame] | 283 | metadata: labels: { |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 284 | app: ID |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 285 | domain: "prod" |
| 286 | component: string |
| 287 | } |
| 288 | // we always have one namesake container |
Marcel van Lohuizen | 3960473 | 2019-12-01 17:26:43 +0100 | [diff] [blame] | 289 | spec: containers: [{ name: ID }] |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | } |
| 293 | EOF |
| 294 | ``` |
| 295 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 296 | By replacing the service and deployment name with `[ID=_]` we have changed the |
| 297 | definition into a template matching any field. |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 298 | CUE bind the field name to `ID` as a result. |
Tarun Gupta Akirala | bb2b651 | 2019-06-03 13:24:12 +0000 | [diff] [blame] | 299 | During importing we used `metadata.name` as a key for the object names, |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 300 | so we can now set this field to `ID`. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 301 | |
Marcel van Lohuizen | 50a01f3 | 2018-12-20 21:47:59 +0100 | [diff] [blame] | 302 | Templates are applied to (are unified with) all entries in the struct in which |
| 303 | they are defined, |
| 304 | so we need to either strip fields specific to the `breaddispatcher` definition, |
| 305 | generalize them, or remove them. |
| 306 | |
| 307 | One of the labels defined in the Kubernetes metadata seems to be always set |
| 308 | to parent directory name. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 309 | We enforce this by defining `component: string`, meaning that a field |
Marcel van Lohuizen | 50a01f3 | 2018-12-20 21:47:59 +0100 | [diff] [blame] | 310 | of name `component` must be set to some string value, and then define this |
| 311 | later on. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 312 | Any underspecified field results in an error when converting to, for instance, |
| 313 | JSON. |
| 314 | So a deployment or service will only be valid if this label is defined. |
| 315 | |
| 316 | <!-- TODO: once cycles in disjunctions are implemented |
| 317 | port: targetPort | int // by default the same as targetPort |
| 318 | targetPort: port | int // by default the same as port |
| 319 | |
| 320 | Note that ports definition for service contains a cycle. |
| 321 | Specifying one of the ports will break the cycle. |
| 322 | The meaning of cycles are well-defined in CUE. |
| 323 | In practice this means that a template writer does not have to make any |
| 324 | assumptions about which of the fields that can be mutually derived from each |
| 325 | other a user of the template will want to specify. |
| 326 | --> |
| 327 | |
| 328 | Let's compare the result of merging our new template to our original snapshot. |
| 329 | |
| 330 | ``` |
Marcel van Lohuizen | db4e4d2 | 2019-04-18 08:43:57 +0200 | [diff] [blame] | 331 | $ cue eval ./... -c > snapshot2 |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 332 | --- ./mon/alertmanager |
Marcel van Lohuizen | 2437f9d | 2019-10-29 12:22:38 +0100 | [diff] [blame] | 333 | service.alertmanager.metadata.labels.component: incomplete value (string): |
| 334 | ./kube.cue:11:24 |
| 335 | service.alertmanager.spec.selector.component: incomplete value (string): |
| 336 | ./kube.cue:11:24 |
| 337 | deployment.alertmanager.spec.template.metadata.labels.component: incomplete value (string): |
| 338 | ./kube.cue:36:28 |
| 339 | service."node-exporter".metadata.labels.component: incomplete value (string): |
| 340 | ./kube.cue:11:24 |
| 341 | ... |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 342 | ``` |
| 343 | |
| 344 | <!-- TODO: better error messages --> |
| 345 | |
| 346 | Oops. |
| 347 | The alert manager does not specify the `component` label. |
| 348 | This demonstrates how constraints can be used to catch inconsistencies |
| 349 | in your configurations. |
| 350 | |
| 351 | As there are very few objects that do not specify this label, we will modify |
| 352 | the configurations to include them everywhere. |
| 353 | We do this by setting a newly defined top-level field in each directory |
| 354 | to the directory name and modify our master template file to use it. |
| 355 | |
Marcel van Lohuizen | 7dbf2dc | 2019-06-07 19:37:04 +0200 | [diff] [blame] | 356 | <!-- |
| 357 | ``` |
| 358 | $ cue add */kube.cue -p kube --list <<EOF |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 359 | Component :: "{{.DisplayPath}}" |
Marcel van Lohuizen | 7dbf2dc | 2019-06-07 19:37:04 +0200 | [diff] [blame] | 360 | EOF |
| 361 | ``` |
| 362 | --> |
| 363 | |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 364 | ``` |
| 365 | # set the component label to our new top-level field |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 366 | $ sed -i.bak 's/component:.*string/component: Component/' kube.cue && rm kube.cue.bak |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 367 | |
| 368 | # add the new top-level field to our previous template definitions |
| 369 | $ cat <<EOF >> kube.cue |
| 370 | |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 371 | Component :: string |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 372 | EOF |
| 373 | |
| 374 | # add a file with the component label to each directory |
| 375 | $ ls -d */ | sed 's/.$//' | xargs -I DIR sh -c 'cd DIR; echo "package kube |
| 376 | |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 377 | Component :: \"DIR\" |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 378 | " > kube.cue; cd ..' |
| 379 | |
| 380 | # format the files |
| 381 | $ cue fmt kube.cue */kube.cue |
| 382 | ``` |
| 383 | |
| 384 | Let's try again to see if it is fixed: |
| 385 | |
| 386 | ``` |
Marcel van Lohuizen | db4e4d2 | 2019-04-18 08:43:57 +0200 | [diff] [blame] | 387 | $ cue eval -c ./... > snapshot2 |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 388 | $ diff snapshot snapshot2 |
| 389 | ... |
| 390 | ``` |
| 391 | |
| 392 | Except for having more consistent labels and some reordering, nothing changed. |
| 393 | We are happy and save the result as the new baseline. |
| 394 | |
| 395 | ``` |
| 396 | $ cp snapshot2 snapshot |
| 397 | ``` |
| 398 | |
| 399 | The corresponding boilerplate can now be removed with `cue trim`. |
| 400 | |
| 401 | ``` |
| 402 | $ find . | grep kube.cue | xargs wc | tail -1 |
| 403 | 1792 3616 34815 total |
| 404 | $ cue trim ./... |
| 405 | $ find . | grep kube.cue | xargs wc | tail -1 |
| 406 | 1223 2374 22903 total |
| 407 | ``` |
| 408 | |
| 409 | `cue trim` removes configuration from files that is already generated |
| 410 | by templates or comprehensions. |
| 411 | In doing so it removed over 500 lines of configuration, or over 30%! |
| 412 | |
| 413 | The following is proof that nothing changed semantically: |
| 414 | |
| 415 | ``` |
Marcel van Lohuizen | 7e3bdda | 2020-03-06 18:28:55 +0100 | [diff] [blame] | 416 | $ cue eval -c ./... > snapshot2 |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 417 | $ diff snapshot snapshot2 | wc |
| 418 | 0 0 0 |
| 419 | ``` |
| 420 | |
| 421 | We can do better, though. |
| 422 | A first thing to note is that DaemonSets and StatefulSets share a similar |
| 423 | structure to Deployments. |
| 424 | We generalize the top-level template as follows: |
| 425 | |
| 426 | ``` |
| 427 | $ cat <<EOF >> kube.cue |
| 428 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 429 | daemonSet: [ID=_]: _spec & { |
Marcel van Lohuizen | 8f9ef31 | 2019-12-01 17:07:59 +0100 | [diff] [blame] | 430 | apiVersion: "apps/v1" |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 431 | kind: "DaemonSet" |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 432 | Name :: ID |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 433 | } |
| 434 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 435 | statefulSet: [ID=_]: _spec & { |
Marcel van Lohuizen | 8f9ef31 | 2019-12-01 17:07:59 +0100 | [diff] [blame] | 436 | apiVersion: "apps/v1" |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 437 | kind: "StatefulSet" |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 438 | Name :: ID |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 439 | } |
| 440 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 441 | deployment: [ID=_]: _spec & { |
Marcel van Lohuizen | 8f9ef31 | 2019-12-01 17:07:59 +0100 | [diff] [blame] | 442 | apiVersion: "apps/v1" |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 443 | kind: "Deployment" |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 444 | Name :: ID |
Marcel van Lohuizen | 3960473 | 2019-12-01 17:26:43 +0100 | [diff] [blame] | 445 | spec: replicas: *1 | int |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 446 | } |
| 447 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 448 | configMap: [ID=_]: { |
| 449 | metadata: name: ID |
| 450 | metadata: labels: component: Component |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | _spec: { |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 454 | Name :: string |
| 455 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 456 | metadata: name: Name |
| 457 | metadata: labels: component: Component |
Marcel van Lohuizen | 275105a | 2019-12-06 21:32:41 +0100 | [diff] [blame] | 458 | spec: selector: {} |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 459 | spec: template: { |
| 460 | metadata: labels: { |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 461 | app: Name |
| 462 | component: Component |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 463 | domain: "prod" |
| 464 | } |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 465 | spec: containers: [{name: Name}] |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | EOF |
| 469 | $ cue fmt |
| 470 | ``` |
| 471 | |
| 472 | The common configuration has been factored out into `_spec`. |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 473 | We introduced `Name` to aid both specifying and referring |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 474 | to the name of an object. |
| 475 | For completeness, we added `configMap` as a top-level entry. |
| 476 | |
| 477 | Note that we have not yet removed the old definition of deployment. |
| 478 | This is fine. |
| 479 | As it is equivalent to the new one, unifying them will have no effect. |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 480 | We leave its removal as an exercise to the reader. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 481 | |
| 482 | Next we observe that all deployments, stateful sets and daemon sets have |
| 483 | an accompanying service which shares many of the same fields. |
| 484 | We add: |
| 485 | |
| 486 | ``` |
| 487 | $ cat <<EOF >> kube.cue |
| 488 | |
| 489 | // Define the _export option and set the default to true |
| 490 | // for all ports defined in all containers. |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 491 | _spec: spec: template: spec: containers: [...{ |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 492 | ports: [...{ |
Marcel van Lohuizen | e5d8d09 | 2019-01-30 15:58:07 +0100 | [diff] [blame] | 493 | _export: *true | false // include the port in the service |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 494 | }] |
| 495 | }] |
| 496 | |
Marcel van Lohuizen | 9af9a90 | 2019-09-07 20:30:10 +0200 | [diff] [blame] | 497 | for x in [deployment, daemonSet, statefulSet] for k, v in x { |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 498 | service: "\(k)": { |
| 499 | spec: selector: v.spec.template.metadata.labels |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 500 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 501 | spec: ports: [ { |
Marcel van Lohuizen | 9af9a90 | 2019-09-07 20:30:10 +0200 | [diff] [blame] | 502 | Port = p.containerPort // Port is an alias |
| 503 | port: *Port | int |
| 504 | targetPort: *Port | int |
| 505 | } for c in v.spec.template.spec.containers |
| 506 | for p in c.ports |
| 507 | if p._export ] |
Marcel van Lohuizen | 9af9a90 | 2019-09-07 20:30:10 +0200 | [diff] [blame] | 508 | } |
| 509 | } |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 510 | EOF |
| 511 | $ cue fmt |
| 512 | ``` |
| 513 | |
| 514 | This example introduces a few new concepts. |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 515 | Open-ended lists are indicated with an ellipsis (`...`). |
| 516 | The value following an ellipsis is unified with any subsequent elements and |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 517 | defines the "type", or template, for additional list elements. |
| 518 | |
| 519 | The `Port` declaration is an alias. |
| 520 | Aliases are only visible in their lexical scope and are not part of the model. |
| 521 | They can be used to make shadowed fields visible within nested scopes or, |
| 522 | in this case, to reduce boilerplate without introducing new fields. |
| 523 | |
| 524 | Finally, this example introduces list and field comprehensions. |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 525 | List comprehensions are analogous to list comprehensions found in other |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 526 | languages. |
| 527 | Field comprehensions allow inserting fields in structs. |
| 528 | In this case, the field comprehension adds a namesake service for any |
| 529 | deployment, daemonSet, and statefulSet. |
| 530 | Field comprehensions can also be used to add a field conditionally. |
| 531 | |
| 532 | |
| 533 | Specifying the `targetPort` is not necessary, but since many files define it, |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 534 | defining it here will allow those definitions to be removed |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 535 | using `cue trim`. |
| 536 | We add an option `_export` for ports defined in containers to specify whether |
| 537 | to include them in the service and explicitly set this to false |
| 538 | for the respective ports in `infra/events`, `infra/tasks`, and `infra/watcher`. |
| 539 | |
| 540 | For the purpose of this tutorial, here are some quick patches: |
| 541 | ``` |
| 542 | $ cat <<EOF >> infra/events/kube.cue |
| 543 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 544 | deployment: events: spec: template: spec: containers: [{ ports: [{_export: false}, _] }] |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 545 | EOF |
| 546 | |
| 547 | $ cat <<EOF >> infra/tasks/kube.cue |
| 548 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 549 | deployment: tasks: spec: template: spec: containers: [{ ports: [{_export: false}, _] }] |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 550 | EOF |
| 551 | |
| 552 | $ cat <<EOF >> infra/watcher/kube.cue |
| 553 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 554 | deployment: watcher: spec: template: spec: containers: [{ ports: [{_export: false}, _] }] |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 555 | EOF |
| 556 | ``` |
| 557 | In practice it would be more proper form to add this field in the original |
| 558 | port declaration. |
| 559 | |
| 560 | We verify that all changes are acceptable and store another snapshot. |
| 561 | Then we run trim to further reduce our configuration: |
| 562 | |
| 563 | ``` |
| 564 | $ cue trim ./... |
| 565 | $ find . | grep kube.cue | xargs wc | tail -1 |
| 566 | 1129 2270 22073 total |
| 567 | ``` |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 568 | This is after removing the rewritten and now redundant deployment definition. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 569 | |
| 570 | We shaved off almost another 100 lines, even after adding the template. |
| 571 | You can verify that the service definitions are now gone in most of the files. |
| 572 | What remains is either some additional configuration, or inconsistencies that |
| 573 | should probably be cleaned up. |
| 574 | |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 575 | But we have another trick up our sleeve. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 576 | With the `-s` or `--simplify` option we can tell `trim` or `fmt` to collapse |
| 577 | structs with a single element onto a single line. For instance: |
| 578 | |
| 579 | ``` |
| 580 | $ head frontend/breaddispatcher/kube.cue |
| 581 | package kube |
| 582 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 583 | deployment: breaddispatcher: { |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 584 | spec: { |
| 585 | template: { |
| 586 | metadata: { |
| 587 | annotations: { |
| 588 | "prometheus.io.scrape": "true" |
| 589 | "prometheus.io.port": "7080" |
| 590 | } |
| 591 | $ cue trim ./... -s |
| 592 | $ head -7 frontend/breaddispatcher/kube.cue |
| 593 | package kube |
| 594 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 595 | deployment: breaddispatcher: spec: template: { |
| 596 | metadata: annotations: { |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 597 | "prometheus.io.scrape": "true" |
| 598 | "prometheus.io.port": "7080" |
| 599 | } |
| 600 | $ find . | grep kube.cue | xargs wc | tail -1 |
| 601 | 975 2116 20264 total |
| 602 | ``` |
| 603 | |
| 604 | Another 150 lines lost! |
| 605 | Collapsing lines like this can improve the readability of a configuration |
| 606 | by removing considerable amounts of punctuation. |
| 607 | |
| 608 | |
| 609 | ### Repeat for several subdirectories |
| 610 | |
| 611 | In the previous section we defined templates for services and deployments |
| 612 | in the root of our directory structure to capture the common traits of all |
| 613 | services and deployments. |
| 614 | In addition, we defined a directory-specific label. |
| 615 | In this section we will look into generalizing the objects per directory. |
| 616 | |
| 617 | |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 618 | #### Directory `frontend` |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 619 | |
| 620 | We observe that all deployments in subdirectories of `frontend` |
| 621 | have a single container with one port, |
| 622 | which is usually `7080`, but sometimes `8080`. |
| 623 | Also, most have two prometheus-related annotations, while some have one. |
| 624 | We leave the inconsistencies in ports, but add both annotations |
| 625 | unconditionally. |
| 626 | |
| 627 | ``` |
| 628 | $ cat <<EOF >> frontend/kube.cue |
| 629 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 630 | deployment: [string]: spec: template: { |
| 631 | metadata: annotations: { |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 632 | "prometheus.io.scrape": "true" |
| 633 | "prometheus.io.port": "\(spec.containers[0].ports[0].containerPort)" |
| 634 | } |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 635 | spec: containers: [{ |
Marcel van Lohuizen | e5d8d09 | 2019-01-30 15:58:07 +0100 | [diff] [blame] | 636 | ports: [{containerPort: *7080 | int}] // 7080 is the default |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 637 | }] |
| 638 | } |
| 639 | EOF |
| 640 | $ cue fmt ./frontend |
| 641 | |
| 642 | # check differences |
Marcel van Lohuizen | 7e3bdda | 2020-03-06 18:28:55 +0100 | [diff] [blame] | 643 | $ cue eval -c ./... > snapshot2 |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 644 | $ diff snapshot snapshot2 |
| 645 | 368a369 |
| 646 | > prometheus.io.port: "7080" |
| 647 | 577a579 |
| 648 | > prometheus.io.port: "8080" |
| 649 | $ cp snapshot2 snapshot |
| 650 | ``` |
| 651 | |
| 652 | Two lines with annotations added, improving consistency. |
| 653 | |
| 654 | ``` |
| 655 | $ cue trim -s ./frontend/... |
| 656 | $ find . | grep kube.cue | xargs wc | tail -1 |
| 657 | 931 2052 19624 total |
| 658 | ``` |
| 659 | |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 660 | Another 40 lines removed. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 661 | We may have gotten used to larger reductions, but at this point there is just |
| 662 | not much left to remove: in some of the frontend files there are only 4 lines |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 663 | of configuration left. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 664 | |
| 665 | |
| 666 | #### Directory `kitchen` |
| 667 | |
| 668 | In this directory we observe that all deployments have without exception |
| 669 | one container with port `8080`, all have the same liveness probe, |
| 670 | a single line of prometheus annotation, and most have |
| 671 | two or three disks with similar patterns. |
| 672 | |
| 673 | Let's add everything but the disks for now: |
| 674 | |
| 675 | ``` |
| 676 | $ cat <<EOF >> kitchen/kube.cue |
| 677 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 678 | deployment: [string]: spec: template: { |
| 679 | metadata: annotations: "prometheus.io.scrape": "true" |
| 680 | spec: containers: [{ |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 681 | ports: [{ |
| 682 | containerPort: 8080 |
| 683 | }] |
| 684 | livenessProbe: { |
| 685 | httpGet: { |
| 686 | path: "/debug/health" |
| 687 | port: 8080 |
| 688 | } |
| 689 | initialDelaySeconds: 40 |
| 690 | periodSeconds: 3 |
| 691 | } |
| 692 | }] |
| 693 | } |
| 694 | EOF |
| 695 | $ cue fmt ./kitchen |
| 696 | ``` |
| 697 | |
| 698 | A diff reveals that one prometheus annotation was added to a service. |
| 699 | We assume this to be an accidental omission and accept the differences |
| 700 | |
| 701 | Disks need to be defined in both the template spec section as well as in |
| 702 | the container where they are used. |
| 703 | We prefer to keep these two definitions together. |
| 704 | We take the volumes definition from `expiditer` (the first config in that |
| 705 | directory with two disks), and generalize it: |
| 706 | |
| 707 | ``` |
| 708 | $ cat <<EOF >> kitchen/kube.cue |
| 709 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 710 | deployment: [ID=_]: spec: template: spec: { |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 711 | hasDisks :: *true | bool |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 712 | |
Marcel van Lohuizen | 9af9a90 | 2019-09-07 20:30:10 +0200 | [diff] [blame] | 713 | // field comprehension using just "if" |
| 714 | if hasDisks { |
| 715 | volumes: [{ |
| 716 | name: *"\(ID)-disk" | string |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 717 | gcePersistentDisk: pdName: *"\(ID)-disk" | string |
| 718 | gcePersistentDisk: fsType: "ext4" |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 719 | }, { |
Marcel van Lohuizen | 9af9a90 | 2019-09-07 20:30:10 +0200 | [diff] [blame] | 720 | name: *"secret-\(ID)" | string |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 721 | secret: secretName: *"\(ID)-secrets" | string |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 722 | }, ...] |
Marcel van Lohuizen | 9af9a90 | 2019-09-07 20:30:10 +0200 | [diff] [blame] | 723 | |
| 724 | containers: [{ |
| 725 | volumeMounts: [{ |
| 726 | name: *"\(ID)-disk" | string |
| 727 | mountPath: *"/logs" | string |
| 728 | }, { |
| 729 | mountPath: *"/etc/certs" | string |
| 730 | name: *"secret-\(ID)" | string |
| 731 | readOnly: true |
| 732 | }, ...] |
| 733 | }] |
| 734 | } |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 735 | } |
| 736 | EOF |
| 737 | |
| 738 | $ cat <<EOF >> kitchen/souschef/kube.cue |
| 739 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 740 | deployment: souschef: spec: template: spec: { |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 741 | hasDisks :: false |
| 742 | } |
| 743 | |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 744 | EOF |
| 745 | $ cue fmt ./kitchen/... |
| 746 | ``` |
| 747 | |
| 748 | This template definition is not ideal: the definitions are positional, so if |
| 749 | configurations were to define the disks in a different order, there would be |
| 750 | no reuse or even conflicts. |
| 751 | Also note that in order to deal with this restriction, almost all field values |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 752 | are just default values and can be overridden by instances. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 753 | A better way would be define a map of volumes, |
| 754 | similarly to how we organized the top-level Kubernetes objects, |
| 755 | and then generate these two sections from this map. |
| 756 | This requires some design, though, and does not belong in a |
| 757 | "quick-and-dirty" tutorial. |
| 758 | Later in this document we introduce a manually optimized configuration. |
| 759 | |
| 760 | We add the two disk by default and define a `_hasDisks` option to opt out. |
| 761 | The `souschef` configuration is the only one that defines no disks. |
| 762 | |
| 763 | ``` |
| 764 | $ cue trim -s ./kitchen/... |
| 765 | |
| 766 | # check differences |
| 767 | $ cue eval ./... > snapshot2 |
| 768 | $ diff snapshot snapshot2 |
| 769 | ... |
| 770 | $ cp snapshot2 snapshot |
| 771 | $ find . | grep kube.cue | xargs wc | tail -1 |
| 772 | 807 1862 17190 total |
| 773 | ``` |
| 774 | |
| 775 | The diff shows that we added the `_hadDisks` option, but otherwise reveals no |
| 776 | differences. |
| 777 | We also reduced the configuration by a sizeable amount once more. |
| 778 | |
| 779 | However, on closer inspection of the remaining files we see a lot of remaining |
| 780 | fields in the disk specifications as a result of inconsistent naming. |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 781 | Reducing configurations like we did in this exercise exposes inconsistencies. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 782 | The inconsistencies can be removed by simply deleting the overrides in the |
| 783 | specific configuration. |
| 784 | Leaving them as is gives a clear signal that a configuration is inconsistent. |
| 785 | |
| 786 | |
| 787 | ### Conclusion of Quick 'n Dirty tutorial |
| 788 | |
| 789 | There is still some gain to be made with the other directories. |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 790 | At nearly a 1000-line, or 55%, reduction, we leave the rest as an exercise to |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 791 | the reader. |
| 792 | |
| 793 | We have shown how CUE can be used to reduce boilerplate, enforce consistencies, |
| 794 | and detect inconsistencies. |
| 795 | Being able to deal with consistencies and inconsistencies is a consequence of |
| 796 | the constraint-based model and harder to do with inheritance-based languages. |
| 797 | |
| 798 | We have indirectly also shown how CUE is well-suited for machine manipulation. |
| 799 | This is a factor of syntax and the order independence that follows from its |
| 800 | semantics. |
| 801 | The `trim` command is one of many possible automated refactor tools made |
| 802 | possible by this property. |
| 803 | Also this would be harder to do with inheritance-based configuration languages. |
| 804 | |
| 805 | |
| 806 | ## Define commands |
| 807 | |
| 808 | The `cue export` command can be used to convert the created configuration back |
| 809 | to JSON. |
| 810 | In our case, this requires a top-level "emit value" |
| 811 | to convert our mapped Kubernetes objects back to a list. |
| 812 | Typically, this output is piped to tools like `kubectl` or `etcdctl`. |
| 813 | |
| 814 | In practice this means typing the same commands ad nauseam. |
| 815 | The next step is often to write wrapper tools. |
| 816 | But as there is often no one-size-fits-all solution, this lead to the |
| 817 | proliferation of marginally useful tools. |
| 818 | The `cue` tool provides an alternative by allowing the declaration of |
| 819 | frequently used commands in CUE itself. |
| 820 | Advantages: |
| 821 | |
| 822 | - added domain knowledge that CUE may use for improved analysis, |
| 823 | - only one language to learn, |
| 824 | - easy discovery of commands, |
| 825 | - no further configuration required, |
| 826 | - enforce uniform CLI standards across commands, |
| 827 | - standardized commands across an organization. |
| 828 | |
| 829 | Commands are defined in files ending with `_tool.cue` in the same package as |
| 830 | where the configuration files are defined on which the commands should operate. |
| 831 | Top-level values in the configuration are visible by the tool files |
| 832 | as long as they are not shadowed by top-level fields in the tool files. |
| 833 | Top-level fields in the tool files are not visible in the configuration files |
| 834 | and are not part of any model. |
| 835 | |
| 836 | The tool definitions also have access to additional builtin packages. |
| 837 | A CUE configuration is fully hermetic, disallowing any outside influence. |
| 838 | This property enables automated analysis and manipulation |
| 839 | such as the `trim` command. |
| 840 | The tool definitions, however, have access to such things as command line flags |
| 841 | and environment variables, random generators, file listings, and so on. |
| 842 | |
| 843 | We define the following tools for our example: |
| 844 | |
| 845 | - ls: list the Kubernetes objects defined in our configuration |
| 846 | - dump: dump all selected objects as a YAML stream |
| 847 | - create: send all selected objects to `kubectl` for creation |
| 848 | |
| 849 | ### Preparations |
| 850 | |
| 851 | To work with Kubernetes we need to convert our map of Kubernetes objects |
| 852 | back to a simple list. |
| 853 | We create the tool file to do just that. |
| 854 | |
| 855 | ``` |
Marcel van Lohuizen | 7dbf2dc | 2019-06-07 19:37:04 +0200 | [diff] [blame] | 856 | $ cat <<EOF > kube_tool.cue |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 857 | package kube |
| 858 | |
| 859 | objects: [ x for v in objectSets for x in v ] |
| 860 | |
| 861 | objectSets: [ |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 862 | service, |
| 863 | deployment, |
| 864 | statefulSet, |
| 865 | daemonSet, |
| 866 | configMap, |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 867 | ] |
| 868 | EOF |
| 869 | ``` |
| 870 | |
| 871 | ### Listing objects |
| 872 | |
| 873 | Commands are defined in the `command` section at the top-level of a tool file. |
| 874 | A `cue` command defines command line flags, environment variables, as well as |
| 875 | a set of tasks. |
| 876 | Examples tasks are load or write a file, dump something to the console, |
| 877 | download a web page, or execute a command. |
| 878 | |
| 879 | We start by defining the `ls` command which dumps all our objects |
| 880 | |
| 881 | ``` |
Marcel van Lohuizen | 7dbf2dc | 2019-06-07 19:37:04 +0200 | [diff] [blame] | 882 | $ cat <<EOF > ls_tool.cue |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 883 | package kube |
| 884 | |
Marcel van Lohuizen | 7dbf2dc | 2019-06-07 19:37:04 +0200 | [diff] [blame] | 885 | import ( |
| 886 | "text/tabwriter" |
| 887 | "tool/cli" |
| 888 | "tool/file" |
| 889 | ) |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 890 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 891 | command: ls: { |
| 892 | task: print: cli.Print & { |
Marcel van Lohuizen | 7dbf2dc | 2019-06-07 19:37:04 +0200 | [diff] [blame] | 893 | text: tabwriter.Write([ |
| 894 | "\(x.kind) \t\(x.metadata.labels.component) \t\(x.metadata.name)" |
| 895 | for x in objects |
| 896 | ]) |
| 897 | } |
| 898 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 899 | task: write: file.Create & { |
Marcel van Lohuizen | 7dbf2dc | 2019-06-07 19:37:04 +0200 | [diff] [blame] | 900 | filename: "foo.txt" |
| 901 | contents: task.print.text |
| 902 | } |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 903 | } |
| 904 | EOF |
| 905 | ``` |
| 906 | <!-- TODO: use "let" once implemented--> |
| 907 | |
| 908 | NOTE: THE API OF THE TASK DEFINITIONS WILL CHANGE. |
| 909 | Although we may keep supporting this form if needed. |
| 910 | |
| 911 | The command is now available in the `cue` tool: |
| 912 | |
| 913 | ``` |
| 914 | $ cue cmd ls ./frontend/maitred |
| 915 | Service frontend maitred |
| 916 | Deployment frontend maitred |
| 917 | ``` |
| 918 | |
| 919 | As long as the name does not conflict with an existing command it can be |
| 920 | used as a top-level command as well: |
| 921 | ``` |
| 922 | $ cue ls ./frontend/maitred |
| 923 | ... |
| 924 | ``` |
| 925 | |
| 926 | If more than one instance is selected the `cue` tool may either operate |
| 927 | on them one by one or merge them. |
| 928 | The default is to merge them. |
Marcel van Lohuizen | 0949135 | 2018-12-20 20:20:54 +0100 | [diff] [blame] | 929 | Different instances of a package are typically not compatible: |
| 930 | different subdirectories may have different specializations. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 931 | A merge pre-expands templates of each instance and then merges their root |
| 932 | values. |
Marcel van Lohuizen | 9818761 | 2019-09-03 12:48:25 +0200 | [diff] [blame] | 933 | The result may contain conflicts, such as our top-level `Component` field, |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 934 | but our per-type maps of Kubernetes objects should be free of conflict |
| 935 | (if there is, we have a problem with Kubernetes down the line). |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 936 | A merge thus gives us a unified view of all objects. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 937 | |
| 938 | ``` |
| 939 | $ cue ls ./... |
Marcel van Lohuizen | 2437f9d | 2019-10-29 12:22:38 +0100 | [diff] [blame] | 940 | Service infra tasks |
| 941 | Service frontend bartender |
| 942 | Service frontend breaddispatcher |
| 943 | Service frontend host |
| 944 | Service frontend maitred |
| 945 | Service frontend valeter |
| 946 | Service frontend waiter |
| 947 | Service frontend waterdispatcher |
| 948 | Service infra download |
| 949 | Service infra etcd |
| 950 | Service infra events |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 951 | |
| 952 | ... |
| 953 | |
Marcel van Lohuizen | 2437f9d | 2019-10-29 12:22:38 +0100 | [diff] [blame] | 954 | Deployment proxy nginx |
| 955 | StatefulSet infra etcd |
| 956 | DaemonSet mon node-exporter |
| 957 | ConfigMap mon alertmanager |
| 958 | ConfigMap mon prometheus |
| 959 | ConfigMap proxy authproxy |
| 960 | ConfigMap proxy nginx |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 961 | ``` |
| 962 | |
| 963 | ### Dumping a YAML Stream |
| 964 | |
| 965 | The following adds a command to dump the selected objects as a YAML stream. |
| 966 | |
| 967 | <!-- |
| 968 | TODO: add command line flags to filter object types. |
| 969 | --> |
| 970 | ``` |
Marcel van Lohuizen | 7dbf2dc | 2019-06-07 19:37:04 +0200 | [diff] [blame] | 971 | $ cat <<EOF > dump_tool.cue |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 972 | package kube |
| 973 | |
Marcel van Lohuizen | 7dbf2dc | 2019-06-07 19:37:04 +0200 | [diff] [blame] | 974 | import ( |
| 975 | "encoding/yaml" |
| 976 | "tool/cli" |
| 977 | ) |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 978 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 979 | command: dump: { |
| 980 | task: print: cli.Print & { |
Marcel van Lohuizen | 7dbf2dc | 2019-06-07 19:37:04 +0200 | [diff] [blame] | 981 | text: yaml.MarshalStream(objects) |
| 982 | } |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 983 | } |
| 984 | EOF |
| 985 | ``` |
| 986 | |
| 987 | <!-- |
| 988 | TODO: with new API as well as conversions implemented |
| 989 | command dump task print: cli.Print(text: yaml.MarshalStream(objects)) |
| 990 | |
| 991 | or without conversions: |
| 992 | command dump task print: cli.Print & {text: yaml.MarshalStream(objects)} |
| 993 | --> |
| 994 | |
| 995 | The `MarshalStream` command converts the list of objects to a '`---`'-separated |
| 996 | stream of YAML values. |
| 997 | |
| 998 | |
| 999 | ### Creating Objects |
| 1000 | |
| 1001 | The `create` command sends a list of objects to `kubectl create`. |
| 1002 | |
| 1003 | ``` |
| 1004 | $ cat <<EOF > create_tool.cue |
| 1005 | package kube |
| 1006 | |
Marcel van Lohuizen | 7dbf2dc | 2019-06-07 19:37:04 +0200 | [diff] [blame] | 1007 | import ( |
| 1008 | "encoding/yaml" |
| 1009 | "tool/exec" |
| 1010 | "tool/cli" |
| 1011 | ) |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1012 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 1013 | command: create: { |
| 1014 | task: kube: exec.Run & { |
Marcel van Lohuizen | 7dbf2dc | 2019-06-07 19:37:04 +0200 | [diff] [blame] | 1015 | cmd: "kubectl create --dry-run -f -" |
| 1016 | stdin: yaml.MarshalStream(objects) |
| 1017 | stdout: string |
| 1018 | } |
| 1019 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 1020 | task: display: cli.Print & { |
Marcel van Lohuizen | 7dbf2dc | 2019-06-07 19:37:04 +0200 | [diff] [blame] | 1021 | text: task.kube.stdout |
| 1022 | } |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1023 | } |
| 1024 | EOF |
| 1025 | ``` |
| 1026 | |
| 1027 | This command has two tasks, named `kube` and `display`. |
| 1028 | The `display` task depends on the output of the `kube` task. |
| 1029 | The `cue` tool does a static analysis of the dependencies and runs all |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 1030 | tasks which dependencies are satisfied in parallel while blocking tasks |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1031 | for which an input is missing. |
| 1032 | |
| 1033 | ``` |
| 1034 | $ cue create ./frontend/... |
| 1035 | service "bartender" created (dry run) |
| 1036 | service "breaddispatcher" created (dry run) |
| 1037 | service "host" created (dry run) |
| 1038 | service "maitred" created (dry run) |
| 1039 | service "valeter" created (dry run) |
| 1040 | service "waiter" created (dry run) |
| 1041 | service "waterdispatcher" created (dry run) |
Marcel van Lohuizen | 8f9ef31 | 2019-12-01 17:07:59 +0100 | [diff] [blame] | 1042 | deployment.apps "bartender" created (dry run) |
| 1043 | deployment.apps "breaddispatcher" created (dry run) |
| 1044 | deployment.apps "host" created (dry run) |
| 1045 | deployment.apps "maitred" created (dry run) |
| 1046 | deployment.apps "valeter" created (dry run) |
| 1047 | deployment.apps "waiter" created (dry run) |
| 1048 | deployment.apps "waterdispatcher" created (dry run) |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1049 | ``` |
| 1050 | |
| 1051 | A production real-life version of this could should omit the `--dry-run` flag |
| 1052 | of course. |
| 1053 | |
Joel Longtine | b654466 | 2019-06-11 16:59:12 +0000 | [diff] [blame] | 1054 | ### Extract CUE templates directly from Kubernetes Go source |
| 1055 | |
Florian Klein | d7d2837 | 2019-11-19 05:14:54 -0800 | [diff] [blame] | 1056 | In order for `cue get go` to generate the CUE templates from Go sources, you first need to have the sources locally: |
| 1057 | |
| 1058 | ``` |
| 1059 | $ go get k8s.io/api/apps/v1 |
Florian Klein | d7d2837 | 2019-11-19 05:14:54 -0800 | [diff] [blame] | 1060 | ``` |
| 1061 | |
Joel Longtine | b654466 | 2019-06-11 16:59:12 +0000 | [diff] [blame] | 1062 | ``` |
| 1063 | $ cue get go k8s.io/api/core/v1 |
Marcel van Lohuizen | 8f9ef31 | 2019-12-01 17:07:59 +0100 | [diff] [blame] | 1064 | $ cue get go k8s.io/api/apps/v1 |
Joel Longtine | b654466 | 2019-06-11 16:59:12 +0000 | [diff] [blame] | 1065 | |
| 1066 | ``` |
| 1067 | |
Marcel van Lohuizen | b13155b | 2019-10-25 16:52:30 +0200 | [diff] [blame] | 1068 | Now that we have the Kubernetes definitions in our module, we can import and use them: |
Joel Longtine | b654466 | 2019-06-11 16:59:12 +0000 | [diff] [blame] | 1069 | |
| 1070 | ``` |
| 1071 | $ cat <<EOF > k8s_defs.cue |
| 1072 | package kube |
| 1073 | |
| 1074 | import ( |
| 1075 | "k8s.io/api/core/v1" |
Marcel van Lohuizen | 8f9ef31 | 2019-12-01 17:07:59 +0100 | [diff] [blame] | 1076 | apps_v1 "k8s.io/api/apps/v1" |
Joel Longtine | b654466 | 2019-06-11 16:59:12 +0000 | [diff] [blame] | 1077 | ) |
| 1078 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 1079 | service: [string]: v1.Service |
Marcel van Lohuizen | 8f9ef31 | 2019-12-01 17:07:59 +0100 | [diff] [blame] | 1080 | deployment: [string]: apps_v1.Deployment |
| 1081 | daemonSet: [string]: apps_v1.DaemonSet |
| 1082 | statefulSet: [string]: apps_v1.StatefulSet |
Joel Longtine | b654466 | 2019-06-11 16:59:12 +0000 | [diff] [blame] | 1083 | EOF |
| 1084 | ``` |
| 1085 | |
| 1086 | And, finally, we'll format again: |
| 1087 | |
| 1088 | ``` |
| 1089 | cue fmt |
| 1090 | ``` |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1091 | |
| 1092 | ## Manually tailored configuration |
| 1093 | |
| 1094 | In Section "Quick 'n Dirty" we showed how to quickly get going with CUE. |
| 1095 | With a bit more deliberation, one can reduce configurations even further. |
Marcel van Lohuizen | 0949135 | 2018-12-20 20:20:54 +0100 | [diff] [blame] | 1096 | Also, we would like to define a configuration that is more generic and less tied |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1097 | to Kubernetes. |
| 1098 | |
| 1099 | We will rely heavily on CUEs order independence, which makes it easy to |
| 1100 | combine two configurations of the same object in a well-defined way. |
| 1101 | This makes it easy, for instance, to put frequently used fields in one file |
| 1102 | and more esoteric one in another and then combine them without fear that one |
| 1103 | will override the other. |
Marcel van Lohuizen | 0949135 | 2018-12-20 20:20:54 +0100 | [diff] [blame] | 1104 | We will take this approach in this section. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1105 | |
| 1106 | The end result of this tutorial is in the `manual` directory. |
| 1107 | In the next sections we will show how to get there. |
| 1108 | |
| 1109 | |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1110 | ### Outline |
| 1111 | |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 1112 | The basic premise of our configuration is to maintain two configurations, |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1113 | a simple and abstract one, and one compatible with Kubernetes. |
| 1114 | The Kubernetes version is automatically generated from the simple configuration. |
| 1115 | Each simplified object has a `kubernetes` section that get gets merged into |
| 1116 | the Kubernetes object upon conversion. |
| 1117 | |
| 1118 | We define one top-level file with our generic definitions. |
| 1119 | |
| 1120 | ``` |
| 1121 | // file cloud.cue |
| 1122 | package cloud |
| 1123 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 1124 | service: [Name=_]: { |
Roger Peppe | 1ce0c51 | 2019-09-24 15:29:39 +0100 | [diff] [blame] | 1125 | name: *Name | string // the name of the service |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1126 | |
| 1127 | ... |
| 1128 | |
| 1129 | // Kubernetes-specific options that get mixed in when converting |
| 1130 | // to Kubernetes. |
| 1131 | kubernetes: { |
| 1132 | } |
| 1133 | } |
| 1134 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 1135 | deployment: [Name=_]: { |
Roger Peppe | 1ce0c51 | 2019-09-24 15:29:39 +0100 | [diff] [blame] | 1136 | name: *Name | string |
Marcel van Lohuizen | 0949135 | 2018-12-20 20:20:54 +0100 | [diff] [blame] | 1137 | ... |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1138 | } |
| 1139 | ``` |
| 1140 | |
| 1141 | A Kubernetes-specific file then contains the definitions to |
| 1142 | convert the generic objects to Kubernetes. |
| 1143 | |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1144 | Overall, the code modeling our services and the code generating the kubernetes |
| 1145 | code is separated, while still allowing to inject Kubernetes-specific |
| 1146 | data into our general model. |
| 1147 | At the same time, we can add additional information to our model without |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 1148 | it ending up in the Kubernetes definitions causing it to barf. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1149 | |
Marcel van Lohuizen | 0949135 | 2018-12-20 20:20:54 +0100 | [diff] [blame] | 1150 | |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1151 | ### Deployment Definition |
| 1152 | |
| 1153 | For our design we assume that all Kubernetes Pod derivatives only define one |
| 1154 | container. |
| 1155 | This is clearly not the case in general, but often it does and it is good |
| 1156 | practice. |
Marcel van Lohuizen | 0949135 | 2018-12-20 20:20:54 +0100 | [diff] [blame] | 1157 | Conveniently, it simplifies our model as well. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1158 | |
| 1159 | We base the model loosely on the master templates we derived in |
| 1160 | Section "Quick 'n Dirty". |
| 1161 | The first step we took is to eliminate `statefulSet` and `daemonSet` and |
| 1162 | rather just have a `deployment` allowing different kinds. |
| 1163 | |
| 1164 | ``` |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 1165 | deployment: [Name=_]: _base & { |
Roger Peppe | 1ce0c51 | 2019-09-24 15:29:39 +0100 | [diff] [blame] | 1166 | name: *Name | string |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1167 | ... |
| 1168 | ``` |
| 1169 | |
| 1170 | The kind only needs to be specified if the deployment is a stateful set or |
| 1171 | daemonset. |
| 1172 | This also eliminates the need for `_spec`. |
| 1173 | |
| 1174 | The next step is to pull common fields, such as `image` to the top level. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1175 | |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 1176 | Arguments can be specified as a map. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1177 | ``` |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 1178 | arg: [string]: string |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1179 | args: [ "-\(k)=\(v)" for k, v in arg ] | [...string] |
| 1180 | ``` |
| 1181 | |
| 1182 | If order matters, users could explicitly specify the list as well. |
| 1183 | |
| 1184 | For ports we define two simple maps from name to port number: |
| 1185 | |
| 1186 | ``` |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 1187 | // expose port defines named ports that is exposed in the service |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 1188 | expose: port: [string]: int |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1189 | |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 1190 | // port defines a named port that is not exposed in the service. |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 1191 | port: [string]: int |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1192 | ``` |
| 1193 | Both maps get defined in the container definition, but only `port` gets |
| 1194 | included in the service definition. |
| 1195 | This may not be the best model, and does not support all features, |
Marcel van Lohuizen | 0949135 | 2018-12-20 20:20:54 +0100 | [diff] [blame] | 1196 | but it shows how one can chose a different representation. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1197 | |
| 1198 | A similar story holds for environment variables. |
| 1199 | In most cases mapping strings to string suffices. |
| 1200 | The testdata uses other options though. |
| 1201 | We define a simple `env` map and an `envSpec` for more elaborate cases: |
| 1202 | |
| 1203 | ``` |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 1204 | env: [string]: string |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1205 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 1206 | envSpec: [string]: {} |
Marcel van Lohuizen | 2437f9d | 2019-10-29 12:22:38 +0100 | [diff] [blame] | 1207 | envSpec: { |
| 1208 | for k, v in env { |
| 1209 | "\(k)" value: v |
| 1210 | } |
| 1211 | } |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1212 | ``` |
| 1213 | The simple map automatically gets mapped into the more elaborate map |
| 1214 | which then presents the full picture. |
| 1215 | |
| 1216 | Finally, our assumption that there is one container per deployment allows us |
| 1217 | to create a single definition for volumes, combining the information for |
| 1218 | volume spec and volume mount. |
| 1219 | |
| 1220 | ``` |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 1221 | volume: [Name=_]: { |
Marcel van Lohuizen | 2437f9d | 2019-10-29 12:22:38 +0100 | [diff] [blame] | 1222 | name: *Name | string |
| 1223 | mountPath: string |
| 1224 | subPath: null | string |
| 1225 | readOnly: bool |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1226 | kubernetes: {} |
| 1227 | } |
| 1228 | ``` |
| 1229 | |
Marcel van Lohuizen | 0949135 | 2018-12-20 20:20:54 +0100 | [diff] [blame] | 1230 | All other fields that we way want to define can go into a generic kubernetes |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1231 | struct that gets merged in with all other generated kubernetes data. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1232 | This even allows us to augment generated data, such as adding additional |
| 1233 | fields to the container. |
| 1234 | |
| 1235 | |
| 1236 | ### Service Definition |
| 1237 | |
| 1238 | The service definition is straightforward. |
| 1239 | As we eliminated stateful and daemon sets, the field comprehension to |
| 1240 | automatically derive a service is now a bit simpler: |
| 1241 | |
| 1242 | ``` |
| 1243 | // define services implied by deployments |
Marcel van Lohuizen | 2437f9d | 2019-10-29 12:22:38 +0100 | [diff] [blame] | 1244 | service: { |
| 1245 | for k, spec in deployment { |
| 1246 | "\(k)": { |
| 1247 | // Copy over all ports exposed from containers. |
| 1248 | for Name, Port in spec.expose.port { |
| 1249 | port: "\(Name)": { |
| 1250 | port: *Port | int |
| 1251 | targetPort: *Port | int |
| 1252 | } |
| 1253 | } |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1254 | |
Marcel van Lohuizen | 2437f9d | 2019-10-29 12:22:38 +0100 | [diff] [blame] | 1255 | // Copy over the labels |
| 1256 | label: spec.label |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 1257 | } |
| 1258 | } |
Marcel van Lohuizen | 2437f9d | 2019-10-29 12:22:38 +0100 | [diff] [blame] | 1259 | } |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1260 | ``` |
| 1261 | |
Marcel van Lohuizen | 50a01f3 | 2018-12-20 21:47:59 +0100 | [diff] [blame] | 1262 | The complete top-level model definitions can be found at |
| 1263 | [doc/tutorial/kubernetes/manual/services/cloud.cue](https://cue.googlesource.com/cue/+/master/doc/tutorial/kubernetes/manual/services/cloud.cue). |
| 1264 | |
| 1265 | The tailorings for this specific project (the labels) are defined |
| 1266 | [here](https://cue.googlesource.com/cue/+/master/doc/tutorial/kubernetes/manual/services/kube.cue). |
| 1267 | |
| 1268 | |
| 1269 | ### Converting to Kubernetes |
| 1270 | |
| 1271 | Converting services is fairly straightforward. |
| 1272 | |
| 1273 | ``` |
Marcel van Lohuizen | 2437f9d | 2019-10-29 12:22:38 +0100 | [diff] [blame] | 1274 | kubernetes: services: { |
Marcel van Lohuizen | 9af9a90 | 2019-09-07 20:30:10 +0200 | [diff] [blame] | 1275 | for k, x in service { |
| 1276 | "\(k)": x.kubernetes & { |
| 1277 | apiVersion: "v1" |
| 1278 | kind: "Service" |
Marcel van Lohuizen | 50a01f3 | 2018-12-20 21:47:59 +0100 | [diff] [blame] | 1279 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 1280 | metadata: name: x.name |
| 1281 | metadata: labels: x.label |
| 1282 | spec: selector: x.label |
Marcel van Lohuizen | 50a01f3 | 2018-12-20 21:47:59 +0100 | [diff] [blame] | 1283 | |
Marcel van Lohuizen | 23623fa | 2019-10-23 19:28:59 +0200 | [diff] [blame] | 1284 | spec: ports: [ p for p in x.port ] |
Marcel van Lohuizen | 9af9a90 | 2019-09-07 20:30:10 +0200 | [diff] [blame] | 1285 | } |
| 1286 | } |
Marcel van Lohuizen | 50a01f3 | 2018-12-20 21:47:59 +0100 | [diff] [blame] | 1287 | } |
| 1288 | ``` |
| 1289 | |
| 1290 | We add the Kubernetes boilerplate, map the top-level fields and mix in |
| 1291 | the raw `kubernetes` fields for each service. |
| 1292 | |
| 1293 | Mapping deployments is a bit more involved, though analogous. |
| 1294 | The complete definitions for Kubernetes conversions can be found at |
| 1295 | [doc/tutorial/kubernetes/manual/services/k8s.cue](https://cue.googlesource.com/cue/+/master/doc/tutorial/kubernetes/manual/services/k8s.cue). |
| 1296 | |
| 1297 | Converting the top-level definitions to concrete Kubernetes code is the hardest |
| 1298 | part of this exercise. |
| 1299 | That said, most CUE users will never have to resort to this level of CUE |
| 1300 | to write configurations. |
| 1301 | For instance, none of the files in the subdirectories contain comprehensions, |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 1302 | not even the template files in these directories (such as `kitchen/kube.cue`). |
Marcel van Lohuizen | 1e0fe9c | 2018-12-21 00:17:06 +0100 | [diff] [blame] | 1303 | Furthermore, none of the configuration files in any of the |
| 1304 | leaf directories contain string interpolations. |
Marcel van Lohuizen | 50a01f3 | 2018-12-20 21:47:59 +0100 | [diff] [blame] | 1305 | |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1306 | |
| 1307 | ### Metrics |
| 1308 | |
| 1309 | The fully written out manual configuration can be found in the `manual` |
| 1310 | subdirectory. |
| 1311 | Running our usual count yields |
| 1312 | ``` |
Marcel van Lohuizen | 7dbf2dc | 2019-06-07 19:37:04 +0200 | [diff] [blame] | 1313 | $ find . | grep kube.cue | xargs wc | tail -1 |
Marcel van Lohuizen | 0949135 | 2018-12-20 20:20:54 +0100 | [diff] [blame] | 1314 | 542 1190 11520 total |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1315 | ``` |
| 1316 | This does not count our conversion templates. |
| 1317 | Assuming that the top-level templates are reusable, and if we don't count them |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 1318 | for both approaches, the manual approach shaves off about another 150 lines. |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1319 | If we count the templates as well, the two approaches are roughly equal. |
| 1320 | |
| 1321 | |
| 1322 | ### Conclusions Manual Configuration |
| 1323 | |
Marcel van Lohuizen | 0949135 | 2018-12-20 20:20:54 +0100 | [diff] [blame] | 1324 | We have shown that we can further compact a configuration by manually |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1325 | optimizing template files. |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 1326 | However, we have also shown that the manual optimization only gives |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1327 | a marginal benefit with respect to the quick-and-dirty semi-automatic reduction. |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 1328 | The benefits for the manual definition largely lies in the organizational |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1329 | flexibility one gets. |
| 1330 | |
| 1331 | Manually tailoring your configurations allows creating an abstraction layer |
| 1332 | between logical definitions and Kubernetes-specific definitions. |
Marcel van Lohuizen | 0949135 | 2018-12-20 20:20:54 +0100 | [diff] [blame] | 1333 | At the same time, CUE's order independence |
Emil Hessman | 66ec959 | 2019-07-14 17:58:27 +0200 | [diff] [blame] | 1334 | makes it easy to mix in low-level Kubernetes configuration wherever it is |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1335 | convenient and applicable. |
| 1336 | |
| 1337 | Manual tailoring also allows us to add our own definitions without breaking |
Marcel van Lohuizen | 0949135 | 2018-12-20 20:20:54 +0100 | [diff] [blame] | 1338 | Kubernetes. |
| 1339 | This is crucial in defining information relevant to definitions, |
Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1340 | but unrelated to Kubernetes, where they belong. |
| 1341 | |
| 1342 | Separating abstract from concrete configuration also allows us to create |
| 1343 | difference adaptors for the same configuration. |
| 1344 | |
| 1345 | |
| 1346 | <!-- TODO: |
| 1347 | ## Conversion to `docker-compose` |
Tarun Gupta Akirala | bb2b651 | 2019-06-03 13:24:12 +0000 | [diff] [blame] | 1348 | --> |