@@ -30,6 +30,7 @@ import (
30
30
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31
31
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
32
32
"k8s.io/apimachinery/pkg/runtime"
33
+ "k8s.io/apimachinery/pkg/types"
33
34
kerrors "k8s.io/apimachinery/pkg/util/errors"
34
35
"k8s.io/client-go/tools/record"
35
36
"k8s.io/klog/v2"
@@ -106,6 +107,14 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
106
107
& clusterv1.Machine {},
107
108
handler .EnqueueRequestsFromMapFunc (r .controlPlaneMachineToCluster ),
108
109
).
110
+ Watches (
111
+ & clusterv1.MachineDeployment {},
112
+ handler .EnqueueRequestsFromMapFunc (r .machineDeploymentToCluster ),
113
+ ).
114
+ Watches (
115
+ & expv1.MachinePool {},
116
+ handler .EnqueueRequestsFromMapFunc (r .machinePoolToCluster ),
117
+ ).
109
118
WithOptions (options ).
110
119
WithEventFilter (predicates .ResourceHasFilterLabel (mgr .GetScheme (), predicateLog , r .WatchFilterValue )).
111
120
Build (r )
@@ -727,3 +736,41 @@ func (r *Reconciler) controlPlaneMachineToCluster(ctx context.Context, o client.
727
736
NamespacedName : util .ObjectKey (cluster ),
728
737
}}
729
738
}
739
+
740
+ // machineDeploymentToCluster is a handler.ToRequestsFunc to be used to enqueue requests for reconciliation
741
+ // for Cluster to update when one of its own MachineDeployments gets updated.
742
+ func (r * Reconciler ) machineDeploymentToCluster (_ context.Context , o client.Object ) []ctrl.Request {
743
+ md , ok := o .(* clusterv1.MachineDeployment )
744
+ if ! ok {
745
+ panic (fmt .Sprintf ("Expected a MachineDeployment but got a %T" , o ))
746
+ }
747
+ if md .Spec .ClusterName == "" {
748
+ return nil
749
+ }
750
+
751
+ return []ctrl.Request {{
752
+ NamespacedName : types.NamespacedName {
753
+ Namespace : md .Namespace ,
754
+ Name : md .Spec .ClusterName ,
755
+ },
756
+ }}
757
+ }
758
+
759
+ // machinePoolToCluster is a handler.ToRequestsFunc to be used to enqueue requests for reconciliation
760
+ // for Cluster to update when one of its own MachinePools gets updated.
761
+ func (r * Reconciler ) machinePoolToCluster (_ context.Context , o client.Object ) []ctrl.Request {
762
+ mp , ok := o .(* expv1.MachinePool )
763
+ if ! ok {
764
+ panic (fmt .Sprintf ("Expected a MachinePool but got a %T" , o ))
765
+ }
766
+ if mp .Spec .ClusterName == "" {
767
+ return nil
768
+ }
769
+
770
+ return []ctrl.Request {{
771
+ NamespacedName : types.NamespacedName {
772
+ Namespace : mp .Namespace ,
773
+ Name : mp .Spec .ClusterName ,
774
+ },
775
+ }}
776
+ }
0 commit comments