Skip to content

✨ Add in-place update hooks to API #12343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions api/runtime/hooks/v1alpha1/inplaceupdate_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
Copyright 2025 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
)

// CanUpdateMachineRequest is the request of the CanUpdateMachine hook.
// This hook is called to determine if an extension can handle specific changes.
// +kubebuilder:object:root=true
type CanUpdateMachineRequest struct {
metav1.TypeMeta `json:",inline"`

// CommonRequest contains fields common to all request types.
CommonRequest `json:",inline"`

// changes is a list of field paths that need to be updated on the machine.
// Examples: ["spec.version", "spec.infrastructureRef.spec.memoryMiB"]
// +required
Changes []string `json:"changes"`
}

var _ ResponseObject = &CanUpdateMachineResponse{}

// CanUpdateMachineResponse is the response of the CanUpdateMachine hook.
// +kubebuilder:object:root=true
type CanUpdateMachineResponse struct {
metav1.TypeMeta `json:",inline"`

// CommonResponse contains Status and Message fields common to all response types.
CommonResponse `json:",inline"`

// acceptedChanges is the subset of requested changes that this extension can handle.
// If empty, the extension cannot handle any of the requested changes.
// +optional
AcceptedChanges []string `json:"acceptedChanges,omitempty"`
}

// CanUpdateMachine is the hook that will be called to determine if an extension
// can handle specific machine changes for in-place updates.
func CanUpdateMachine(*CanUpdateMachineRequest, *CanUpdateMachineResponse) {}

// UpdateMachineRequest is the request of the UpdateMachine hook.
// This hook is called to perform the actual in-place update on a machine.
// +kubebuilder:object:root=true
type UpdateMachineRequest struct {
metav1.TypeMeta `json:",inline"`

// CommonRequest contains fields common to all request types.
CommonRequest `json:",inline"`

// machineRef is a reference to the machine object the in-place update hook corresponds to.
// Updaters should fetch the latest machine state using this reference.
// +required
MachineRef corev1.ObjectReference `json:"machineRef"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's please not use corev1.ObjectReference, because it has many fields we do not care about.

I would suggest to create an new type ObjectReference with only the fields we care about

}

var _ RetryResponseObject = &UpdateMachineResponse{}

// UpdateMachineStatus represents the status of an in-place update operation.
// +kubebuilder:validation:Enum=InProgress;Done;Failed
type UpdateMachineStatus string

const (
// UpdateMachineStatusInProgress indicates the update is still in progress.
UpdateMachineStatusInProgress UpdateMachineStatus = "InProgress"
// UpdateMachineStatusDone indicates the update has completed successfully.
UpdateMachineStatusDone UpdateMachineStatus = "Done"
// UpdateMachineStatusFailed indicates the update has failed.
UpdateMachineStatusFailed UpdateMachineStatus = "Failed"
)

// UpdateMachineResponse is the response of the UpdateMachine hook.
// +kubebuilder:object:root=true
type UpdateMachineResponse struct {
metav1.TypeMeta `json:",inline"`

// CommonRetryResponse contains Status, Message and RetryAfterSeconds fields.
CommonRetryResponse `json:",inline"`

// updateStatus indicates the current status of the update operation.
// +required
UpdateStatus UpdateMachineStatus `json:"updateStatus"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: do we need this or can we infer the same info from CommonRetryResponse:

  • status=Success + retryAfterSeconds > 0 -> in progress
  • status=Success + retryAfterSeconds = 0 -> completed
  • status=Failure -> failed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, I will add a change to the doc comment to make it clear

}

// UpdateMachine is the hook that will be called to perform in-place updates on a machine.
// This hook should be idempotent and can be called multiple times for the same machine
// until it reports Done or Failed status.
func UpdateMachine(*UpdateMachineRequest, *UpdateMachineResponse) {}

func init() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably need to start working to a book page with this documentation, but considering there are still details to be figured out, I'm also ok to defer

catalogBuilder.RegisterHook(CanUpdateMachine, &runtimecatalog.HookMeta{
Tags: []string{"In-Place Update Hooks"},
Summary: "Cluster API Runtime will call this hook to determine if an extension can handle specific machine changes",
Description: "Called during update planning to determine if an extension can handle machine changes. " +
"The extension should respond with the subset of changes it can handle for in-place updates.\n" +
"\n" +
"Notes:\n" +
"- This hook is called during the planning phase of updates\n" +
"- The request contains a list of required changes (field paths)\n" +
"- Extensions should return only the changes they can confidently handle\n" +
"- If no extension can cover all changes, CAPI will fallback to rolling updates\n",
})

catalogBuilder.RegisterHook(UpdateMachine, &runtimecatalog.HookMeta{
Tags: []string{"In-Place Update Hooks"},
Summary: "Cluster API Runtime will call this hook to perform in-place updates on a machine",
Description: "Cluster API Runtime will call this hook to perform the actual in-place update on a machine. " +
"The hook will be called repeatedly until it reports Done or Failed status.\n" +
"\n" +
"Notes:\n" +
"- This hook must be idempotent - it can be called multiple times for the same machine\n" +
"- Extensions should fetch the latest machine state using the provided reference\n" +
"- The hook should return InProgress status while the update is ongoing\n" +
"- Use RetryAfterSeconds to control polling frequency\n" +
"- The hook should perform updates based on the current machine spec\n",
})
}
111 changes: 111 additions & 0 deletions api/runtime/hooks/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading