@@ -46,6 +46,7 @@ import (
4646 controlplanev1 "github.com/rancher/cluster-api-provider-rke2/controlplane/api/v1beta2"
4747 "github.com/rancher/cluster-api-provider-rke2/pkg/capi/hooks"
4848 "github.com/rancher/cluster-api-provider-rke2/pkg/capi/inplace"
49+ "github.com/rancher/cluster-api-provider-rke2/pkg/rke2/desiredstate"
4950)
5051
5152// UpToDateResult is the result of calling the UpToDate func for a Machine.
@@ -141,7 +142,7 @@ func NewControlPlane(
141142 machinesUpToDateResults := map [string ]UpToDateResult {}
142143
143144 for _ , m := range ownedMachines {
144- upToDate , result , err := UpToDate (ctx , m , rcp , infraObjects , rke2Configs )
145+ upToDate , result , err := UpToDate (ctx , client , cluster , m , rcp , infraObjects , rke2Configs )
145146 if err != nil {
146147 return nil , err
147148 }
@@ -628,8 +629,14 @@ func (c *ControlPlane) UsesEmbeddedEtcd() bool {
628629
629630// UpToDate checks if a Machine is up to date with the control plane's configuration.
630631// If not, messages explaining why are provided with different level of detail for logs and conditions.
631- func UpToDate (ctx context.Context , machine * clusterv1.Machine , rcp * controlplanev1.RKE2ControlPlane ,
632- infraConfigs map [string ]* unstructured.Unstructured , machineConfigs map [string ]* bootstrapv1.RKE2Config ,
632+ func UpToDate (
633+ ctx context.Context ,
634+ c client.Client ,
635+ cluster * clusterv1.Cluster ,
636+ machine * clusterv1.Machine ,
637+ rcp * controlplanev1.RKE2ControlPlane ,
638+ infraConfigs map [string ]* unstructured.Unstructured ,
639+ machineConfigs map [string ]* bootstrapv1.RKE2Config ,
633640) (bool , * UpToDateResult , error ) {
634641 res := & UpToDateResult {
635642 EligibleForInPlaceUpdate : true ,
@@ -655,7 +662,11 @@ func UpToDate(ctx context.Context, machine *clusterv1.Machine, rcp *controlplane
655662 }
656663
657664 // Machines that do not match with rcp config.
658- matches , specLogMessages , specConditionMessages := matchesMachineSpec (ctx , infraConfigs , machineConfigs , rcp , machine )
665+ matches , specLogMessages , specConditionMessages , err := matchesMachineSpec (ctx , c , cluster , infraConfigs , machineConfigs , rcp , machine , res )
666+ if err != nil {
667+ return false , nil , err
668+ }
669+
659670 if ! matches {
660671 res .LogMessages = append (res .LogMessages , specLogMessages ... )
661672 res .ConditionMessages = append (res .ConditionMessages , specConditionMessages ... )
@@ -681,14 +692,55 @@ func UpToDate(ctx context.Context, machine *clusterv1.Machine, rcp *controlplane
681692// - are not relevant for the rollout decision (ex: failureDomain).
682693func matchesMachineSpec (
683694 ctx context.Context ,
695+ c client.Client ,
696+ cluster * clusterv1.Cluster ,
684697 infraConfigs map [string ]* unstructured.Unstructured ,
685698 machineConfigs map [string ]* bootstrapv1.RKE2Config ,
686699 rcp * controlplanev1.RKE2ControlPlane ,
687700 machine * clusterv1.Machine ,
688- ) (bool , []string , []string ) {
701+ res * UpToDateResult ,
702+ ) (bool , []string , []string , error ) {
689703 logMessages := []string {}
690704 conditionMessages := []string {}
691705
706+ if cluster != nil {
707+ desiredMachine , err := desiredstate .ComputeDesiredMachine (
708+ rcp , cluster ,
709+ machine .Spec .InfrastructureRef , machine .Spec .Bootstrap .ConfigRef ,
710+ machine .Spec .FailureDomain , machine ,
711+ )
712+ if err != nil {
713+ return false , nil , nil , fmt .Errorf ("failed to compute desired Machine for %s: %w" , machine .Name , err )
714+ }
715+
716+ // Note: spec.version is not mutated in-place by syncMachines and accordingly
717+ // not updated by desiredstate.ComputeDesiredMachine, so we have to update it here.
718+ // Note: spec.failureDomain is in general only changed on delete/create, so we don't have to update it here for in-place.
719+ desiredMachine .Spec .Version = rcp .Spec .Version
720+ res .DesiredMachine = desiredMachine
721+ // Note: Intentionally not storing currentMachine as it can change later, e.g. through syncMachines.
722+
723+ if res .CurrentRKE2Config != nil {
724+ desiredRKE2Config , err := desiredstate .ComputeDesiredRKE2Config (rcp , cluster , res .CurrentRKE2Config .Name , res .CurrentRKE2Config )
725+ if err != nil {
726+ return false , nil , nil , fmt .Errorf ("failed to compute desired RKE2Config for %s: %w" , machine .Name , err )
727+ }
728+
729+ res .DesiredRKE2Config = desiredRKE2Config
730+ }
731+
732+ if res .CurrentInfraMachine != nil {
733+ desiredInfraMachine , err := desiredstate .ComputeDesiredInfraMachine (
734+ ctx , c , rcp , cluster , res .CurrentInfraMachine .GetName (), res .CurrentInfraMachine ,
735+ )
736+ if err != nil {
737+ return false , nil , nil , fmt .Errorf ("failed to compute desired InfraMachine for %s: %w" , machine .Name , err )
738+ }
739+
740+ res .DesiredInfraMachine = desiredInfraMachine
741+ }
742+ }
743+
692744 if ! collections .MatchesKubernetesVersion (rcp .Spec .Version )(machine ) {
693745 machineVersion := ""
694746 if machine != nil && machine .Spec .Version != "" {
@@ -711,8 +763,8 @@ func matchesMachineSpec(
711763 }
712764
713765 if len (logMessages ) > 0 || len (conditionMessages ) > 0 {
714- return false , logMessages , conditionMessages
766+ return false , logMessages , conditionMessages , nil
715767 }
716768
717- return true , nil , nil
769+ return true , nil , nil , nil
718770}
0 commit comments