@@ -58,9 +58,10 @@ import (
5858 "sigs.k8s.io/controller-runtime/pkg/client"
5959 "sigs.k8s.io/controller-runtime/pkg/handler"
6060 "sigs.k8s.io/controller-runtime/pkg/log"
61- "sigs.k8s.io/controller-runtime/pkg/source "
61+ "sigs.k8s.io/controller-runtime/pkg/reconcile "
6262
6363 tokenpkg "github.com/canonical/cluster-api-bootstrap-provider-microk8s/pkg/token"
64+ "sigs.k8s.io/controller-runtime/pkg/builder"
6465)
6566
6667type InitLocker interface {
@@ -611,7 +612,7 @@ func (r *MicroK8sConfigReconciler) storeBootstrapData(ctx context.Context, scope
611612 Name : scope .Config .Name ,
612613 Namespace : scope .Config .Namespace ,
613614 Labels : map [string ]string {
614- clusterv1 .ClusterLabelName : scope .Cluster .Name ,
615+ clusterv1 .ClusterNameLabel : scope .Cluster .Name ,
615616 },
616617 OwnerReferences : []metav1.OwnerReference {
617618 {
@@ -792,38 +793,37 @@ func (r *MicroK8sConfigReconciler) SetupWithManager(ctx context.Context, mgr ctr
792793
793794 b := ctrl .NewControllerManagedBy (mgr ).
794795 For (& bootstrapclusterxk8siov1beta1.MicroK8sConfig {}).
795- Watches (& source.Kind {Type : & clusterv1.Machine {}},
796- handler .EnqueueRequestsFromMapFunc (r .MachineToBootstrapMapFunc )).
797- WithEventFilter (predicates .ResourceNotPausedAndHasFilterLabel (ctrl .LoggerFrom (ctx ),
798- r .WatchFilterValue ))
796+ Watches (
797+ & clusterv1.Machine {},
798+ handler .EnqueueRequestsFromMapFunc (r .MachineToBootstrapMapFunc ),
799+ ).
800+ WithEventFilter (predicates .ResourceNotPausedAndHasFilterLabel (mgr .GetScheme (), mgr .GetLogger (), r .WatchFilterValue ))
799801
800802 if feature .Gates .Enabled (feature .MachinePool ) {
801803 b = b .Watches (
802- & source. Kind { Type : & expv1.MachinePool {} },
804+ & expv1.MachinePool {},
803805 handler .EnqueueRequestsFromMapFunc (r .MachineToBootstrapMapFunc ),
804- ).WithEventFilter (predicates .ResourceNotPausedAndHasFilterLabel (ctrl . LoggerFrom ( ctx ), r .WatchFilterValue ))
806+ ).WithEventFilter (predicates .ResourceNotPausedAndHasFilterLabel (mgr . GetScheme (), mgr . GetLogger ( ), r .WatchFilterValue ))
805807 }
806808
807- c , err := b .Build (r )
808- if err != nil {
809- return errors .Wrap (err , "failed setting up with a controller manager" )
810- }
811- err = c .Watch (
812- & source.Kind {Type : & clusterv1.Cluster {}},
809+ b = b .Watches (
810+ & clusterv1.Cluster {},
813811 handler .EnqueueRequestsFromMapFunc (r .ClusterToMicroK8sConfigs ),
814- predicates . All ( ctrl . LoggerFrom ( ctx ),
815- predicates .ClusterUnpausedAndInfrastructureReady (ctrl . LoggerFrom ( ctx )),
816- predicates .ResourceHasFilterLabel (ctrl . LoggerFrom ( ctx ), r .WatchFilterValue ),
812+ builder . WithPredicates (
813+ predicates .ClusterUnpausedAndInfrastructureReady (mgr . GetScheme (), mgr . GetLogger ( )),
814+ predicates .ResourceHasFilterLabel (mgr . GetScheme (), mgr . GetLogger ( ), r .WatchFilterValue ),
817815 ),
818816 )
817+
818+ _ , err := b .Build (r )
819819 if err != nil {
820- return errors .Wrap (err , "failed adding Watch for Clusters to controller manager" )
820+ return errors .Wrap (err , "failed setting up with a controller manager" )
821821 }
822822
823823 return nil
824824}
825825
826- func (r * MicroK8sConfigReconciler ) ClusterToMicroK8sConfigs (o client.Object ) []ctrl.Request {
826+ func (r * MicroK8sConfigReconciler ) ClusterToMicroK8sConfigs (ctx context. Context , o client.Object ) []ctrl.Request {
827827 result := []ctrl.Request {}
828828
829829 c , ok := o .(* clusterv1.Cluster )
@@ -834,45 +834,48 @@ func (r *MicroK8sConfigReconciler) ClusterToMicroK8sConfigs(o client.Object) []c
834834 selectors := []client.ListOption {
835835 client .InNamespace (c .Namespace ),
836836 client.MatchingLabels {
837- clusterv1 .ClusterLabelName : c .Name ,
837+ clusterv1 .ClusterNameLabel : c .Name ,
838838 },
839839 }
840840
841841 machineList := & clusterv1.MachineList {}
842- if err := r .Client .List (context . TODO () , machineList , selectors ... ); err != nil {
842+ if err := r .Client .List (ctx , machineList , selectors ... ); err != nil {
843843 return nil
844844 }
845845
846846 for _ , m := range machineList .Items {
847847 if m .Spec .Bootstrap .ConfigRef != nil &&
848848 m .Spec .Bootstrap .ConfigRef .GroupVersionKind ().GroupKind () == v1beta1 .GroupVersion .WithKind ("MicroK8sConfig" ).GroupKind () {
849849 name := client.ObjectKey {Namespace : m .Namespace , Name : m .Spec .Bootstrap .ConfigRef .Name }
850- result = append (result , ctrl .Request {NamespacedName : name })
850+ result = append (result , reconcile .Request {NamespacedName : name })
851851 }
852852 }
853853
854854 if feature .Gates .Enabled (feature .MachinePool ) {
855855 machinePoolList := & expv1.MachinePoolList {}
856- if err := r .Client .List (context . TODO () , machinePoolList , selectors ... ); err != nil {
856+ if err := r .Client .List (ctx , machinePoolList , selectors ... ); err != nil {
857857 return nil
858858 }
859859
860860 for _ , mp := range machinePoolList .Items {
861861 if mp .Spec .Template .Spec .Bootstrap .ConfigRef != nil &&
862862 mp .Spec .Template .Spec .Bootstrap .ConfigRef .GroupVersionKind ().GroupKind () == v1beta1 .GroupVersion .WithKind ("MicroK8sConfig" ).GroupKind () {
863863 name := client.ObjectKey {Namespace : mp .Namespace , Name : mp .Spec .Template .Spec .Bootstrap .ConfigRef .Name }
864- result = append (result , ctrl .Request {NamespacedName : name })
864+ result = append (result , reconcile .Request {NamespacedName : name })
865865 }
866866 }
867867 }
868868
869869 return result
870870}
871871
872- func (r * MicroK8sConfigReconciler ) MachineToBootstrapMapFunc (o client.Object ) []ctrl.Request {
872+ func (r * MicroK8sConfigReconciler ) MachineToBootstrapMapFunc (ctx context. Context , o client.Object ) []ctrl.Request {
873873 m , ok := o .(* clusterv1.Machine )
874874 if ! ok {
875- panic (fmt .Sprintf ("Expected a Machine but got a %T" , o ))
875+ _ , ok = o .(* expv1.MachinePool )
876+ if ! ok {
877+ panic (fmt .Sprintf ("Expected a Machine or MachinePool but got a %T" , o ))
878+ }
876879 }
877880
878881 result := []ctrl.Request {}
@@ -886,8 +889,8 @@ func (r *MicroK8sConfigReconciler) MachineToBootstrapMapFunc(o client.Object) []
886889func (r * MicroK8sConfigReconciler ) getControlPlaneMachinesForCluster (ctx context.Context ,
887890 cluster client.ObjectKey ) ([]clusterv1.Machine , error ) {
888891 selector := map [string ]string {
889- clusterv1 .ClusterLabelName : cluster .Name ,
890- clusterv1 .MachineControlPlaneLabelName : "" ,
892+ clusterv1 .ClusterNameLabel : cluster .Name ,
893+ clusterv1 .MachineControlPlaneNameLabel : "" ,
891894 }
892895
893896 machineList := clusterv1.MachineList {}
0 commit comments