Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update progress on podvolumebackup CR and restic log #4

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Dockerfile.el7
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM openshift/origin-release:golang-1.11 AS builder
COPY . /go/src/github.com/heptio/velero
RUN cd /go/src/github.com/heptio/velero \
&& CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' ./cmd/velero

RUN mkdir /go/src/github.com/restic \
&& cd /go/src/github.com/restic \
&& git clone https://github.com/fusor/restic -b fusor-v0.9.4 \
&& cd /go/src/github.com/restic/restic \
&& CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' ./cmd/restic

FROM centos:7
RUN yum -y update && yum clean all

COPY --from=builder /go/src/github.com/heptio/velero/velero velero
COPY --from=builder /go/src/github.com/restic/restic/restic /usr/bin/restic

USER nobody:nobody

ENTRYPOINT ["/velero"]
3 changes: 3 additions & 0 deletions pkg/apis/ark/v1/pod_volume_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ type PodVolumeBackupStatus struct {
// Phase is the current state of the PodVolumeBackup.
Phase PodVolumeBackupPhase `json:"phase"`

// Progress shows the current progress copying data for the backup
Progress string `json:"progress"`

// Path is the full path within the controller pod being backed up.
Path string `json:"path"`

Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/velero/v1/pod_volume_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ type PodVolumeBackupStatus struct {
// Phase is the current state of the PodVolumeBackup.
Phase PodVolumeBackupPhase `json:"phase"`

// Progress shows the current progress copying data for the backup
Progress string `json:"progress"`

// Path is the full path within the controller pod being backed up.
Path string `json:"path"`

Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/pod_volume_backup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
informers "github.com/heptio/velero/pkg/generated/informers/externalversions/velero/v1"
listers "github.com/heptio/velero/pkg/generated/listers/velero/v1"
"github.com/heptio/velero/pkg/restic"
veleroexec "github.com/heptio/velero/pkg/util/exec"
"github.com/heptio/velero/pkg/util/filesystem"
"github.com/heptio/velero/pkg/util/kube"
)
Expand Down Expand Up @@ -228,7 +227,7 @@ func (c *podVolumeBackupController) processBackup(req *velerov1api.PodVolumeBack

var stdout, stderr string

if stdout, stderr, err = veleroexec.RunCommand(resticCmd.Cmd()); err != nil {
if stdout, stderr, err = c.RunBackupCommand(resticCmd.Cmd(), req); err != nil {
log.WithError(errors.WithStack(err)).Errorf("Error running command=%s, stdout=%s, stderr=%s", resticCmd.String(), stdout, stderr)
return c.fail(req, fmt.Sprintf("error running restic backup, stderr=%s: %s", stderr, err.Error()), log)
}
Expand All @@ -243,6 +242,7 @@ func (c *podVolumeBackupController) processBackup(req *velerov1api.PodVolumeBack
// update status to Completed with path & snapshot id
req, err = c.patchPodVolumeBackup(req, func(r *velerov1api.PodVolumeBackup) {
r.Status.Path = path
r.Status.Progress = "100%"
r.Status.SnapshotID = snapshotID
r.Status.Phase = velerov1api.PodVolumeBackupPhaseCompleted
})
Expand Down
76 changes: 76 additions & 0 deletions pkg/controller/pod_volume_backup_exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package controller

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"sync"
"time"

velerov1api "github.com/heptio/velero/pkg/apis/velero/v1"
)

type Progress struct {
PercentDone float64 `json:"percent_done"`
}

func (c *podVolumeBackupController) RunBackupCommand(cmd *exec.Cmd, req *velerov1api.PodVolumeBackup) (string, string, error) {

var stdoutBuf, stderrBuf, stdoutStatBuf bytes.Buffer
stdoutIn, _ := cmd.StdoutPipe()
stderrIn, _ := cmd.StderrPipe()

var errStdout error
stdout := io.MultiWriter(os.Stdout, &stdoutBuf, &stdoutStatBuf)
stderr := io.MultiWriter(os.Stderr, &stderrBuf)

err := cmd.Start()
var wg sync.WaitGroup

wg.Add(1)
go func() {
_, errStdout = io.Copy(stdout, stdoutIn)
wg.Done()
}()

wg.Add(1)
go func() {
var line string
var progress Progress
for {
time.Sleep(10 * time.Second)
scanner := bufio.NewScanner(&stdoutStatBuf)
line = ""
for scanner.Scan() {
line = (scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
break
}
stdoutStatBuf.Reset()
err = json.Unmarshal([]byte(line), &progress)
if err != nil {
break
}
_, err = c.patchPodVolumeBackup(req, func(r *velerov1api.PodVolumeBackup) {
r.Status.Progress = fmt.Sprintf("%.2f%%", progress.PercentDone*100)
})
}

wg.Done()
}()

_, _ = io.Copy(stderr, stderrIn)
wg.Wait()

err = cmd.Wait()

outStr, errStr := string(stdoutBuf.Bytes()), string(stderrBuf.Bytes())

return outStr, errStr, err
}
3 changes: 3 additions & 0 deletions pkg/restic/backupper.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ func newPodVolumeBackup(backup *velerov1api.Backup, pod *corev1api.Pod, volumeNa
BackupStorageLocation: backup.Spec.StorageLocation,
RepoIdentifier: repoIdentifier,
},
Status: velerov1api.PodVolumeBackupStatus{
Progress: "0%",
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/restic/command_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func BackupCommand(repoIdentifier, passwordFile, path string, tags map[string]st
PasswordFile: passwordFile,
Dir: path,
Args: []string{"."},
ExtraFlags: append(backupTagFlags(tags), "--host=velero"),
ExtraFlags: append(backupTagFlags(tags), "--host=velero", "--json"),
}
}

Expand Down