|
| 1 | +/* |
| 2 | +Copyright 2025 SUSE. |
| 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 controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "k8s.io/klog/v2" |
| 24 | + ctrl "sigs.k8s.io/controller-runtime" |
| 25 | + |
| 26 | + clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" |
| 27 | + |
| 28 | + "github.com/rancher/cluster-api-provider-rke2/pkg/rke2" |
| 29 | +) |
| 30 | + |
| 31 | +func (r *RKE2ControlPlaneReconciler) tryInPlaceUpdate( |
| 32 | + ctx context.Context, |
| 33 | + controlPlane *rke2.ControlPlane, |
| 34 | + machineToInPlaceUpdate *clusterv1.Machine, |
| 35 | + machineUpToDateResult rke2.UpToDateResult, |
| 36 | +) (fallbackToScaleDown bool, _ ctrl.Result, _ error) { |
| 37 | + // 1. Preflight checks for all machines. |
| 38 | + if resultForAllMachines := r.preflightChecks(ctx, controlPlane); !resultForAllMachines.IsZero() { |
| 39 | + if result := r.preflightChecks(ctx, controlPlane, machineToInPlaceUpdate); result.IsZero() { |
| 40 | + return true, ctrl.Result{}, nil |
| 41 | + } |
| 42 | + |
| 43 | + return false, resultForAllMachines, nil |
| 44 | + } |
| 45 | + |
| 46 | + // 2. CanUpdateMachine check. |
| 47 | + canUpdate, err := r.canUpdateMachine(ctx, machineToInPlaceUpdate, machineUpToDateResult) |
| 48 | + if err != nil { |
| 49 | + return false, ctrl.Result{}, fmt.Errorf("failed to determine if Machine %s can be updated in-place: %w", klog.KObj(machineToInPlaceUpdate), err) |
| 50 | + } |
| 51 | + |
| 52 | + if !canUpdate { |
| 53 | + return true, ctrl.Result{}, nil |
| 54 | + } |
| 55 | + |
| 56 | + return false, ctrl.Result{}, r.triggerInPlaceUpdate(ctx, machineToInPlaceUpdate, machineUpToDateResult) |
| 57 | +} |
0 commit comments