|
| 1 | +# Implementing in-place update hooks |
| 2 | + |
| 3 | +<aside class="note warning"> |
| 4 | + |
| 5 | +<h1>Caution</h1> |
| 6 | + |
| 7 | +Please note Runtime SDK is an advanced feature. If implemented incorrectly, a failing Runtime Extension can severely impact the Cluster API runtime. |
| 8 | + |
| 9 | +</aside> |
| 10 | + |
| 11 | +## Introduction |
| 12 | + |
| 13 | +The proposal for [in-place updates in Cluster API](https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/20240807-in-place-updates.md) |
| 14 | +introduced extensions allowing users to execute changes on existing machines without deleting the machines and creating a new one. |
| 15 | + |
| 16 | +Notably, the Cluster API user experience remain the same as of today no matter of the in-place update feature is enabled |
| 17 | +or not e.g. in order to trigger a MachineDeployment rollout, you have to rotate a template, etc. |
| 18 | + |
| 19 | +Users should care ONLY about the desired state (as of today). |
| 20 | + |
| 21 | +Cluster API is responsible to choose the best strategy to achieve desired state, and with the introduction of |
| 22 | +update extensions, Cluster API is expanding the set of tools that can be used to achieve the desired state. |
| 23 | + |
| 24 | +If external update extensions can not cover the totality of the desired changes, CAPI will fall back to Cluster API’s default, |
| 25 | +immutable rollouts. |
| 26 | + |
| 27 | +Cluster API will be also responsible to determine which Machine/MachineSet should be updated, as well as to handle rollout |
| 28 | +options like MaxSurge/MaxUnavailable. With this regard: |
| 29 | + |
| 30 | +- Machines updating in-place are considered not available, because in-place updates are always considered as potentially disruptive. |
| 31 | + - For control plane machines, if maxSurge is one, a new machine must be created first, then as soon as there is |
| 32 | + “buffer” for in-place, in-place update can proceed. |
| 33 | + - KCP will not use in-place in case it will detect that it can impact health of the control plane. |
| 34 | + - For workers machines, if maxUnavailable is zero, a new machine must be created first, then as soon as there |
| 35 | + is “buffer” for in-place, in-place update can proceed. |
| 36 | + - When in-place is possible, the system should try to in-place update as many machines as possible. |
| 37 | + In practice, this means that maxSurge might be not fully used (it is used only for scale up by one if maxUnavailable=0). |
| 38 | + - No in-place updates are performed for workers machines when using rollout strategy `OnDelete`. |
| 39 | + |
| 40 | +<aside class="note warning"> |
| 41 | + |
| 42 | +<h1>Important!</h1> |
| 43 | + |
| 44 | +Cluster API will call the in-place extensions only if the `InPlaceUpdates` feature flag is enabled. |
| 45 | + |
| 46 | +Also, please note that the current implementation of the [in-place updates proposal](https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/20240807-in-place-updates.md) only allows registering one extension for the `CanUpdateMachine`, `CanUpdateMachineSet` and `UpdateMachine` hooks. |
| 47 | + |
| 48 | +</aside> |
| 49 | + |
| 50 | +<!-- TOC --> |
| 51 | +* [Implementing in-place update hooks](#implementing-in-place-update-hooks) |
| 52 | + * [Introduction](#introduction) |
| 53 | + * [Guidelines](#guidelines) |
| 54 | + * [Definitions](#definitions) |
| 55 | + * [CanUpdateMachine](#canupdatemachine) |
| 56 | + * [CanUpdateMachineSet](#canupdatemachineset) |
| 57 | + * [UpdateMachine](#updatemachine) |
| 58 | +<!-- TOC --> |
| 59 | + |
| 60 | +## Guidelines |
| 61 | + |
| 62 | +All guidelines defined in [Implementing Runtime Extensions](implement-extensions.md#guidelines) apply to the |
| 63 | +implementation of Runtime Extensions for upgrade plan hooks as well. |
| 64 | + |
| 65 | +In summary, Runtime Extensions are components that should be designed, written and deployed with great caution given |
| 66 | +that they can affect the proper functioning of the Cluster API runtime. A poorly implemented Runtime Extension could |
| 67 | +potentially block upgrade transitions from happening. |
| 68 | + |
| 69 | +Following recommendations are especially relevant: |
| 70 | + |
| 71 | +* [Timeouts](implement-extensions.md#timeouts) |
| 72 | +* [Idempotence](implement-extensions.md#idempotence) |
| 73 | +* [Deterministic result](implement-extensions.md#deterministic-result) |
| 74 | +* [Error messages](implement-extensions.md#error-messages) |
| 75 | +* [Error management](implement-extensions.md#error-management) |
| 76 | +* [Avoid dependencies](implement-extensions.md#avoid-dependencies) |
| 77 | + |
| 78 | +## Definitions |
| 79 | + |
| 80 | +For additional details about the OpenAPI spec of the upgrade plan hooks, please download the [`runtime-sdk-openapi.yaml`]({{#releaselink repo:"https://github.com/kubernetes-sigs/cluster-api" gomodule:"sigs.k8s.io/cluster-api" asset:"runtime-sdk-openapi.yaml" version:"1.11.x"}}) |
| 81 | +file and then open it from the [Swagger UI](https://editor.swagger.io/). |
| 82 | + |
| 83 | +### CanUpdateMachine |
| 84 | + |
| 85 | +This hook is called by KCP when performing the "can update in-place" for a control plane machine. |
| 86 | + |
| 87 | +Example request: |
| 88 | + |
| 89 | +```yaml |
| 90 | +apiVersion: hooks.runtime.cluster.x-k8s.io/v1alpha1 |
| 91 | +kind: CanUpdateMachineRequest |
| 92 | +settings: <Runtime Extension settings> |
| 93 | +current: |
| 94 | + machine: |
| 95 | + apiVersion: cluster.x-k8s.io/v1beta2 |
| 96 | + kind: Machine |
| 97 | + metadata: |
| 98 | + name: test-cluster |
| 99 | + namespace: test-ns |
| 100 | + spec: |
| 101 | + ... |
| 102 | + infrastructureMachine: |
| 103 | + apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 |
| 104 | + kind: VSphereMachine |
| 105 | + metadata: |
| 106 | + name: test-cluster |
| 107 | + namespace: test-ns |
| 108 | + spec: |
| 109 | + ... |
| 110 | + boostrapConfig: |
| 111 | + apiVersion: bootstrap.cluster.x-k8s.io/v1beta1 |
| 112 | + kind: KubeadmConfig |
| 113 | + metadata: |
| 114 | + name: test-cluster |
| 115 | + namespace: test-ns |
| 116 | + spec: |
| 117 | + ... |
| 118 | +desired: |
| 119 | + machine: |
| 120 | + ... |
| 121 | + infrastructureMachine: |
| 122 | + ... |
| 123 | + boostrapConfig: |
| 124 | + ... |
| 125 | +``` |
| 126 | +
|
| 127 | +Note: |
| 128 | +- All the objects will have the latest API version known by Cluster API. |
| 129 | +- Only spec is provided, status fields are not included |
| 130 | +- In a future release, when registering more than one extension for the `CanUpdateMachine` will be supported, the current state will already include changes that can be handled in-place by other runtime extensions. |
| 131 | + |
| 132 | +Example Response: |
| 133 | + |
| 134 | +```yaml |
| 135 | +apiVersion: hooks.runtime.cluster.x-k8s.io/v1alpha1 |
| 136 | +kind: CanUpdateMachineResponse |
| 137 | +status: Success # or Failure |
| 138 | +message: "error message if status == Failure" |
| 139 | +machinePatch: |
| 140 | + patchType: JSONPatch |
| 141 | + patch: <JSON-patch> |
| 142 | +infrastructureMachinePatch: |
| 143 | + ... |
| 144 | +boostrapConfigPatch: |
| 145 | + ... |
| 146 | +``` |
| 147 | + |
| 148 | +Note: |
| 149 | +- Extensions should return per-object patches to be applied on current objects to indicate which changes they can handle in-place. |
| 150 | +- Only fields in Machine/InfraMachine/BootstrapConfig spec have to be covered by patches |
| 151 | +- Patches must be in JSONPatch or JSONMergePatch format |
| 152 | + |
| 153 | +### CanUpdateMachineSet |
| 154 | + |
| 155 | +This hook is called by the MachineDeployment controller when performing the "can update in-place" for all the Machines controlled by |
| 156 | +a MachineSet. |
| 157 | + |
| 158 | +Example request: |
| 159 | + |
| 160 | +```yaml |
| 161 | +apiVersion: hooks.runtime.cluster.x-k8s.io/v1alpha1 |
| 162 | +kind: CanUpdateMachineSetRequest |
| 163 | +settings: <Runtime Extension settings> |
| 164 | +current: |
| 165 | + machineSet: |
| 166 | + apiVersion: cluster.x-k8s.io/v1beta2 |
| 167 | + kind: MachineSet |
| 168 | + metadata: |
| 169 | + name: test-cluster |
| 170 | + namespace: test-ns |
| 171 | + spec: |
| 172 | + ... |
| 173 | + infrastructureMachineTemplate: |
| 174 | + apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 |
| 175 | + kind: VSphereMachineTemplate |
| 176 | + metadata: |
| 177 | + name: test-cluster |
| 178 | + namespace: test-ns |
| 179 | + spec: |
| 180 | + ... |
| 181 | + boostrapConfigTemplate: |
| 182 | + apiVersion: bootstrap.cluster.x-k8s.io/v1beta1 |
| 183 | + kind: KubeadmConfigTemplate |
| 184 | + metadata: |
| 185 | + name: test-cluster |
| 186 | + namespace: test-ns |
| 187 | + spec: |
| 188 | + ... |
| 189 | +desired: |
| 190 | + machineSet: |
| 191 | + ... |
| 192 | + infrastructureMachineTemplate: |
| 193 | + ... |
| 194 | + boostrapConfigTemplate: |
| 195 | + ... |
| 196 | +``` |
| 197 | + |
| 198 | +Note: |
| 199 | +- All the objects will have the latest API version known by Cluster API. |
| 200 | +- Only spec is provided, status fields are not included |
| 201 | +- In a future release, when registering more than one extension for the `CanUpdateMachineSet` will be supported, the current state will already include changes that can be handled in-place by other runtime extensions. |
| 202 | + |
| 203 | +Example Response: |
| 204 | + |
| 205 | +```yaml |
| 206 | +apiVersion: hooks.runtime.cluster.x-k8s.io/v1alpha1 |
| 207 | +kind: CanUpdateMachineSetResponse |
| 208 | +status: Success # or Failure |
| 209 | +message: "error message if status == Failure" |
| 210 | +machineSetPatch: |
| 211 | + patchType: JSONPatch |
| 212 | + patch: <JSON-patch> |
| 213 | +infrastructureMachineTemplatePatch: |
| 214 | + ... |
| 215 | +boostrapConfigTemplatePatch: |
| 216 | + ... |
| 217 | +``` |
| 218 | + |
| 219 | +Note: |
| 220 | +- Extensions should return per-object patches to be applied on current objects to indicate which changes they can handle in-place. |
| 221 | +- Only fields in Machine/InfraMachine/BootstrapConfig spec have to be covered by patches |
| 222 | +- Patches must be in JSONPatch or JSONMergePatch format |
| 223 | + |
| 224 | +### UpdateMachine |
| 225 | + |
| 226 | +This hook is called by the Machine controller when performing the in-place updates for a Machine. |
| 227 | + |
| 228 | +Example request: |
| 229 | + |
| 230 | +```yaml |
| 231 | +apiVersion: hooks.runtime.cluster.x-k8s.io/v1alpha1 |
| 232 | +kind: UpdateMachineRequest |
| 233 | +settings: <Runtime Extension settings> |
| 234 | +desired: |
| 235 | + machine: |
| 236 | + apiVersion: cluster.x-k8s.io/v1beta2 |
| 237 | + kind: Machine |
| 238 | + metadata: |
| 239 | + name: test-cluster |
| 240 | + namespace: test-ns |
| 241 | + spec: |
| 242 | + ... |
| 243 | + infrastructureMachineTemplate: |
| 244 | + apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 |
| 245 | + kind: VSphereMachineTemplate |
| 246 | + metadata: |
| 247 | + name: test-cluster |
| 248 | + namespace: test-ns |
| 249 | + spec: |
| 250 | + ... |
| 251 | + boostrapConfigTemplate: |
| 252 | + apiVersion: bootstrap.cluster.x-k8s.io/v1beta1 |
| 253 | + kind: KubeadmConfigTemplate |
| 254 | + metadata: |
| 255 | + name: test-cluster |
| 256 | + namespace: test-ns |
| 257 | + spec: |
| 258 | + ... |
| 259 | +``` |
| 260 | + |
| 261 | +Note: |
| 262 | +- Only desired is provided (the external updater extension should know current state of the Machine). |
| 263 | +- Only spec is provided, status fields are not included |
| 264 | + |
| 265 | +Example Response: |
| 266 | + |
| 267 | +```yaml |
| 268 | +apiVersion: hooks.runtime.cluster.x-k8s.io/v1alpha1 |
| 269 | +kind: UpdateMachineSetResponse |
| 270 | +status: Success # or Failure |
| 271 | +message: "error message if status == Failure" |
| 272 | +retryAfterSeconds: 10 |
| 273 | +``` |
| 274 | + |
| 275 | +Note: |
| 276 | +- The status of the update operation is determined by the CommonRetryResponse fields: |
| 277 | + - Status=Success + RetryAfterSeconds > 0: update is in progress |
| 278 | + - Status=Success + RetryAfterSeconds = 0: update completed successfully |
| 279 | + - Status=Failure: update failed |
0 commit comments