Skip to content

Commit

Permalink
Add MD/MP watches to Cluster controller
Browse files Browse the repository at this point in the history
  • Loading branch information
sbueringer committed Nov 12, 2024
1 parent 1cd1543 commit 1641d9a
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions internal/controllers/cluster/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -106,6 +107,14 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
&clusterv1.Machine{},
handler.EnqueueRequestsFromMapFunc(r.controlPlaneMachineToCluster),
).
Watches(
&clusterv1.MachineDeployment{},
handler.EnqueueRequestsFromMapFunc(r.machineDeploymentToCluster),
).
Watches(
&expv1.MachinePool{},
handler.EnqueueRequestsFromMapFunc(r.machinePoolToCluster),
).
WithOptions(options).
WithEventFilter(predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue)).
Build(r)
Expand Down Expand Up @@ -726,3 +735,41 @@ func (r *Reconciler) controlPlaneMachineToCluster(ctx context.Context, o client.
NamespacedName: util.ObjectKey(cluster),
}}
}

// machineDeploymentToCluster is a handler.ToRequestsFunc to be used to enqueue requests for reconciliation
// for Cluster to update when one of its own MachineDeployments gets updated.
func (r *Reconciler) machineDeploymentToCluster(_ context.Context, o client.Object) []ctrl.Request {
md, ok := o.(*clusterv1.MachineDeployment)
if !ok {
panic(fmt.Sprintf("Expected a MachineDeployment but got a %T", o))
}
if md.Spec.ClusterName == "" {
return nil
}

return []ctrl.Request{{
NamespacedName: types.NamespacedName{
Namespace: md.Namespace,
Name: md.Spec.ClusterName,
},
}}
}

// machinePoolToCluster is a handler.ToRequestsFunc to be used to enqueue requests for reconciliation
// for Cluster to update when one of its own MachinePools gets updated.
func (r *Reconciler) machinePoolToCluster(_ context.Context, o client.Object) []ctrl.Request {
mp, ok := o.(*expv1.MachinePool)
if !ok {
panic(fmt.Sprintf("Expected a MachinePool but got a %T", o))
}
if mp.Spec.ClusterName == "" {
return nil
}

return []ctrl.Request{{
NamespacedName: types.NamespacedName{
Namespace: mp.Namespace,
Name: mp.Spec.ClusterName,
},
}}
}

0 comments on commit 1641d9a

Please sign in to comment.