Skip to content

Commit 5dfcdee

Browse files
committed
Fix fetching activity logs in e2e for ClusterClass clusters
1 parent d809e63 commit 5dfcdee

File tree

1 file changed

+45
-33
lines changed

1 file changed

+45
-33
lines changed

test/e2e/azure_clusterproxy.go

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import (
4040
. "github.com/onsi/ginkgo/v2"
4141
. "github.com/onsi/gomega"
4242
corev1 "k8s.io/api/core/v1"
43-
apierrors "k8s.io/apimachinery/pkg/api/errors"
4443
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
4544
"k8s.io/apimachinery/pkg/runtime"
4645
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -223,45 +222,58 @@ func (acp *AzureClusterProxy) collectActivityLogs(ctx context.Context, namespace
223222
activityLogsClient, err := armmonitor.NewActivityLogsClient(getSubscriptionID(Default), cred, nil)
224223
Expect(err).NotTo(HaveOccurred())
225224

226-
var groupName string
227225
clusterClient := acp.GetClient()
228-
workloadCluster, err := getAzureCluster(timeoutctx, clusterClient, namespace, name)
229-
if apierrors.IsNotFound(err) {
230-
controlPlane, err := getAzureManagedControlPlane(timeoutctx, clusterClient, namespace, name)
231-
if apierrors.IsNotFound(err) {
232-
asoCluster, err := getAzureASOManagedCluster(timeoutctx, clusterClient, namespace, name)
233-
if err != nil {
234-
// Failing to fetch logs should not cause the test to fail
235-
Logf("Error fetching activity logs for cluster %s in namespace %s. Not able to find the AzureASOManagedCluster on the management cluster: %v", name, namespace, err)
236-
return
237-
}
238-
for _, resource := range asoCluster.Spec.Resources {
239-
u := &unstructured.Unstructured{}
240-
Expect(u.UnmarshalJSON(resource.Raw)).To(Succeed())
241-
if u.GroupVersionKind().Kind != "ResourceGroup" {
242-
continue
243-
}
244-
// AzureName might not be specified in the CAPZ resource. GET the rg to make sure we have it.
245-
rg := &asoresourcesv1.ResourceGroup{}
246-
Expect(clusterClient.Get(ctx, client.ObjectKey{Namespace: namespace, Name: u.GetName()}, rg)).To(Succeed())
247-
groupName = rg.AzureName()
248-
break
249-
}
250-
} else {
251-
if err != nil {
252-
// Failing to fetch logs should not cause the test to fail
253-
Logf("Error fetching activity logs for cluster %s in namespace %s. Not able to find the AzureManagedControlPlane on the management cluster: %v", name, namespace, err)
254-
return
255-
}
256-
groupName = controlPlane.Spec.ResourceGroupName
257-
}
258-
} else {
226+
cluster := framework.GetClusterByName(ctx, framework.GetClusterByNameInput{
227+
Getter: clusterClient,
228+
Name: name,
229+
Namespace: namespace,
230+
})
231+
if cluster.Spec.InfrastructureRef == nil {
232+
Logf("No infrastructure for cluster %s/%s", namespace, name)
233+
return
234+
}
235+
236+
var groupName string
237+
switch cluster.Spec.InfrastructureRef.Kind {
238+
case infrav1.AzureClusterKind:
239+
workloadCluster, err := getAzureCluster(timeoutctx, clusterClient, cluster.Spec.InfrastructureRef.Namespace, cluster.Spec.InfrastructureRef.Name)
259240
if err != nil {
260241
// Failing to fetch logs should not cause the test to fail
261242
Logf("Error fetching activity logs for cluster %s in namespace %s. Not able to find the workload cluster on the management cluster: %v", name, namespace, err)
262243
return
263244
}
264245
groupName = workloadCluster.Spec.ResourceGroup
246+
case infrav1.AzureManagedClusterKind:
247+
if cluster.Spec.ControlPlaneRef == nil {
248+
Logf("No control plane for cluster %s/%s", namespace, name)
249+
return
250+
}
251+
controlPlane, err := getAzureManagedControlPlane(timeoutctx, clusterClient, cluster.Spec.ControlPlaneRef.Namespace, cluster.Spec.ControlPlaneRef.Name)
252+
if err != nil {
253+
// Failing to fetch logs should not cause the test to fail
254+
Logf("Error fetching activity logs for cluster %s in namespace %s. Not able to find the AzureManagedControlPlane on the management cluster: %v", name, namespace, err)
255+
return
256+
}
257+
groupName = controlPlane.Spec.ResourceGroupName
258+
case infrav1.AzureASOManagedClusterKind:
259+
asoCluster, err := getAzureASOManagedCluster(timeoutctx, clusterClient, cluster.Spec.InfrastructureRef.Namespace, cluster.Spec.InfrastructureRef.Name)
260+
if err != nil {
261+
// Failing to fetch logs should not cause the test to fail
262+
Logf("Error fetching activity logs for cluster %s in namespace %s. Not able to find the AzureASOManagedCluster on the management cluster: %v", name, namespace, err)
263+
return
264+
}
265+
for _, resource := range asoCluster.Spec.Resources {
266+
u := &unstructured.Unstructured{}
267+
Expect(u.UnmarshalJSON(resource.Raw)).To(Succeed())
268+
if u.GroupVersionKind().Kind != "ResourceGroup" {
269+
continue
270+
}
271+
// AzureName might not be specified in the CAPZ resource. GET the rg to make sure we have it.
272+
rg := &asoresourcesv1.ResourceGroup{}
273+
Expect(clusterClient.Get(ctx, client.ObjectKey{Namespace: namespace, Name: u.GetName()}, rg)).To(Succeed())
274+
groupName = rg.AzureName()
275+
break
276+
}
265277
}
266278

267279
start := time.Now().Add(-2 * time.Hour).UTC().Format(time.RFC3339)

0 commit comments

Comments
 (0)