|
| 1 | +# Getting Started |
| 2 | + |
| 3 | +First check the [prerequisites](./02_prereq.md). |
| 4 | + |
| 5 | +## Setup environment |
| 6 | + |
| 7 | +This exercise will walk through a basic Hello world example. The API is already generated |
| 8 | + |
| 9 | +This exercise will walk through a basic Hello world example. The API is already generated |
| 10 | + |
| 11 | +clone the choreo-examples git repo |
| 12 | + |
| 13 | +```bash |
| 14 | +git clone https://github.com/kform-dev/choreo-examples |
| 15 | +``` |
| 16 | + |
| 17 | +Best to use 2 windows, one for the choreo server and one for the choreo client, since the choreo server will serve the system |
| 18 | + |
| 19 | +## choreo server |
| 20 | + |
| 21 | +start the choreoserver |
| 22 | + |
| 23 | +``` |
| 24 | +choreoctl server start choreo-examples/hello-world/ |
| 25 | +``` |
| 26 | + |
| 27 | +The choreoserver support a version controlled backend but we dont explore this in this exercise. |
| 28 | + |
| 29 | +```json |
| 30 | +{"time":"2024-09-30T19:26:06.771564+02:00","level":"INFO","message":"server started","logger":"choreoctl-logger","data":{"name":"choreoServer","address":"127.0.0.1:51000"}} |
| 31 | +branchstore update main oldstate <nil> -> newstate CheckedOut |
| 32 | +``` |
| 33 | + |
| 34 | +## choreo client |
| 35 | + |
| 36 | +With the following command we can explore the api(s) supported by the system. We see the helloworlds api being present, which got loaded when we started the server |
| 37 | + |
| 38 | +```bash |
| 39 | +choreoctl api-resources |
| 40 | +``` |
| 41 | + |
| 42 | +```bash |
| 43 | +&{upstreamrefs choreo.kform.dev v1alpha1 UpstreamRef false [pkg knet]} |
| 44 | +&{libraries choreo.kform.dev v1alpha1 Library false [choreo]} |
| 45 | +&{apiresources choreo.kform.dev v1alpha1 APIResources true []} |
| 46 | +&{configgenerators choreo.kform.dev v1alpha1 ConfigGenerator false [pkg knet]} |
| 47 | +&{customresourcedefinitions apiextensions.k8s.io v1 CustomResourceDefinition false []} |
| 48 | +&{reconcilers choreo.kform.dev v1alpha1 Reconciler false [choreo]} |
| 49 | +&{helloworlds example.com v1alpha1 HelloWorld HelloWorldList true []} |
| 50 | +``` |
| 51 | + |
| 52 | +When executing the following command no result should be shown, since no hello world resources are loaded |
| 53 | + |
| 54 | +``` |
| 55 | +choreoctl get customresourcedefinitions.apiextensions.k8s.io |
| 56 | +``` |
| 57 | + |
| 58 | +Autocompletion should work, maybe try TAB completion iso copying the full command |
| 59 | + |
| 60 | +Now run the reconciler |
| 61 | + |
| 62 | +``` |
| 63 | +choreoctl run once |
| 64 | +``` |
| 65 | + |
| 66 | +you should see the reconciler `example.com.helloworlds.helloworld` being executed. |
| 67 | + |
| 68 | +``` |
| 69 | +execution success, time(sec) 0.0031725 |
| 70 | +Reconciler Start Stop Requeue Error |
| 71 | +example.com.helloworlds.helloworld 2 2 0 0 |
| 72 | +``` |
| 73 | + |
| 74 | +What just happened? |
| 75 | + |
| 76 | +a. the reconciler got loaded |
| 77 | + |
| 78 | +/// details | HelloWorld Reconciler |
| 79 | + |
| 80 | +```yaml |
| 81 | +--8<-- |
| 82 | +https://raw.githubusercontent.com/kform-dev/choreo-examples/main/hello-world/reconcilers/example.com.helloworlds.helloworld.star |
| 83 | +--8<-- |
| 84 | +``` |
| 85 | + |
| 86 | +/// |
| 87 | + |
| 88 | +b. The reconciler registered to be informed on any HelloWorld resource change |
| 89 | + |
| 90 | +```yaml |
| 91 | + group: example.com |
| 92 | + version: v1alpha1 |
| 93 | + kind: HelloWorld |
| 94 | +``` |
| 95 | +
|
| 96 | +/// details | HelloWorld Reconciler Hook |
| 97 | +
|
| 98 | +```yaml |
| 99 | +--8<-- |
| 100 | +https://raw.githubusercontent.com/kform-dev/choreo-examples/main/hello-world/reconcilers/example.com.helloworlds.helloworld.yaml |
| 101 | +--8<-- |
| 102 | +``` |
| 103 | + |
| 104 | +/// |
| 105 | + |
| 106 | +c. The reconciler business logic got triggered by adding this HelloWorld manifest |
| 107 | + |
| 108 | +/// details | Hello World manifest |
| 109 | + |
| 110 | +```yaml |
| 111 | +--8<-- |
| 112 | +https://raw.githubusercontent.com/kform-dev/choreo-examples/main/hello-world/in/example.com.helloworlds.test.yaml |
| 113 | +--8<-- |
| 114 | +``` |
| 115 | + |
| 116 | +/// |
| 117 | + |
| 118 | +let's see if it performed its job, by looking at the details of the HelloWorld manifest |
| 119 | + |
| 120 | +``` |
| 121 | +choreoctl get helloworlds.example.com test -o yaml |
| 122 | +``` |
| 123 | + |
| 124 | +We should see spec.greeting being changed to `hello choreo` |
| 125 | + |
| 126 | +```yaml |
| 127 | +apiVersion: example.com/v1alpha1 |
| 128 | +kind: HelloWorld |
| 129 | +metadata: |
| 130 | + annotations: |
| 131 | + api.choreo.kform.dev/origin: '{"kind":"File"}' |
| 132 | + creationTimestamp: "2024-09-30T17:49:34Z" |
| 133 | + generation: 1 |
| 134 | + name: test |
| 135 | + namespace: default |
| 136 | + resourceVersion: "1" |
| 137 | + uid: deedbf64-b348-477e-9fbb-d2738ab4f3b0 |
| 138 | +spec: |
| 139 | + greeting: hello choreo |
| 140 | +status: |
| 141 | + conditions: |
| 142 | + - lastTransitionTime: "2024-09-30T17:49:34Z" |
| 143 | + message: "" |
| 144 | + reason: Ready |
| 145 | + status: "True" |
| 146 | + type: Ready |
| 147 | +``` |
| 148 | +
|
| 149 | +🎉 You ran you first choreo reconciler. 🤘 |
| 150 | +
|
| 151 | +Did you notice none of this required a kubernetes cluster? |
| 152 | +Choreo applies the kubernetes principles w/o imposing all the kubernetes container orchestration primitives. |
| 153 | +
|
| 154 | +Try changing the business logic from `Hello Choreo` to `hello <your name>` and execute the business logic again |
| 155 | + |
| 156 | +```python |
| 157 | +def reconcile(helloworld): |
| 158 | + spec = helloworld.get("spec", {}) |
| 159 | + spec["greeting"] = "hello wim" |
| 160 | + helloworld['spec'] = spec |
| 161 | + return reconcile_result(helloworld, False, 0, conditionType, "", False) |
| 162 | +``` |
| 163 | + |
| 164 | +This should result in the following outcome if we run the business logic again. |
| 165 | + |
| 166 | +``` |
| 167 | +choreoctl run once |
| 168 | +``` |
| 169 | +
|
| 170 | +```yaml |
| 171 | +apiVersion: example.com/v1alpha1 |
| 172 | +kind: HelloWorld |
| 173 | +metadata: |
| 174 | + annotations: |
| 175 | + api.choreo.kform.dev/origin: '{"kind":"File"}' |
| 176 | + creationTimestamp: "2024-09-30T17:49:34Z" |
| 177 | + generation: 1 |
| 178 | + name: test |
| 179 | + namespace: default |
| 180 | + resourceVersion: "1" |
| 181 | + uid: deedbf64-b348-477e-9fbb-d2738ab4f3b0 |
| 182 | +spec: |
| 183 | + greeting: hello wim |
| 184 | +status: |
| 185 | + conditions: |
| 186 | + - lastTransitionTime: "2024-09-30T17:49:34Z" |
| 187 | + message: "" |
| 188 | + reason: Ready |
| 189 | + status: "True" |
| 190 | + type: Ready |
| 191 | +``` |
| 192 | + |
| 193 | +You can also introduce an error and see what happens; e.g. change `greeting` to `greetings` which is an invalid json key in the schema. |
| 194 | + |
| 195 | +```python |
| 196 | +def reconcile(helloworld): |
| 197 | + spec = helloworld.get("spec", {}) |
| 198 | + spec["greetings"] = "hello wim" |
| 199 | + helloworld['spec'] = spec |
| 200 | + return reconcile_result(helloworld, False, 0, conditionType, "", False) |
| 201 | +``` |
| 202 | + |
| 203 | +when executing |
| 204 | + |
| 205 | +``` |
| 206 | +choreoctl run once |
| 207 | +``` |
| 208 | + |
| 209 | +the following result is obtained, indicating the schema error |
| 210 | + |
| 211 | +```bash |
| 212 | +execution failed example.com.helloworlds.helloworld.HelloWorld.example.com.test rpc error: code = InvalidArgument desc = fieldmanager apply failed err: failed to create typed patch object (default/test; example.com/v1alpha1, Kind=HelloWorld): .spec.greetings: field not declared in schema |
| 213 | +``` |
0 commit comments