Skip to content

Commit 1641d9a

Browse files
committed
Add MD/MP watches to Cluster controller
1 parent 1cd1543 commit 1641d9a

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

internal/controllers/cluster/cluster_controller.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3232
"k8s.io/apimachinery/pkg/runtime"
33+
"k8s.io/apimachinery/pkg/types"
3334
kerrors "k8s.io/apimachinery/pkg/util/errors"
3435
"k8s.io/client-go/tools/record"
3536
"k8s.io/klog/v2"
@@ -106,6 +107,14 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
106107
&clusterv1.Machine{},
107108
handler.EnqueueRequestsFromMapFunc(r.controlPlaneMachineToCluster),
108109
).
110+
Watches(
111+
&clusterv1.MachineDeployment{},
112+
handler.EnqueueRequestsFromMapFunc(r.machineDeploymentToCluster),
113+
).
114+
Watches(
115+
&expv1.MachinePool{},
116+
handler.EnqueueRequestsFromMapFunc(r.machinePoolToCluster),
117+
).
109118
WithOptions(options).
110119
WithEventFilter(predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue)).
111120
Build(r)
@@ -726,3 +735,41 @@ func (r *Reconciler) controlPlaneMachineToCluster(ctx context.Context, o client.
726735
NamespacedName: util.ObjectKey(cluster),
727736
}}
728737
}
738+
739+
// machineDeploymentToCluster is a handler.ToRequestsFunc to be used to enqueue requests for reconciliation
740+
// for Cluster to update when one of its own MachineDeployments gets updated.
741+
func (r *Reconciler) machineDeploymentToCluster(_ context.Context, o client.Object) []ctrl.Request {
742+
md, ok := o.(*clusterv1.MachineDeployment)
743+
if !ok {
744+
panic(fmt.Sprintf("Expected a MachineDeployment but got a %T", o))
745+
}
746+
if md.Spec.ClusterName == "" {
747+
return nil
748+
}
749+
750+
return []ctrl.Request{{
751+
NamespacedName: types.NamespacedName{
752+
Namespace: md.Namespace,
753+
Name: md.Spec.ClusterName,
754+
},
755+
}}
756+
}
757+
758+
// machinePoolToCluster is a handler.ToRequestsFunc to be used to enqueue requests for reconciliation
759+
// for Cluster to update when one of its own MachinePools gets updated.
760+
func (r *Reconciler) machinePoolToCluster(_ context.Context, o client.Object) []ctrl.Request {
761+
mp, ok := o.(*expv1.MachinePool)
762+
if !ok {
763+
panic(fmt.Sprintf("Expected a MachinePool but got a %T", o))
764+
}
765+
if mp.Spec.ClusterName == "" {
766+
return nil
767+
}
768+
769+
return []ctrl.Request{{
770+
NamespacedName: types.NamespacedName{
771+
Namespace: mp.Namespace,
772+
Name: mp.Spec.ClusterName,
773+
},
774+
}}
775+
}

0 commit comments

Comments
 (0)