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