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

Add exit code log and possible memory shortage warning log for Restic command failure #6459

Merged
merged 1 commit into from
Jul 7, 2023
Merged
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
1 change: 1 addition & 0 deletions changelogs/unreleased/6459-blackpiglet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add exit code log and possible memory shortage warning log for Restic command failure.
2 changes: 1 addition & 1 deletion pkg/repository/restic/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (r *RepositoryService) exec(cmd *restic.Command, bsl *velerov1api.BackupSto
cmd.ExtraFlags = append(cmd.ExtraFlags, skipTLSRet)
}

stdout, stderr, err := veleroexec.RunCommand(cmd.Cmd())
stdout, stderr, err := veleroexec.RunCommandWithLog(cmd.Cmd(), r.log)
r.log.WithFields(logrus.Fields{
"repository": cmd.RepoName(),
"command": cmd.String(),
Expand Down
4 changes: 3 additions & 1 deletion pkg/restic/exec_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func RunBackup(backupCmd *Command, log logrus.FieldLogger, updater uploader.Prog

err := cmd.Start()
if err != nil {
exec.LogErrorAsExitCode(err, log)
return stdoutBuf.String(), stderrBuf.String(), err
}

Expand Down Expand Up @@ -119,6 +120,7 @@ func RunBackup(backupCmd *Command, log logrus.FieldLogger, updater uploader.Prog

err = cmd.Wait()
if err != nil {
exec.LogErrorAsExitCode(err, log)
return stdoutBuf.String(), stderrBuf.String(), err
}
quit <- struct{}{}
Expand Down Expand Up @@ -229,7 +231,7 @@ func RunRestore(restoreCmd *Command, log logrus.FieldLogger, updater uploader.Pr
}
}()

stdout, stderr, err := exec.RunCommand(restoreCmd.Cmd())
stdout, stderr, err := exec.RunCommandWithLog(restoreCmd.Cmd(), log)
quit <- struct{}{}

// update progress to 100%
Expand Down
23 changes: 23 additions & 0 deletions pkg/util/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os/exec"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// RunCommand runs a command and returns its stdout, stderr, and its returned
Expand Down Expand Up @@ -52,3 +53,25 @@ func RunCommand(cmd *exec.Cmd) (string, string, error) {

return stdout, stderr, runErr
}

func RunCommandWithLog(cmd *exec.Cmd, log logrus.FieldLogger) (string, string, error) {
stdout, stderr, err := RunCommand(cmd)
LogErrorAsExitCode(err, log)
return stdout, stderr, err
}

func LogErrorAsExitCode(err error, log logrus.FieldLogger) {
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
log.Errorf("Restic command fail with ExitCode: %d. Process ID is %d, Exit error is: %s", exitError.ExitCode(), exitError.Pid(), exitError.String())
// Golang's os.exec -1 ExitCode means signal kill. Usually this is caused
// by CGroup's OOM. Log a warning to notice user.
// https://github.com/golang/go/blob/master/src/os/exec_posix.go#L128-L136
if exitError.ExitCode() == -1 {
log.Warnf("The ExitCode is -1, which means the process is terminated by signal. Usually this is caused by CGroup kill due to out of memory. Please check whether there is such information in the work nodes' dmesg log.")
}
} else {
log.WithError(err).Info("Error cannot be convert to ExitError format.")
}
}
}
Loading