Skip to content

Commit 526da98

Browse files
Add in-place update hooks to API
Signed-off-by: Alexandr Demicev <alexandr.demicev@suse.com>
1 parent db916ae commit 526da98

File tree

3 files changed

+517
-0
lines changed

3 files changed

+517
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
22+
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
23+
)
24+
25+
// CanUpdateMachineRequest is the request of the CanUpdateMachine hook.
26+
// This hook is called to determine if an extension can handle specific changes.
27+
// +kubebuilder:object:root=true
28+
type CanUpdateMachineRequest struct {
29+
metav1.TypeMeta `json:",inline"`
30+
31+
// CommonRequest contains fields common to all request types.
32+
CommonRequest `json:",inline"`
33+
34+
// changes is a list of field paths that need to be updated on the machine.
35+
// Examples: ["spec.version", "spec.infrastructureRef.spec.memoryMiB"]
36+
// +required
37+
Changes []string `json:"changes"`
38+
}
39+
40+
var _ ResponseObject = &CanUpdateMachineResponse{}
41+
42+
// CanUpdateMachineResponse is the response of the CanUpdateMachine hook.
43+
// +kubebuilder:object:root=true
44+
type CanUpdateMachineResponse struct {
45+
metav1.TypeMeta `json:",inline"`
46+
47+
// CommonResponse contains Status and Message fields common to all response types.
48+
CommonResponse `json:",inline"`
49+
50+
// acceptedChanges is the subset of requested changes that this extension can handle.
51+
// If empty, the extension cannot handle any of the requested changes.
52+
// +optional
53+
AcceptedChanges []string `json:"acceptedChanges,omitempty"`
54+
}
55+
56+
// CanUpdateMachine is the hook that will be called to determine if an extension
57+
// can handle specific machine changes for in-place updates.
58+
func CanUpdateMachine(*CanUpdateMachineRequest, *CanUpdateMachineResponse) {}
59+
60+
// UpdateMachineRequest is the request of the UpdateMachine hook.
61+
// This hook is called to perform the actual in-place update on a machine.
62+
// +kubebuilder:object:root=true
63+
type UpdateMachineRequest struct {
64+
metav1.TypeMeta `json:",inline"`
65+
66+
// CommonRequest contains fields common to all request types.
67+
CommonRequest `json:",inline"`
68+
69+
// machineRef is a reference to the machine object the in-place update hook corresponds to.
70+
// Updaters should fetch the latest machine state using this reference.
71+
// +required
72+
MachineRef ObjectReference `json:"machineRef"`
73+
}
74+
75+
var _ RetryResponseObject = &UpdateMachineResponse{}
76+
77+
// UpdateMachineResponse is the response of the UpdateMachine hook.
78+
// The status of the update operation is determined by the CommonRetryResponse fields:
79+
// - Status=Success + RetryAfterSeconds > 0: update is in progress
80+
// - Status=Success + RetryAfterSeconds = 0: update completed successfully
81+
// - Status=Failure: update failed
82+
// +kubebuilder:object:root=true
83+
type UpdateMachineResponse struct {
84+
metav1.TypeMeta `json:",inline"`
85+
86+
// CommonRetryResponse contains Status, Message and RetryAfterSeconds fields.
87+
CommonRetryResponse `json:",inline"`
88+
}
89+
90+
// ObjectReference represents a reference to a Kubernetes object.
91+
type ObjectReference struct {
92+
// name is the name of the referenced object.
93+
// +required
94+
Name string `json:"name"`
95+
96+
// namespace is the namespace of the referenced object.
97+
// +required
98+
Namespace string `json:"namespace"`
99+
}
100+
101+
// UpdateMachine is the hook that will be called to perform in-place updates on a machine.
102+
// This hook should be idempotent and can be called multiple times for the same machine
103+
// until it reports Done or Failed status.
104+
func UpdateMachine(*UpdateMachineRequest, *UpdateMachineResponse) {}
105+
106+
func init() {
107+
catalogBuilder.RegisterHook(CanUpdateMachine, &runtimecatalog.HookMeta{
108+
Tags: []string{"In-Place Update Hooks"},
109+
Summary: "Cluster API Runtime will call this hook to determine if an extension can handle specific machine changes",
110+
Description: "Called during update planning to determine if an extension can handle machine changes. " +
111+
"The extension should respond with the subset of changes it can handle for in-place updates.\n" +
112+
"\n" +
113+
"Notes:\n" +
114+
"- This hook is called during the planning phase of updates\n" +
115+
"- The request contains a list of required changes (field paths)\n" +
116+
"- Extensions should return only the changes they can confidently handle\n" +
117+
"- If no extension can cover all changes, CAPI will fallback to rolling updates\n",
118+
})
119+
120+
catalogBuilder.RegisterHook(UpdateMachine, &runtimecatalog.HookMeta{
121+
Tags: []string{"In-Place Update Hooks"},
122+
Summary: "Cluster API Runtime will call this hook to perform in-place updates on a machine",
123+
Description: "Cluster API Runtime will call this hook to perform the actual in-place update on a machine. " +
124+
"The hook will be called repeatedly until it reports Done or Failed status.\n" +
125+
"\n" +
126+
"Notes:\n" +
127+
"- This hook must be idempotent - it can be called multiple times for the same machine\n" +
128+
"- Extensions should fetch the latest machine state using the provided reference\n" +
129+
"- The hook should return InProgress status while the update is ongoing\n" +
130+
"- Use RetryAfterSeconds to control polling frequency\n" +
131+
"- The hook should perform updates based on the current machine spec\n",
132+
})
133+
}

api/runtime/hooks/v1alpha1/zz_generated.deepcopy.go

Lines changed: 126 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)