Skip to content

Commit 5af14ff

Browse files
authored
Merge pull request #3362 from willie-yao/cherry-pick-3322-to-release-1.8
[release-1.8] Fetch AzureCluster name from OwnerCluster instead of assuming ClusterName = AzureCluster.Name
2 parents dcd004c + 3dcdc99 commit 5af14ff

File tree

5 files changed

+160
-31
lines changed

5 files changed

+160
-31
lines changed

api/v1beta1/azuremachine_default.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,29 +174,51 @@ func (s *AzureMachineSpec) SetNetworkInterfacesDefaults() {
174174
}
175175
}
176176

177-
// GetSubscriptionID returns the subscription ID for the given cluster name and namespace.
178-
func GetSubscriptionID(cli client.Client, clusterName string, namespace string, maxAttempts int) (string, error) {
177+
// GetOwnerAzureClusterNameAndNamespace returns the owner azure cluster's name and namespace for the given cluster name and namespace.
178+
func GetOwnerAzureClusterNameAndNamespace(cli client.Client, clusterName string, namespace string, maxAttempts int) (azureClusterName string, azureClusterNamespace string, err error) {
179179
ctx := context.Background()
180180

181-
ownerCluster := &AzureCluster{}
181+
ownerCluster := &clusterv1.Cluster{}
182182
key := client.ObjectKey{
183183
Namespace: namespace,
184184
Name: clusterName,
185185
}
186186

187187
for i := 1; ; i++ {
188-
err := cli.Get(ctx, key, ownerCluster)
189-
if err != nil {
188+
if err := cli.Get(ctx, key, ownerCluster); err != nil {
190189
if i > maxAttempts {
191-
return "", errors.Wrapf(err, "failed to find owner cluster for %s/%s", namespace, clusterName)
190+
return "", "", errors.Wrapf(err, "failed to find owner cluster for %s/%s", namespace, clusterName)
191+
}
192+
time.Sleep(1 * time.Second)
193+
continue
194+
}
195+
break
196+
}
197+
198+
return ownerCluster.Spec.InfrastructureRef.Name, ownerCluster.Spec.InfrastructureRef.Namespace, nil
199+
}
200+
201+
// GetSubscriptionID returns the subscription ID for the AzureCluster given the cluster name and namespace.
202+
func GetSubscriptionID(cli client.Client, ownerAzureClusterName string, ownerAzureClusterNamespace string, maxAttempts int) (string, error) {
203+
ctx := context.Background()
204+
205+
ownerAzureCluster := &AzureCluster{}
206+
key := client.ObjectKey{
207+
Namespace: ownerAzureClusterNamespace,
208+
Name: ownerAzureClusterName,
209+
}
210+
for i := 1; ; i++ {
211+
if err := cli.Get(ctx, key, ownerAzureCluster); err != nil {
212+
if i >= maxAttempts {
213+
return "", errors.Wrapf(err, "failed to find AzureCluster for owner cluster %s/%s", ownerAzureClusterNamespace, ownerAzureClusterName)
192214
}
193215
time.Sleep(1 * time.Second)
194216
continue
195217
}
196218
break
197219
}
198220

199-
return ownerCluster.Spec.SubscriptionID, nil
221+
return ownerAzureCluster.Spec.SubscriptionID, nil
200222
}
201223

202224
// SetDefaults sets to the defaults for the AzureMachineSpec.
@@ -211,7 +233,12 @@ func (m *AzureMachine) SetDefaults(client client.Client) {
211233
ctrl.Log.WithName("SetDefault").Error(errors.Errorf("failed to fetch owner ClusterName for AzureMachine %s/%s", m.Namespace, m.Name), "failed to fetch ClusterName")
212234
}
213235

214-
subscriptionID, err := GetSubscriptionID(client, clusterName, m.Namespace, 5)
236+
ownerAzureClusterName, ownerAzureClusterNamespace, err := GetOwnerAzureClusterNameAndNamespace(client, clusterName, m.Namespace, 5)
237+
if err != nil {
238+
ctrl.Log.WithName("SetDefault").Error(err, "failed to fetch owner cluster for AzureMachine %s/%s", m.Namespace, m.Name)
239+
}
240+
241+
subscriptionID, err := GetSubscriptionID(client, ownerAzureClusterName, ownerAzureClusterNamespace, 5)
215242
if err != nil {
216243
ctrl.Log.WithName("SetDefault").Error(err, "failed to fetch subscription ID for AzureMachine %s/%s", m.Namespace, m.Name)
217244
}

api/v1beta1/azuremachine_default_test.go

Lines changed: 94 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import (
2626
"github.com/google/uuid"
2727
. "github.com/onsi/gomega"
2828
"github.com/pkg/errors"
29+
corev1 "k8s.io/api/core/v1"
2930
"k8s.io/utils/pointer"
31+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3032
"sigs.k8s.io/controller-runtime/pkg/client"
3133
)
3234

@@ -399,39 +401,101 @@ func TestAzureMachineSpec_SetNetworkInterfacesDefaults(t *testing.T) {
399401
}
400402
}
401403

404+
func TestAzureMachineSpec_GetOwnerCluster(t *testing.T) {
405+
tests := []struct {
406+
name string
407+
maxAttempts int
408+
wantedName string
409+
wantedNamespace string
410+
wantErr bool
411+
}{
412+
{
413+
name: "ownerCluster is returned",
414+
maxAttempts: 1,
415+
wantedName: "test-cluster",
416+
wantedNamespace: "default",
417+
wantErr: false,
418+
},
419+
{
420+
name: "ownerCluster is returned after 2 attempts",
421+
maxAttempts: 2,
422+
wantedName: "test-cluster",
423+
wantedNamespace: "default",
424+
wantErr: false,
425+
},
426+
{
427+
name: "ownerCluster is not returned after 5 attempts",
428+
maxAttempts: 5,
429+
wantedName: "test-cluster",
430+
wantedNamespace: "default",
431+
wantErr: true,
432+
},
433+
}
434+
435+
for _, tc := range tests {
436+
t.Run(tc.name, func(t *testing.T) {
437+
g := NewWithT(t)
438+
client := mockClient{ReturnError: tc.wantErr}
439+
name, namespace, err := GetOwnerAzureClusterNameAndNamespace(client, "test-cluster", "default", tc.maxAttempts)
440+
if tc.wantErr {
441+
g.Expect(err).To(HaveOccurred())
442+
} else {
443+
g.Expect(err).NotTo(HaveOccurred())
444+
g.Expect(name).To(Equal(tc.wantedName))
445+
g.Expect(namespace).To(Equal(tc.wantedNamespace))
446+
}
447+
})
448+
}
449+
}
450+
402451
func TestAzureMachineSpec_GetSubscriptionID(t *testing.T) {
403452
g := NewWithT(t)
404453

405454
tests := []struct {
406-
name string
407-
maxAttempts int
408-
want string
409-
wantErr bool
455+
name string
456+
maxAttempts int
457+
ownerAzureClusterName string
458+
ownerAzureClusterNamespace string
459+
want string
460+
wantErr bool
410461
}{
411462
{
412-
name: "subscription ID is returned",
413-
maxAttempts: 1,
414-
want: "test-subscription-id",
415-
wantErr: false,
463+
name: "empty owner cluster name returns error",
464+
maxAttempts: 1,
465+
ownerAzureClusterName: "",
466+
want: "test-subscription-id",
467+
wantErr: true,
416468
},
417469
{
418-
name: "subscription ID is returned after 2 attempts",
419-
maxAttempts: 2,
420-
want: "test-subscription-id",
421-
wantErr: false,
470+
name: "subscription ID is returned",
471+
maxAttempts: 1,
472+
ownerAzureClusterName: "test-cluster",
473+
ownerAzureClusterNamespace: "default",
474+
want: "test-subscription-id",
475+
wantErr: false,
422476
},
423477
{
424-
name: "subscription ID is not returned after 5 attempts",
425-
maxAttempts: 5,
426-
want: "",
427-
wantErr: true,
478+
name: "subscription ID is returned after 2 attempts",
479+
maxAttempts: 2,
480+
ownerAzureClusterName: "test-cluster",
481+
ownerAzureClusterNamespace: "default",
482+
want: "test-subscription-id",
483+
wantErr: false,
484+
},
485+
{
486+
name: "subscription ID is not returned after 5 attempts",
487+
maxAttempts: 5,
488+
ownerAzureClusterName: "test-cluster",
489+
ownerAzureClusterNamespace: "default",
490+
want: "",
491+
wantErr: true,
428492
},
429493
}
430494

431495
for _, tc := range tests {
432496
t.Run(tc.name, func(t *testing.T) {
433497
client := mockClient{ReturnError: tc.wantErr}
434-
result, err := GetSubscriptionID(client, "test-cluster", "default", tc.maxAttempts)
498+
result, err := GetSubscriptionID(client, tc.ownerAzureClusterName, tc.ownerAzureClusterNamespace, tc.maxAttempts)
435499
if tc.wantErr {
436500
g.Expect(err).To(HaveOccurred())
437501
} else {
@@ -451,9 +515,19 @@ func (m mockClient) Get(ctx context.Context, key client.ObjectKey, obj client.Ob
451515
if m.ReturnError {
452516
return errors.New("AzureCluster not found: failed to find owner cluster for test-cluster")
453517
}
454-
ac := &AzureCluster{}
455-
ac.Spec.SubscriptionID = "test-subscription-id"
456-
obj.(*AzureCluster).Spec.SubscriptionID = ac.Spec.SubscriptionID
518+
// Check if we're calling Get on an AzureCluster or a Cluster
519+
switch obj := obj.(type) {
520+
case *AzureCluster:
521+
obj.Spec.SubscriptionID = "test-subscription-id"
522+
case *clusterv1.Cluster:
523+
obj.Spec.InfrastructureRef = &corev1.ObjectReference{
524+
Kind: "AzureCluster",
525+
Name: "test-cluster",
526+
Namespace: "default",
527+
}
528+
default:
529+
return errors.New("unexpected object type")
530+
}
457531

458532
return nil
459533
}

api/v1beta1/azuremachine_webhook_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ import (
2222

2323
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute"
2424
. "github.com/onsi/gomega"
25+
"github.com/pkg/errors"
26+
corev1 "k8s.io/api/core/v1"
2527
"k8s.io/apimachinery/pkg/api/resource"
2628
"k8s.io/utils/pointer"
29+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2730
"sigs.k8s.io/controller-runtime/pkg/client"
2831
)
2932

@@ -712,7 +715,17 @@ type mockDefaultClient struct {
712715
}
713716

714717
func (m mockDefaultClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
715-
obj.(*AzureCluster).Spec.SubscriptionID = m.SubscriptionID
718+
switch obj := obj.(type) {
719+
case *AzureCluster:
720+
obj.Spec.SubscriptionID = m.SubscriptionID
721+
case *clusterv1.Cluster:
722+
obj.Spec.InfrastructureRef = &corev1.ObjectReference{
723+
Kind: "AzureCluster",
724+
Name: "test-cluster",
725+
}
726+
default:
727+
return errors.New("invalid object type")
728+
}
716729
return nil
717730
}
718731

exp/api/v1beta1/azuremachinepool_default.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ func (amp *AzureMachinePool) SetDefaults(client client.Client) {
4040
ctrl.Log.WithName("AzureMachinePoolLogger").Error(err, "findParentMachinePool failed")
4141
}
4242

43-
subscriptionID, err := infrav1.GetSubscriptionID(client, machinePool.Spec.ClusterName, machinePool.Namespace, 5)
43+
ownerAzureClusterName, ownerAzureClusterNamespace, err := infrav1.GetOwnerAzureClusterNameAndNamespace(client, machinePool.Spec.ClusterName, machinePool.Namespace, 5)
44+
if err != nil {
45+
ctrl.Log.WithName("AzureMachinePoolLogger").Error(err, "failed to get owner cluster")
46+
}
47+
48+
subscriptionID, err := infrav1.GetSubscriptionID(client, ownerAzureClusterName, ownerAzureClusterNamespace, 5)
4449
if err != nil {
4550
ctrl.Log.WithName("AzureMachinePoolLogger").Error(err, "getSubscriptionID failed")
4651
}

exp/api/v1beta1/azuremachinepool_webhook_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,17 @@ type mockDefaultClient struct {
261261
}
262262

263263
func (m mockDefaultClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
264-
obj.(*infrav1.AzureCluster).Spec.SubscriptionID = m.SubscriptionID
264+
switch obj := obj.(type) {
265+
case *infrav1.AzureCluster:
266+
obj.Spec.SubscriptionID = m.SubscriptionID
267+
case *clusterv1.Cluster:
268+
obj.Spec.InfrastructureRef = &corev1.ObjectReference{
269+
Kind: "AzureCluster",
270+
Name: "test-cluster",
271+
}
272+
default:
273+
return errors.New("invalid object type")
274+
}
265275
return nil
266276
}
267277

0 commit comments

Comments
 (0)