|
| 1 | +# Running your Variant command as a Kubernetes controller |
| 2 | + |
| 3 | +Running `variant` with a few environment variables allows you to turn your command into a Kubernetes controller. |
| 4 | + |
| 5 | +Let's say your command had a pair of jobs to upsert and destroy the your stack by using e.g. Terraform and/or Helmfile: |
| 6 | + |
| 7 | +- `variant run apply --env prod --ref abcd1234` |
| 8 | +- `variant run destroy --env prod --ref abcd1234` |
| 9 | + |
| 10 | +```console |
| 11 | +$ cat main.variant |
| 12 | +option "env" { |
| 13 | + type = string |
| 14 | +} |
| 15 | + |
| 16 | +option "ref" { |
| 17 | + type = string |
| 18 | +} |
| 19 | + |
| 20 | +job "apply" { |
| 21 | + exec { |
| 22 | + command = "echo" |
| 23 | + args = ["Deploying ${opt.ref} to ${opt.env}"] |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +job "destroy" { |
| 28 | + exec { |
| 29 | + command = "echo" |
| 30 | + args = ["Destroying ${opt.env}"] |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +You can turn this into a Kubernetes controller by setting `<PREFIX>_JOB_ON_APPLY` to the job ran on resource creation or update, |
| 36 | +and `<PREFIX>_JOB_ON_DESTROY` to the job ran on resource deletion. The only remaining required envvar is `VARIANT_CONTROLLER_NAME`, |
| 37 | +which must be set to whatever name that the controller uses as the name of itself. |
| 38 | + |
| 39 | +As we've seen in the example in the beginning of this section, the on-apply job is `apply` and the on-destroy job is `destroy` so that `variant` invocation |
| 40 | +should look like: |
| 41 | + |
| 42 | +```console |
| 43 | +$ VARIANT_CONTROLLER_JOB_ON_APPLY=apply \ |
| 44 | + VARIANT_CONTROLLER_JOB_ON_DESTROY=destroy \ |
| 45 | + VARIANT_CONTROLLER_NAME=resource \ |
| 46 | + variant |
| 47 | +``` |
| 48 | + |
| 49 | +`variant` uses `core.variant.run/v1beta1` `Resource` as the resource to be reconciled by the controller. |
| 50 | + |
| 51 | +That being said, you can let the controller reconcile your resource by creating a `Resource` object with correct arguments - |
| 52 | +`env` and `ref` in this example - under the object's `spec` field: |
| 53 | + |
| 54 | +```console |
| 55 | +$ kubectl apply -f <(cat EOS |
| 56 | +apiVersion: core.variant.run/v1beta1 |
| 57 | +kind: Resource |
| 58 | +metadata: |
| 59 | + name: myresource |
| 60 | +spec: |
| 61 | + env: preview |
| 62 | + ref: abc1234 |
| 63 | +EOS |
| 64 | +) |
| 65 | +``` |
| 66 | + |
| 67 | +Within a few seconds, the controller will reconcile your `Resource` by running `variant run apply --env preview --ref abc1234`. |
| 68 | + |
| 69 | +You can verify that by tailing controller logs by `kubectl logs`, or browsing the `Reconcilation` object that is created by |
| 70 | +the controller to record the reconcilation details: |
| 71 | + |
| 72 | +```console |
| 73 | +$ kubectl get reconcilation |
| 74 | +NAME AGE |
| 75 | +myresource-2 12m |
| 76 | +``` |
| 77 | + |
| 78 | +```console |
| 79 | +$ kubectl get -o yaml reconcilation myresource-2 |
| 80 | +apiVersion: core.variant.run/v1beta1 |
| 81 | +kind: Reconcilation |
| 82 | +metadata: |
| 83 | + creationTimestamp: "2020-10-28T12:05:55Z" |
| 84 | + generation: 1 |
| 85 | + labels: |
| 86 | + core.variant.run/controller: resource |
| 87 | + core.variant.run/event: apply |
| 88 | + core.variant.run/pod: YOUR_HOST_OR_POD_NAME |
| 89 | + name: myresource-2 |
| 90 | + namespace: default |
| 91 | +spec: |
| 92 | + combinedLogs: |
| 93 | + data: | |
| 94 | + Deploying abc2345 to preview |
| 95 | + job: apply |
| 96 | + resource: |
| 97 | + env: preview |
| 98 | + ref: abc2345 |
| 99 | +``` |
| 100 | + |
| 101 | +Updating the `Resource` object will result in `variant` running `variant run apply` with the updated arguments: |
| 102 | + |
| 103 | +```console |
| 104 | +$ kubectl apply -f <(cat <<EOS |
| 105 | +apiVersion: core.variant.run/v1beta1 |
| 106 | +kind: Resource |
| 107 | +metadata: |
| 108 | + name: myresource |
| 109 | +spec: |
| 110 | + env: preview |
| 111 | + ref: abc2345 |
| 112 | +EOS |
| 113 | +) |
| 114 | +``` |
| 115 | + |
| 116 | +```console |
| 117 | +$ kubectl get reconcilation |
| 118 | +NAME AGE |
| 119 | +myresource-2 12m |
| 120 | +myresource-3 12m |
| 121 | +``` |
| 122 | + |
| 123 | +```cnosole |
| 124 | +apiVersion: core.variant.run/v1beta1urce-3 |
| 125 | +kind: Reconcilation |
| 126 | +metadata: |
| 127 | + creationTimestamp: "2020-10-28T12:06:10Z" |
| 128 | + generation: 1 |
| 129 | + labels: |
| 130 | + core.variant.run/controller: resource |
| 131 | + core.variant.run/event: apply |
| 132 | + core.variant.run/pod: YOUR_HOST_OR_POD_NAME |
| 133 | + name: myresource-3 |
| 134 | + namespace: default |
| 135 | +spec: |
| 136 | + combinedLogs: |
| 137 | + data: | |
| 138 | + Deploying abc1234 to preview |
| 139 | + job: apply |
| 140 | + resource: |
| 141 | + env: preview |
| 142 | + ref: abc1234 |
| 143 | +``` |
| 144 | + |
| 145 | +Finally, deleting the `Resource` will let `variant` destroy the underlying resources by running `variant run destroy` |
| 146 | +as you've configured: |
| 147 | + |
| 148 | +```console |
| 149 | +$ kubectl get reconcilation |
| 150 | +NAME AGE |
| 151 | +myresource-2 19m |
| 152 | +myresource-3 19m |
| 153 | +myresource-4 9s |
| 154 | +``` |
| 155 | + |
| 156 | +```console |
| 157 | +$ kubectl get reconcilation -o yaml myresource-4 |
| 158 | +apiVersion: core.variant.run/v1beta1 |
| 159 | +kind: Reconcilation |
| 160 | +metadata: |
| 161 | + creationTimestamp: "2020-10-28T12:25:32Z" |
| 162 | + generation: 1 |
| 163 | + labels: |
| 164 | + core.variant.run/controller: resource |
| 165 | + core.variant.run/event: apply |
| 166 | + core.variant.run/pod: YOUR_POD_OR_HOST_NAME |
| 167 | + name: myresource-4 |
| 168 | + namespace: default |
| 169 | +spec: |
| 170 | + combinedLogs: |
| 171 | + data: | |
| 172 | + Destroying preview |
| 173 | + job: destroy |
| 174 | + resource: |
| 175 | + env: preview |
| 176 | + ref: abc1234 |
| 177 | +``` |
| 178 | + |
| 179 | +Now, go build a container image of your Variant command and deploy it as a Kubernetes deployment, and you've finished |
| 180 | +deploying your first Variant-powered Kubernetes controller :smile: |
| 181 | + |
| 182 | +We've used simple `echo` as the implementations of `apply` and `destroy` jobs. |
| 183 | +But obviously you can run any combinations of tools within your jobs to easily manage whatever "stack". |
| 184 | + |
| 185 | +The list of tools may include: |
| 186 | + |
| 187 | +- Terraform |
| 188 | +- AWS CDK |
| 189 | +- Kubectl |
| 190 | +- Helm |
| 191 | +- Helmfile |
| 192 | +- Waypoint |
0 commit comments