Skip to content

Commit e38eec6

Browse files
committed
Fix owner group checking
1 parent 11a4202 commit e38eec6

9 files changed

+28
-14
lines changed

api/v1beta1/azuremachine_default_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ func (m mockClient) Get(ctx context.Context, key client.ObjectKey, obj client.Ob
564564
case *AzureCluster:
565565
obj.Spec.SubscriptionID = "test-subscription-id"
566566
case *clusterv1.Cluster:
567+
obj.Namespace = "default"
567568
obj.Spec = clusterv1.ClusterSpec{
568569
InfrastructureRef: clusterv1.ContractVersionedObjectReference{
569570
Kind: AzureClusterKind,

controllers/agentpooladopt_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (r *AgentPoolAdoptReconciler) Reconcile(ctx context.Context, req ctrl.Reque
8282
}
8383

8484
for _, owner := range agentPool.GetOwnerReferences() {
85-
if matchesASOManagedAPIGroup(owner.APIVersion) &&
85+
if apiVersionMatchesASOManagedAPIGroup(owner.APIVersion) &&
8686
owner.Kind == infrav1.AzureASOManagedMachinePoolKind {
8787
return ctrl.Result{}, nil
8888
}
@@ -124,7 +124,7 @@ func (r *AgentPoolAdoptReconciler) Reconcile(ctx context.Context, req ctrl.Reque
124124
}
125125
var managedControlPlaneOwner *metav1.OwnerReference
126126
for _, owner := range managedCluster.GetOwnerReferences() {
127-
if matchesASOManagedAPIGroup(owner.APIVersion) &&
127+
if apiVersionMatchesASOManagedAPIGroup(owner.APIVersion) &&
128128
owner.Kind == infrav1.AzureASOManagedControlPlaneKind &&
129129
owner.Name == agentPool.Owner().Name {
130130
managedControlPlaneOwner = ptr.To(owner)

controllers/azureasomanagedcluster_controller.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26+
"k8s.io/apimachinery/pkg/runtime/schema"
2627
"k8s.io/utils/ptr"
2728
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
2829
"sigs.k8s.io/cluster-api/controllers/external"
@@ -146,7 +147,7 @@ func asoManagedControlPlaneToManagedClusterMap(c client.Client) handler.MapFunc
146147

147148
if cluster == nil ||
148149
!cluster.Spec.InfrastructureRef.IsDefined() ||
149-
!matchesASOManagedAPIGroup(cluster.Spec.InfrastructureRef.APIGroup) ||
150+
!groupMatchesASOManagedAPIGroup(cluster.Spec.InfrastructureRef.APIGroup) ||
150151
cluster.Spec.InfrastructureRef.Kind != infrav1.AzureASOManagedClusterKind {
151152
return nil
152153
}
@@ -162,7 +163,12 @@ func asoManagedControlPlaneToManagedClusterMap(c client.Client) handler.MapFunc
162163
}
163164
}
164165

165-
func matchesASOManagedAPIGroup(group string) bool {
166+
func apiVersionMatchesASOManagedAPIGroup(apiVersion string) bool {
167+
gv, _ := schema.ParseGroupVersion(apiVersion)
168+
return groupMatchesASOManagedAPIGroup(gv.Group)
169+
}
170+
171+
func groupMatchesASOManagedAPIGroup(group string) bool {
166172
return group == infrav1.GroupVersion.Group
167173
}
168174

@@ -229,7 +235,7 @@ func (r *AzureASOManagedClusterReconciler) reconcileNormal(ctx context.Context,
229235
return ctrl.Result{}, nil
230236
}
231237
if !cluster.Spec.ControlPlaneRef.IsDefined() ||
232-
!matchesASOManagedAPIGroup(cluster.Spec.ControlPlaneRef.APIGroup) ||
238+
!groupMatchesASOManagedAPIGroup(cluster.Spec.ControlPlaneRef.APIGroup) ||
233239
cluster.Spec.ControlPlaneRef.Kind != infrav1.AzureASOManagedControlPlaneKind {
234240
return ctrl.Result{}, reconcile.TerminalError(errInvalidControlPlaneKind)
235241
}

controllers/azureasomanagedcontrolplane_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (r *AzureASOManagedControlPlaneReconciler) SetupWithManager(ctx context.Con
115115
func clusterToAzureASOManagedControlPlane(_ context.Context, o client.Object) []ctrl.Request {
116116
controlPlaneRef := o.(*clusterv1.Cluster).Spec.ControlPlaneRef
117117
if controlPlaneRef.IsDefined() &&
118-
matchesASOManagedAPIGroup(controlPlaneRef.APIGroup) &&
118+
groupMatchesASOManagedAPIGroup(controlPlaneRef.APIGroup) &&
119119
controlPlaneRef.Kind == infrav1.AzureASOManagedControlPlaneKind {
120120
return []ctrl.Request{{NamespacedName: client.ObjectKey{Namespace: o.GetNamespace(), Name: controlPlaneRef.Name}}}
121121
}
@@ -199,7 +199,7 @@ func (r *AzureASOManagedControlPlaneReconciler) reconcileNormal(ctx context.Cont
199199
return ctrl.Result{}, nil
200200
}
201201
if !cluster.Spec.InfrastructureRef.IsDefined() ||
202-
!matchesASOManagedAPIGroup(cluster.Spec.InfrastructureRef.APIGroup) ||
202+
!groupMatchesASOManagedAPIGroup(cluster.Spec.InfrastructureRef.APIGroup) ||
203203
cluster.Spec.InfrastructureRef.Kind != infrav1.AzureASOManagedClusterKind {
204204
return ctrl.Result{}, reconcile.TerminalError(errInvalidClusterKind)
205205
}

controllers/azureasomanagedmachinepool_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func (r *AzureASOManagedMachinePoolReconciler) Reconcile(ctx context.Context, re
184184
return ctrl.Result{}, nil
185185
}
186186
if !cluster.Spec.ControlPlaneRef.IsDefined() ||
187-
!matchesASOManagedAPIGroup(cluster.Spec.ControlPlaneRef.APIGroup) ||
187+
!groupMatchesASOManagedAPIGroup(cluster.Spec.ControlPlaneRef.APIGroup) ||
188188
cluster.Spec.ControlPlaneRef.Kind != infrav1.AzureASOManagedControlPlaneKind {
189189
return ctrl.Result{}, reconcile.TerminalError(fmt.Errorf("AzureASOManagedMachinePool cannot be used without AzureASOManagedControlPlane"))
190190
}

controllers/azuremanagedcontrolplane_controller_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,19 @@ import (
4848
func TestClusterToAzureManagedControlPlane(t *testing.T) {
4949
tests := []struct {
5050
name string
51+
namespace string
5152
controlPlaneRef clusterv1.ContractVersionedObjectReference
5253
expected []ctrl.Request
5354
}{
5455
{
5556
name: "nil",
57+
namespace: "",
5658
controlPlaneRef: clusterv1.ContractVersionedObjectReference{},
5759
expected: nil,
5860
},
5961
{
60-
name: "bad kind",
62+
name: "bad kind",
63+
namespace: "",
6164
controlPlaneRef: clusterv1.ContractVersionedObjectReference{
6265
Kind: "NotAzureManagedControlPlane",
6366
Name: "foo",
@@ -66,7 +69,8 @@ func TestClusterToAzureManagedControlPlane(t *testing.T) {
6669
expected: nil,
6770
},
6871
{
69-
name: "ok",
72+
name: "ok",
73+
namespace: "namespace",
7074
controlPlaneRef: clusterv1.ContractVersionedObjectReference{
7175
Kind: infrav1.AzureManagedControlPlaneKind,
7276
Name: "name",
@@ -87,6 +91,9 @@ func TestClusterToAzureManagedControlPlane(t *testing.T) {
8791
t.Run(test.name, func(t *testing.T) {
8892
g := NewWithT(t)
8993
actual := (&AzureManagedControlPlaneReconciler{}).ClusterToAzureManagedControlPlane(t.Context(), &clusterv1.Cluster{
94+
ObjectMeta: metav1.ObjectMeta{
95+
Namespace: test.namespace,
96+
},
9097
Spec: clusterv1.ClusterSpec{
9198
ControlPlaneRef: test.controlPlaneRef,
9299
},

controllers/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,7 @@ func Test_ManagedMachinePoolToInfrastructureMapFunc(t *testing.T) {
13301330
Setup: func(logMock *mock_log.MockLogSink) {
13311331
logMock.EXPECT().Init(logr.RuntimeInfo{CallDepth: 1})
13321332
logMock.EXPECT().Enabled(4).Return(true)
1333-
logMock.EXPECT().Info(4, "attempt to map incorrect type", "type", "*v1beta1.Cluster")
1333+
logMock.EXPECT().Info(4, "attempt to map incorrect type", "type", "*v1beta2.Cluster")
13341334
},
13351335
Expect: func(g *GomegaWithT, reqs []reconcile.Request) {
13361336
g.Expect(reqs).To(BeEmpty())

controllers/managedclusteradopt_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (r *ManagedClusterAdoptReconciler) Reconcile(ctx context.Context, req ctrl.
8787
}
8888

8989
for _, owner := range managedCluster.GetOwnerReferences() {
90-
if matchesASOManagedAPIGroup(owner.APIVersion) &&
90+
if apiVersionMatchesASOManagedAPIGroup(owner.APIVersion) &&
9191
owner.Kind == infrav1.AzureASOManagedControlPlaneKind {
9292
return ctrl.Result{}, nil
9393
}

exp/controllers/helpers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func Test_MachinePoolToInfrastructureMapFunc(t *testing.T) {
220220
Setup: func(logMock *mock_log.MockLogSink) {
221221
logMock.EXPECT().Init(logr.RuntimeInfo{CallDepth: 1})
222222
logMock.EXPECT().Enabled(4).Return(true)
223-
logMock.EXPECT().Info(4, "attempt to map incorrect type", "type", "*v1beta1.Cluster")
223+
logMock.EXPECT().Info(4, "attempt to map incorrect type", "type", "*v1beta2.Cluster")
224224
},
225225
Expect: func(g *GomegaWithT, reqs []reconcile.Request) {
226226
g.Expect(reqs).To(BeEmpty())
@@ -264,7 +264,7 @@ func Test_azureClusterToAzureMachinePoolsFunc(t *testing.T) {
264264
sink := mock_log.NewMockLogSink(mockCtrl)
265265
fakeClient := fake.NewClientBuilder().WithScheme(newScheme(g)).Build()
266266
sink.EXPECT().Init(logr.RuntimeInfo{CallDepth: 1})
267-
sink.EXPECT().Error(gomockinternal.ErrStrEq("expected a AzureCluster but got a *v1beta1.MachinePool"), "failed to get AzureCluster")
267+
sink.EXPECT().Error(gomockinternal.ErrStrEq("expected a AzureCluster but got a *v1beta2.MachinePool"), "failed to get AzureCluster")
268268

269269
return sink, mockCtrl, fakeClient
270270
},

0 commit comments

Comments
 (0)