Skip to content

Commit 4c1c497

Browse files
authored
testing: Recover describe/log functions from panic when backup fails (#733)
* handle panic for describe functions when backup fails * reduce velero logs panic recover repeated code
1 parent 05ad54d commit 4c1c497

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

tests/e2e/lib/velero_helpers.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lib
33
import (
44
"bytes"
55
"context"
6+
"fmt"
67
"log"
78
"regexp"
89
"strings"
@@ -33,7 +34,7 @@ func GetVeleroClient() (veleroClientset.Interface, error) {
3334
}
3435

3536
// https://github.com/vmware-tanzu/velero/blob/11bfe82342c9f54c63f40d3e97313ce763b446f2/pkg/cmd/cli/backup/describe.go#L77-L111
36-
func DescribeBackup(ocClient client.Client, backup velero.Backup) string {
37+
func DescribeBackup(ocClient client.Client, backup velero.Backup) (backupDescription string) {
3738
err := ocClient.Get(context.Background(), client.ObjectKey{
3839
Namespace: backup.Namespace,
3940
Name: backup.Name,
@@ -75,7 +76,15 @@ func DescribeBackup(ocClient client.Client, backup velero.Backup) string {
7576
log.Printf("error getting VolumeSnapshotContent objects for backup %s: %v\n", backup.Name, err)
7677
}
7778
}
78-
79+
// output.DescribeBackup is a helper function from velero CLI that attempts to download logs for a backup.
80+
// if a backup failed, this function may panic. Recover from the panic and return string of backup object
81+
defer func() {
82+
if r := recover(); r != nil {
83+
log.Printf("Recovered from panic in DescribeBackup: %v\n", r)
84+
log.Print("returning backup object instead")
85+
backupDescription = fmt.Sprint(backup)
86+
}
87+
}()
7988
return output.DescribeBackup(context.Background(), ocClient, &backup, deleteRequestList.Items, podVolumeBackupList.Items, vscList.Items, details, veleroClient, insecureSkipTLSVerify, caCertFile)
8089
}
8190

@@ -104,25 +113,36 @@ func DescribeRestore(ocClient client.Client, restore velero.Restore) string {
104113
return output.DescribeRestore(context.Background(), ocClient, &restore, podvolumeRestoreList.Items, details, veleroClient, insecureSkipTLSVerify, caCertFile)
105114
}
106115

107-
func BackupLogs(ocClient client.Client, backup velero.Backup) string {
116+
func BackupLogs(ocClient client.Client, backup velero.Backup) (backupLogs string) {
108117
insecureSkipTLSVerify := true
109118
caCertFile := ""
110119
// new io.Writer that store the logs in a string
111120
logs := &bytes.Buffer{}
112121
// new io.Writer that store the logs in a string
113-
122+
// if a backup failed, this function may panic. Recover from the panic and return container logs
123+
defer func() {
124+
if r := recover(); r != nil {
125+
backupLogs = recoverFromPanicLogs(backup.Namespace, r, "BackupLogs")
126+
127+
}
128+
}()
114129
downloadrequest.Stream(context.Background(), ocClient, backup.Namespace, backup.Name, velero.DownloadTargetKindBackupLog, logs, time.Minute, insecureSkipTLSVerify, caCertFile)
115130

116131
return logs.String()
117132
}
118133

119-
func RestoreLogs(ocClient client.Client, restore velero.Restore) string {
134+
func RestoreLogs(ocClient client.Client, restore velero.Restore) (restoreLogs string) {
120135
insecureSkipTLSVerify := true
121136
caCertFile := ""
122137
// new io.Writer that store the logs in a string
123138
logs := &bytes.Buffer{}
124139
// new io.Writer that store the logs in a string
125-
140+
// if a backup failed, this function may panic. Recover from the panic and return container logs
141+
defer func() {
142+
if r := recover(); r != nil {
143+
restoreLogs = recoverFromPanicLogs(restore.Namespace, r, "RestoreLogs")
144+
}
145+
}()
126146
downloadrequest.Stream(context.Background(), ocClient, restore.Namespace, restore.Name, velero.DownloadTargetKindRestoreLog, logs, time.Minute, insecureSkipTLSVerify, caCertFile)
127147

128148
return logs.String()
@@ -136,6 +156,16 @@ var errorIgnorePatterns = []string{
136156
"blob unknown",
137157
}
138158

159+
func recoverFromPanicLogs(veleroNamespace string, panicReason interface{}, panicFrom string) string {
160+
log.Printf("Recovered from panic in %s: %v\n", panicFrom, panicReason)
161+
log.Print("returning container logs instead")
162+
containerLogs, err := GetVeleroContainerLogs(veleroNamespace)
163+
if err != nil {
164+
log.Printf("error getting container logs: %v\n", err)
165+
}
166+
return containerLogs
167+
}
168+
139169
func BackupErrorLogs(ocClient client.Client, backup velero.Backup) []string {
140170
bl := BackupLogs(ocClient, backup)
141171
errorRegex, err := regexp.Compile("error|Error")

0 commit comments

Comments
 (0)