@@ -32,24 +32,24 @@ import (
3232 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3333 "sigs.k8s.io/cluster-api/controllers/external"
3434 "sigs.k8s.io/cluster-api/util"
35+ "sigs.k8s.io/cluster-api/util/collections"
3536 "sigs.k8s.io/cluster-api/util/conditions"
3637 "sigs.k8s.io/cluster-api/util/patch"
3738 ctrl "sigs.k8s.io/controller-runtime"
3839
3940 bootstrapv1 "github.com/k3s-io/cluster-api-k3s/bootstrap/api/v1beta2"
4041 controlplanev1 "github.com/k3s-io/cluster-api-k3s/controlplane/api/v1beta2"
4142 k3s "github.com/k3s-io/cluster-api-k3s/pkg/k3s"
42- "github.com/k3s-io/cluster-api-k3s/pkg/machinefilters"
4343)
4444
4545var ErrPreConditionFailed = errors .New ("precondition check failed" )
4646
4747func (r * KThreesControlPlaneReconciler ) initializeControlPlane (ctx context.Context , cluster * clusterv1.Cluster , kcp * controlplanev1.KThreesControlPlane , controlPlane * k3s.ControlPlane ) (ctrl.Result , error ) {
48- logger := controlPlane . Logger ( )
48+ logger := ctrl . LoggerFrom ( ctx )
4949
5050 // Perform an uncached read of all the owned machines. This check is in place to make sure
5151 // that the controller cache is not misbehaving and we end up initializing the cluster more than once.
52- ownedMachines , err := r .managementClusterUncached .GetMachinesForCluster (ctx , util .ObjectKey (cluster ), machinefilters .OwnedMachines (kcp ))
52+ ownedMachines , err := r .managementClusterUncached .GetMachinesForCluster (ctx , util .ObjectKey (cluster ), collections .OwnedMachines (kcp ))
5353 if err != nil {
5454 logger .Error (err , "failed to perform an uncached read of control plane machines for cluster" )
5555 return ctrl.Result {}, err
@@ -62,7 +62,7 @@ func (r *KThreesControlPlaneReconciler) initializeControlPlane(ctx context.Conte
6262 }
6363
6464 bootstrapSpec := controlPlane .InitialControlPlaneConfig ()
65- fd := controlPlane .NextFailureDomainForScaleUp ()
65+ fd := controlPlane .NextFailureDomainForScaleUp (ctx )
6666 if err := r .cloneConfigsAndGenerateMachine (ctx , cluster , kcp , bootstrapSpec , fd ); err != nil {
6767 logger .Error (err , "Failed to create initial control plane Machine" )
6868 r .recorder .Eventf (kcp , corev1 .EventTypeWarning , "FailedInitialization" , "Failed to create initial control plane Machine for cluster %s/%s control plane: %v" , cluster .Namespace , cluster .Name , err )
@@ -74,7 +74,7 @@ func (r *KThreesControlPlaneReconciler) initializeControlPlane(ctx context.Conte
7474}
7575
7676func (r * KThreesControlPlaneReconciler ) scaleUpControlPlane (ctx context.Context , cluster * clusterv1.Cluster , kcp * controlplanev1.KThreesControlPlane , controlPlane * k3s.ControlPlane ) (ctrl.Result , error ) {
77- logger := controlPlane . Logger ( )
77+ logger := ctrl . LoggerFrom ( ctx )
7878
7979 // Run preflight checks to ensure that the control plane is stable before proceeding with a scale up/scale down operation; if not, wait.
8080 if result , err := r .preflightChecks (ctx , controlPlane ); err != nil || ! result .IsZero () {
@@ -83,7 +83,7 @@ func (r *KThreesControlPlaneReconciler) scaleUpControlPlane(ctx context.Context,
8383
8484 // Create the bootstrap configuration
8585 bootstrapSpec := controlPlane .JoinControlPlaneConfig ()
86- fd := controlPlane .NextFailureDomainForScaleUp ()
86+ fd := controlPlane .NextFailureDomainForScaleUp (ctx )
8787 if err := r .cloneConfigsAndGenerateMachine (ctx , cluster , kcp , bootstrapSpec , fd ); err != nil {
8888 logger .Error (err , "Failed to create additional control plane Machine" )
8989 r .recorder .Eventf (kcp , corev1 .EventTypeWarning , "FailedScaleUp" , "Failed to create additional control plane Machine for cluster %s/%s control plane: %v" , cluster .Namespace , cluster .Name , err )
@@ -99,12 +99,12 @@ func (r *KThreesControlPlaneReconciler) scaleDownControlPlane(
9999 cluster * clusterv1.Cluster ,
100100 kcp * controlplanev1.KThreesControlPlane ,
101101 controlPlane * k3s.ControlPlane ,
102- outdatedMachines k3s. FilterableMachineCollection ,
102+ outdatedMachines collections. Machines ,
103103) (ctrl.Result , error ) {
104- logger := controlPlane . Logger ( )
104+ logger := ctrl . LoggerFrom ( ctx )
105105
106106 // Pick the Machine that we should scale down.
107- machineToDelete , err := selectMachineForScaleDown (controlPlane , outdatedMachines )
107+ machineToDelete , err := selectMachineForScaleDown (ctx , controlPlane , outdatedMachines )
108108 if err != nil {
109109 return ctrl.Result {}, fmt .Errorf ("failed to select machine for scale down: %w" , err )
110110 }
@@ -179,7 +179,7 @@ func (r *KThreesControlPlaneReconciler) preflightChecks(_ context.Context, contr
179179
180180 // If there are deleting machines, wait for the operation to complete.
181181 if controlPlane .HasDeletingMachine () {
182- logger .Info ("Waiting for machines to be deleted" , "Machines" , strings .Join (controlPlane .Machines .Filter (machinefilters .HasDeletionTimestamp ).Names (), ", " ))
182+ logger .Info ("Waiting for machines to be deleted" , "Machines" , strings .Join (controlPlane .Machines .Filter (collections .HasDeletionTimestamp ).Names (), ", " ))
183183 return ctrl.Result {RequeueAfter : deleteRequeueAfter }, nil
184184 }
185185
@@ -237,7 +237,7 @@ func preflightCheckCondition(kind string, obj conditions.Getter, condition clust
237237 return nil
238238}
239239
240- func selectMachineForScaleDown (controlPlane * k3s.ControlPlane , outdatedMachines k3s. FilterableMachineCollection ) (* clusterv1.Machine , error ) {
240+ func selectMachineForScaleDown (ctx context. Context , controlPlane * k3s.ControlPlane , outdatedMachines collections. Machines ) (* clusterv1.Machine , error ) {
241241 machines := controlPlane .Machines
242242 switch {
243243 case controlPlane .MachineWithDeleteAnnotation (outdatedMachines ).Len () > 0 :
@@ -247,7 +247,7 @@ func selectMachineForScaleDown(controlPlane *k3s.ControlPlane, outdatedMachines
247247 case outdatedMachines .Len () > 0 :
248248 machines = outdatedMachines
249249 }
250- return controlPlane .MachineInFailureDomainWithMostMachines (machines )
250+ return controlPlane .MachineInFailureDomainWithMostMachines (ctx , machines )
251251}
252252
253253func (r * KThreesControlPlaneReconciler ) cloneConfigsAndGenerateMachine (ctx context.Context , cluster * clusterv1.Cluster , kcp * controlplanev1.KThreesControlPlane , bootstrapSpec * bootstrapv1.KThreesConfigSpec , failureDomain * string ) error {
0 commit comments