-
Notifications
You must be signed in to change notification settings - Fork 1.4k
✨ Implement GenerateUpgradePlan handler #12927
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
Merged
k8s-ci-robot
merged 1 commit into
kubernetes-sigs:main
from
sivchari:implement-generate-upgrade-plan-handler
Nov 4, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| 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 upgradeplan contains the handlers for the upgrade plan hook. | ||
| // | ||
| // The implementation of the handlers is specifically designed for Cluster API E2E tests use cases. | ||
| // When implementing custom RuntimeExtension, it is only required to expose HandlerFunc with the | ||
| // signature defined in sigs.k8s.io/cluster-api/api/runtime/hooks/v1alpha1. | ||
| package upgradeplan | ||
|
|
||
| import ( | ||
| "context" | ||
| "slices" | ||
|
|
||
| "k8s.io/klog/v2" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| runtimehooksv1 "sigs.k8s.io/cluster-api/api/runtime/hooks/v1alpha1" | ||
| "sigs.k8s.io/cluster-api/exp/topology/desiredstate" | ||
| "sigs.k8s.io/cluster-api/test/infrastructure/kind" | ||
| ) | ||
|
|
||
| // ExtensionHandlers provides a common struct for the upgrade plan hook handler. | ||
| // NOTE: it is not mandatory to use a ExtensionHandlers in custom RuntimeExtension, what is important | ||
| // is to expose HandlerFunc with the signature defined in sigs.k8s.io/cluster-api/api/runtime/hooks/v1alpha1. | ||
| type ExtensionHandlers struct { | ||
| client client.Client | ||
| } | ||
|
|
||
| // NewExtensionHandlers returns a ExtensionHandlers for the upgrade plan hook handler. | ||
| func NewExtensionHandlers(client client.Client) *ExtensionHandlers { | ||
| return &ExtensionHandlers{ | ||
| client: client, | ||
| } | ||
| } | ||
|
|
||
| // DoGenerateUpgradePlan implements the HandlerFunc for the GenerateUpgradePlan hook. | ||
| // This hook generates an upgrade plan based on the Kubernetes versions available in the kind mapper, thus allowing E2E tests to control the behavior during tests. | ||
| // NOTE: custom RuntimeExtension, must implement the body of this func according to the specific use case. | ||
| func (m *ExtensionHandlers) DoGenerateUpgradePlan(ctx context.Context, request *runtimehooksv1.GenerateUpgradePlanRequest, response *runtimehooksv1.GenerateUpgradePlanResponse) { | ||
| log := ctrl.LoggerFrom(ctx).WithValues("Cluster", klog.KObj(&request.Cluster)) | ||
| ctx = ctrl.LoggerInto(ctx, log) | ||
| log.Info("GenerateUpgradePlan is called") | ||
|
|
||
| // Get the list of Kubernetes versions from the kind mapper. | ||
| versions := kind.GetKubernetesVersions() | ||
| if !slices.Contains(versions, request.ToKubernetesVersion) { | ||
| versions = append(versions, request.ToKubernetesVersion) | ||
| } | ||
|
|
||
| // Use GetUpgradePlanFromClusterClassVersions to generate the upgrade plan. | ||
| getUpgradePlan := desiredstate.GetUpgradePlanFromClusterClassVersions(versions) | ||
|
|
||
| // Call the upgrade plan function. | ||
| controlPlaneUpgrades, workersUpgrades, err := getUpgradePlan(ctx, request.ToKubernetesVersion, request.FromControlPlaneKubernetesVersion, request.FromWorkersKubernetesVersion) | ||
| if err != nil { | ||
| response.Status = runtimehooksv1.ResponseStatusFailure | ||
| response.Message = err.Error() | ||
| return | ||
| } | ||
|
|
||
| // Convert the upgrade plans to UpgradeStep format. | ||
| response.ControlPlaneUpgrades = make([]runtimehooksv1.UpgradeStep, len(controlPlaneUpgrades)) | ||
| for i, version := range controlPlaneUpgrades { | ||
| response.ControlPlaneUpgrades[i] = runtimehooksv1.UpgradeStep{ | ||
| Version: version, | ||
| } | ||
| } | ||
|
|
||
| response.WorkersUpgrades = make([]runtimehooksv1.UpgradeStep, len(workersUpgrades)) | ||
| for i, version := range workersUpgrades { | ||
| response.WorkersUpgrades[i] = runtimehooksv1.UpgradeStep{ | ||
| Version: version, | ||
| } | ||
| } | ||
|
|
||
| response.Status = runtimehooksv1.ResponseStatusSuccess | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.