|
1 | 1 | package lib
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "context"
|
5 | 6 | "fmt"
|
6 | 7 | "os"
|
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + "time" |
7 | 11 |
|
8 | 12 | snapshotv1beta1api "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1beta1"
|
9 | 13 | snapshotv1beta1client "github.com/kubernetes-csi/external-snapshotter/client/v4/clientset/versioned"
|
10 | 14 | velero "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
11 | 15 | pkgbackup "github.com/vmware-tanzu/velero/pkg/backup"
|
12 | 16 | "github.com/vmware-tanzu/velero/pkg/cmd"
|
| 17 | + "github.com/vmware-tanzu/velero/pkg/cmd/util/downloadrequest" |
13 | 18 | "github.com/vmware-tanzu/velero/pkg/cmd/util/output"
|
14 | 19 | "github.com/vmware-tanzu/velero/pkg/features"
|
15 | 20 | veleroClientset "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned"
|
@@ -94,6 +99,60 @@ func DescribeRestore(ocClient client.Client, restore velero.Restore) string {
|
94 | 99 | if err != nil {
|
95 | 100 | fmt.Fprintf(os.Stderr, "error getting PodVolumeRestores for restore %s: %v\n", restore.Name, err)
|
96 | 101 | }
|
97 |
| - |
| 102 | + |
98 | 103 | return output.DescribeRestore(context.Background(), ocClient, &restore, podvolumeRestoreList.Items, details, veleroClient, insecureSkipTLSVerify, caCertFile)
|
99 | 104 | }
|
| 105 | + |
| 106 | +func BackupLogs(ocClient client.Client, backup velero.Backup) string { |
| 107 | + insecureSkipTLSVerify := true |
| 108 | + caCertFile := "" |
| 109 | + // new io.Writer that store the logs in a string |
| 110 | + logs := &bytes.Buffer{} |
| 111 | + // new io.Writer that store the logs in a string |
| 112 | + |
| 113 | + downloadrequest.Stream(context.Background(), ocClient, backup.Namespace, backup.Name, velero.DownloadTargetKindBackupLog, logs, time.Minute, insecureSkipTLSVerify, caCertFile) |
| 114 | + |
| 115 | + return logs.String() |
| 116 | +} |
| 117 | + |
| 118 | +func RestoreLogs(ocClient client.Client, restore velero.Restore) string { |
| 119 | + insecureSkipTLSVerify := true |
| 120 | + caCertFile := "" |
| 121 | + // new io.Writer that store the logs in a string |
| 122 | + logs := &bytes.Buffer{} |
| 123 | + // new io.Writer that store the logs in a string |
| 124 | + |
| 125 | + downloadrequest.Stream(context.Background(), ocClient, restore.Namespace, restore.Name, velero.DownloadTargetKindRestoreLog, logs, time.Minute, insecureSkipTLSVerify, caCertFile) |
| 126 | + |
| 127 | + return logs.String() |
| 128 | +} |
| 129 | + |
| 130 | +func BackupErrorLogs(ocClient client.Client, backup velero.Backup) []string { |
| 131 | + bl := BackupLogs(ocClient, backup) |
| 132 | + errorRegex, err := regexp.Compile("error|Error") |
| 133 | + if err != nil { |
| 134 | + return []string{"could not compile regex: ", err.Error()} |
| 135 | + } |
| 136 | + logLines := []string{} |
| 137 | + for _, line := range strings.Split(bl, "\n") { |
| 138 | + if errorRegex.MatchString(line) { |
| 139 | + logLines = append(logLines, line) |
| 140 | + } |
| 141 | + } |
| 142 | + return logLines |
| 143 | +} |
| 144 | + |
| 145 | +func RestoreErrorLogs(ocClient client.Client, restore velero.Restore) []string { |
| 146 | + rl := RestoreLogs(ocClient, restore) |
| 147 | + errorRegex, err := regexp.Compile("error|Error") |
| 148 | + if err != nil { |
| 149 | + return []string{"could not compile regex: ", err.Error()} |
| 150 | + } |
| 151 | + logLines := []string{} |
| 152 | + for _, line := range strings.Split(rl, "\n") { |
| 153 | + if errorRegex.MatchString(line) { |
| 154 | + logLines = append(logLines, line) |
| 155 | + } |
| 156 | + } |
| 157 | + return logLines |
| 158 | +} |
0 commit comments