Skip to content

Commit

Permalink
Merge pull request #556 from clarklee92/print-the-previous-log
Browse files Browse the repository at this point in the history
Print the previous log of the container(just like kubectl)
  • Loading branch information
k8s-ci-robot authored Dec 9, 2019
2 parents 1da46ca + 0e13d35 commit 4497522
Showing 1 changed file with 14 additions and 0 deletions.
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

0 comments on commit 4497522

Please sign in to comment.