Skip to content

Commit c0c341f

Browse files
authored
Merge pull request #2252 from jackfrancis/test-log-collection-panic
test: don’t panic during log collection
2 parents 40d18ef + 32b7bbc commit c0c341f

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

test/e2e/azure_logcollector.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,19 @@ func (k AzureLogCollector) CollectMachinePoolLog(ctx context.Context, management
108108
}
109109

110110
for i, instance := range mp.Spec.ProviderIDList {
111-
hostname := mp.Status.NodeRefs[i].Name
111+
if mp.Status.NodeRefs != nil && len(mp.Status.NodeRefs) >= (i+1) {
112+
hostname := mp.Status.NodeRefs[i].Name
112113

113-
if err := collectLogsFromNode(ctx, managementClusterClient, cluster, hostname, isWindows, filepath.Join(outputPath, hostname)); err != nil {
114-
errors = append(errors, err)
115-
}
114+
if err := collectLogsFromNode(ctx, managementClusterClient, cluster, hostname, isWindows, filepath.Join(outputPath, hostname)); err != nil {
115+
errors = append(errors, err)
116+
}
116117

117-
if err := collectVMSSBootLog(ctx, instance, filepath.Join(outputPath, hostname)); err != nil {
118-
errors = append(errors, err)
118+
if err := collectVMSSBootLog(ctx, instance, filepath.Join(outputPath, hostname)); err != nil {
119+
errors = append(errors, err)
120+
}
121+
} else {
122+
Logf("MachinePool instance %s does not have a corresponding NodeRef", instance)
123+
Logf("Skipping log collection for MachinePool instance %s", instance)
119124
}
120125
}
121126

@@ -128,7 +133,7 @@ func collectLogsFromNode(ctx context.Context, managementClusterClient client.Cli
128133
if isWindows {
129134
nodeOSType = "Windows"
130135
}
131-
Logf("INFO: Collecting logs for %s node %s in cluster %s in namespace %s\n", nodeOSType, hostname, cluster.Name, cluster.Namespace)
136+
Logf("Collecting logs for %s node %s in cluster %s in namespace %s\n", nodeOSType, hostname, cluster.Name, cluster.Namespace)
132137

133138
controlPlaneEndpoint := cluster.Spec.ControlPlaneEndpoint.Host
134139

@@ -165,7 +170,7 @@ func getHostname(m *clusterv1.Machine, isWindows bool) string {
165170
if len(m.Status.Addresses) > 0 {
166171
hostname = m.Status.Addresses[0].Address
167172
} else {
168-
Logf("INFO: Unable to collect logs as node doesn't have addresses")
173+
Logf("Unable to collect logs as node doesn't have addresses")
169174
}
170175
}
171176
return hostname
@@ -342,7 +347,7 @@ func windowsNetworkLogs(execToPathFn func(outputFileName string, command string,
342347

343348
// collectVMBootLog collects boot logs of the vm by using azure boot diagnostics.
344349
func collectVMBootLog(ctx context.Context, am *v1beta1.AzureMachine, outputPath string) error {
345-
Logf("INFO: Collecting boot logs for AzureMachine %s\n", am.GetName())
350+
Logf("Collecting boot logs for AzureMachine %s\n", am.GetName())
346351

347352
resourceId := strings.TrimPrefix(*am.Spec.ProviderID, azure.ProviderIDPrefix)
348353
resource, err := autorest.ParseResourceID(resourceId)
@@ -380,7 +385,7 @@ func collectVMSSBootLog(ctx context.Context, providerID string, outputPath strin
380385
return errors.Wrap(err, "failed to parse resource id")
381386
}
382387

383-
Logf("INFO: Collecting boot logs for VMSS instance %s of scale set %s\n", instanceId, resource.ResourceName)
388+
Logf("Collecting boot logs for VMSS instance %s of scale set %s\n", instanceId, resource.ResourceName)
384389

385390
settings, err := auth.GetSettingsFromEnvironment()
386391
if err != nil {

0 commit comments

Comments
 (0)