@@ -19,7 +19,6 @@ package controllers
1919import (
2020 "context"
2121 "fmt"
22- "sort"
2322 "strings"
2423 "time"
2524
@@ -67,7 +66,7 @@ const (
6766)
6867
6968type managementCluster interface {
70- GetMachinesForCluster (ctx context.Context , cluster types.NamespacedName , filters ... func ( machine * clusterv1. Machine ) bool ) ([] * clusterv1. Machine , error )
69+ GetMachinesForCluster (ctx context.Context , cluster types.NamespacedName , filters ... internal. MachineFilter ) (internal. FilterableMachineCollection , error )
7170 TargetClusterControlPlaneIsHealthy (ctx context.Context , clusterKey types.NamespacedName , controlPlaneName string ) error
7271 TargetClusterEtcdIsHealthy (ctx context.Context , clusterKey types.NamespacedName , controlPlaneName string ) error
7372}
@@ -235,8 +234,7 @@ func (r *KubeadmControlPlaneReconciler) reconcile(ctx context.Context, cluster *
235234 }
236235
237236 currentConfigurationHash := hash .Compute (& kcp .Spec )
238- requireUpgrade := internal .FilterMachines (
239- ownedMachines ,
237+ requireUpgrade := ownedMachines .AnyFilter (
240238 internal .Not (internal .MatchesConfigurationHash (currentConfigurationHash )),
241239 internal .OlderThan (kcp .Spec .UpgradeAfter ),
242240 )
@@ -254,7 +252,7 @@ func (r *KubeadmControlPlaneReconciler) reconcile(ctx context.Context, cluster *
254252 }
255253
256254 // If we've made it this far, we don't need to worry about Machines that are older than kcp.Spec.UpgradeAfter
257- currentMachines := internal . FilterMachines ( ownedMachines , internal .MatchesConfigurationHash (currentConfigurationHash ))
255+ currentMachines := ownedMachines . Filter ( internal .MatchesConfigurationHash (currentConfigurationHash ))
258256 numMachines := len (currentMachines )
259257 desiredReplicas := int (* kcp .Spec .Replicas )
260258
@@ -312,7 +310,7 @@ func (r *KubeadmControlPlaneReconciler) updateStatus(ctx context.Context, kcp *c
312310 return errors .Wrap (err , "failed to get list of owned machines" )
313311 }
314312
315- currentMachines := internal . FilterMachines ( ownedMachines , internal .MatchesConfigurationHash (hash .Compute (& kcp .Spec )))
313+ currentMachines := ownedMachines . Filter ( internal .MatchesConfigurationHash (hash .Compute (& kcp .Spec )))
316314 kcp .Status .UpdatedReplicas = int32 (len (currentMachines ))
317315
318316 replicas := int32 (len (ownedMachines ))
@@ -347,7 +345,7 @@ func (r *KubeadmControlPlaneReconciler) updateStatus(ctx context.Context, kcp *c
347345 return nil
348346}
349347
350- func (r * KubeadmControlPlaneReconciler ) upgradeControlPlane (ctx context.Context , cluster * clusterv1.Cluster , kcp * controlplanev1.KubeadmControlPlane , requireUpgrade [] * clusterv1. Machine ) error {
348+ func (r * KubeadmControlPlaneReconciler ) upgradeControlPlane (ctx context.Context , cluster * clusterv1.Cluster , kcp * controlplanev1.KubeadmControlPlane , requireUpgrade internal. FilterableMachineCollection ) error {
351349
352350 // TODO: verify health for each existing replica
353351 // TODO: mark an old Machine via the label kubeadm.controlplane.cluster.x-k8s.io/selected-for-upgrade
@@ -412,13 +410,13 @@ func (r *KubeadmControlPlaneReconciler) scaleDownControlPlane(ctx context.Contex
412410 }
413411
414412 // Wait for any delete in progress to complete before deleting another Machine
415- if len (internal . FilterMachines ( ownedMachines , internal .HasDeletionTimestamp )) > 0 {
413+ if len (ownedMachines . Filter ( internal .HasDeletionTimestamp )) > 0 {
416414 return ctrl.Result {RequeueAfter : DeleteRequeueAfter }, nil
417415 }
418416
419- machineToDelete , err := oldestMachine ( ownedMachines )
420- if err ! = nil {
421- return ctrl.Result {}, errors .Wrap ( err , "failed to pick control plane Machine to delete" )
417+ machineToDelete := ownedMachines . Oldest ( )
418+ if machineToDelete = = nil {
419+ return ctrl.Result {}, errors .New ( "failed to pick control plane Machine to delete" )
422420 }
423421
424422 if err := r .Client .Delete (ctx , machineToDelete ); err != nil && ! apierrors .IsNotFound (err ) {
@@ -719,11 +717,3 @@ func getMachineNode(ctx context.Context, crClient client.Client, machine *cluste
719717
720718 return node , nil
721719}
722-
723- func oldestMachine (machines []* clusterv1.Machine ) (* clusterv1.Machine , error ) {
724- if len (machines ) == 0 {
725- return & clusterv1.Machine {}, errors .New ("no machines given" )
726- }
727- sort .Sort (util .MachinesByCreationTimestamp (machines ))
728- return machines [0 ], nil
729- }
0 commit comments