|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + corev1 "k8s.io/api/core/v1" |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | + |
| 23 | + runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog" |
| 24 | +) |
| 25 | + |
| 26 | +// CanUpdateMachineRequest is the request of the CanUpdateMachine hook. |
| 27 | +// This hook is called to determine if an extension can handle specific changes. |
| 28 | +// +kubebuilder:object:root=true |
| 29 | +type CanUpdateMachineRequest struct { |
| 30 | + metav1.TypeMeta `json:",inline"` |
| 31 | + |
| 32 | + // CommonRequest contains fields common to all request types. |
| 33 | + CommonRequest `json:",inline"` |
| 34 | + |
| 35 | + // changes is a list of field paths that need to be updated on the machine. |
| 36 | + // Examples: ["spec.version", "spec.infrastructureRef.spec.memoryMiB"] |
| 37 | + // +required |
| 38 | + Changes []string `json:"changes"` |
| 39 | +} |
| 40 | + |
| 41 | +var _ ResponseObject = &CanUpdateMachineResponse{} |
| 42 | + |
| 43 | +// CanUpdateMachineResponse is the response of the CanUpdateMachine hook. |
| 44 | +// +kubebuilder:object:root=true |
| 45 | +type CanUpdateMachineResponse struct { |
| 46 | + metav1.TypeMeta `json:",inline"` |
| 47 | + |
| 48 | + // CommonResponse contains Status and Message fields common to all response types. |
| 49 | + CommonResponse `json:",inline"` |
| 50 | + |
| 51 | + // acceptedChanges is the subset of requested changes that this extension can handle. |
| 52 | + // If empty, the extension cannot handle any of the requested changes. |
| 53 | + // +optional |
| 54 | + AcceptedChanges []string `json:"acceptedChanges,omitempty"` |
| 55 | +} |
| 56 | + |
| 57 | +// CanUpdateMachine is the hook that will be called to determine if an extension |
| 58 | +// can handle specific machine changes for in-place updates. |
| 59 | +func CanUpdateMachine(*CanUpdateMachineRequest, *CanUpdateMachineResponse) {} |
| 60 | + |
| 61 | +// UpdateMachineRequest is the request of the UpdateMachine hook. |
| 62 | +// This hook is called to perform the actual in-place update on a machine. |
| 63 | +// +kubebuilder:object:root=true |
| 64 | +type UpdateMachineRequest struct { |
| 65 | + metav1.TypeMeta `json:",inline"` |
| 66 | + |
| 67 | + // CommonRequest contains fields common to all request types. |
| 68 | + CommonRequest `json:",inline"` |
| 69 | + |
| 70 | + // machineRef is a reference to the machine object the in-place update hook corresponds to. |
| 71 | + // Updaters should fetch the latest machine state using this reference. |
| 72 | + // +required |
| 73 | + MachineRef corev1.ObjectReference `json:"machineRef"` |
| 74 | +} |
| 75 | + |
| 76 | +var _ RetryResponseObject = &UpdateMachineResponse{} |
| 77 | + |
| 78 | +// UpdateMachineStatus represents the status of an in-place update operation. |
| 79 | +// +kubebuilder:validation:Enum=InProgress;Done;Failed |
| 80 | +type UpdateMachineStatus string |
| 81 | + |
| 82 | +const ( |
| 83 | + // UpdateMachineStatusInProgress indicates the update is still in progress. |
| 84 | + UpdateMachineStatusInProgress UpdateMachineStatus = "InProgress" |
| 85 | + // UpdateMachineStatusDone indicates the update has completed successfully. |
| 86 | + UpdateMachineStatusDone UpdateMachineStatus = "Done" |
| 87 | + // UpdateMachineStatusFailed indicates the update has failed. |
| 88 | + UpdateMachineStatusFailed UpdateMachineStatus = "Failed" |
| 89 | +) |
| 90 | + |
| 91 | +// UpdateMachineResponse is the response of the UpdateMachine hook. |
| 92 | +// +kubebuilder:object:root=true |
| 93 | +type UpdateMachineResponse struct { |
| 94 | + metav1.TypeMeta `json:",inline"` |
| 95 | + |
| 96 | + // CommonRetryResponse contains Status, Message and RetryAfterSeconds fields. |
| 97 | + CommonRetryResponse `json:",inline"` |
| 98 | + |
| 99 | + // updateStatus indicates the current status of the update operation. |
| 100 | + // +required |
| 101 | + UpdateStatus UpdateMachineStatus `json:"updateStatus"` |
| 102 | +} |
| 103 | + |
| 104 | +// UpdateMachine is the hook that will be called to perform in-place updates on a machine. |
| 105 | +// This hook should be idempotent and can be called multiple times for the same machine |
| 106 | +// until it reports Done or Failed status. |
| 107 | +func UpdateMachine(*UpdateMachineRequest, *UpdateMachineResponse) {} |
| 108 | + |
| 109 | +func init() { |
| 110 | + catalogBuilder.RegisterHook(CanUpdateMachine, &runtimecatalog.HookMeta{ |
| 111 | + Tags: []string{"In-Place Update Hooks"}, |
| 112 | + Summary: "Cluster API Runtime will call this hook to determine if an extension can handle specific machine changes", |
| 113 | + Description: "Called during update planning to determine if an extension can handle machine changes. " + |
| 114 | + "The extension should respond with the subset of changes it can handle for in-place updates.\n" + |
| 115 | + "\n" + |
| 116 | + "Notes:\n" + |
| 117 | + "- This hook is called during the planning phase of updates\n" + |
| 118 | + "- The request contains a list of required changes (field paths)\n" + |
| 119 | + "- Extensions should return only the changes they can confidently handle\n" + |
| 120 | + "- If no extension can cover all changes, CAPI will fallback to rolling updates\n", |
| 121 | + }) |
| 122 | + |
| 123 | + catalogBuilder.RegisterHook(UpdateMachine, &runtimecatalog.HookMeta{ |
| 124 | + Tags: []string{"In-Place Update Hooks"}, |
| 125 | + Summary: "Cluster API Runtime will call this hook to perform in-place updates on a machine", |
| 126 | + Description: "Cluster API Runtime will call this hook to perform the actual in-place update on a machine. " + |
| 127 | + "The hook will be called repeatedly until it reports Done or Failed status.\n" + |
| 128 | + "\n" + |
| 129 | + "Notes:\n" + |
| 130 | + "- This hook must be idempotent - it can be called multiple times for the same machine\n" + |
| 131 | + "- Extensions should fetch the latest machine state using the provided reference\n" + |
| 132 | + "- The hook should return InProgress status while the update is ongoing\n" + |
| 133 | + "- Use RetryAfterSeconds to control polling frequency\n" + |
| 134 | + "- The hook should perform updates based on the current machine spec\n", |
| 135 | + }) |
| 136 | +} |
0 commit comments