Skip to content

Commit e524895

Browse files
nojnhuhk8s-infra-cherrypick-robot
authored andcommitted
fix Cluster to AzureManagedControlPlane mapper
1 parent 60e137d commit e524895

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

controllers/azuremanagedcontrolplane_controller.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (amcpr *AzureManagedControlPlaneReconciler) SetupWithManager(ctx context.Co
9898
// Add a watch on clusterv1.Cluster object for unpause & ready notifications.
9999
if err = c.Watch(
100100
&source.Kind{Type: &clusterv1.Cluster{}},
101-
handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(ctx, infrav1.GroupVersion.WithKind("AzureManagedControlPlane"), mgr.GetClient(), &infrav1.AzureManagedControlPlane{})),
101+
handler.EnqueueRequestsFromMapFunc(amcpr.ClusterToAzureManagedControlPlane),
102102
predicates.ClusterUnpausedAndInfrastructureReady(log),
103103
predicates.ResourceNotPausedAndHasFilterLabel(log, amcpr.WatchFilterValue),
104104
); err != nil {
@@ -285,3 +285,19 @@ func (amcpr *AzureManagedControlPlaneReconciler) reconcileDelete(ctx context.Con
285285

286286
return reconcile.Result{}, nil
287287
}
288+
289+
// ClusterToAzureManagedControlPlane is a handler.ToRequestsFunc to be used to enqueue requests for
290+
// reconciliation for AzureManagedControlPlane based on updates to a Cluster.
291+
func (amcpr *AzureManagedControlPlaneReconciler) ClusterToAzureManagedControlPlane(o client.Object) []ctrl.Request {
292+
c, ok := o.(*clusterv1.Cluster)
293+
if !ok {
294+
panic(fmt.Sprintf("Expected a Cluster but got a %T", o))
295+
}
296+
297+
controlPlaneRef := c.Spec.ControlPlaneRef
298+
if controlPlaneRef != nil && controlPlaneRef.Kind == "AzureManagedControlPlane" {
299+
return []ctrl.Request{{NamespacedName: client.ObjectKey{Namespace: controlPlaneRef.Namespace, Name: controlPlaneRef.Name}}}
300+
}
301+
302+
return nil
303+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package controllers
17+
18+
import (
19+
"testing"
20+
21+
. "github.com/onsi/gomega"
22+
corev1 "k8s.io/api/core/v1"
23+
"k8s.io/apimachinery/pkg/types"
24+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
25+
ctrl "sigs.k8s.io/controller-runtime"
26+
)
27+
28+
func TestClusterToAzureManagedControlPlane(t *testing.T) {
29+
tests := []struct {
30+
name string
31+
controlPlaneRef *corev1.ObjectReference
32+
expected []ctrl.Request
33+
}{
34+
{
35+
name: "nil",
36+
controlPlaneRef: nil,
37+
expected: nil,
38+
},
39+
{
40+
name: "bad kind",
41+
controlPlaneRef: &corev1.ObjectReference{
42+
Kind: "NotAzureManagedControlPlane",
43+
},
44+
expected: nil,
45+
},
46+
{
47+
name: "ok",
48+
controlPlaneRef: &corev1.ObjectReference{
49+
Kind: "AzureManagedControlPlane",
50+
Name: "name",
51+
Namespace: "namespace",
52+
},
53+
expected: []ctrl.Request{
54+
{
55+
NamespacedName: types.NamespacedName{
56+
Name: "name",
57+
Namespace: "namespace",
58+
},
59+
},
60+
},
61+
},
62+
}
63+
64+
for _, test := range tests {
65+
t.Run(test.name, func(t *testing.T) {
66+
g := NewWithT(t)
67+
actual := (&AzureManagedControlPlaneReconciler{}).ClusterToAzureManagedControlPlane(&clusterv1.Cluster{
68+
Spec: clusterv1.ClusterSpec{
69+
ControlPlaneRef: test.controlPlaneRef,
70+
},
71+
})
72+
if test.expected == nil {
73+
g.Expect(actual).To(BeNil())
74+
} else {
75+
g.Expect(actual).To(Equal(test.expected))
76+
}
77+
})
78+
}
79+
}

0 commit comments

Comments
 (0)