diff --git a/controllers/azuremanagedcontrolplane_controller.go b/controllers/azuremanagedcontrolplane_controller.go index 56984c7024b..0b982025229 100644 --- a/controllers/azuremanagedcontrolplane_controller.go +++ b/controllers/azuremanagedcontrolplane_controller.go @@ -98,7 +98,7 @@ func (amcpr *AzureManagedControlPlaneReconciler) SetupWithManager(ctx context.Co // Add a watch on clusterv1.Cluster object for unpause & ready notifications. if err = c.Watch( &source.Kind{Type: &clusterv1.Cluster{}}, - handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(ctx, infrav1.GroupVersion.WithKind("AzureManagedControlPlane"), mgr.GetClient(), &infrav1.AzureManagedControlPlane{})), + handler.EnqueueRequestsFromMapFunc(amcpr.ClusterToAzureManagedControlPlane), predicates.ClusterUnpausedAndInfrastructureReady(log), predicates.ResourceNotPausedAndHasFilterLabel(log, amcpr.WatchFilterValue), ); err != nil { @@ -285,3 +285,19 @@ func (amcpr *AzureManagedControlPlaneReconciler) reconcileDelete(ctx context.Con return reconcile.Result{}, nil } + +// ClusterToAzureManagedControlPlane is a handler.ToRequestsFunc to be used to enqueue requests for +// reconciliation for AzureManagedControlPlane based on updates to a Cluster. +func (amcpr *AzureManagedControlPlaneReconciler) ClusterToAzureManagedControlPlane(o client.Object) []ctrl.Request { + c, ok := o.(*clusterv1.Cluster) + if !ok { + panic(fmt.Sprintf("Expected a Cluster but got a %T", o)) + } + + controlPlaneRef := c.Spec.ControlPlaneRef + if controlPlaneRef != nil && controlPlaneRef.Kind == "AzureManagedControlPlane" { + return []ctrl.Request{{NamespacedName: client.ObjectKey{Namespace: controlPlaneRef.Namespace, Name: controlPlaneRef.Name}}} + } + + return nil +} diff --git a/controllers/azuremanagedcontrolplane_controller_test.go b/controllers/azuremanagedcontrolplane_controller_test.go new file mode 100644 index 00000000000..35b275d5b0a --- /dev/null +++ b/controllers/azuremanagedcontrolplane_controller_test.go @@ -0,0 +1,79 @@ +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package controllers + +import ( + "testing" + + . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/types" + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" + ctrl "sigs.k8s.io/controller-runtime" +) + +func TestClusterToAzureManagedControlPlane(t *testing.T) { + tests := []struct { + name string + controlPlaneRef *corev1.ObjectReference + expected []ctrl.Request + }{ + { + name: "nil", + controlPlaneRef: nil, + expected: nil, + }, + { + name: "bad kind", + controlPlaneRef: &corev1.ObjectReference{ + Kind: "NotAzureManagedControlPlane", + }, + expected: nil, + }, + { + name: "ok", + controlPlaneRef: &corev1.ObjectReference{ + Kind: "AzureManagedControlPlane", + Name: "name", + Namespace: "namespace", + }, + expected: []ctrl.Request{ + { + NamespacedName: types.NamespacedName{ + Name: "name", + Namespace: "namespace", + }, + }, + }, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + g := NewWithT(t) + actual := (&AzureManagedControlPlaneReconciler{}).ClusterToAzureManagedControlPlane(&clusterv1.Cluster{ + Spec: clusterv1.ClusterSpec{ + ControlPlaneRef: test.controlPlaneRef, + }, + }) + if test.expected == nil { + g.Expect(actual).To(BeNil()) + } else { + g.Expect(actual).To(Equal(test.expected)) + } + }) + } +}