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

Print the previous log of the container(just like kubectl) #556

Merged
Merged
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
14 changes: 14 additions & 0 deletions cmd/crictl/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"os"
"strings"
"time"

timetypes "github.com/docker/docker/api/types/time"
Expand All @@ -40,6 +41,10 @@ var logsCommand = cli.Command{
Name: "follow, f",
Usage: "Follow log output",
},
cli.BoolFlag{
Name: "previous, p",
Usage: "Print the logs for the previous instance of the container in a pod if it exists",
},
cli.Int64Flag{
Name: "tail",
Value: -1,
Expand Down Expand Up @@ -77,6 +82,7 @@ var logsCommand = cli.Command{
return err
}
timestamp := ctx.Bool("timestamps")
previous := ctx.Bool("previous")
logOptions := logs.NewLogOptions(&v1.PodLogOptions{
Follow: ctx.Bool("follow"),
TailLines: &tailLines,
Expand All @@ -92,6 +98,14 @@ var logsCommand = cli.Command{
if logPath == "" {
return fmt.Errorf("The container has not set log path")
}
if previous {
containerAttempt := status.GetMetadata().Attempt
if containerAttempt == uint32(0) {
return fmt.Errorf("Previous terminated container %s not found", status.GetMetadata().Name)
}
logPath = fmt.Sprintf("%s%s%s", logPath[:strings.LastIndex(logPath, "/")+1], fmt.Sprint(containerAttempt-1),
logPath[strings.LastIndex(logPath, "."):])
}
return logs.ReadLogs(context.Background(), logPath, status.GetId(), logOptions, runtimeService, os.Stdout, os.Stderr)
},
}
Expand Down