Skip to content

Commit d026cc3

Browse files
authored
Merge pull request #1383 from CecileRobertMichon/boot-logs
Collect boot logs even when bootstrap fails
2 parents 07631c4 + c94314b commit d026cc3

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

test/e2e/azure_logcollector.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,16 @@ type AzureLogCollector struct{}
4545
func (k AzureLogCollector) CollectMachineLog(ctx context.Context, managementClusterClient client.Client, m *clusterv1.Machine, outputPath string) error {
4646
var errors []error
4747

48-
if err := collectLogsFromNode(ctx, managementClusterClient, m, outputPath); err != nil {
48+
am, err := getAzureMachine(ctx, managementClusterClient, m)
49+
if err != nil {
50+
return err
51+
}
52+
53+
if err := collectLogsFromNode(ctx, managementClusterClient, m, am, outputPath); err != nil {
4954
errors = append(errors, err)
5055
}
5156

52-
if err := collectBootLog(ctx, m, outputPath); err != nil {
57+
if err := collectBootLog(ctx, am, outputPath); err != nil {
5358
errors = append(errors, err)
5459
}
5560

@@ -58,17 +63,14 @@ func (k AzureLogCollector) CollectMachineLog(ctx context.Context, managementClus
5863
}
5964

6065
// collectLogsFromNode collects logs from various sources by ssh'ing into the node
61-
func collectLogsFromNode(ctx context.Context, managementClusterClient client.Client, m *clusterv1.Machine, outputPath string) error {
66+
func collectLogsFromNode(ctx context.Context, managementClusterClient client.Client, m *clusterv1.Machine, am *v1alpha4.AzureMachine, outputPath string) error {
6267
cluster, err := util.GetClusterFromMetadata(ctx, managementClusterClient, m.ObjectMeta)
6368
if err != nil {
6469
return err
6570
}
6671

6772
Logf("INFO: Collecting logs for machine %s in cluster %s in namespace %s\n", m.GetName(), cluster.Name, cluster.Namespace)
68-
isWindows, err := isNodeWindows(ctx, managementClusterClient, m)
69-
if err != nil {
70-
return err
71-
}
73+
isWindows := isNodeWindows(am)
7274

7375
controlPlaneEndpoint := cluster.Spec.ControlPlaneEndpoint.Host
7476
hostname := m.Spec.InfrastructureRef.Name
@@ -108,18 +110,19 @@ func collectLogsFromNode(ctx context.Context, managementClusterClient client.Cli
108110
return kinderrors.AggregateConcurrent(linuxLogs(execToPathFn))
109111
}
110112

111-
func isNodeWindows(ctx context.Context, managementClusterClient client.Client, m *clusterv1.Machine) (bool, error) {
113+
func isNodeWindows(am *v1alpha4.AzureMachine) bool {
114+
return am.Spec.OSDisk.OSType == azure.WindowsOS
115+
}
116+
117+
func getAzureMachine(ctx context.Context, managementClusterClient client.Client, m *clusterv1.Machine) (*v1alpha4.AzureMachine, error) {
112118
key := client.ObjectKey{
113119
Namespace: m.Spec.InfrastructureRef.Namespace,
114120
Name: m.Spec.InfrastructureRef.Name,
115121
}
116122

117123
azMachine := &v1alpha4.AzureMachine{}
118-
if err := managementClusterClient.Get(ctx, key, azMachine); err != nil {
119-
return false, err
120-
}
121-
122-
return azMachine.Spec.OSDisk.OSType == azure.WindowsOS, nil
124+
err := managementClusterClient.Get(ctx, key, azMachine)
125+
return azMachine, err
123126
}
124127

125128
func linuxLogs(execToPathFn func(outputFileName string, command string, args ...string) func() error) []func() error {
@@ -251,8 +254,10 @@ func windowsNetworkLogs(execToPathFn func(outputFileName string, command string,
251254
}
252255

253256
// collectBootLog collects boot logs of the vm by using azure boot diagnostics
254-
func collectBootLog(ctx context.Context, m *clusterv1.Machine, outputPath string) error {
255-
resourceId := strings.TrimPrefix(*m.Spec.ProviderID, azure.ProviderIDPrefix)
257+
func collectBootLog(ctx context.Context, am *v1alpha4.AzureMachine, outputPath string) error {
258+
Logf("INFO: Collecting boot logs for AzureMachine %s\n", am.GetName())
259+
260+
resourceId := strings.TrimPrefix(*am.Spec.ProviderID, azure.ProviderIDPrefix)
256261
resource, err := autorest.ParseResourceID(resourceId)
257262
if err != nil {
258263
return errors.Wrap(err, "failed to parse resource id")

test/e2e/azure_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
. "github.com/onsi/gomega"
3232
corev1 "k8s.io/api/core/v1"
3333
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34+
"k8s.io/apimachinery/pkg/types"
3435
"k8s.io/utils/pointer"
3536
capi_e2e "sigs.k8s.io/cluster-api/test/e2e"
3637
"sigs.k8s.io/cluster-api/test/framework/clusterctl"
@@ -75,6 +76,10 @@ var _ = Describe("Workload cluster creation", func() {
7576
})
7677

7778
AfterEach(func() {
79+
if result.Cluster == nil {
80+
// this means the cluster failed to come up. We make an attempt to find the cluster to be able to fetch logs for the failed bootstrapping.
81+
_ = bootstrapClusterProxy.GetClient().Get(ctx, types.NamespacedName{Name: clusterName, Namespace: namespace.Name}, result.Cluster)
82+
}
7883
dumpSpecResourcesAndCleanup(ctx, specName, bootstrapClusterProxy, artifactFolder, namespace, cancelWatches, result.Cluster, e2eConfig.GetIntervals, skipCleanup)
7984
Expect(os.Unsetenv(AzureResourceGroup)).NotTo(HaveOccurred())
8085
Expect(os.Unsetenv(AzureVNetName)).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)