Skip to content

Commit 4aab716

Browse files
committed
update progress on podvolumebackup CR and restic log
1 parent 0ef1811 commit 4aab716

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-3
lines changed

pkg/apis/ark/v1/pod_volume_backup.go

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ type PodVolumeBackupStatus struct {
7272
// Phase is the current state of the PodVolumeBackup.
7373
Phase PodVolumeBackupPhase `json:"phase"`
7474

75+
// Progress shows the current progress copying data for the backup
76+
Progress string `json:"progress"`
77+
7578
// Path is the full path within the controller pod being backed up.
7679
Path string `json:"path"`
7780

pkg/controller/pod_volume_backup_controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
informers "github.com/heptio/velero/pkg/generated/informers/externalversions/velero/v1"
3838
listers "github.com/heptio/velero/pkg/generated/listers/velero/v1"
3939
"github.com/heptio/velero/pkg/restic"
40-
veleroexec "github.com/heptio/velero/pkg/util/exec"
4140
"github.com/heptio/velero/pkg/util/filesystem"
4241
"github.com/heptio/velero/pkg/util/kube"
4342
)
@@ -228,7 +227,7 @@ func (c *podVolumeBackupController) processBackup(req *velerov1api.PodVolumeBack
228227

229228
var stdout, stderr string
230229

231-
if stdout, stderr, err = veleroexec.RunCommand(resticCmd.Cmd()); err != nil {
230+
if stdout, stderr, err = c.RunBackupCommand(resticCmd.Cmd(), req); err != nil {
232231
log.WithError(errors.WithStack(err)).Errorf("Error running command=%s, stdout=%s, stderr=%s", resticCmd.String(), stdout, stderr)
233232
return c.fail(req, fmt.Sprintf("error running restic backup, stderr=%s: %s", stderr, err.Error()), log)
234233
}
@@ -243,6 +242,7 @@ func (c *podVolumeBackupController) processBackup(req *velerov1api.PodVolumeBack
243242
// update status to Completed with path & snapshot id
244243
req, err = c.patchPodVolumeBackup(req, func(r *velerov1api.PodVolumeBackup) {
245244
r.Status.Path = path
245+
r.Status.Progress = "100%"
246246
r.Status.SnapshotID = snapshotID
247247
r.Status.Phase = velerov1api.PodVolumeBackupPhaseCompleted
248248
})
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package controller
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"encoding/json"
7+
"fmt"
8+
"io"
9+
"os"
10+
"os/exec"
11+
"sync"
12+
"time"
13+
14+
velerov1api "github.com/heptio/velero/pkg/apis/velero/v1"
15+
)
16+
17+
type Progress struct {
18+
PercentDone float64 `json:"percent_done"`
19+
}
20+
21+
func (c *podVolumeBackupController) RunBackupCommand(cmd *exec.Cmd, req *velerov1api.PodVolumeBackup) (string, string, error) {
22+
23+
var stdoutBuf, stderrBuf, stdoutStatBuf bytes.Buffer
24+
stdoutIn, _ := cmd.StdoutPipe()
25+
stderrIn, _ := cmd.StderrPipe()
26+
27+
var errStdout error
28+
stdout := io.MultiWriter(os.Stdout, &stdoutBuf, &stdoutStatBuf)
29+
stderr := io.MultiWriter(os.Stderr, &stderrBuf)
30+
31+
err := cmd.Start()
32+
var wg sync.WaitGroup
33+
34+
wg.Add(1)
35+
go func() {
36+
_, errStdout = io.Copy(stdout, stdoutIn)
37+
wg.Done()
38+
}()
39+
40+
wg.Add(1)
41+
go func() {
42+
var line string
43+
var progress Progress
44+
for {
45+
time.Sleep(10 * time.Second)
46+
scanner := bufio.NewScanner(&stdoutStatBuf)
47+
line = ""
48+
for scanner.Scan() {
49+
line = (scanner.Text())
50+
}
51+
if err := scanner.Err(); err != nil {
52+
fmt.Fprintln(os.Stderr, "reading standard input:", err)
53+
break
54+
}
55+
stdoutStatBuf.Reset()
56+
err = json.Unmarshal([]byte(line), &progress)
57+
if err != nil {
58+
break
59+
}
60+
_, err = c.patchPodVolumeBackup(req, func(r *velerov1api.PodVolumeBackup) {
61+
r.Status.Progress = fmt.Sprintf("%.2f%%", progress.PercentDone*100)
62+
})
63+
}
64+
65+
wg.Done()
66+
}()
67+
68+
_, _ = io.Copy(stderr, stderrIn)
69+
wg.Wait()
70+
71+
err = cmd.Wait()
72+
73+
outStr, errStr := string(stdoutBuf.Bytes()), string(stderrBuf.Bytes())
74+
75+
return outStr, errStr, err
76+
}

pkg/restic/backupper.go

+3
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ func newPodVolumeBackup(backup *velerov1api.Backup, pod *corev1api.Pod, volumeNa
257257
BackupStorageLocation: backup.Spec.StorageLocation,
258258
RepoIdentifier: repoIdentifier,
259259
},
260+
Status: velerov1api.PodVolumeBackupStatus{
261+
Progress: "0%",
262+
},
260263
}
261264
}
262265

pkg/restic/command_factory.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func BackupCommand(repoIdentifier, passwordFile, path string, tags map[string]st
3434
PasswordFile: passwordFile,
3535
Dir: path,
3636
Args: []string{"."},
37-
ExtraFlags: append(backupTagFlags(tags), "--host=velero"),
37+
ExtraFlags: append(backupTagFlags(tags), "--host=velero", "--json"),
3838
}
3939
}
4040

0 commit comments

Comments
 (0)