Skip to content

Commit e5c47ee

Browse files
authored
Merge pull request #11408 from sbueringer/pr-add-watches-to-cluster-ctrl
🌱 Add MD/MP watches to Cluster controller
2 parents 5e91e9f + 1641d9a commit e5c47ee

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)
@@ -727,3 +736,41 @@ func (r *Reconciler) controlPlaneMachineToCluster(ctx context.Context, o client.
727736
NamespacedName: util.ObjectKey(cluster),
728737
}}
729738
}
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

Comments
 (0)